5.6. Creating a JNDI LookupHibernate is now ready to use. Let's move to the webapp subproject to create the necessary JNDI lookups. The first step toward using it is creating a JNDI reference to it in jboss-web.xml. Example 5-8 shows what the file looks like now. Example 5-8. jboss-web.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.3V2//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_3_2.dtd"> <jboss-web> <resource-ref> <res-ref-name>jdbc/JBossAtWorkDS</res-ref-name> <jndi-name>java:/JBossAtWorkDS</jndi-name> </resource-ref> <resource-ref> <res-ref-name>hibernate/SessionFactory</res-ref-name> <jndi-name>java:/hibernate/SessionFactory</jndi-name> </resource-ref> </jboss-web> Remember that <res-ref-name> is the local ENC-style name. With the implied java:comp/env/ prefix, the full ENC-style JNDI name for our Hibernate service is java:comp/env/hibernate/SessionFactory. The global JNDI name (<jndi-name>) matches the setting in hibernate-service.xmljava:/hibernate/SessionFactory. Example 5-9 shows what the web.xml file now looks like. Example 5-9. web.xml<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" 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"> <servlet> <servlet-name>Controller</servlet-name> <servlet-class>com.jbossatwork.ControllerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Controller</servlet-name> <url-pattern>/controller/*</url-pattern> </servlet-mapping> <resource-ref > <res-ref-name>jdbc/JBossAtWorkDS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <resource-ref> <res-ref-name>hibernate/SessionFactory</res-ref-name> <res-type>net.sf.hibernate.SessionFactory</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app> Of course, our trusty friend XDoclet does all the file generation, courtesy of the tags in ControllerServlet (Example 5-10). Example 5-10. ControllerServlet.java/** * @web.servlet * name="Controller" * * @web.servlet-mapping * url-pattern="/controller/*" * * @web.resource-ref * name="jdbc/JBossAtWorkDS" * type="javax.sql.DataSource" * auth="Container" * * @jboss.resource-ref * res-ref-name="jdbc/JBossAtWorkDS" * jndi-name="java:/JBossAtWorkDS" * * @web.resource-ref * name="hibernate/SessionFactory" * type="net.sf.hibernate.SessionFactory" * auth="Container" * * @jboss.resource-ref * res-ref-name="hibernate/SessionFactory" * jndi-name="java:/hibernate/SessionFactory" */ public class ControllerServlet extends HttpServle Recall that we perform JNDI lookups via the ServiceLocator class in common/src/com/jbossatwork/util. ServiceLocator.getHibernateSessionFactory( ) returns a SessionFactory. Realistically, usually we just need to get a Hibernate Session, so a convenience method in Example 5-11 just returns a Session. Example 5-11. ServiceLocator.javapublic static SessionFactory getHibernateSessionFactory( String jndiSessionFactoryName) throws ServiceLocatorException { SessionFactory sessionFactory = null; try { Context ctx = new InitialContext( ); sessionFactory = (SessionFactory) ctx.lookup(jndiSessionFactoryName); } catch (ClassCastException cce) { throw new ServiceLocatorException(cce); } catch (NamingException ne) { throw new ServiceLocatorException(ne); } return sessionFactory; } public static Session getHibernateSession( String jndiSessionFactoryName) throws ServiceLocatorException { Session session = null; try { session = getHibernateSessionFactory(jndiSessionFactoryName).openSession( ); } catch (Exception e) { throw new ServiceLocatorException(e); } return session; } For this class to compile, we need to make sure that the Hibernate JARs are on the classpath somewhere. You can either copy the Hibernate jars found in $JBOSS_HOME/server/default/deploy/jboss-hibernate.deployer to common/compile-lib, or refer to them in place as we did in Example 5-12. (Be sure that the environment variable $JBOSS_HOME is defined before you run this.) Example 5-12. build.xml<property name="hibernate.lib.dir" value="${env.JBOSS_HOME}/server/default/deploy/jboss-hibernate.deployer"/> <!-- ====================================================== = This builds the classpath used for compilation. NOTE: This is independent of your system classpath ====================================================== = --> <path > <fileset dir="${compile.lib.dir}"> <include name="**/*.jar"/> </fileset> <fileset dir="${lib.dir}"> <include name="**/*.jar"/> </fileset> <fileset dir="${hibernate.lib.dir}"> <include name="**/*.jar"/> </fileset> </path> |