在远程方法调用中运用 《代理类》

//Connector.java
package proxy1;
import java.io.*;
import java.net.*;
import java.util.*;
public class Connector {
  private String host;
  private int port;
  private Socket skt;
  private InputStream is;
  private ObjectInputStream ois;
  private OutputStream os;
  private ObjectOutputStream oos;
  public Connector(String host,int port)throws Exception{
     this.host=host;
     this.port=port;
     connect(host,port);
  }
  public void send(Object obj)throws Exception{
    oos.writeObject(obj);
  }
  public Object receive() throws Exception{
    return ois.readObject();
  }
  public void connect()throws Exception{
    connect(host,port);
  }
  public void connect(String host,int port)throws Exception{
    skt=new Socket(host,port);
    os=skt.getOutputStream();
    oos=new ObjectOutputStream(os);
    is=skt.getInputStream();
    ois=new ObjectInputStream(is);
  }
  public void close(){
    try{
    }finally{
      try{
        ois.close();
        oos.close();
        skt.close();
      }catch(Exception e){
        System.out.println("Connector.close: "+e);
      }
    }
  }
}

//Call.java

package proxy1;
import java.io.*;
public class Call implements Serializable{
  private String className;  //表示类名
  private String methodName; //表示方法名
  private Class[] paramTypes; //表示方法参数类型
  private Object[] params; //表示方法参数值
  private Object result;  //表示方法的返回值或者方法抛出的异常
  
  public Call(){}
  public Call(String className,String methodName,Class[] paramTypes,
                            Object[] params){
    this.className=className;
    this.methodName=methodName;
    this.paramTypes=paramTypes;
    this.params=params;
  }
  
  public String getClassName(){return className;}
  public void setClassName(String className){this.className=className;}
  public String getMethodName(){return methodName;}
  public void setMethodName(String methodName){this.methodName=methodName;}
  public Class[] getParamTypes(){return paramTypes;}
  public void setParamTypes(Class[] paramTypes){this.paramTypes=paramTypes;}
  public Object[] getParams(){return params;}
  public void setParams(Object[] params){this.params=params;}
  public Object getResult(){return result;}
  public void setResult(Object result){this.result=result;}
  public String toString(){
    return "className="+className+" methodName="+methodName;
  }

 //RemoteException.java

 

package proxy1;

 

public class RemoteException extends Exception{
  public RemoteException(Throwable e){
    super(e);
  }
}

 

 

 //静态代理类

package proxy1;
import java.util.Date;
public class HelloServiceProxy implements HelloService{
  private String host;
  private int port;
  
  public HelloServiceProxy(String host,int port){
     this.host=host;
     this.port=port;
  }
  public String echo(String msg)throws RemoteException{
    Connector connector=null;
    try{
      connector=new Connector(host,port);
      Call call=new Call("proxy1.HelloService","echo",new Class[]{String.class},new Object[]{msg}); 
      connector.send(call);
      call=(Call)connector.receive();
      Object result=call.getResult();
      if(result instanceof Throwable)
        throw new RemoteException((Throwable)result);
      else
        return (String)result;
    }catch(Exception e){
       throw new RemoteException(e);
    }finally{if(connector!=null)connector.close();}
  }
  public Date getTime()throws RemoteException{
    Connector connector=null;
    try{
      connector=new Connector(host,port);
      Call call=new Call("proxy1.HelloService","getTime",new Class[]{},new Object[]{}); 
      connector.send(call);
      call=(Call)connector.receive();
      Object result=call.getResult();
      if(result instanceof Throwable)
        throw new RemoteException((Throwable)result);
      else
        return (Date)result;
     }catch(Exception e){
       throw new RemoteException(e);
     }finally{if(connector!=null)connector.close();}
  }

 //动态代理类

package proxy1;
import java.lang.reflect.*;
public class ProxyFactory {
  public static Object getProxy(final Class classType,final String host,final int port){
    InvocationHandler handler=new InvocationHandler(){
      public Object invoke(Object proxy,Method method,Object args[])
                                                       throws Exception{
        Connector connector=null;
        try{
          connector=new Connector(host,port);
          Call call=new Call(classType.getName(),
              method.getName(),method.getParameterTypes(),args); 
          connector.send(call);
          call=(Call)connector.receive();
          Object result=call.getResult();
          if(result instanceof Throwable)
            throw new RemoteException((Throwable)result);
          else
            return result;
        }finally{if(connector!=null)connector.close();}
      }
    };
    return Proxy.newProxyInstance(classType.getClassLoader(),
                                          new Class[]{classType},
                                          handler);
  }
}

 

posted @ 2018-11-30 22:00  lomt5  阅读(77)  评论(0)    收藏  举报