package com.sharetop.shareadsystem.admanager;import java.rmi.*;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.*;
import com.sharetop.shareadsystem.usermanager.UserBean;
import com.sharetop.shareadsystem.SadsException;
import java.util.*;/**
 * Sharetop shareAdSystem's ADManager EJB group.
 * Sharetop Software Studio.Copyright(C)2002
 * @author:yancheng([email protected])
 * @version 1.0
 * */
public class SadsSiteManagerBean implements SessionBean {
  private SessionContext sessionContext;
  private SadsSiteinfoHome siteHome;
  private SadsAdtypeHome typeHome;
  private ArrayList siteList=null;
  private UserBean user;  public void ejbCreate(UserBean user) {
    this.user=user;
    readEnvironmentEntity();
  }
  public void ejbRemove() {
  }
  public void ejbActivate() {
  }
  public void ejbPassivate() {
  }
  public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
  }  //business method
  /**
   * 列出所有栏目
   * */
  public ArrayList listSite() throws SadsException {
    if( !user.getUserRole().equals("sysadmin") )  throw new SadsException("No privilege.");
    if( siteList==null ){
     try{
        siteList=new ArrayList();
        Collection coll = siteHome.findAll();
        Iterator iter = coll.iterator();
        while(iter.hasNext()){
          siteList.add(((SadsSiteinfo)iter.next()).getColumnInfo());
        }
      }
      catch(Exception ex){
        throw new SadsException("Error list all column");
      }
    }//end if
    return siteList;
  }
  /**
   * 新增栏目
   * @param name 栏目名
   * @param url 栏目URL
   * */
  public void addColumn(String name,String url) throws SadsException {
    if( !user.getUserRole().equals("sysadmin") )  throw new SadsException("No privilege.");
    String ctime = Long.toString(new Date().getTime());
    try{
      siteHome.create(ctime,name,url);
    }
    catch(Exception ex){
      throw new SadsException("Error add column.");
    }
  }
  /**
   * 删除栏目
   * @param id 栏目ID
   * */
  public void deleteColumn(String id) throws SadsException{
    if( !user.getUserRole().equals("sysadmin") )  throw new SadsException("No privilege.");
    try{
      siteHome.findByPrimaryKey(id).remove();
    }
    catch(Exception ex){
      throw new SadsException("Error delete column.");
    }
  }
  /**
   * 修改栏目信息
   * @param name 栏目名
   * @param url 栏目URL
   * */
  public void modifyColumn(String id,String name,String url) throws SadsException {
    if( !user.getUserRole().equals("sysadmin") )  throw new SadsException("No privilege.");
    try{
      SadsSiteinfo siteInfo = siteHome.findByPrimaryKey(id);
      siteInfo.setColumnInfo(name,url);
    }
    catch(Exception ex){
      throw new SadsException("Error modify column.");
    }
  }
  /**
   * 获取广告代码
   * @param id 广告类型ID
   * */
  public String getAdCode(String id) throws SadsException{
    String res=null;
    if( !user.getUserRole().equals("operator") )  throw new SadsException("No privilege.");
    try{
      SadsAdtype adtype = typeHome.findByPrimaryKey(id);
      String[] adtypeInfo = adtype.getAdType();
      res = adtypeInfo[1];
    }
    catch(Exception ex){
      throw new SadsException("Error get Advertisement Code");
    }
    return res;
  }  //helper method
  /**
   * 读环境变量
   * */
  private void readEnvironmentEntity(){
     try{
        Context ictx=new InitialContext();
        siteHome = (SadsSiteinfoHome)PortableRemoteObject.narrow(
            ictx.lookup("java:comp/env/ejb/SiteInfoEJB"),
            SadsSiteinfoHome.class);
        typeHome = (SadsAdtypeHome)PortableRemoteObject.narrow(
            ictx.lookup("java:comp/env/ejb/AdTypeEJB"),
            SadsAdtypeHome.class);
     }
     catch(Exception ex){
      throw new EJBException(ex);
     }
  }
}

