Browse Source

Add method annotation to help filter dispatch

pull/1/head
yichen 2 years ago
parent
commit
beef09925b
  1. 13
      service/src/main/java/com/fanruan/annotation/LocalMethod.java
  2. 16
      service/src/main/java/com/fanruan/annotation/NotImplemented.java
  3. 8
      service/src/main/java/com/fanruan/exception/NotImplementedException.java
  4. 52
      service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java
  5. 86
      service/src/main/java/com/fanruan/proxy/interceptor/InterceptorUtils.java
  6. 42
      service/src/main/java/com/fanruan/service/jdbc/AbstractBind.java
  7. 61
      service/src/main/java/com/fanruan/service/jdbc/BasedBind.java
  8. 71
      service/src/main/java/com/fanruan/service/jdbc/ServiceArray.java
  9. 73
      service/src/main/java/com/fanruan/service/jdbc/ServiceBlob.java
  10. 83
      service/src/main/java/com/fanruan/service/jdbc/ServiceClob.java
  11. 17
      service/src/main/java/com/fanruan/service/jdbc/ServiceDataBaseMetaData.java
  12. 69
      service/src/main/java/com/fanruan/service/jdbc/ServiceParameterMetaData.java
  13. 29
      service/src/main/java/com/fanruan/service/jdbc/ServiceStruct.java
  14. 86
      service/src/main/java/com/fanruan/service/jdbc/connection/ServiceConnection.java
  15. 18
      service/src/main/java/com/fanruan/service/jdbc/driver/ServiceDriver.java
  16. 23
      service/src/main/java/com/fanruan/service/jdbc/resultset/ServiceResultSet.java
  17. 1090
      service/src/main/java/com/fanruan/service/jdbc/statement/ServiceCallableStatement.java
  18. 22
      service/src/main/java/com/fanruan/service/jdbc/statement/ServicePreparedStatement.java
  19. 22
      service/src/main/java/com/fanruan/service/jdbc/statement/ServiceStatement.java

13
service/src/main/java/com/fanruan/annotation/LocalMethod.java

@ -0,0 +1,13 @@
package com.fanruan.annotation;
import java.lang.annotation.*;
/**
* @author Yichen Dai
* @date 2022/8/31 18:34
*/
@Inherited
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LocalMethod {
}

16
service/src/main/java/com/fanruan/annotation/NotImplemented.java

@ -0,0 +1,16 @@
package com.fanruan.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* mark the method not implement
* @author Yichen Dai
* @date 2022/8/31 10:30
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotImplemented {
}

8
service/src/main/java/com/fanruan/exception/NotImplementedException.java

@ -0,0 +1,8 @@
package com.fanruan.exception;
/**
* @author Yichen Dai
* @date 2022/8/31 10:36
*/
public class NotImplementedException extends RuntimeException{
}

52
service/src/main/java/com/fanruan/proxy/interceptor/Interceptor.java

