论坛首页 Java版 Webwork

[原创]hessian使用心得!(改为例子)

浏览 5225 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2005-03-29
最近被JAVA 序例化问题折磨了几天.把RMI、Hessian、HTTP invoker、Burlap这四种轻量级分布式组件技术都试用了一遍。

最后选定hessian作为异构环境下的组件传输方式。由于hessian不能序例化复杂的类。因此采用MAP,把BEAN的属性和值倒到MAP中进行转输。

下面给出服务器端的一个远程IMPL,作用是把认证中心的用户信息,用户所属组织,用户的角色,角色的权限提取出来。

[code:1]
package remote.passCenter.service;

import java.util.*;

import org.apache.log4j.*;
import common.spring.dao.*;
import remote.passCenter.pojo.*;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.BeanUtils;

/**
* <p>Title: 远程调用,取用户信息</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2004</p>
*
* <p>Company: </p>
*
* @author  段洪杰
* @version 1.0
*/
public class LogonRemoteServiceImpl
    implements ILogonRemoteService {

  private IBaseDAO baseDAO;

  /** 初始化域 */
  Class sessionService = new Session().getClass();

  Class userService = new User().getClass();

  Class roleService = new Role().getClass();

  Class authService = new Auth().getClass();

  Class teamService = new Team().getClass();

  private static Logger log = Logger.getLogger(LogonRemoteServiceImpl.class);
  /**
   * 检查是否已登录,包括IP认证
   * @param sid String
   * @return boolean
   * @throws FacadeRemoteServiceException
   */
  public boolean check(String sid, String ip) throws
      LogonRemoteServiceException {
    baseDAO.init(sessionService);
    boolean result;
    Session session = null;
    try {
      session = (Session) baseDAO.getObjectById(sid);
    }
    catch (Exception ex) {
      //throw new FacadeRemoteServiceException(ex);
      // 没有查到ID的数据时出错,.
      result = false;
    }
    if (session == null) {
      result = false;
    }
    else {
      String ip1 = session.getIp();
      if (ip1 != null && ip1.equals(ip)) {
        result = true;
      }
      else {
        result = false;
      }
    }
    return result;
  }

  /**
   * 取得用户
   * 把class的属性放到MAP中,通过hessian传输
   * @param sid String
   * @return User
   * @throws FacadeRemoteServiceException
   */
  public Map getUser(String sid) throws LogonRemoteServiceException {
    baseDAO.init(sessionService);
    Session session = null;
    User user = null;
    Map userMap = new Hashtable();

    try {
      session = (Session) baseDAO.getObjectById(sid);
    }
    catch (Exception ex) {
      // 没有查到ID的数据时出错.
    }

    try {
      if (session != null) {
        String registerName = session.getRegisterName();
        baseDAO.init(userService);
        user = (User) baseDAO.getObjectByRegisterName(registerName);
        if (user != null) {
          //把user转为MAP
          userMap = BeanUtils.describe(user);
        }

      }
    }
    catch (Exception ex) {
      throw new LogonRemoteServiceException(ex);
    }
    return userMap;
  }
  /**
   * 取得角色
   * 把class的属性放到MAP中,通过hessian传输
   * @param sid String
   * @return Map
   * @throws LogonRemoteServiceException
   */
  public Map getRole(String sid) throws LogonRemoteServiceException {

    Map roleMap = new Hashtable();
    Map userMap = getUser(sid);

    //如果没有找到用户,则返回.
    if (!userMap.isEmpty()) {
      String roleId =(String) userMap.get("roleId");
      if (roleId != null && !roleId.equals("")) {
        try {
          baseDAO.init(roleService);
          Role role = (Role) baseDAO.getObjectById(roleId);
          if (role != null) {
            //把role转为MAP
            roleMap = BeanUtils.describe(role);
          }
        }
        catch (Exception ex) {
          throw new LogonRemoteServiceException(ex);
        }
      }
    }
    return roleMap;
  }
  /**
   * 取得组织
   * 把class的属性放到MAP中,通过hessian传输
   * @param sid String
   * @return Map
   * @throws LogonRemoteServiceException
   */
  public Map getTeam(String sid) throws LogonRemoteServiceException {

    Map teamMap = new Hashtable();
    Map userMap = getUser(sid);

    //如果没有找到用户,则返回.
    if (!userMap.isEmpty()) {
      String teamId =(String) userMap.get("teamId");
      if (teamId != null && !teamId.equals("")) {
        try {
          baseDAO.init(teamService);
          Team team = (Team) baseDAO.getObjectById(teamId);
          if (team != null) {
            //把team转为MAP
            teamMap = BeanUtils.describe(team);
          }
        }
        catch (Exception ex) {
          throw new LogonRemoteServiceException(ex);
        }
      }
    }
    return teamMap;
  }
 
  /**
  * 取得权限
  * 把class的属性放到MAP中,通过hessian传输
  * @param sid String
  * @return Map
  * @throws LogonRemoteServiceException
  */
public Map getAuth(String sid) throws LogonRemoteServiceException {

   Map authMap = new Hashtable();
   Map roleMap = getRole(sid);

   //如果没有找到用户,则返回.
   if (!roleMap.isEmpty()) {
     String authId =(String) roleMap.get("authId");
     if (authId != null && !authId.equals("")) {
       try {
         baseDAO.init(authService);
         Auth auth = (Auth) baseDAO.getObjectById(authId);
         if (auth != null) {
           //把auth转为MAP
           authMap = BeanUtils.describe(auth);
         }
       }
       catch (Exception ex) {
         throw new LogonRemoteServiceException(ex);
       }
     }
   }
   return authMap;
}

  ///////////////////////////////////////////////////


  public void setBaseDAO(IBaseDAO baseDAO) {
    this.baseDAO = baseDAO;
  }

  public IBaseDAO getBaseDAO() {
    return baseDAO;
  }
}

