[jboss-cvs] JBossAS SVN: r83663 - in projects/ejb3/trunk/docs/tutorial/jndibinding: META-INF and 2 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Jan 30 04:23:57 EST 2009
Author: jaikiran
Date: 2009-01-30 04:23:57 -0500 (Fri, 30 Jan 2009)
New Revision: 83663
Added:
projects/ejb3/trunk/docs/tutorial/jndibinding/META-INF/
projects/ejb3/trunk/docs/tutorial/jndibinding/META-INF/persistence.xml
projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/Customer.java
projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/CustomerBean.java
projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/CustomerManager.java
Modified:
projects/ejb3/trunk/docs/tutorial/jndibinding/build.xml
projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/client/Client.java
Log:
Updated the tutorial to include an example for binding the entitymanager and entitymanagerfactory to jndi
Added: projects/ejb3/trunk/docs/tutorial/jndibinding/META-INF/persistence.xml
===================================================================
--- projects/ejb3/trunk/docs/tutorial/jndibinding/META-INF/persistence.xml (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/jndibinding/META-INF/persistence.xml 2009-01-30 09:23:57 UTC (rev 83663)
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<persistence xmlns="http://java.sun.com/xml/ns/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">
+ <persistence-unit name="tempdb">
+ <jta-data-source>java:/DefaultDS</jta-data-source>
+ <properties>
+ <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
+ <property name="jboss.entity.manager.jndi.name" value="java:/jboss-ejb3-tutorial-jndibinding-EntityManager"/>
+ <property name="jboss.entity.manager.factory.jndi.name" value="java:/jboss-ejb3-tutorial-jndibinding-EntityManagerFactory"/>
+
+ </properties>
+ </persistence-unit>
+</persistence>
Modified: projects/ejb3/trunk/docs/tutorial/jndibinding/build.xml
===================================================================
--- projects/ejb3/trunk/docs/tutorial/jndibinding/build.xml 2009-01-30 09:15:19 UTC (rev 83662)
+++ projects/ejb3/trunk/docs/tutorial/jndibinding/build.xml 2009-01-30 09:23:57 UTC (rev 83663)
@@ -54,6 +54,9 @@
<fileset dir="${build.classes.dir}">
<include name="**/*.class"/>
</fileset>
+ <fileset dir=".">
+ <include name="META-INF/persistence.xml"/>
+ </fileset>
</jar>
<copy file="build/${build.artifact}" todir="${jboss.home}/server/${jboss.server.config}/deploy"/>
</target>
Added: projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/Customer.java
===================================================================
--- projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/Customer.java (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/Customer.java 2009-01-30 09:23:57 UTC (rev 83663)
@@ -0,0 +1,65 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.tutorial.jndibinding.bean;
+
+import java.io.Serializable;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+/**
+ * Customer
+ *
+ * @author Jaikiran Pai
+ * @version $Revision: $
+ */
+ at Entity
+public class Customer implements Serializable
+{
+ private long id;
+
+ private String name;
+
+ @Id
+ @GeneratedValue
+ public long getId()
+ {
+ return id;
+ }
+
+ public void setId(long id)
+ {
+ this.id = id;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+}
Added: projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/CustomerBean.java
===================================================================
--- projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/CustomerBean.java (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/CustomerBean.java 2009-01-30 09:23:57 UTC (rev 83663)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.tutorial.jndibinding.bean;
+
+import javax.ejb.Remote;
+import javax.ejb.Stateless;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.persistence.EntityManager;
+
+import org.jboss.ejb3.annotation.RemoteBinding;
+import org.jboss.logging.Logger;
+
+/**
+ * CustomerBean
+ *
+ * @author Jaikiran Pai
+ * @version $Revision: $
+ */
+ at Stateless
+ at Remote(CustomerManager.class)
+ at RemoteBinding(jndiBinding = "CustomerManager")
+public class CustomerBean implements CustomerManager
+{
+ // Logger
+ private Logger logger = Logger.getLogger(CustomerBean.class);
+
+ // must match the one in persistence.xml
+ private static final String ENTITY_MANAGER_JNDI_NAME = "java:/jboss-ejb3-tutorial-jndibinding-EntityManager";
+
+ public long createCustomer(String name)
+ {
+ Customer customer = new Customer();
+ customer.setName(name);
+ // get the entity manager from JNDI
+ try
+ {
+ Context ctx = new InitialContext();
+ EntityManager em = (EntityManager) ctx.lookup(ENTITY_MANAGER_JNDI_NAME);
+ logger.debug("Entitymanager found in jndi: " + em);
+ em.persist(customer);
+ logger.debug("Created customer named " + name + " with id " + customer.getId());
+ return customer.getId();
+ }
+ catch (NamingException ne)
+ {
+ logger.error("Could not create customer with name " + name, ne);
+ throw new RuntimeException(ne);
+ }
+ }
+
+ public Customer getCustomer(long id)
+ {
+ try
+ {
+ Context ctx = new InitialContext();
+ EntityManager em = (EntityManager) ctx.lookup(ENTITY_MANAGER_JNDI_NAME);
+ logger.debug("Entitymanager found in jndi: " + em);
+ return em.find(Customer.class,id);
+ }
+ catch (NamingException ne)
+ {
+ logger.error("Could not find customer with id " + id, ne);
+ throw new RuntimeException(ne);
+ }
+ }
+
+}
Added: projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/CustomerManager.java
===================================================================
--- projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/CustomerManager.java (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/bean/CustomerManager.java 2009-01-30 09:23:57 UTC (rev 83663)
@@ -0,0 +1,36 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.tutorial.jndibinding.bean;
+
+/**
+ * CustomerManager
+ *
+ * @author Jaikiran Pai
+ * @version $Revision: $
+ */
+public interface CustomerManager
+{
+
+ long createCustomer(String name);
+
+ Customer getCustomer(long id);
+}
Modified: projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/client/Client.java
===================================================================
--- projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/client/Client.java 2009-01-30 09:15:19 UTC (rev 83662)
+++ projects/ejb3/trunk/docs/tutorial/jndibinding/src/org/jboss/tutorial/jndibinding/client/Client.java 2009-01-30 09:23:57 UTC (rev 83663)
@@ -21,10 +21,12 @@
*/
package org.jboss.tutorial.jndibinding.client;
+import javax.naming.InitialContext;
+
import org.jboss.tutorial.jndibinding.bean.Calculator;
+import org.jboss.tutorial.jndibinding.bean.Customer;
+import org.jboss.tutorial.jndibinding.bean.CustomerManager;
-import javax.naming.InitialContext;
-
public class Client
{
public static void main(String[] args) throws Exception
@@ -34,5 +36,13 @@
System.out.println("1 + 1 = " + calculator.add(1, 1));
System.out.println("1 - 1 = " + calculator.subtract(1, 1));
+
+ // now let's use the customer bean to ensure that the entity manager is bound
+ // to jndi
+ CustomerManager customerManager = (CustomerManager) ctx.lookup("CustomerManager");
+ long id = customerManager.createCustomer("Jaikiran");
+ System.out.println("Created customer with id = " + id);
+ Customer customer = customerManager.getCustomer(id);
+ System.out.println("Customer's name is " + customer.getName());
}
}
More information about the jboss-cvs-commits
mailing list