@ -2,13 +2,12 @@ package com.fanruan.proxy.interceptor;
import com.corundumstudio.socketio.SocketIOClient; import com.corundumstudio.socketio.SocketIOClient;
import com.fanruan.annotation.RemoteClass;
import com.fanruan.ServerStater; import com.fanruan.ServerStater;
import com.fanruan.cache.ClientCache; import com.fanruan.cache.ClientCache;
import com.fanruan.cache.ClientWrapper; import com.fanruan.cache.ClientWrapper;
import com.fanruan.cache.LockAndCondition; import com.fanruan.cache.LockAndCondition;
import com.fanruan.pojo.message.RpcRequest; import com.fanruan.pojo.message.RpcRequest;
import com.fanruan.utils.Commons; import com.fanruan.service.jdbc.AbstractBind;
import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy; import net.sf.cglib.proxy.MethodProxy;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -41,7 +40,8 @@ public class Interceptor implements MethodInterceptor {
@Override @Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
if(InterceptorUtils.isInExcludedList(method.getName())){ if(InterceptorUtils.isNotImplemented(method) ||
InterceptorUtils.isLocalMethod(method)){
return methodProxy.invokeSuper(o, objects); return methodProxy.invokeSuper(o, objects);
} }
// Parameters injection of class MyDriver's construction method will be delayed util the first "connect" method was intercepted // Parameters injection of class MyDriver's construction method will be delayed util the first "connect" method was intercepted
@ -55,38 +55,9 @@ public class Interceptor implements MethodInterceptor {
if(client == null){ if(client == null){
client = ClientCache.getClient(agentId, dbName); client = ClientCache.getClient(agentId, dbName);
} }
logger.debug("start invoke " + method.getName()); logger.debug("start invoke " + method.getName());
RpcRequest rpcRequest = new RpcRequest(); RpcRequest rpcRequest = InterceptorUtils.generateRequest(clazz, o, method, objects);
rpcRequest
.setID(Commons.getID())
.setMethodName(method.getName())
.setArgs(objects)
.setArgTypes(getArgTypes(objects));
// get RPC class name from annotation
// if null, send the name of this clazz
if(clazz.isAnnotationPresent(RemoteClass.class)){
RemoteClass annotation = clazz.getAnnotation(RemoteClass.class);
rpcRequest.setServiceClassName(annotation.remoteClassName());
}else{
rpcRequest.setServiceClassName(clazz.getName());
}
// // Some instance need to be bound one-to-one, to make sure the operator happen in service
// // will be deliver to this specific corresponding instance.
// if(InterceptorUtils.isInBindList(o)){
// rpcRequest.setBinding(true);
// }
// IDtoInvoke is an unique ID to identify an one-to-one binding relation.
// It comes from rpcRequest in which the instance in the agent is created.
String idToInvoke = InterceptorUtils.getInvokeHelper(o, "getID", String.class);
if(idToInvoke != null){
rpcRequest.setIDToInvoke(idToInvoke);
}
FutureTask<Object> futureTask = new FutureTask<>( FutureTask<Object> futureTask = new FutureTask<>(
() -> { () -> {
@ -123,25 +94,14 @@ public class Interceptor implements MethodInterceptor {
return res; return res;
} }
Object returnObj = methodProxy.invokeSuper(o, objects); Object returnObj = methodProxy.invokeSuper(o, objects);
// If the return instance is corresponding with another instance in agent, set the binding ID. // If the return instance is corresponding with another instance in agent, set the binding ID.
if (InterceptorUtils.isInBindList(returnObj)){ if (InterceptorUtils.isInBindList(returnObj)){
InterceptorUtils.setInvokeHelper(returnObj, "setID", rpcRequest.getID()); AbstractBind ab = (AbstractBind) returnObj;
ab.setID(rpcRequest.getID());
} }
return returnObj; return returnObj;
} }
public Class<?>[] getArgTypes(Object[] objects){
int n = objects.length;
Class<?>[] argTypes = new Class[n];
for(int i=0; i<n; i++){
argTypes[i] = objects[i].getClass();
}
return argTypes;
}
} }

86
service/src/main/java/com/fanruan/proxy/interceptor/InterceptorUtils.java

@ -1,5 +1,13 @@
package com.fanruan.proxy.interceptor; package com.fanruan.proxy.interceptor;
import com.fanruan.annotation.LocalMethod;
import com.fanruan.annotation.NotImplemented;
import com.fanruan.annotation.RemoteClass;
import com.fanruan.pojo.message.RpcRequest;
import com.fanruan.service.jdbc.AbstractBind;
import com.fanruan.utils.Commons;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -10,15 +18,6 @@ public class InterceptorUtils {
private static final String[] EXCLUDED_METHOD_LIST = new String[]{ private static final String[] EXCLUDED_METHOD_LIST = new String[]{
"toString", "toString",
"hashCode", "hashCode",
"setInfo",
"setSql",
"getSql",
"setID",
"getID"
};
private static final String[] NEED_REPLY_LIST = new String[]{
"next",
}; };
private static final String[] WRAPPER_CLASS_LIST = new String[]{ private static final String[] WRAPPER_CLASS_LIST = new String[]{
@ -38,6 +37,11 @@ public class InterceptorUtils {
"Statement", "Statement",
"PreparedStatement", "PreparedStatement",
"ResultSet", "ResultSet",
"MetaData",
"Clob",
"Blob",
"Array",
"Struct"
}; };
public static boolean isInExcludedList(String methodName){ public static boolean isInExcludedList(String methodName){
@ -49,18 +53,6 @@ public class InterceptorUtils {
return false; return false;
} }
public static boolean isInReplyList(String methodName){
String pattern = "get.*";
boolean isMatch = Pattern.matches(pattern, methodName);
for(String ex : NEED_REPLY_LIST){
if(ex.equals(methodName)){
return true;
}
}
return isMatch;
}
public static boolean isWraps(Object o){ public static boolean isWraps(Object o){
for(String ex : WRAPPER_CLASS_LIST){ for(String ex : WRAPPER_CLASS_LIST){
if(ex.equals(getClassName(o.getClass().getName()))){ if(ex.equals(getClassName(o.getClass().getName()))){
@ -95,24 +87,48 @@ public class InterceptorUtils {
return false; return false;
} }
public static void setInvokeHelper(Object o, String methodName, String ID){
try { public static RpcRequest generateRequest(Class<?> clazz, Object o, Method method, Object[] objects){
Method method = o.getClass().getDeclaredMethod(methodName, String.class);
method.invoke(o, ID);
} catch (Exception e) { RpcRequest rpcRequest = new RpcRequest();
e.printStackTrace(); rpcRequest
.setID(Commons.getID())
.setMethodName(method.getName())
.setArgs(objects)
.setArgTypes(method.getParameterTypes());
// get RPC class name from annotation
// if null, send the name of this clazz
if(clazz.isAnnotationPresent(RemoteClass.class)){
RemoteClass annotation = clazz.getAnnotation(RemoteClass.class);
rpcRequest.setServiceClassName(annotation.remoteClassName());
}else{
rpcRequest.setServiceClassName(clazz.getName());
}
// IDtoInvoke is an unique ID to identify an one-to-one binding relation.
// It comes from rpcRequest in which the instance in the agent is created.
AbstractBind ab = (AbstractBind) o;
String idToInvoke = ab.getID();
if(idToInvoke != null){
rpcRequest.setIDToInvoke(idToInvoke);
} }
return rpcRequest;
} }
public static <T> T getInvokeHelper(Object o, String methodName, Class<?> T){ public static boolean isNotImplemented(Method method) {
try { if(method.isAnnotationPresent(NotImplemented.class)){
Method method = o.getClass().getDeclaredMethod(methodName); return true;
T res = (T) method.invoke(o);
return res;
} catch (Exception e) {
e.printStackTrace();
} }
return null; return false;
} }
public static boolean isLocalMethod(Method method) {
if(method.isAnnotationPresent(LocalMethod.class)){
return true;
}
return false;
}
} }

42
service/src/main/java/com/fanruan/service/jdbc/AbstractBind.java

@ -0,0 +1,42 @@
package com.fanruan.service.jdbc;
import com.fanruan.annotation.LocalMethod;
import java.util.Properties;
/**
* @author Yichen Dai
* @date 2022/8/25 16:56
*/
public abstract class AbstractBind{
private String ID;
protected Properties info;
/**
* These corresponding code is to make the format correct, because the getID()
* will be called, even if the filed is never not null.
* @return ID
*/
public String getID(){
return this.ID;
}
/**
* These corresponding code is to make the format correct, because the getID()
* will be called, even if the filed is never not null.
* @return ID
*/
public void setID(String ID){
this.ID = ID;
}
/**
* Set info to bind class generated by this class
* @param info properties need to bind with agent
*/
public void setInfo(Properties info){
this.info = info;
}
}

61
service/src/main/java/com/fanruan/service/jdbc/BasedBind.java

@ -0,0 +1,61 @@
package com.fanruan.service.jdbc;
import com.fanruan.annotation.LocalMethod;
import com.fanruan.service.jdbc.AbstractBind;
import java.util.Properties;
/**
* @author Yichen Dai
* @date 2022/8/31 20:35
*/
public class BasedBind extends AbstractBind {
@LocalMethod
@Override
public String getID() {
return super.getID();
}
@LocalMethod
@Override
public void setID(String ID) {
super.setID(ID);
}
@LocalMethod
@Override
public void setInfo(Properties info) {
super.setInfo(info);
}
@LocalMethod
@Override
public int hashCode() {
return super.hashCode();
}
@LocalMethod
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@LocalMethod
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@LocalMethod
@Override
public String toString() {
return super.toString();
}
@LocalMethod
@Override
protected void finalize() throws Throwable {
super.finalize();
}
}

71
service/src/main/java/com/fanruan/service/jdbc/ServiceArray.java

@ -0,0 +1,71 @@
package com.fanruan.service.jdbc;
import com.fanruan.annotation.RemoteClass;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
/**
* @author Yichen Dai
* @date 2022/8/31 16:51
*/
@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.AgentArray")
public class ServiceArray extends BasedBind implements Array {
@Override
public String getBaseTypeName() throws SQLException {
return null;
}
@Override
public int getBaseType() throws SQLException {
return 0;
}
@Override
public Object getArray() throws SQLException {
return null;
}
@Override
public Object getArray(Map<String, Class<?>> map) throws SQLException {
return null;
}
@Override
public Object getArray(long index, int count) throws SQLException {
return null;
}
@Override
public Object getArray(long index, int count, Map<String, Class<?>> map) throws SQLException {
return null;
}
@Override
public ResultSet getResultSet() throws SQLException {
return null;
}
@Override
public ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException {
return null;
}
@Override
public ResultSet getResultSet(long index, int count) throws SQLException {
return null;
}
@Override
public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map) throws SQLException {
return null;
}
@Override
public void free() throws SQLException {
}
}

73
service/src/main/java/com/fanruan/service/jdbc/ServiceBlob.java

@ -0,0 +1,73 @@
package com.fanruan.service.jdbc;
import com.fanruan.annotation.RemoteClass;
import com.fanruan.service.jdbc.AbstractBind;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;
/**
* @author Yichen Dai
* @date 2022/8/29 20:32
*/
@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.AgentBlob")
public class ServiceBlob extends BasedBind implements Blob {
@Override
public long length() throws SQLException {
return 0;
}
@Override
public byte[] getBytes(long pos, int length) throws SQLException {
return new byte[0];
}
@Override
public InputStream getBinaryStream() throws SQLException {
return null;
}
@Override
public long position(byte[] pattern, long start) throws SQLException {
return 0;
}
@Override
public long position(Blob pattern, long start) throws SQLException {
return 0;
}
@Override
public int setBytes(long pos, byte[] bytes) throws SQLException {
return 0;
}
@Override
public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {
return 0;
}
@Override
public OutputStream setBinaryStream(long pos) throws SQLException {
return null;
}
@Override
public void truncate(long len) throws SQLException {
}
@Override
public void free() throws SQLException {
}
@Override
public InputStream getBinaryStream(long pos, long length) throws SQLException {
return null;
}
}

83
service/src/main/java/com/fanruan/service/jdbc/ServiceClob.java

@ -0,0 +1,83 @@
package com.fanruan.service.jdbc;
import com.fanruan.annotation.RemoteClass;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Clob;
import java.sql.SQLException;
/**
* @author Yichen Dai
* @date 2022/8/29 20:24
*/
@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.AgentClob")
public class ServiceClob extends BasedBind implements Clob {
@Override
public long length() throws SQLException {
return 0;
}
@Override
public String getSubString(long pos, int length) throws SQLException {
return null;
}
@Override
public Reader getCharacterStream() throws SQLException {
return null;
}
@Override
public InputStream getAsciiStream() throws SQLException {
return null;
}
@Override
public long position(String searchstr, long start) throws SQLException {
return 0;
}
@Override
public long position(Clob searchstr, long start) throws SQLException {
return 0;
}
@Override
public int setString(long pos, String str) throws SQLException {
return 0;
}
@Override
public int setString(long pos, String str, int offset, int len) throws SQLException {
return 0;
}
@Override
public OutputStream setAsciiStream(long pos) throws SQLException {
return null;
}
@Override
public Writer setCharacterStream(long pos) throws SQLException {
return null;
}
@Override
public void truncate(long len) throws SQLException {
}
@Override
public void free() throws SQLException {
}
@Override
public Reader getCharacterStream(long pos, long length) throws SQLException {
return null;
}
}

17
service/src/main/java/com/fanruan/service/ServiceDataBaseMetaData.java → service/src/main/java/com/fanruan/service/jdbc/ServiceDataBaseMetaData.java

@ -1,17 +1,19 @@
package com.fanruan.service; package com.fanruan.service.jdbc;
import com.fanruan.annotation.LocalMethod;
import com.fanruan.annotation.RemoteClass;
import com.fanruan.service.jdbc.driver.ServiceDriver; import com.fanruan.service.jdbc.driver.ServiceDriver;
import java.sql.Connection; import java.sql.*;
import java.sql.ResultSet; import java.util.Map;
import java.sql.RowIdLifetime;
import java.sql.SQLException;
/** /**
* @author 85065 * @author Yichen Dai
*/ */
public class ServiceDataBaseMetaData implements java.sql.DatabaseMetaData{ @RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.AgentDataBaseMetaData")
public class ServiceDataBaseMetaData extends BasedBind implements java.sql.DatabaseMetaData{
@Override @Override
public boolean allProceduresAreCallable() throws SQLException { public boolean allProceduresAreCallable() throws SQLException {
return false; return false;
@ -752,6 +754,7 @@ public class ServiceDataBaseMetaData implements java.sql.DatabaseMetaData{
return null; return null;
} }
@LocalMethod
@Override @Override
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
return null; return null;

69
service/src/main/java/com/fanruan/service/jdbc/ServiceParameterMetaData.java

@ -0,0 +1,69 @@
package com.fanruan.service.jdbc;
import com.fanruan.annotation.RemoteClass;
import java.sql.ParameterMetaData;
import java.sql.SQLException;
/**
* @author Yichen Dai
* @date 2022/8/25 11:13
*/
@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.AgentParameterMetaData")
public class ServiceParameterMetaData extends BasedBind implements ParameterMetaData {
@Override
public int getParameterCount() throws SQLException {
return 0;
}
@Override
public int isNullable(int param) throws SQLException {
return 0;
}
@Override
public boolean isSigned(int param) throws SQLException {
return false;
}
@Override
public int getPrecision(int param) throws SQLException {
return 0;
}
@Override
public int getScale(int param) throws SQLException {
return 0;
}
@Override
public int getParameterType(int param) throws SQLException {
return 0;
}
@Override
public String getParameterTypeName(int param) throws SQLException {
return null;
}
@Override
public String getParameterClassName(int param) throws SQLException {
return null;
}
@Override
public int getParameterMode(int param) throws SQLException {
return 0;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
}

29
service/src/main/java/com/fanruan/service/jdbc/ServiceStruct.java

@ -0,0 +1,29 @@
package com.fanruan.service.jdbc;
import com.fanruan.annotation.RemoteClass;
import java.sql.SQLException;
import java.sql.Struct;
import java.util.Map;
/**
* @author Yichen Dai
* @date 2022/8/31 17:03
*/
@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.AgentStruct")
public class ServiceStruct extends BasedBind implements Struct {
@Override
public String getSQLTypeName() throws SQLException {
return null;
}
@Override
public Object[] getAttributes() throws SQLException {
return new Object[0];
}
@Override
public Object[] getAttributes(Map<String, Class<?>> map) throws SQLException {
return new Object[0];
}
}

86
service/src/main/java/com/fanruan/service/jdbc/connection/ServiceConnection.java

@ -2,12 +2,17 @@ package com.fanruan.service.jdbc.connection;
import com.corundumstudio.socketio.SocketIOClient; import com.corundumstudio.socketio.SocketIOClient;
import com.fanruan.annotation.NotImplemented;
import com.fanruan.annotation.RemoteClass; import com.fanruan.annotation.RemoteClass;
import com.fanruan.cache.ClientCache; import com.fanruan.cache.ClientCache;
import com.fanruan.service.ServiceDataBaseMetaData; import com.fanruan.service.jdbc.AbstractBind;
import com.fanruan.service.jdbc.BasedBind;
import com.fanruan.service.jdbc.ServiceDataBaseMetaData;
import com.fanruan.service.jdbc.statement.ServiceCallableStatement;
import com.fanruan.service.jdbc.statement.ServicePreparedStatement; import com.fanruan.service.jdbc.statement.ServicePreparedStatement;
import com.fanruan.service.jdbc.statement.ServiceStatement; import com.fanruan.service.jdbc.statement.ServiceStatement;
import com.fanruan.proxy.ProxyFactory; import com.fanruan.proxy.ProxyFactory;
import com.fanruan.exception.NotImplementedException;
import java.sql.*; import java.sql.*;
import java.util.Map; import java.util.Map;
@ -18,30 +23,11 @@ import java.util.concurrent.Executor;
* @author Yichen Dai * @author Yichen Dai
*/ */
@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.connection.AgentConnection") @RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.connection.AgentConnection")
public class ServiceConnection implements Connection { public class ServiceConnection extends BasedBind implements Connection {
private String ID;
private Properties info;
private boolean autoCommit = true;
SocketIOClient client;
public ServiceConnection(){ public ServiceConnection(){
} }
public void setID(String ID){
this.ID = ID;
}
public String getID(){
return this.ID;
}
public void setInfo(Properties info){
this.info = info;
client = ClientCache.getClient(info.getProperty("agentID"), info.getProperty("agentDBName"));
}
@Override @Override
public Statement createStatement(){ public Statement createStatement(){
ServiceStatement st = (ServiceStatement) ProxyFactory.getProxy(ServiceStatement.class, info); ServiceStatement st = (ServiceStatement) ProxyFactory.getProxy(ServiceStatement.class, info);
@ -52,15 +38,15 @@ public class ServiceConnection implements Connection {
@Override @Override
public PreparedStatement prepareStatement(String sql) throws SQLException { public PreparedStatement prepareStatement(String sql) throws SQLException {
ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info); ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info);
// 将需要准备的sql 加入 properties 中, 将用以标识生成的 ResultSet
info.setProperty("PreparedSQL", sql);
pst.setInfo(info); pst.setInfo(info);
return pst; return pst;
} }
@Override @Override
public CallableStatement prepareCall(String sql) throws SQLException { public CallableStatement prepareCall(String sql) throws SQLException {
return null; ServiceCallableStatement cst = (ServiceCallableStatement) ProxyFactory.getProxy(ServiceCallableStatement.class, info);
cst.setInfo(info);
return cst;
} }
@Override @Override
@ -70,12 +56,11 @@ public class ServiceConnection implements Connection {
@Override @Override
public void setAutoCommit(boolean autoCommit) throws SQLException { public void setAutoCommit(boolean autoCommit) throws SQLException {
this.autoCommit = autoCommit;
} }
@Override @Override
public boolean getAutoCommit() throws SQLException { public boolean getAutoCommit() throws SQLException {
return autoCommit; return true;
} }
@Override @Override
@ -90,17 +75,16 @@ public class ServiceConnection implements Connection {
@Override @Override
public void close() throws SQLException { public void close() throws SQLException {
client.disconnect();
} }
@Override @Override
public boolean isClosed() throws SQLException { public boolean isClosed() throws SQLException {
return client.isChannelOpen(); return true;
} }
@Override @Override
public DatabaseMetaData getMetaData() throws SQLException { public DatabaseMetaData getMetaData() throws SQLException {
return new ServiceDataBaseMetaData(); return (DatabaseMetaData) ProxyFactory.getProxy(ServiceDataBaseMetaData.class, info);
} }
@Override @Override
@ -145,17 +129,23 @@ public class ServiceConnection implements Connection {
@Override @Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
return null; ServiceStatement st = (ServiceStatement) ProxyFactory.getProxy(ServiceStatement.class, info);
st.setInfo(info);
return st;
} }
@Override @Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return null; ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info);
pst.setInfo(info);
return pst;
} }
@Override @Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return null; ServiceCallableStatement cst = (ServiceCallableStatement) ProxyFactory.getProxy(ServiceCallableStatement.class, info);
cst.setInfo(info);
return cst;
} }
@Override @Override
@ -200,32 +190,44 @@ public class ServiceConnection implements Connection {
@Override @Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return null; ServiceStatement st = (ServiceStatement) ProxyFactory.getProxy(ServiceStatement.class, info);
st.setInfo(info);
return st;
} }
@Override @Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return null; ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info);
pst.setInfo(info);
return pst;
} }
@Override @Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return null; ServiceCallableStatement cst = (ServiceCallableStatement) ProxyFactory.getProxy(ServiceCallableStatement.class, info);
cst.setInfo(info);
return cst;
} }
@Override @Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
return null; ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info);
pst.setInfo(info);
return pst;
} }
@Override @Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
return null; ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info);
pst.setInfo(info);
return pst;
} }
@Override @Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
return null; ServicePreparedStatement pst = (ServicePreparedStatement) ProxyFactory.getProxy(ServicePreparedStatement.class, info);
pst.setInfo(info);
return pst;
} }
@Override @Override
@ -309,12 +311,14 @@ public class ServiceConnection implements Connection {
} }
@Override @Override
@NotImplemented
public <T> T unwrap(Class<T> iface) throws SQLException { public <T> T unwrap(Class<T> iface) throws SQLException {
return null; throw new NotImplementedException();
} }
@Override @Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false; public boolean isWrapperFor(Class<?> iface) throws NotImplementedException {
return true;
} }
} }

