Hi everybody,
As I am developing a J2EE application which includes session and entity beans I need your help or some technical advice.
Here is my situation:
When deploying my EJB package in JBOSS, the JNDI lookup over the ServiceLocator class works fine and the application is running well.
But after exporting the whole thing to the new Sun ONE Application Server, there is always something going wrong.
So far, after installation, I've done the follwing before deploying:
- install the app server
- create a jdbc ressource and ressouce pool (jndi name: jdbc/MySqlDS)
- change ejb-jar.xml
- create sun-ejb-sun.xml
- pack everything to a JAR
- include the JAR in an empty EAR
- deploy
When trying to test with a dummy client, I always get the error notice:
"javax.naming.NameNotFoundException: comp not bound
at EventGuideEJB.Patterns.ServiceLocator.getHome(ServiceLocator.java:..)
..."
When trying to change the name paramters for the lookup in the getHome-method, the error notice varies from
a) with: context.lookup("java:comp/env/ejb/"+name)
-> see above
b) with: context.lookup("java:ejb/"+name)
"javax.naming.NameNotFoundException: ejb not bound
at EventGuideEJB.Patterns.ServiceLocator.getHome(ServiceLocator.java:...)
..."
c) with: context.lookup(name)
"javax.naming.CommunicationException: No route to host: Datagram send failed
[Root exception is java.net.NoRouteToHostException: No route to host: Datagram send failed]
at EventGuideEJB.Patterns.ServiceLocator.getHome(ServiceLocator.java:...)
..."
or after manually adding the Hasmap details holding the naming-properties from the jndi.properties file straight
to the program code:
c) with: context.lookup(name)
"javax.naming.NameNotFoundException: mySupposedEJBJndiName not bound
at EventGuideEJB.Patterns.ServiceLocator.getHome(ServiceLocator.java:...)
..."
All errors occur in the same line (I'll highlight that in the code fragments)
For better understanding, here are my
A - ServiceLocator class
B - ejb-jar.xml file
C - sun-ejb-jar.xml file
A - ServiceLocator
-------------------
package EventGuideEJB.Patterns;
import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.ejb.EJBMetaData;
import javax.ejb.EJBObject;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;
import java.io.*;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Hashtable;
public class ServiceLocator implements HomeFinder, DataSourceFinder, Serializable
{
private static ServiceLocator instance = null;
private Context context = null;
private Hashtable properties = null;
private HashMap homeCache = null;
private HashMap homeLocalCache = null;
private HashMap dataSourceCache = null;
private ServiceLocator(Hashtable properties) throws ServiceLocatorException
{
this.properties = properties;
this.init();
}
private ServiceLocator() throws ServiceLocatorException
{
this.properties = new Hashtable();
this.properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
this.properties.put("java.naming.provider.url","jnp://localhost:1099");
this.init();
}
public static ServiceLocator getInstance(Hashtable properties) throws ServiceLocatorException
{
if (instance == null)
{
instance = new ServiceLocator(properties);
}
return instance;
}
public static ServiceLocator getInstance() throws ServiceLocatorException
{
if (instance == null)
{
instance = new ServiceLocator();
}
return instance;
}
private void init() throws ServiceLocatorException
{
try
{
if (this.properties != null)
{
this.context = new InitialContext(this.properties);
}
else
{
this.context = new InitialContext();
}
this.homeCache = new HashMap();
this.homeLocalCache = new HashMap();
this.dataSourceCache = new HashMap();
}
catch (NamingException e)
{
throw new ServiceLocatorException(e);
}
}
public EJBObject getService(String id) throws ServiceLocatorException
{
if (id == null)
{
throw new ServiceLocatorException("...");
}
try
{
byte[] bytes = new String(id).getBytes();
InputStream io = new ByteArrayInputStream(bytes);
ObjectInputStream os = new ObjectInputStream(io);
javax.ejb.Handle handle = (javax.ejb.Handle) os.readObject();
return handle.getEJBObject();
}
catch (Exception ex)
{
throw new ServiceLocatorException("....");
}
}
protected String getId(EJBObject session) throws ServiceLocatorException
{
try
{
javax.ejb.Handle handle = session.getHandle();
ByteArrayOutputStream fo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(fo);
so.writeObject(handle);
so.flush();
so.close();
return new String(fo.toByteArray());
}
catch (RemoteException ex)
{
throw new ServiceLocatorException(".....");
}
catch (IOException ex)
{
throw new ServiceLocatorException("... ...");
}
}
public EJBHome getHome(String name, boolean cached) throws ServiceLocatorException
{
Object temp = null;
if (cached)
{
temp = this.homeCache.get(name);
}
EJBHome home = null;
if (temp != null && temp instanceof EJBHome)
{
return (EJBHome) temp;
}
else
{
try
{
Object ref = context.lookup("java:comp/env/ejb/"+name); //HERE OCCURS THE ERROR
EJBMetaData meta = ((EJBHome) ref).getEJBMetaData();
home = (EJBHome) PortableRemoteObject.narrow(ref, meta.getHomeInterfaceClass());
if (!meta.isSession() || meta.isStatelessSession())
{
this.homeCache.put(name, home);
}
}
catch (Exception e)
{
throw new ServiceLocatorException(e);
}
}
return home;
}
public EJBHome getHome(String name, Class cls, boolean cached) throws ServiceLocatorException
{
Object temp = null;
if (cached)
{
temp = this.homeCache.get(name);
}
EJBHome home = null;
if (temp != null && temp instanceof EJBHome)
{
return (EJBHome) temp;
}
else
{
try
{
Object ref = context.lookup("java:comp/env/ejb/"+name); //HERE OCCURS THE ERROR
home = (EJBHome) PortableRemoteObject.narrow(ref, cls);
EJBMetaData meta = home.getEJBMetaData();
if (!meta.isSession() || meta.isStatelessSession())
{
this.homeCache.put(name, home);
}
}
catch (Exception e)
{
throw new ServiceLocatorException(e);
}
}
return home;
}
public EJBHome getHome(String name, Class cls) throws ServiceLocatorException
{
return getHome(name, cls, true);
}
public EJBHome getHome(String name) throws ServiceLocatorException
{
return getHome(name, true);
}
public EJBLocalHome getLocalHome(String name) throws ServiceLocatorException
{
return this.getLocalHome(name, true);
}
public EJBLocalHome getLocalHome(String name, boolean cached) throws ServiceLocatorException
{
Object temp = null;
if (cached)
{
temp = this.homeLocalCache.get(name);
}
EJBLocalHome home = null;
if (temp != null && temp instanceof EJBLocalHome)
{
return (EJBLocalHome) temp;
}
else
{
try
{
home = (EJBLocalHome) context.lookup("java:comp/env/ejb/"+name);
this.homeLocalCache.put(name, home);
}
catch (Exception e)
{
throw new ServiceLocatorException(e);
}
}
return home;
}
public DataSource getDataSource(String name) throws ServiceLocatorException
{
return this.getDataSource(name, true);
}
public DataSource getDataSource(String name, boolean cached) throws ServiceLocatorException
{
DataSource ds = null;
if (cached)
{
ds = (DataSource) this.dataSourceCache.get(name);
}
if (ds != null && ds instanceof DataSource)
{
return ds;
}
else
{
try
{
ds = (DataSource) context.lookup("java:comp/env/jdbc/"+name);
this.dataSourceCache.put(name, ds);
}
catch (Exception e)
{
throw new ServiceLocatorException(e);
}
}
return ds;
}
}
B - ejb-jar.xml
----------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<enterprise-beans>
<session>
<description>Fuehrt die Veranstaltungssuche durch.</description>
<ejb-name>SucheCollection</ejb-name>
<home>EventGuideEJB.BusinessLogic.SucheCollectionHome</home>
<local-home>EventGuideEJB.BusinessLogic.SucheCollectionLocalHome</local-home>
<remote>EventGuideEJB.BusinessLogic.SucheCollection</remote>
<local>EventGuideEJB.BusinessLogic.SucheCollectionLocal</local>
<ejb-class>EventGuideEJB.BusinessLogic.SucheCollectionBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
<ejb-local-ref>
<ejb-ref-name>ejb/EventLocal</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>EventGuideEJB.DataManagement.EventLocalHome</local-home>
<local>EventGuideEJB.DataManagement.EventLocal</local>
<ejb-link>EventGuideEJB.jar#Event</ejb-link>
</ejb-local-ref>
</session>
<session>
<description>Fuehrt Detailanzeige und Ticketreservierungen durch.</description>
<ejb-name>Details</ejb-name>
<home>EventGuideEJB.BusinessLogic.DetailsHome</home>
<local-home>EventGuideEJB.BusinessLogic.DetailsLocalHome</local-home>
<remote>EventGuideEJB.BusinessLogic.Details</remote>
<local>EventGuideEJB.BusinessLogic.DetailsLocal</local>
<ejb-class>EventGuideEJB.BusinessLogic.DetailsBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<ejb-local-ref>
<ejb-ref-name>ejb/TicketLocal</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>EventGuideEJB.DataManagement.TicketLocalHome</local-home>
<local>EventGuideEJB.DataManagement.TicketLocal</local>
<ejb-link>EventGuideEJB.jar#Ticket</ejb-link>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/KontaktLocal</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>EventGuideEJB.DataManagement.KontaktLocalHome</local-home>
<local>EventGuideEJB.DataManagement.KontaktLocal</local>
<ejb-link>EventGuideEJB.jar#Kontakt</ejb-link>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/OrtLocal</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>EventGuideEJB.DataManagement.OrtLocalHome</local-home>
<local>EventGuideEJB.DataManagement.OrtLocal</local>
<ejb-link>EventGuideEJB.jar#Ort</ejb-link>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/ReservierungLocal</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>EventGuideEJB.DataManagement.ReservierungLocalHome</local-home>
<local>EventGuideEJB.DataManagement.ReservierungLocal</local>
<ejb-link>EventGuideEJB.jar#Reservierung</ejb-link>
</ejb-local-ref>
</session>
<entity>
<description>Dies ist eine Event Entity Bean</description>
<ejb-name>Event</ejb-name>
<home>EventGuideEJB.DataManagement.EventHome</home>
<remote>EventGuideEJB.DataManagement.Event</remote>
<local-home>EventGuideEJB.DataManagement.EventLocalHome</local-home>
<local>EventGuideEJB.DataManagement.EventLocal</local>
<ejb-class>EventGuideEJB.DataManagement.EventBean</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>EventGuideEJB.DataManagement.EventPK</prim-key-class>
<reentrant>False</reentrant>
<ejb-local-ref>
<ejb-ref-name>ejb/OrtLocal</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>EventGuideEJB.DataManagement.OrtLocalHome</local-home>
<local>EventGuideEJB.DataManagement.OrtLocal</local>
<ejb-link>EventGuideEJB.jar#Ort</ejb-link>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/NachbarbezirkLocal</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>EventGuideEJB.DataManagement.NachbarbezirkLocalHome</local-home>
<local>EventGuideEJB.DataManagement.NachbarbezirkLocal</local>
<ejb-link>EventGuideEJB.jar#Nachbarbezirk</ejb-link>
</ejb-local-ref>
</entity>
<entity>
<description>Dies ist eine Kontakt Entity Bean</description>
<ejb-name>Kontakt</ejb-name>
<home>EventGuideEJB.DataManagement.KontaktHome</home>
<remote>EventGuideEJB.DataManagement.Kontakt</remote>
<local-home>EventGuideEJB.DataManagement.KontaktLocalHome</local-home>
<local>EventGuideEJB.DataManagement.KontaktLocal</local>
<ejb-class>EventGuideEJB.DataManagement.KontaktBean</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>EventGuideEJB.DataManagement.KontaktPK</prim-key-class>
<reentrant>False</reentrant>
</entity>
<entity>
<description>Dies ist eine Nachbarbezirk Entity Bean</description>
<ejb-name>Nachbarbezirk</ejb-name>
<home>EventGuideEJB.DataManagement.NachbarbezirkHome</home>
<remote>EventGuideEJB.DataManagement.Nachbarbezirk</remote>
<local-home>EventGuideEJB.DataManagement.NachbarbezirkLocalHome</local-home>
<local>EventGuideEJB.DataManagement.NachbarbezirkLocal</local>
<ejb-class>EventGuideEJB.DataManagement.NachbarbezirkBean</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>EventGuideEJB.DataManagement.NachbarbezirkPK</prim-key-class>
<reentrant>False</reentrant>
</entity>
<entity>
<description>Dies ist eine Ort Entity Bean</description>
<ejb-name>Ort</ejb-name>
<home>EventGuideEJB.DataManagement.OrtHome</home>
<remote>EventGuideEJB.DataManagement.Ort</remote>
<local-home>EventGuideEJB.DataManagement.OrtLocalHome</local-home>
<local>EventGuideEJB.DataManagement.OrtLocal</local>
<ejb-class>EventGuideEJB.DataManagement.OrtBean</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>EventGuideEJB.DataManagement.OrtPK</prim-key-class>
<reentrant>False</reentrant>
</entity>
<entity>
<description>Dies ist eine Ticket Entity Bean</description>
<ejb-name>Ticket</ejb-name>
<home>EventGuideEJB.DataManagement.TicketHome</home>
<remote>EventGuideEJB.DataManagement.Ticket</remote>
<local-home>EventGuideEJB.DataManagement.TicketLocalHome</local-home>
<local>EventGuideEJB.DataManagement.TicketLocal</local>
<ejb-class>EventGuideEJB.DataManagement.TicketBean</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>EventGuideEJB.DataManagement.TicketPK</prim-key-class>
<reentrant>False</reentrant>
</entity>
<entity>
<description>Dies ist eine Reservierung Entity Bean</description>
<ejb-name>Reservierung</ejb-name>
<home>EventGuideEJB.DataManagement.ReservierungHome</home>
<remote>EventGuideEJB.DataManagement.Reservierung</remote>
<local-home>EventGuideEJB.DataManagement.ReservierungLocalHome</local-home>
<local>EventGuideEJB.DataManagement.ReservierungLocal</local>
<ejb-class>EventGuideEJB.DataManagement.ReservierungBean</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>EventGuideEJB.DataManagement.ReservierungPK</prim-key-class>
<reentrant>False</reentrant>
</entity>
</enterprise-beans>
</ejb-jar>
C - sun-ejb-jar.xml
--------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.0 EJB 2.1//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_2_1-0.dtd">
<sun-ejb-jar>
<enterprise-beans>
<name>EventGuide</name>
<ejb>
<ejb-name>SucheCollection</ejb-name>
<jndi-name>SucheCollection</jndi-name>
</ejb>
<ejb>
<ejb-name>Details</ejb-name>
<jndi-name>Details</jndi-name>
</ejb>
<ejb>
<ejb-name>Event</ejb-name>
<jndi-name>Event</jndi-name>
</ejb>
<ejb>
<ejb-name>Ort</ejb-name>
<jndi-name>Ort</jndi-name>
</ejb>
<ejb>
<ejb-name>Kontakt</ejb-name>
<jndi-name>Kontakt</jndi-name>
</ejb>
<ejb>
<ejb-name>Nachbarbezirk</ejb-name>
<jndi-name>Nachbarbezirk</jndi-name>
</ejb>
<ejb>
<ejb-name>Ticket</ejb-name>
<jndi-name>Ticket</jndi-name>
</ejb>
<ejb>
<ejb-name>Reservierung</ejb-name>
<jndi-name>Reservierung</jndi-name>
</ejb>
</enterprise-beans>
</sun-ejb-jar>
---
So, please help me dealing with that, as I couldn't find any documented hint.
Did I miss anything in the configuration or program code ? Is there a difference about the paramters in the lookup for local references in contrast to the lookup of remote references ? How do I specify a local JNDI name in the sun-ejb-jar.xml file ?
Thank you very much in advance.
Best regards from Berlin
Andre