X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2FJpaBuilder.java;h=e4cdcfbf7ce3110a88c622f1328d90be22804326;hb=187fce39126c59e974ab86a2af05b0b3ebd2c284;hp=8b6e339935f01206ee3f795e16a8df4be49beeed;hpb=062bbb86bce78fd1328ccfa718c491db465bc1f4;p=utils diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java index 8b6e3399..e4cdcfbf 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java +++ b/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java @@ -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,17 +20,14 @@ 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; - /** * Utility for building an appropriately configured EntityManagerFactory. The * idea is that a persistence.xml is used unchanged from the production version. @@ -27,110 +39,127 @@ import org.wamblee.support.jndi.StubInitialContextFactory; */ public class JpaBuilder { - private static final Logger LOGGER = Logger.getLogger(JpaBuilder.class - .getName()); + private static final Logger LOGGER = Logger.getLogger(JpaBuilder.class + .getName()); + + /** + * Callback interface to execute some JPA code within a transaction with the + * entitymanager to use provided as input. + */ + public static interface JpaUnitOfWork { + /** + * Executes the unit of work. A transaction has been started. + * + * @param aEm + * Entity manager. + * @return Result of the execute method. If you don't want to return + * anything use Void for the return type and return + * null from the implementation. + */ + T execute(EntityManager aEm) throws Exception; + } + + private PersistenceUnitDescription persistenceUnit; + private String url; + private String user; + private String password; + private EntityManagerFactory factory; - /** - * Callback interface to execute some JPA code within a transaction with the - * entitymanager to use provided as input. - */ - public static interface JpaUnitOfWork { - /** - * Executes the unit of work. A transaction has been started. - * @param em Entity manager. - * @return Result of the execute method. If you don't want to return anything use - * Void for the return type and return null from the implementation. - */ - T execute(EntityManager em); - } + /** + * Constructs the builder. + * + * @param aUrl JDBC URL + * @param aUser User name + * @param aPassword Password. + * @param aPersistenceUnit + * Persistence unit. + */ + public JpaBuilder(String aUrl, String aUser, String aPassword, + PersistenceUnitDescription aPersistenceUnit) { + persistenceUnit = aPersistenceUnit; + url = aUrl; + user = aUser; + password = aPassword; + } - private PersistenceUnitDescription persistenceUnit; - private DataSource dataSource; - private EntityManagerFactory factory; + /** + * Starts the builder, which in particular, mocks JNDI, binds the datasource + * the JNDI where the persistence unit expects it, creates the entity + * manager factory, and forces creation of the database schema. + */ + public void start() throws Exception { + factory = createFactory(); + try { + execute(new JpaUnitOfWork() { + public Void execute(EntityManager aEm) { + // Empty, just to trigger database schema creation. + return null; + } + }); + } catch (PersistenceException e) { + factory.close(); + throw e; + } + } - /** - * Constructs the builder. - * - * @param aDataSource - * Datasource of database. - * @param aPersistenceUnit - * Persistence unit. - */ - public JpaBuilder(DataSource aDataSource, - PersistenceUnitDescription aPersistenceUnit) { - persistenceUnit = aPersistenceUnit; - dataSource = aDataSource; - StubInitialContextFactory.register(); - } + /** + * Stops the entity manager factory and disables JNDI mocking. + */ + public void stop() { + StubInitialContextFactory.unregister(); + factory.close(); + } - /** - * Starts the builder, which in particular, mocks JNDI, binds the datasource - * the JNDI where the persistence unit expects it, creates the entity - * manager factory, and forces creation of the database schema. - */ - public void start() throws Exception { - try { - InitialContext ctx = new InitialContext(); - ctx.bind(persistenceUnit.getJndiName(), dataSource); - } catch (NamingException e) { - throw new RuntimeException("JNDI problem", e); - } - factory = createFactory(); - execute(new JpaUnitOfWork() { - public Void execute(EntityManager em) { - // Empty, just to trigger database schema creation. - return null; - } - }); - } + /** + * Creates a new entity manager factory. Typically not used by test code. + * + * @return Entity manager factory. + */ + public EntityManagerFactory createFactory() { + Map jpaProps = new TreeMap(); + + 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); - /** - * Stops the entity manager factory and disables JNDI mocking. - */ - public void stop() { - StubInitialContextFactory.unregister(); - factory.close(); - } + JpaCustomizerBuilder.getCustomizer().customize(persistenceUnit, + jpaProps); + + // jpaProps.put("javax.persistence.provider", + // HibernatePersistence.class.getName()); + EntityManagerFactory emf = Persistence.createEntityManagerFactory( + persistenceUnit.getUnitName(), jpaProps); - /** - * Creates a new entity manager factory. Typically not used by test code. - * @return Entity manager factory. - */ - public EntityManagerFactory createFactory() { - Map jpaProps = new TreeMap(); - - JpaCustomizerBuilder.getCustomizer().customize(persistenceUnit, jpaProps); - - //jpaProps.put("javax.persistence.provider", HibernatePersistence.class.getName()); - EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnit - .getUnitName(), jpaProps); - - LOGGER.info("Using " + factory.getClass()); - return factory; - } + LOGGER.info("Using " + emf.getClass()); + return emf; + } - /** - * Executes a unit of work. This creates an entitymanager and runs the - * {@link JpaUnitOfWork#execute(EntityManager)} within a transaction, passing - * it the entity manager. Use of this method saves a lot of typing for applications. - * - * @param aWork Work to execute. - * @return The return value of the execute method of the unit of work. - */ - public T execute(JpaUnitOfWork aWork) throws Exception { - EntityManager em = factory.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - try { - T value = aWork.execute(em); - transaction.commit(); - return value; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Exception occured", e); - transaction.rollback(); - throw e; - } finally { - em.close(); - } - } + /** + * Executes a unit of work. This creates an entitymanager and runs the + * {@link JpaUnitOfWork#execute(EntityManager)} within a transaction, + * passing it the entity manager. Use of this method saves a lot of typing + * for applications. + * + * @param aWork + * Work to execute. + * @return The return value of the execute method of the unit of work. + */ + public T execute(JpaUnitOfWork aWork) throws Exception { + EntityManager em = factory.createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + transaction.begin(); + try { + T value = aWork.execute(em); + transaction.commit(); + return value; + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Exception occured", e); + transaction.rollback(); + throw e; + } finally { + em.close(); + } + } }