cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r503665 - in /cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest: OpenEJBContainer.java jpa/ContainerEntityManagerCase.java jpa/ItestSetup.java
Date Mon, 05 Feb 2007 12:59:46 GMT
Author: aadamchik
Date: Mon Feb  5 04:59:45 2007
New Revision: 503665

URL: http://svn.apache.org/viewvc?view=rev&rev=503665
Log:
CAY-744: JTA EntityManager
(refactoring OpenEJB container startup code in a sperate reusable class)

Added:
    cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/OpenEJBContainer.java
Removed:
    cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/jpa/ContainerEntityManagerCase.java
Modified:
    cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/jpa/ItestSetup.java

Added: cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/OpenEJBContainer.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/OpenEJBContainer.java?view=auto&rev=503665
==============================================================================
--- cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/OpenEJBContainer.java
(added)
+++ cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/OpenEJBContainer.java
Mon Feb  5 04:59:45 2007
@@ -0,0 +1,115 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.cayenne.itest;
+
+import java.io.File;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.transaction.TransactionManager;
+import javax.transaction.TransactionSynchronizationRegistry;
+
+import org.apache.cayenne.project.CayenneUserDir;
+import org.apache.cayenne.util.Util;
+import org.apache.geronimo.transaction.jta11.GeronimoTransactionManagerJTA11;
+import org.apache.openejb.client.LocalInitialContextFactory;
+
+/**
+ * A test OpenEJB container object that provides JNDI and JTA environment to the
+ * integration tests.
+ * 
+ * @author Andrus Adamchik
+ */
+public class OpenEJBContainer {
+
+    private static OpenEJBContainer singletonContainer;
+
+    public static OpenEJBContainer getContainer() {
+        if (singletonContainer == null) {
+            singletonContainer = new OpenEJBContainer();
+        }
+
+        return singletonContainer;
+    }
+
+    private File openEjbHome;
+    private GeronimoTransactionManagerJTA11 txManager;
+
+    private OpenEJBContainer() {
+        setupOpenEJBHome();
+        setupContainerProperties();
+
+        try {
+            bootstrapContainer();
+        }
+        catch (Exception e) {
+            throw new RuntimeException("Error bootrapping OpenEJB container", e);
+        }
+    }
+
+    public TransactionSynchronizationRegistry getTxSyncRegistry() {
+        return txManager;
+    }
+
+    public TransactionManager getTxManager() {
+        return txManager;
+    }
+
+    private void setupOpenEJBHome() {
+
+        File currentDir = new File(System.getProperty("user.dir"));
+        openEjbHome = new File(currentDir, "target" + File.separatorChar + "openejb-out");
+        openEjbHome.mkdirs();
+
+        File openEjbConf = new File(openEjbHome, "conf");
+        openEjbConf.mkdirs();
+
+        File openEjbLogs = new File(openEjbConf, "logs");
+        openEjbLogs.mkdirs();
+
+        // to see Cayenne output, copy default logging properties to openejb dir
+        File logConfig = new File(openEjbConf, "logging.properties");
+        File defaultLogConfig = CayenneUserDir.getInstance().resolveFile(
+                "cayenne-log.properties");
+
+        if (defaultLogConfig.exists() && !Util.copy(defaultLogConfig, logConfig))
{
+            throw new RuntimeException("Can't copy logging configuration to " + logConfig);
+        }
+    }
+
+    private void setupContainerProperties() {
+        System.setProperty(
+                Context.INITIAL_CONTEXT_FACTORY,
+                LocalInitialContextFactory.class.getName());
+        System.setProperty("openejb.home", openEjbHome.getAbsolutePath());
+    }
+
+    private void bootstrapContainer() throws Exception {
+
+        // somehow OpenEJB LocalInitialContextFactory requires 2 IC's to be initilaized to
+        // fully bootstrap the environment, so do one empty run here, and then use a
+        // different IC for binding the environment.
+        new InitialContext();
+
+        this.txManager = new GeronimoTransactionManagerJTA11();
+        new InitialContext().bind(
+                "java:comp/TransactionSynchronizationRegistry",
+                txManager);
+    }
+}

Modified: cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/jpa/ItestSetup.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/jpa/ItestSetup.java?view=diff&rev=503665&r1=503664&r2=503665
==============================================================================
--- cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/jpa/ItestSetup.java
(original)
+++ cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/jpa/ItestSetup.java
Mon Feb  5 04:59:45 2007
@@ -26,11 +26,15 @@
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
 import javax.sql.DataSource;
-
-import org.apache.cayenne.itest.ItestDBUtils;
+import javax.transaction.TransactionSynchronizationRegistry;
 
 import junit.framework.Assert;
 
+import org.apache.cayenne.itest.ItestDBUtils;
+import org.apache.cayenne.itest.OpenEJBContainer;
+import org.apache.openejb.persistence.JtaEntityManager;
+import org.apache.openejb.persistence.JtaEntityManagerRegistry;
+
 public class ItestSetup {
 
     public static final String SCHEMA_SCRIPT_URL = "itest.schema.script";
@@ -45,6 +49,7 @@
     protected ItestDataSourceManager dataSourceManager;
     protected String jpaUnit;
     protected ItestDBUtils dbHelper;
+    protected JtaEntityManagerRegistry openEJBEMRegistry;
 
     public static void initInstance(Properties properties) {
         String schemaScript = properties.getProperty(SCHEMA_SCRIPT_URL);
@@ -74,16 +79,39 @@
         this.dbHelper = new ItestDBUtils(dataSourceManager.getDataSource());
     }
 
-    public DataSource getDataSource() {
-        return dataSourceManager.getDataSource();
+    protected JtaEntityManagerRegistry getOpenEJBEMRegistry() {
+        if (openEJBEMRegistry == null) {
+            TransactionSynchronizationRegistry txRegistry = OpenEJBContainer
+                    .getContainer()
+                    .getTxSyncRegistry();
+            openEJBEMRegistry = new JtaEntityManagerRegistry(txRegistry);
+        }
+
+        return openEJBEMRegistry;
     }
 
-    public EntityManager createEntityManager() {
+    protected EntityManagerFactory getSharedFactory() {
         if (sharedFactory == null) {
             sharedFactory = createEntityManagerFactory();
         }
 
-        return sharedFactory.createEntityManager();
+        return sharedFactory;
+    }
+
+    public DataSource getDataSource() {
+        return dataSourceManager.getDataSource();
+    }
+
+    public EntityManager createEntityManager() {
+        return getSharedFactory().createEntityManager();
+    }
+
+    public EntityManager createContainerManagedEntityManager() {
+        return new JtaEntityManager(
+                getOpenEJBEMRegistry(),
+                getSharedFactory(),
+                new Properties(),
+                false);
     }
 
     public EntityManagerFactory createEntityManagerFactory() {



Mime
View raw message