Author: rmannibucau
Date: Sat Aug 11 20:12:25 2012
New Revision: 1372017

URL: http://svn.apache.org/viewvc?rev=1372017&view=rev
Log:
support table prefix through jpa unit property openejb.jpa.table_prefix

Added:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/openjpa/PrefixMappingRepository.java
    
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/JPAThreadContext.java
    
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/eclipselink/
    
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/eclipselink/PrefixSessionCustomizer.java
    
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/hibernate/
    
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/hibernate/PrefixNamingStrategy.java
Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java
    openejb/trunk/openejb/container/openejb-jpa-integration/pom.xml

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java?rev=1372017&r1=1372016&r2=1372017&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java
 Sat Aug 11 20:12:25 2012
@@ -24,6 +24,7 @@ import org.apache.openejb.jee.JAXBContex
 import org.apache.openejb.jee.Persistence;
 import org.apache.openejb.jee.PersistenceUnitCaching;
 import org.apache.openejb.jee.PersistenceUnitValidationMode;
+import org.apache.openejb.jpa.integration.JPAThreadContext;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.monitoring.DynamicMBeanWrapper;
 import org.apache.openejb.monitoring.LocalMBeanServer;
@@ -88,17 +89,24 @@ public class ReloadableEntityManagerFact
 
     public ReloadableEntityManagerFactory(final ClassLoader cl, final 
EntityManagerFactoryCallable callable, final Properties props) {
         classLoader = cl;
-        try {
-            delegate = callable.call();
-        } catch (Exception e) {
-            throw new OpenEJBRuntimeException(e);
-        }
         entityManagerFactoryCallable = callable;
+        createDelegate();
 
         logCriteriaJpql = logCriteriaQueryJpql(props);
         logCriteriaJpqlLevel = logCriteriaQueryJpqlLevel(props);
     }
 
+    private void createDelegate() {
+        JPAThreadContext.infos.put("properties", 
entityManagerFactoryCallable.getUnitInfo().getProperties());
+        try {
+            delegate = entityManagerFactoryCallable.call();
+        } catch (Exception e) {
+            throw new OpenEJBRuntimeException(e);
+        } finally {
+            JPAThreadContext.infos.clear();
+        }
+    }
+
     private String logCriteriaQueryJpqlLevel(final Properties props) {
         return 
SystemInstance.get().getOptions().get(OPENEJB_JPA_CRITERIA_LOG_JPQL_LEVEL, 
props.getProperty(OPENEJB_JPA_CRITERIA_LOG_JPQL_LEVEL, "INFO"));
     }
@@ -232,7 +240,7 @@ public class ReloadableEntityManagerFact
     // Note: it uses the old unitInfo but properties can be modified (not 
