远程技术的研究
动态代理必须实现的方式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.equals(java.lang.Object.class))
{
Object value = args;
if(value == null || !Proxy.isProxyClass(value.getClass()))
{
return new Boolean(false);
} else
{
BurlapProxy handler = (BurlapProxy)Proxy.getInvocationHandler(value);
return new Boolean(_url.equals(handler.getURL()));
}
}
if(methodName.equals("hashCode") && params.length == 0)
return new Integer(_url.hashCode());
if(methodName.equals("getBurlapType"))
return proxy.getClass().getInterfaces().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)
{
throw new 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)
{
throw new BurlapRuntimeException(String.valueOf(e));
}
catch(IOException e) { }
if(is != null)
is.close();
throw new BurlapProtocolException(sb.toString());
}
}<
页:
[1]