code style improvements.
[utils] / test / enterprise / src / main / java / org / wamblee / support / persistence / JpaBuilder.java
index f83172ebffeda250f23741b7f5f0e0ca7186e346..e4cdcfbf7ce3110a88c622f1328d90be22804326 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2005-2010 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.wamblee.support.persistence;
 
 import java.util.Map;
@@ -5,13 +20,11 @@ import java.util.TreeMap;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.EntityTransaction;
 import javax.persistence.Persistence;
-import javax.sql.DataSource;
+import javax.persistence.PersistenceException;
 
 import org.wamblee.support.jndi.StubInitialContextFactory;
 
@@ -37,32 +50,36 @@ public class JpaBuilder {
         /**
          * Executes the unit of work. A transaction has been started.
          * 
-         * @param em
+         * @param aEm
          *            Entity manager.
          * @return Result of the execute method. If you don't want to return
          *         anything use <code>Void</code> for the return type and return
          *         null from the implementation.
          */
-        T execute(EntityManager em);
+        T execute(EntityManager aEm) throws Exception;
     }
 
     private PersistenceUnitDescription persistenceUnit;
-    private DataSource dataSource;
+    private String url; 
+    private String user; 
+    private String password; 
     private EntityManagerFactory factory;
 
     /**
      * Constructs the builder.
      * 
-     * @param aDataSource
-     *            Datasource of database.
+     * @param aUrl JDBC URL
+     * @param aUser User name
+     * @param aPassword Password.
      * @param aPersistenceUnit
      *            Persistence unit.
      */
-    public JpaBuilder(DataSource aDataSource,
+    public JpaBuilder(String aUrl, String aUser, String aPassword,
         PersistenceUnitDescription aPersistenceUnit) {
         persistenceUnit = aPersistenceUnit;
-        dataSource = aDataSource;
-        StubInitialContextFactory.register();
+        url = aUrl; 
+        user = aUser; 
+        password = aPassword; 
     }
 
     /**
@@ -71,19 +88,18 @@ public class JpaBuilder {
      * manager factory, and forces creation of the database schema.
      */
     public void start() throws Exception {
+        factory = createFactory();
         try {
-            InitialContext ctx = new InitialContext();
-            ctx.bind(persistenceUnit.getJndiName(), dataSource);
-        } catch (NamingException e) {
-            throw new RuntimeException("JNDI problem", e);
+            execute(new JpaUnitOfWork<Void>() {
+                public Void execute(EntityManager aEm) {
+                    // Empty, just to trigger database schema creation.
+                    return null;
+                }
+            });
+        } catch (PersistenceException e) {
+            factory.close();
+            throw e;
         }
-        factory = createFactory();
-        execute(new JpaUnitOfWork<Void>() {
-            public Void execute(EntityManager em) {
-                // Empty, just to trigger database schema creation.
-                return null;
-            }
-        });
     }
 
     /**
@@ -101,17 +117,23 @@ public class JpaBuilder {
      */
     public EntityManagerFactory createFactory() {
         Map<String, String> jpaProps = new TreeMap<String, String>();
+        
+        jpaProps.put("javax.persistence.jtaDataSource", null);
+        jpaProps.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
+        jpaProps.put("javax.persistence.jdbc.url", url);
+        jpaProps.put("javax.persistence.jdbc.user", user);
+        jpaProps.put("javax.persistence.jdbc.password", password);
 
         JpaCustomizerBuilder.getCustomizer().customize(persistenceUnit,
             jpaProps);
-
+        
         // jpaProps.put("javax.persistence.provider",
         // HibernatePersistence.class.getName());
-        EntityManagerFactory factory = Persistence.createEntityManagerFactory(
+        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
             persistenceUnit.getUnitName(), jpaProps);
 
-        LOGGER.info("Using " + factory.getClass());
-        return factory;
+        LOGGER.info("Using " + emf.getClass());
+        return emf;
     }
 
     /**