4 Replies Latest reply on Aug 8, 2007 12:40 AM by Ryan Daniels

    Cannot Get InitialContext Lookup to Work

    Ryan Daniels Newbie


      I am using JBoss 4.2.1. I am using an EJB 3.0 Entity and service. I have tried everything I can think of to get the InitialContext look to work, but I am having no luck at all. I have tried dozens of things and looked a dozens of web sites and tried to follow that they told me to do. I feel on the verge of giving up on JBoss and moving to a different server.

      Alot of the information I found was discouraging like "X or Y does not work with JBoss" ..."Z is not completely implemented". I think I have ended up more confused on what to do than before I started.

      I have a WAR and an EJB JAR in my EAR. I want to access the service facade in my EJB JAR. What is the correct way to do this with InitialContext lookup and what must be set up for it to work?

      Any help is deeply appreciated.

      Kurzweil4

        • 1. Re: Cannot Get InitialContext Lookup to Work
          Jinu Thomas Newbie

          Can you please paste your code as to how you are carrying out the lookup?

          • 2. Re: Cannot Get InitialContext Lookup to Work
            Ryan Daniels Newbie

            My code has various things commented out that I have experimented with.

            Here is the persistence.xml. I am sure that my data connection is working properly because I tested it by manually opening the data connection in a JSP.

            <?xml version="1.0" encoding="windows-1252" ?>
            <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
             <persistence-unit name="Model">
             <jta-data-source>java:/hr</jta-data-source>
             <class>ice.data.table.model.Departments</class>
             <!--class>ice.data.table.model.DepPublicFacade</class>
             <class>ice.data.table.model.DepPublicFacadeLocal</class>
             <class>ice.data.table.model.DepPublicFacadeBean</class-->
             <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
             <property name="hibernate.hbm2ddl.auto" value="validate"/>
             </properties>
             </persistence-unit>
            </persistence>
            


            Here in the web.xml, I tried to create an ejb reference, but the problem I ran into was that JBoss gave me an error that it was expecting a local home tag, and since I am using EJB 3.0, I don't have one. I updated the document schema version to 2.5, but JBoss still asked for a local home tag.

            <?xml version = '1.0' encoding = 'windows-1252'?>
            <!--web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"-->
            <web-app version= "2.5"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
             <description>Empty web.xml file for Web Application</description>
             <context-param>
             <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
             <param-value>server</param-value>
             </context-param>
             <context-param>
             <param-name>javax.faces.application.CONFIG_FILES</param-name>
             <param-value>/WEB-INF/faces-config.xml</param-value>
             </context-param>
             <!--ejb-local-ref>
             <ejb-ref-name>ejb/DepPublicFacade</ejb-ref-name>
             <ejb-ref-type>Session</ejb-ref-type>
             <local>ice.data.table.model.DepPublicFacade</local>
             <ejb-link>DepPublicFacade</ejb-link>
             </ejb-local-ref-->
             <listener>
             <listener-class>com.icesoft.faces.util.event.servlet.ContextEventRepeater</listener-class>
             </listener>
             <listener>
             <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
             </listener>
             <servlet>
             <servlet-name>Faces Servlet</servlet-name>
             <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
             <load-on-startup>1</load-on-startup>
             </servlet>
             <servlet>
             <servlet-name>Persistent Faces Servlet</servlet-name>
             <servlet-class>com.icesoft.faces.webapp.xmlhttp.PersistentFacesServlet</servlet-class>
             <load-on-startup>1</load-on-startup>
             </servlet>
             <servlet>
             <servlet-name>Blocking Servlet</servlet-name>
             <servlet-class>com.icesoft.faces.webapp.xmlhttp.BlockingServlet</servlet-class>
             <load-on-startup>1</load-on-startup>
             </servlet>
             <servlet>
             <servlet-name>uploadServlet</servlet-name>
             <servlet-class>com.icesoft.faces.component.inputfile.FileUploadServlet</servlet-class>
             <load-on-startup>1</load-on-startup>
             </servlet>
             <servlet-mapping>
             <servlet-name>Faces Servlet</servlet-name>
             <url-pattern>/faces/*</url-pattern>
             </servlet-mapping>
             <servlet-mapping>
             <servlet-name>Persistent Faces Servlet</servlet-name>
             <url-pattern>/xmlhttp/*</url-pattern>
             </servlet-mapping>
             <servlet-mapping>
             <servlet-name>Persistent Faces Servlet</servlet-name>
             <url-pattern>*.iface</url-pattern>
             </servlet-mapping>
             <servlet-mapping>
             <servlet-name>Persistent Faces Servlet</servlet-name>
             <url-pattern>*.jspx</url-pattern>
             </servlet-mapping>
             <servlet-mapping>
             <servlet-name>Blocking Servlet</servlet-name>
             <url-pattern>/block/*</url-pattern>
             </servlet-mapping>
             <servlet-mapping>
             <servlet-name>uploadServlet</servlet-name>
             <url-pattern>/uploadHtml</url-pattern>
             </servlet-mapping>
             <session-config>
             <session-timeout>35</session-timeout>
             </session-config>
             <mime-mapping>
             <extension>html</extension>
             <mime-type>text/html</mime-type>
             </mime-mapping>
             <mime-mapping>
             <extension>txt</extension>
             <mime-type>text/plain</mime-type>
             </mime-mapping>
            </web-app>
            


            My Entity. I tried various ways of structuring the JNDI name for lookup() from various web sites, but none of them worked.

            package ice.data.table.model;
            
            import java.io.Serializable;
            
            import javax.annotation.Resource;
            
            import javax.persistence.Column;
            import javax.persistence.Entity;
            import javax.persistence.Id;
            import javax.persistence.NamedQuery;
            
            @Entity
            @NamedQuery( name = "Departments.findAll",
             query = "select o from Departments o" )
            public class Departments implements Serializable
            {
             @Id
             @Column( name="DEPARTMENT_ID", nullable = false )
             private Long departmentId;
             @Column( name="DEPARTMENT_NAME", nullable = false )
             private String departmentName;
             @Column( name="LOCATION_ID" )
             private Long locationId;
             @Column( name="MANAGER_ID" )
             private Long managerId;
            
             public Departments()
             {
             }
            
             public Long getDepartmentId()
             {
             return departmentId;
             }
            
             public void setDepartmentId( Long departmentId )
             {
             this.departmentId = departmentId;
             }
            
             public String getDepartmentName()
             {
             return departmentName;
             }
            
             public void setDepartmentName( String departmentName )
             {
             this.departmentName = departmentName;
             }
            
             public Long getLocationId()
             {
             return locationId;
             }
            
             public void setLocationId( Long locationId )
             {
             this.locationId = locationId;
             }
            
             public Long getManagerId()
             {
             return managerId;
             }
            
             public void setManagerId( Long managerId )
             {
             this.managerId = managerId;
             }
            }
            


            My service:

            package ice.data.table.model;
            
            import java.util.List;
            
            import javax.ejb.EJB;
            import javax.ejb.Stateless;
            
            import javax.persistence.EntityManager;
            import javax.persistence.PersistenceContext;
            
            @Stateless( name="DepPublicFacade" )
            public class DepPublicFacadeBean implements DepPublicFacade, DepPublicFacadeLocal
            {
             @PersistenceContext( unitName="Model" )
             private EntityManager em;
            
             public DepPublicFacadeBean()
             {
             }
            
             public Object mergeEntity( Object entity )
             {
             return em.merge(entity);
             }
            
             public Object persistEntity( Object entity )
             {
             em.persist(entity);
             return entity;
             }
            
             /** <code>select o from Departments o</code> */
             public List<Departments> queryDepartmentsFindAll()
             {
             return em.createNamedQuery("Departments.findAll").getResultList();
             }
            
             public void removeDepartments( Departments departments )
             {
             departments = em.find(Departments.class, departments.getDepartmentId());
             em.remove(departments);
             }
            }
            


            My local and remotes:

            package ice.data.table.model;
            
            import java.util.List;
            
            import javax.ejb.Remote;
            
            @Remote
            public interface DepPublicFacade
            {
             Object mergeEntity( Object entity );
            
             Object persistEntity( Object entity );
            
             List<Departments> queryDepartmentsFindAll();
            
             void removeDepartments( Departments departments );
            }
            
            package ice.data.table.model;
            
            import java.util.List;
            
            import javax.ejb.Local;
            
            @Local
            public interface DepPublicFacadeLocal
            {
             Object mergeEntity( Object entity );
            
             Object persistEntity( Object entity );
            
             List<Departments> queryDepartmentsFindAll();
            
             void removeDepartments( Departments departments );
            }
            


            And finally the bean from my WAR where I am calling the above code from the EJB JAR:

            package ice.data.table.view;
            
            import ice.data.table.model.DepPublicFacade;
            import ice.data.table.model.DepPublicFacadeLocal;
            import ice.data.table.model.Departments;
            import java.util.List;
            
            import javax.ejb.EJB;
            
            import javax.ejb.EJBContext;
            
            import javax.ejb.SessionContext;
            
            import javax.naming.Binding;
            import javax.naming.Context;
            import javax.naming.InitialContext;
            import javax.naming.NameClassPair;
            import javax.naming.NamingEnumeration;
            import javax.naming.NamingException;
            
            public class DepartmentsBean
            {
             private DepPublicFacade model;
            
             public DepartmentsBean()
             {
             try
             {
             final Context context = getInitialContext();
            
             /*
             NamingEnumeration<NameClassPair> ne = context.list( "java:comp/env" ); //"IceDataTable/DepPublicFacade.remote" );///DepPublicFacade/local" );
            
             NameClassPair ncp;
            
             System.out.println( ne.hasMore() );
            
             while ( ne.hasMore() )
             {
            
             ncp = ( NameClassPair ) ne.next();
            
             System.out.println( ncp.getName() );
             System.out.println( "\t" + ncp.getClass() );
             System.out.println( "\t" + ncp.getClassName() );
            
             }
             /*
             while ( ne.hasMore() )
             {
             ncp = ( NameClassPair ) ne.next();
             System.out.println( ncp.getName() + ": " + ncp.getClass() + ": " + ncp.getNameInNamespace() + ": " + ncp.getClassName() );
             }*/
             //model = (DepPublicFacade)context.lookup("DepPublicFacade");
             model = (DepPublicFacade)context.lookup("java:comp/env/DepPublicFacade");
             }
             catch (Exception ex)
             {
             ex.printStackTrace();
             }
             }
            
             public List getDepartments()
             {
             return model.queryDepartmentsFindAll();
             }
            
             private static Context getInitialContext() throws NamingException
             {
             // Get InitialContext for Embedded OC4J
             // The embedded server must be running for lookups to succeed.
             return new InitialContext();
             }
            }
            


            Thank you for your response. Any help you can provide is appreciated.

            Kurzweil4


            • 3. Re: Cannot Get InitialContext Lookup to Work
              Jinu Thomas Newbie

              Can you deploy your EJB separately as a JAR inside JBoss? And then deploy your application as war.

              Just try to get InitialContext somewhat like this:

              Properties properties = new Properties();
              properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
              properties.put("java.naming.provider.url", "localhost:1099");
              properties.put("java.naming.factory.url.pkgs", "org.jboss.naming");
              InitialContext initialcontext = new InitialContext();
              Object obj1 = initialcontext.lookup("ConnectionFactory");

              The lookup you need to provide in EJB-JAR.xml which you'll deploy as a JAR.
              Keep your code commented in Web.xml. No need to change anything else I suppose.

              Just give it a try. Let's see if it works.

              • 4. Re: Cannot Get InitialContext Lookup to Work
                Ryan Daniels Newbie


                Thanks for your reply. I will try it.

                By the way ...once I do that ...what is the correct lookup string for my service class?

                Kurzweil4