Failed to lookup LocalBean from a session bean.
eric138 Nov 27, 2001 2:28 AMHello,
Is it possible to reference the Local Interface from a Session Bean?
I tried but failed. Would you please to tell me why or what my mistake is? The following are some of the sources. If you like, I can send you all the sources.
jboss.xml
===================================
<?xml version="1.0" encoding="Cp1252"?>
false
<container-configurations />
<resource-managers />
<enterprise-beans>
<ejb-name>CounterControllerEJB</ejb-name>
<jndi-name>
ejb/counter/CounterController
</jndi-name>
<configuration-name></configuration-name>
<ejb-name>CounterEJB</ejb-name>
<jndi-name>
ejb/local/counter/Counter
</jndi-name>
<configuration-name></configuration-name>
</enterprise-beans>
ejb-jar.xml
========================
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC
"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN"
"http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">
<ejb-jar>
<display-name>Counter</display-name>
<enterprise-beans>
Models Counter
<ejb-name>CounterEJB</ejb-name>
<local-home>counter.interfaces.CounterLocalHome</local-home>
counter.interfaces.CounterLocal
<ejb-class>counter.bean.CounterEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
False
<cmp-version>2.x</cmp-version>
<cmp-field><field-name>counterName</field-name></cmp-field>
<cmp-field><field-name>counter</field-name></cmp-field>
<primkey-field>counterName</primkey-field>
Models Counter controller
<ejb-name>CounterControllerEJB</ejb-name>
counter.interfaces.CounterControllerHome
counter.interfaces.CounterController
<ejb-class>counter.bean.CounterControllerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<ejb-local-ref>
<ejb-ref-name>ejb/local/counter/Counter</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>counter.interfaces.CounterLocalHome</local-home>
counter.interfaces.CounterLocal
<ejb-link>CounterEJB</ejb-link>
</ejb-local-ref>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<ejb-name>CounterEJB</ejb-name>
<method-intf>Local</method-intf>
<method-name>*</method-name>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
CounterControllerEJB.java
================================
package counter.bean;
import java.util.Collection;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import counter.interfaces.CounterLocal;
import counter.interfaces.CounterLocalHome;
public class CounterControllerEJB implements SessionBean {
public static final String COUNTER_PATH = "java:comp/env/ejb/local/counter/Counter";
/** Helper function to get the home interface of a specified Bean. This is
* used by most of the methods in this class.
*/
private Object getHome (String path, Class type) {
try {
InitialContext jndiContext = new InitialContext();
Object ref = jndiContext.lookup(path);
return ref;
//return PortableRemoteObject.narrow(ref, type);
} catch (Exception e) {
e.printStackTrace();
throw new EJBException(e);
}
}
public long increaseCounter(String counterName) throws RemoteException {
return increaseCounter(counterName, 1);
}
public long increaseCounter(String counterName, long num) throws RemoteException {
CounterLocalHome counterHome = (CounterLocalHome)getHome(COUNTER_PATH, CounterLocalHome.class);
long counter = -1;
try {
CounterLocal oldCounter = (CounterLocal)counterHome.findByPrimaryKey(counterName);
counter = oldCounter.increaseCounter(num);
} catch(Exception e) {
System.out.println("Counter<" + counterName + "> not existed, create a new Counter.");
try {
CounterLocal newCounter = counterHome.create(counterName);
counter = newCounter.increaseCounter(num);
} catch(Exception e1) {
e1.printStackTrace();
}
}
return counter;
}
public long decreaseCounter(String counterName) throws RemoteException {
return decreaseCounter(counterName, 1);
}
public long decreaseCounter(String counterName, long num) throws RemoteException {
CounterLocalHome counterHome = (CounterLocalHome)getHome(COUNTER_PATH, CounterLocalHome.class);
long counter = -1;
try {
CounterLocal oldCounter = (CounterLocal)counterHome.findByPrimaryKey(counterName);
counter = oldCounter.increaseCounter(num);
} catch(Exception e) {
System.out.println("Counter<" + counterName + "> not existed.");
}
return counter;
}
public CounterLocal lookup(String counterName) throws RemoteException, FinderException {
CounterLocalHome home = (CounterLocalHome) getHome(COUNTER_PATH, CounterLocalHome.class);
return (CounterLocal)home.findByPrimaryKey(counterName);
}
/**
* Returns an array of all Counters in the collection
*/
public CounterLocal[] findAll() throws RemoteException, FinderException {
CounterLocalHome home = (CounterLocalHome) getHome(COUNTER_PATH, CounterLocalHome.class);
Collection all = home.findAll();
return (CounterLocal[]) all.toArray(new CounterLocal[0]);
}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}