解决方案 »

  1.   

    sharetop放心,我不会少你分的,请再看
    Context sessionContext=new InitialContext();(1)UserEntityRemoteHome userHome=(UserEntityRemoteHome)PortableRemoteObject.narrow(sessionContext.lookup("java:comp/env/ejb/UserEntityBeanRemote"),UserEntityRemoteHome.class);(2)UserEntityRemoteHome userHome=(UserEntityRemoteHome)sessionContext.lookup("UserEntityRemoteHome");
    UserEntityRemote user=userHome.findByPrimaryKey(userId);
    这两种方法都不行报错:
    java.rmi.RemoteException: EJB Exception: ; nested exception is: 
    javax.ejb.EJBException
     - with nested exception:
    [javax.naming.NameNotFoundException: Unable to resolve UserEntityRemoteHome. Resolved: '' Unresolved:'UserEntityRemoteHome' ; remaining name '']javax.naming.NameNotFoundException: Unable to resolve UserEntityRemoteHome. Resolved: '' Unresolved:'UserEntityRemoteHome' ; remaining name '' <<no stack trace available>>-- Failed: isUser(zack1024, 8224036)-- Return value from isUser(zack1024, 8224036): false.--------------- nested within: ------------------javax.ejb.EJBException
     - with nested exception:
    [javax.naming.NameNotFoundException: Unable to resolve UserEntityRemoteHome. Resolved: '' Unresolved:'UserEntityRemoteHome' ; remaining name ''] <<no stack trace available>>
      

  2.   

    UserEntityRemoteHome你是否指定过这个JNDI名,是否指定对了?就是这个错了
      

  3.   

    还没赶上回答:)不错,根本的问题在于你的布署,你是如何设置jndi名的,是用什么服务器?还有,一般我们lookup的不是什么home或remote接口,是一个jndi名字。比如我用jboss,ejb-jar.xml文件中是这样的:        <session>
                <ejb-name>SadsSiteManagerEJB</ejb-name>
                <home>com.sharetop.shareadsystem.admanager.SadsSiteManagerHome</home>
                <remote>com.sharetop.shareadsystem.admanager.SadsSiteManager</remote>
                <ejb-class>com.sharetop.shareadsystem.admanager.SadsSiteManagerBean</ejb-class>
                <session-type>Stateful</session-type>
                <transaction-type>Container</transaction-type>
                <ejb-ref>
                    <description />
                    <ejb-ref-name>ejb/SiteInfoEJB</ejb-ref-name>
                    <ejb-ref-type>Entity</ejb-ref-type>
                    <home>com.sharetop.shareadsystem.admanager.SadsSiteinfoHome</home>
                    <remote>com.sharetop.shareadsystem.admanager.SadsSiteinfo</remote>
                    <ejb-link>SadsSiteinfoEJB</ejb-link>
                </ejb-ref>
                <ejb-ref>
                    <description />
                    <ejb-ref-name>ejb/AdTypeEJB</ejb-ref-name>
                    <ejb-ref-type>Entity</ejb-ref-type>
                    <home>com.sharetop.shareadsystem.admanager.SadsAdtypeHome</home>
                    <remote>com.sharetop.shareadsystem.admanager.SadsAdtype</remote>
                    <ejb-link>SadsAdtypeEJB</ejb-link>
                </ejb-ref>
            </session>
      

  4.   

    我看了你的xml的文档,应该是要描述两个bean的参考关系,我用jb+weblogic要怎样做,难道手工编辑xml文档么?
      

  5.   

    我给你个网址,上面有介绍用JBuilder4+Weblogic做EJB的sample,还不错,应该比较容易理解。看看吧http://www.5xsoft.com/data/200109/0411375101.htm这上面用BAS4.5,其实到了JBuilder6中就不需要BAS支持,也可以新建EJB,不过你要将Weblogic和JBuilder先集成好,自己试试看吧
      

  6.   

    jackyhj(幽谷客) 
    问题不在那,单独的entitybean,sessionbean我都会做了,现在我的问题是调用的问题
      

  7.   

    不是吧,我怎么没发现,我用jb6,用ejb2.0designer
      

  8.   

    (1)UserEntityRemoteHome userHome=(UserEntityRemoteHome)PortableRemoteObject.narrow(sessionContext.lookup("java:comp/env/ejb/UserEntityBeanRemote"),UserEntityRemoteHome.class);(2)UserEntityRemoteHome userHome=(UserEntityRemoteHome)sessionContext.lookup("UserEntityRemoteHome");
    有什么差别
      

  9.   

    xioyoo(xioyoo)
    ???
    真的,那奇怪了,我就是用第二种方法连上的,而wrox的那本ejb编程指南上有个例子也完全是这种方式