18
service/src/main/java/com/fanruan/service/jdbc/driver/ServiceDriver.java

@ -1,6 +1,8 @@
package com.fanruan.service.jdbc.driver; package com.fanruan.service.jdbc.driver;
import com.fanruan.annotation.RemoteClass; import com.fanruan.annotation.RemoteClass;
import com.fanruan.service.jdbc.AbstractBind;
import com.fanruan.service.jdbc.BasedBind;
import com.fanruan.service.jdbc.connection.ServiceConnection; import com.fanruan.service.jdbc.connection.ServiceConnection;
import com.fanruan.proxy.ProxyFactory; import com.fanruan.proxy.ProxyFactory;
@ -13,11 +15,10 @@ import java.util.logging.Logger;
* @author Yichen Dai * @author Yichen Dai
*/ */
@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.driver.AgentDriver") @RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.driver.AgentDriver")
public class ServiceDriver implements Driver { public class ServiceDriver extends BasedBind implements Driver {
static public final int DRIVER_VERSION_MAJOR = 1; static public final int DRIVER_VERSION_MAJOR = 1;
static public final int DRIVER_VERSION_MINOR = 1; static public final int DRIVER_VERSION_MINOR = 1;
private String ID;
//依靠静态函数块注册驱动 //依靠静态函数块注册驱动
static{ static{
@ -29,19 +30,6 @@ public class ServiceDriver implements Driver {
} }
/**
* These corresponding code is to make the format correct, because the getID()
* will be called, even if the filed is never not null.
* @return ID
*/
public String getID(){
return this.ID;
}
public void setID(String ID){
this.ID = ID;
}
@Override @Override
public Connection connect(String url, Properties info){ public Connection connect(String url, Properties info){
String dbName = info.getProperty("agentDBName"); String dbName = info.getProperty("agentDBName");

23
service/src/main/java/com/fanruan/service/jdbc/resultset/ServiceResultSet.java

@ -2,6 +2,8 @@ package com.fanruan.service.jdbc.resultset;
import com.fanruan.annotation.RemoteClass; import com.fanruan.annotation.RemoteClass;
import com.fanruan.service.jdbc.AbstractBind;
import com.fanruan.service.jdbc.BasedBind;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
@ -16,26 +18,7 @@ import java.util.Map;
* @author Yichen Dai * @author Yichen Dai
*/ */
@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.resultset.AgentResultSet") @RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.resultset.AgentResultSet")
public class ServiceResultSet implements ResultSet { public class ServiceResultSet extends BasedBind implements ResultSet {
private String sql;
private String ID;
public String getID(){
return this.ID;
}
public void setID(String ID){
this.ID = ID;
}
public void setSql(String sql){
this.sql = sql;
}
public String getSql(){
return this.sql;
}
@Override @Override
public boolean next() throws SQLException { public boolean next() throws SQLException {

1090
service/src/main/java/com/fanruan/service/jdbc/statement/ServiceCallableStatement.java

File diff suppressed because it is too large Load Diff

22
service/src/main/java/com/fanruan/service/jdbc/statement/ServicePreparedStatement.java

@ -1,6 +1,8 @@
package com.fanruan.service.jdbc.statement; package com.fanruan.service.jdbc.statement;
import com.fanruan.annotation.RemoteClass; import com.fanruan.annotation.RemoteClass;
import com.fanruan.service.jdbc.AbstractBind;
import com.fanruan.service.jdbc.BasedBind;
import com.fanruan.service.jdbc.resultset.ServiceResultSet; import com.fanruan.service.jdbc.resultset.ServiceResultSet;
import com.fanruan.proxy.ProxyFactory; import com.fanruan.proxy.ProxyFactory;
@ -16,24 +18,7 @@ import java.util.Properties;
* @author Yichen Dai * @author Yichen Dai
*/ */
@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.statement.AgentPreparedStatement") @RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.statement.AgentPreparedStatement")
public class ServicePreparedStatement implements PreparedStatement { public class ServicePreparedStatement extends BasedBind implements PreparedStatement {
private Properties info;
private String ID;
public ServicePreparedStatement() {}
public String getID(){
return this.ID;
}
public void setID(String ID){
this.ID = ID;
}
public void setInfo(Properties info) {this.info = info;}
@Override @Override
public ResultSet executeQuery() throws SQLException { public ResultSet executeQuery() throws SQLException {
@ -41,7 +26,6 @@ public class ServicePreparedStatement implements PreparedStatement {
throw new SQLException("This Statement is closed."); throw new SQLException("This Statement is closed.");
} }
ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info); ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info);
rs.setSql(info.getProperty("PreparedSQL"));
return rs; return rs;
} }

22
service/src/main/java/com/fanruan/service/jdbc/statement/ServiceStatement.java

@ -1,6 +1,8 @@
package com.fanruan.service.jdbc.statement; package com.fanruan.service.jdbc.statement;
import com.fanruan.annotation.RemoteClass; import com.fanruan.annotation.RemoteClass;
import com.fanruan.service.jdbc.AbstractBind;
import com.fanruan.service.jdbc.BasedBind;
import com.fanruan.service.jdbc.resultset.ServiceResultSet; import com.fanruan.service.jdbc.resultset.ServiceResultSet;
import com.fanruan.proxy.ProxyFactory; import com.fanruan.proxy.ProxyFactory;
@ -11,24 +13,7 @@ import java.util.Properties;
* @author Yichen Dai * @author Yichen Dai
*/ */
@RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.statement.AgentStatement") @RemoteClass(remoteClassName = "com.fanruan.agent.jdbc.statement.AgentStatement")
public class ServiceStatement implements Statement { public class ServiceStatement extends BasedBind implements Statement {
private Properties info;
private String ID;
public ServiceStatement() {}
public String getID(){
return this.ID;
}
public void setID(String ID){
this.ID = ID;
}
public void setInfo(Properties info){
this.info = info;
}
@Override @Override
public ResultSet executeQuery(String sql) throws SQLException { public ResultSet executeQuery(String sql) throws SQLException {
@ -36,7 +21,6 @@ public class ServiceStatement implements Statement {
throw new SQLException("This Statement is closed."); throw new SQLException("This Statement is closed.");
} }
ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info); ServiceResultSet rs = (ServiceResultSet) ProxyFactory.getProxy(ServiceResultSet.class, info);
rs.setSql(sql);
return rs; return rs;
} }

Loading…
Cancel
Save