在远程访问中服务端生成响应的骨架,在客户端访问远程服务必须使用响应的代理,
我们(⊙o⊙)看一看远程骨架类和远程代理的源代码分析吧。
注意在看源代码时你必须学会反射机制哦,如果不会可以首先查看
JDK API慢慢看也可以(⊙o⊙)哦
反射机制使用很广泛希望不会的同时可以参考我的 Java基础有类似反射基础的东东个(⊙o⊙)哦
同时必须Java的网络编程否则可能头疼?(+﹏+)~狂晕
开始分析了~\(≧▽≦)/~啦啦啦~\(≧▽≦)/~啦啦啦
客户端必须使用BurlapProxyFactory类
远程服务代理
package com.caucho.burlap.client;
import com.caucho.burlap.io.AbstractBurlapInput;
import com.caucho.burlap.io.BurlapOutput;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
使用JDK动态代理的实现代理
publicclass BurlapProxy
implements InvocationHandler
{
BurlapProxy(BurlapProxyFactory factory, URL url)
{
_factory = factory;
_url = url;
}
public URL getURL()
{
return_url;
}
动态代理必须实现的方式
public Object invoke(Object proxy, Method method, Object args[])
throws Throwable
{
String methodName;
InputStream is;
methodName = method.getName();
Class params[] = method.getParameterTypes();
if(methodName.equals("equals") && params.length == 1 && params[0].equals(java.lang.Object.class))
{
Object value = args[0];
if(value == null || !Proxy.isProxyClass(value.getClass()))
{
returnnew Boolean(false);
} else
{
BurlapProxy handler = (BurlapProxy)Proxy.getInvocationHandler(value);
returnnew Boolean(_url.equals(handler.getURL()));
}
}
if(methodName.equals("hashCode") && params.length == 0)
returnnew Integer(_url.hashCode());
if(methodName.equals("getBurlapType"))
return proxy.getClass().getInterfaces()[0].getName();
if(methodName.equals("getBurlapURL"))
return_url.toString();
if(methodName.equals("toString") && params.length == 0)
return"BurlapProxy[" + _url + "]";
is = null;
Object obj;
try
{
创建URLConnection对象根据url获取相应的响应流由上面的MicroBurlapOutput可以知道输出的流一定是一个关于服务信息的xml文件。
URLConnection conn = _factory.openConnection(_url);
conn.setRequestProperty("Content-Type", "text/xml");
获取响应的信息
OutputStream os = conn.getOutputStream();
将响应的信息进行封装
BurlapOutput out = _factory.getBurlapOutput(os);
if(_factory.isOverloadEnabled())
if(args != null)
methodName = methodName + "__" + args.length;
else
methodName = methodName + "__0";
out.call(methodName, args);
os.flush();
if(conn instanceof HttpURLConnection)
{
HttpURLConnection httpConn = (HttpURLConnection)conn;
int code = 500;
try
{
code = httpConn.getResponseCode();
}
catch(Exception e)
{
thrownew BurlapRuntimeException(String.valueOf(e));
}
注意响应的代码号非200即出现异常信息必须是处理异常,封装发送值客户端信息
if(code != 200)
{
StringBuffer sb = new StringBuffer();
try
{
is = httpConn.getInputStream();
int ch;
if(is != null)
{
while((ch = is.read()) >= 0)
sb.append((char)ch);
is.close();
}
is = httpConn.getErrorStream();
if(is != null)
while((ch = is.read()) >= 0)
sb.append((char)ch);
}
catch(FileNotFoundException e)
{
thrownew BurlapRuntimeException(String.valueOf(e));
}
catch(IOException e) { }
if(is != null)
is.close();
thrownew BurlapProtocolException(sb.toString());
}
}
catch(IOException e) { }
if(is != null)
is.close();
thrownew BurlapProtocolException(sb.toString());
}
}
is = conn.getInputStream();
AbstractBurlapInput in = _factory.getBurlapInput(is);
obj = in.readReply(method.getReturnType());
}
catch(BurlapProtocolException e)
{
thrownew BurlapRuntimeException(e);
}
if(is != null)
is.close();
return obj;
}
private BurlapProxyFactory _factory;
private URL _url;
}