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.
*/
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<T> {
+ /**
+ * 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 <code>Void</code> for the return type and return
+ * null from the implementation.
+ */
+ T execute(EntityManager em);
+ }
+
+ private PersistenceUnitDescription persistenceUnit;
+ private DataSource dataSource;
+ private EntityManagerFactory factory;
+
+ /**
+ * Constructs the builder.
+ *
+ * @param aDataSource
+ * Datasource of database.
+ * @param aPersistenceUnit
+ * Persistence unit.
+ */
+ public JpaBuilder(DataSource aDataSource,
+ PersistenceUnitDescription aPersistenceUnit) {
+ persistenceUnit = aPersistenceUnit;
+ dataSource = aDataSource;
+ StubInitialContextFactory.register();
+ }
- /**
- * Callback interface to execute some JPA code within a transaction with the
- * entitymanager to use provided as input.
- */
- public static interface JpaUnitOfWork<T> {
- /**
- * 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
- * <code>Void</code> for the return type and return null from the implementation.
- */
- T execute(EntityManager em);
- }
+ /**
+ * 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<Void>() {
+ public Void execute(EntityManager em) {
+ // Empty, just to trigger database schema creation.
+ return null;
+ }
+ });
+ }
- private PersistenceUnitDescription persistenceUnit;
- private DataSource dataSource;
- private EntityManagerFactory factory;
+ /**
+ * Stops the entity manager factory and disables JNDI mocking.
+ */
+ public void stop() {
+ StubInitialContextFactory.unregister();
+ factory.close();
+ }
- /**
- * Constructs the builder.
- *
- * @param aDataSource
- * Datasource of database.
- * @param aPersistenceUnit
- * Persistence unit.
- */
- public JpaBuilder(DataSource aDataSource,
- PersistenceUnitDescription aPersistenceUnit) {
- persistenceUnit = aPersistenceUnit;
- dataSource = aDataSource;
- StubInitialContextFactory.register();
- }
+ /**
+ * Creates a new entity manager factory. Typically not used by test code.
+ *
+ * @return Entity manager factory.
+ */
+ public EntityManagerFactory createFactory() {
+ Map<String, String> jpaProps = new TreeMap<String, String>();
- /**
- * 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<Void>() {
- public Void execute(EntityManager em) {
- // Empty, just to trigger database schema creation.
- return null;
- }
- });
- }
+ JpaCustomizerBuilder.getCustomizer().customize(persistenceUnit,
+ jpaProps);
- /**
- * Stops the entity manager factory and disables JNDI mocking.
- */
- public void stop() {
- StubInitialContextFactory.unregister();
- factory.close();
- }
+ // jpaProps.put("javax.persistence.provider",
+ // HibernatePersistence.class.getName());
+ EntityManagerFactory factory = 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<String, String> jpaProps = new TreeMap<String, String>();
-
- 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 " + factory.getClass());
+ return factory;
+ }
- /**
- * 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> T execute(JpaUnitOfWork<T> 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> T execute(JpaUnitOfWork<T> 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();
+ }
+ }
}