managed classes, provider...)
     public synchronized void reload() {
         try {
-            delegate = entityManagerFactoryCallable.call();
+            createDelegate();
         } catch (Exception e) {
             LOGGER.error("can't replace EntityManagerFactory " + delegate, e);
         }

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java?rev=1372017&r1=1372016&r2=1372017&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java
 Sat Aug 11 20:12:25 2012
@@ -17,12 +17,14 @@
 package org.apache.openejb.config;
 
 import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.OpenEJBRuntimeException;
 import org.apache.openejb.assembler.classic.AppInfo;
 import org.apache.openejb.assembler.classic.ClassListInfo;
 import org.apache.openejb.assembler.classic.ClientInfo;
 import org.apache.openejb.assembler.classic.ConnectorInfo;
 import org.apache.openejb.assembler.classic.EjbJarInfo;
 import org.apache.openejb.assembler.classic.EnterpriseBeanInfo;
+import org.apache.openejb.assembler.classic.EntityManagerFactoryCallable;
 import org.apache.openejb.assembler.classic.HandlerChainInfo;
 import org.apache.openejb.assembler.classic.JndiEncInfo;
 import org.apache.openejb.assembler.classic.MdbContainerInfo;
@@ -583,8 +585,10 @@ class AppInfoBuilder {
         public static final String OPENJPA_RUNTIME_UNENHANCED_CLASSES = 
"openjpa.RuntimeUnenhancedClasses";
         public static final String DEFAULT_RUNTIME_UNENHANCED_CLASSES = 
"supported";
         public static final String REMOVE_DEFAULT_RUNTIME_UNENHANCED_CLASSES = 
"disable";
-        public static final String OPENJPA_MANAGED_RUNTIME = 
"openjpa.ManagedRuntime";
-        public static final String DEFAULT_MANAGED_RUNTIME = 
"invocation(TransactionManagerMethod=org.apache.openejb.OpenEJB.getTransactionManager)";
+
+        public static final String TABLE_PREFIX = "openejb.jpa.table_prefix";
+        public static final String OPENJPA_METADATA_REPOSITORY = 
"openjpa.MetaDataRepository";
+        public static final String PREFIX_METADATA_REPOSITORY = 
"org.apache.openejb.openjpa.PrefixMappingRepository";
 
         public static final String PROVIDER_PROP = 
"javax.persistence.provider";
         public static final String TRANSACTIONTYPE_PROP = 
"javax.persistence.transactionType";
@@ -594,6 +598,11 @@ class AppInfoBuilder {
 
         public static final String HIBERNATE_TRANSACTION_MANAGER_LOOKUP_CLASS 
= "hibernate.transaction.manager_lookup_class";
         public static final String HIBERNATE_JTA_PLATFORM = 
"hibernate.transaction.jta.platform";
+        public static final String HIBERNATE_EJB_NAMING_STRATEGY_PROP = 
"hibernate.ejb.naming_strategy";
+        private static final String HIBERNATE_EJB_NAMING_STRATEGY = 
"org.apache.openejb.jpa.integration.hibernate.PrefixNamingStrategy";
+
+        private static final String ECLIPSELINK_SESSION_CUSTOMIZER = 
"eclipselink.session.customizer";
+        private static final String PREFIX_SESSION_CUSTOMIZER = 
"org.apache.openejb.jpa.integration.eclipselink.PrefixSessionCustomizer";
 
         private static String providerEnv;
         private static String transactionTypeEnv;
@@ -631,6 +640,16 @@ class AppInfoBuilder {
                 }
                 // info.persistenceUnitRootUrl = null; // to avoid HHH015010
 
+                final String prefix = 
info.properties.getProperty(TABLE_PREFIX);
+                if (prefix != null) {
+                    if 
(info.properties.containsKey(HIBERNATE_EJB_NAMING_STRATEGY_PROP)) {
+                        logger.warning("can't statisfy table prefix since you 
provided a " + HIBERNATE_EJB_NAMING_STRATEGY_PROP + " property");
+                    } else {
+                        // to pass the config to the impl
+                        
info.properties.setProperty(HIBERNATE_EJB_NAMING_STRATEGY_PROP, 
HIBERNATE_EJB_NAMING_STRATEGY);
+                    }
+                }
+
                 if (className == null || 
className.startsWith("org.hibernate.transaction") || 
className.startsWith("org.hibernate.service.jta.platform")){
                     final ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
                     String key = HIBERNATE_JTA_PLATFORM;
@@ -664,6 +683,11 @@ class AppInfoBuilder {
                 String lookupProperty = "toplink.target-server";
                 String openejbLookupClass = MakeTxLookup.TOPLINK_FACTORY;
 
+                final String prefix = 
info.properties.getProperty(TABLE_PREFIX);
+                if (prefix != null) {
+                    logger.warning("table prefix feature is not supported for 
toplink");
+                }
+
                 String className = info.properties.getProperty(lookupProperty);
 
                 if (className == null || 
className.startsWith("oracle.toplink.transaction")){
@@ -691,6 +715,18 @@ class AppInfoBuilder {
                         // ignored
                     }
                 }
+
+                final String prefix = 
info.properties.getProperty(TABLE_PREFIX);
+                if (prefix != null) {
+                    if 
(info.properties.containsKey(ECLIPSELINK_SESSION_CUSTOMIZER)) {
+                        logger.warning("can't statisfy table prefix since you 
provided a " + ECLIPSELINK_SESSION_CUSTOMIZER + " property, add a call to 
org.apache.openejb.jpa.integration.eclipselink.PrefixSessionCustomizer");
+                    } else {
+                        // force eager loading otherwise we'll not get the 
prefix in the customizer
+                        
info.properties.setProperty(EntityManagerFactoryCallable.OPENEJB_JPA_INIT_ENTITYMANAGER,
 "true");
+                        // to pass the config to the impl
+                        
info.properties.setProperty(ECLIPSELINK_SESSION_CUSTOMIZER, 
PREFIX_SESSION_CUSTOMIZER);
+                    }
+                }
             }  else if (info.provider == null || 
"org.apache.openjpa.persistence.PersistenceProviderImpl".equals(info.provider)){
 
                 // Apply the overrides that apply to all persistence units of 
this provider
@@ -708,15 +744,14 @@ class AppInfoBuilder {
                                                                     + 
OPENJPA_RUNTIME_UNENHANCED_CLASSES);
                 }
 
-                /**
-                // default OpenJPA values are not good for us OPENEJB-1798
-                String managedRuntime = 
info.properties.getProperty(OPENJPA_MANAGED_RUNTIME);
-                if (managedRuntime == null){
-                    info.properties.setProperty(OPENJPA_MANAGED_RUNTIME, 
DEFAULT_MANAGED_RUNTIME);
-                    logger.debug("Adjusting PersistenceUnit(name=" + info.name 
+ ") property to "
-                            + OPENJPA_MANAGED_RUNTIME + "=" + 
DEFAULT_MANAGED_RUNTIME);
+                final String prefix = 
info.properties.getProperty(TABLE_PREFIX);
+                if (prefix != null) {
+                    final String mapping = 
info.properties.getProperty(OPENJPA_METADATA_REPOSITORY);
+                    if (mapping != null && 
!"org.apache.openjpa.jdbc.meta.MappingRepository".equals(mapping)) {
+                        throw new OpenEJBRuntimeException("can't honor table 
prefixes since you provided a custom mapping repository: " + mapping);
+                    }
+                    info.properties.setProperty(OPENJPA_METADATA_REPOSITORY, 
PREFIX_METADATA_REPOSITORY + "(prefix=" + prefix + ")");
                 }
-                */
 
                 final Set<String> keys = new 
HashSet<String>(info.properties.stringPropertyNames());
                 for (String key : keys) {

Added: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/openjpa/PrefixMappingRepository.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/openjpa/PrefixMappingRepository.java?rev=1372017&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/openjpa/PrefixMappingRepository.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/openjpa/PrefixMappingRepository.java
 Sat Aug 11 20:12:25 2012
@@ -0,0 +1,39 @@
+package org.apache.openejb.openjpa;
+
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.FieldMapping;
+import org.apache.openjpa.jdbc.meta.MappingRepository;
+import org.apache.openjpa.jdbc.schema.Schema;
+import org.apache.openjpa.lib.conf.Configurations;
+import org.apache.openjpa.persistence.jdbc.PersistenceMappingDefaults;
+
+public class PrefixMappingRepository extends MappingRepository {
+    private String prefix;
+
+    public PrefixMappingRepository() {
+        setMappingDefaults(new PrefixMappingDefaults());
+    }
+
+    public void setPrefix(final String prefix) {
+        this.prefix = prefix;
+    }
+
+    @Override
+    public void endConfiguration() {
+        super.endConfiguration();
+        Configurations.configureInstance(getMappingDefaults(), 
getConfiguration(), "jdbc.MappingDefaults");
+        ((PrefixMappingDefaults) getMappingDefaults()).endConfiguration();
+    }
+
+    private class PrefixMappingDefaults extends PersistenceMappingDefaults {
+        @Override
+        public String getTableName(final ClassMapping cls, final Schema 
schema) {
+            return prefix + super.getTableName(cls, schema);
+        }
+
+        @Override
+        public String getTableName(final FieldMapping fm, final Schema schema) 
{
+            return prefix + super.getTableName(fm, schema);
+        }
+    }
+}

Modified: openejb/trunk/openejb/container/openejb-jpa-integration/pom.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-jpa-integration/pom.xml?rev=1372017&r1=1372016&r2=1372017&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-jpa-integration/pom.xml (original)
+++ openejb/trunk/openejb/container/openejb-jpa-integration/pom.xml Sat Aug 11 
20:12:25 2012
@@ -37,6 +37,18 @@
       <version>${xbeanVersion}</version>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>org.hibernate</groupId>
+      <artifactId>hibernate-entitymanager</artifactId>
+      <version>4.1.4.Final</version>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
+      <groupId>org.eclipse.persistence</groupId>
+      <artifactId>eclipselink</artifactId>
+      <version>2.3.0</version>
+      <optional>true</optional>
+    </dependency>
   </dependencies>
 
   <build>

Added: 
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/JPAThreadContext.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/JPAThreadContext.java?rev=1372017&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/JPAThreadContext.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/JPAThreadContext.java
 Sat Aug 11 20:12:25 2012
@@ -0,0 +1,25 @@
+/*
+ * 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.openejb.jpa.integration;
+
+import java.util.HashMap;
+import java.util.Map;
+
+// use to store info while creating the EMF
+public class JPAThreadContext {
+    public static final Map<String, Object> infos = new HashMap<String, 
Object>();
+}

Added: 
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/eclipselink/PrefixSessionCustomizer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/eclipselink/PrefixSessionCustomizer.java?rev=1372017&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/eclipselink/PrefixSessionCustomizer.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/eclipselink/PrefixSessionCustomizer.java
 Sat Aug 11 20:12:25 2012
@@ -0,0 +1,39 @@
+/*
+ * 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.openejb.jpa.integration.eclipselink;
+
+import org.apache.openejb.jpa.integration.JPAThreadContext;
+import org.eclipse.persistence.config.SessionCustomizer;
+import org.eclipse.persistence.descriptors.ClassDescriptor;
+import org.eclipse.persistence.internal.helper.DatabaseTable;
+import org.eclipse.persistence.sessions.Session;
+
+import java.util.Properties;
+
+public class PrefixSessionCustomizer implements SessionCustomizer {
+    @Override
+    public void customize(final Session session) throws Exception {
+        if (JPAThreadContext.infos.containsKey("properties")) {
+            final String prefix = ((Properties) 
JPAThreadContext.infos.get("properties")).getProperty("openejb.jpa.table_prefix");
+            for (ClassDescriptor cd : session.getDescriptors().values()) {
+                for (DatabaseTable table : cd.getTables()) {
+                    table.setName(prefix + table.getName());
+                }
+            }
+        }
+    }
+}

Added: 
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/hibernate/PrefixNamingStrategy.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/hibernate/PrefixNamingStrategy.java?rev=1372017&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/hibernate/PrefixNamingStrategy.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-jpa-integration/src/main/java/org/apache/openejb/jpa/integration/hibernate/PrefixNamingStrategy.java
 Sat Aug 11 20:12:25 2012
@@ -0,0 +1,48 @@
+/*
+ * 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.openejb.jpa.integration.hibernate;
+
+import org.apache.openejb.jpa.integration.JPAThreadContext;
+import org.hibernate.cfg.ImprovedNamingStrategy;
+
+import java.util.Properties;
+
+public class PrefixNamingStrategy extends ImprovedNamingStrategy {
+    private String prefix;
+
+    @Override
+    public String tableName(final String tableName) {
+        return getPrefix() + super.tableName(tableName);
+    }
+
+    @Override
+    public String classToTableName(String className) {
+        return getPrefix() + super.classToTableName(className);
+    }
+
+    public String getPrefix() {
+        if (prefix == null) {
+            if (JPAThreadContext.infos.containsKey("properties")) {
+                prefix = ((Properties) 
JPAThreadContext.infos.get("properties")).getProperty("openejb.jpa.table_prefix");
+            }
+            if (prefix == null) {
+                prefix = "";
+            }
+        }
+        return prefix;
+    }
+}


Reply via email to