Hessian源码分析和Hack --让Hessian携带远程调用端的信息
时间:2011-06-25 iteye
项目选定Hessian作为web service的实现方式,确实很轻量级,速度就跟直接用socket差不多,全是二进制传送节约了不少开销。但是在使用过程中有业务需要是必须获得远程端的ip地址,主机名等信息的。翻便Hessian的文档和google了n次未果,迫不得已到caucho和spring论坛去问,都没有得到答复。今天心一横把hessian的源代码加入到项目中单步跟踪,总算有点小收获。献丑分享出来,一方面给需要的朋友,主要还是希望各位找找是否存在bug,以及是否有更好的改良。
一:先撇开Spring不谈,来看看纯Hessian的调用
按照hessian文档里边介绍的demo,在web.xml里边如下配置
Java代码
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>example.BasicService</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>example.Basic</param-value>
</init-param>
</servlet>
<servlet-mapping>
<url-pattern>/hello</url-pattern>
<servlet-name>hello</servlet-name>
</servlet-mapping>
由此可知Hessian调用的入口是HessianServlet这个Servlet,进去看看
Hessian源码分析和Hack --让Hessian携带远程调用端的信息(2)
时间:2011-06-25 iteye
Java代码
/**
* Servlet for serving Hessian services.
*/
public class HessianServlet extends GenericServlet {
private Class _homeAPI;
private Object _homeImpl;
private Class _objectAPI;
private Object _objectImpl;
private HessianSkeleton _homeSkeleton;
private HessianSkeleton _objectSkeleton;
private SerializerFactory _serializerFactory;
public String getServletInfo()
{
return "Hessian Servlet";
}
/**
* Sets the home api.
*/
public void setHomeAPI(Class api)
{
_homeAPI = api;
}
/**
* Sets the home implementation
*/
public void setHome(Object home)
{
_homeImpl = home;
}
/**
* Sets the object api.
*/
public void setObjectAPI(Class api)
{
_objectAPI = api;
}
/**
* Sets the object implementation
*/
public void setObject(Object object)
{
_objectImpl = object;
}
/**
* Sets the service class.
*/
public void setService(Object service)
{
setHome(service);
}
/**
* Sets the api-class.
*/
public void setAPIClass(Class api)
{
setHomeAPI(api);
}
/**
* Gets the api-class.
*/
public Class getAPIClass()
{
return _homeAPI;
}
/**
* Sets the serializer factory.
*/
public void setSerializerFactory(SerializerFactory factory)
{
_serializerFactory = factory;
}
/**
* Gets the serializer factory.
*/
public SerializerFactory getSerializerFactory()
{
if (_serializerFactory == null)
_serializerFactory = new S
|