[/code:1]
   
最后更新时间:2005-03-29
这只能算一个hessian的例子吧,心得呢?
   
0 请登录后投票
最后更新时间:2005-03-29
zzeric 写道
这只能算一个hessian的例子吧,心得呢?


标题改为例子了!
   
0 请登录后投票
最后更新时间:2005-03-29
hessian是同构背景下的remotion方案,比如java<---->java
   
0 请登录后投票
最后更新时间:2005-03-29
ruby 写道
hessian是同构背景下的remotion方案,比如java<---->java


请你不要误导别人,是异构的!

Hessian and/or Burlap might provide significant value when operating in a heterogeneous environment,
because they explicitly allow for non-Java clients. However, non-Java support is still limited. Known problems
include the serialization of Hibernate objects in combination with lazily initializing collections. If you have
such a data model, consider using RMI or HTTP invokers instead of Hessian.
   
0 请登录后投票
最后更新时间:2005-03-30
非java的实现做得不好
   
0 请登录后投票
最后更新时间:2005-03-30
dhj1 写道
ruby 写道
hessian是同构背景下的remotion方案,比如java<---->java


请你不要误导别人,是异构的!

Hessian and/or Burlap might provide significant value when operating in a heterogeneous environment,
because they explicitly allow for non-Java clients. However, non-Java support is still limited. Known problems
include the serialization of Hibernate objects in combination with lazily initializing collections. If you have
such a data model, consider using RMI or HTTP invokers instead of Hessian.


建议段兄不如把异构调用(non-java)的实现贴出来,这样更有意义。同构的看hessian的helloworld例子更简单。
   
0 请登录后投票
最后更新时间:2005-04-01
前段时间看过这个, Java和C#之间可以互通, 只要Java和C#两边的Class Name和Package Name一致. Hessian的C#实现可以把Hessian Spec里指定的类型在两边做个协调, 而自定义的类就要通过一直的名字来做映射.

不过使用下来C#的实现存在不少问题需要自己改些东西.
   
0 请登录后投票
最后更新时间:2005-04-01
老段举例子老是这么不得要领。你那代码跟hessian有嘛关系?
   
0 请登录后投票
最后更新时间:2005-04-02
webservice一定比这个慢是不是
   
0 请登录后投票
论坛首页 Java版 Webwork

跳转论坛:
JavaEye推荐