cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r503210 - in /cayenne/main/trunk/framework/cayenne-jpa-unpublished/src: main/java/org/apache/cayenne/jpa/ main/java/org/apache/cayenne/jpa/conf/ test/java/org/apache/cayenne/jpa/ test/java/org/apache/cayenne/jpa/conf/
Date Sat, 03 Feb 2007 14:21:27 GMT
Author: aadamchik
Date: Sat Feb  3 06:21:26 2007
New Revision: 503210

URL: http://svn.apache.org/viewvc?view=rev&rev=503210
Log:
JPA package structure refactoring and provider class renaming

Removed:
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaPersistenceProvider.java
Modified:
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaUnit.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaUnitClassLoader.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/Provider.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/UnitDescriptorParser.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/JpaUnitTest.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/conf/UnitDescriptorParserTest.java

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaUnit.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaUnit.java?view=diff&rev=503210&r1=503209&r2=503210
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaUnit.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaUnit.java
Sat Feb  3 06:21:26 2007
@@ -17,7 +17,6 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.jpa;
 
 import java.net.MalformedURLException;
@@ -33,7 +32,6 @@
 import javax.persistence.spi.PersistenceUnitTransactionType;
 import javax.sql.DataSource;
 
-
 /**
  * A <code>javax.persistence.spi.PersistenceUnitInfo</code> implementor used
by Cayenne
  * JPA provider.
@@ -72,7 +70,7 @@
     }
 
     public String getPersistenceProviderClassName() {
-        return getProperty(JpaPersistenceProvider.PROVIDER_PROPERTY);
+        return getProperty(Provider.PROVIDER_PROPERTY);
     }
 
     /**
@@ -95,7 +93,7 @@
     }
 
     public PersistenceUnitTransactionType getTransactionType() {
-        String type = getProperty(JpaPersistenceProvider.TRANSACTION_TYPE_PROPERTY);
+        String type = getProperty(Provider.TRANSACTION_TYPE_PROPERTY);
         return type != null
                 ? PersistenceUnitTransactionType.valueOf(type)
                 : DEFAULT_TRANSACTION_TYPE;
@@ -106,11 +104,11 @@
     }
 
     JpaDataSourceFactory getJpaDataSourceFactory() {
-        String factory = getProperty(JpaPersistenceProvider.DATA_SOURCE_FACTORY_PROPERTY);
+        String factory = getProperty(Provider.DATA_SOURCE_FACTORY_PROPERTY);
 
         if (factory == null) {
             throw new JpaProviderException("No value for '"
-                    + JpaPersistenceProvider.DATA_SOURCE_FACTORY_PROPERTY
+                    + Provider.DATA_SOURCE_FACTORY_PROPERTY
                     + "' property - can't build DataSource factory.");
         }
 
@@ -128,12 +126,12 @@
     }
 
     public DataSource getJtaDataSource() {
-        String name = getProperty(JpaPersistenceProvider.JTA_DATA_SOURCE_PROPERTY);
+        String name = getProperty(Provider.JTA_DATA_SOURCE_PROPERTY);
         return getJpaDataSourceFactory().getJtaDataSource(name, this);
     }
 
     public DataSource getNonJtaDataSource() {
-        String name = getProperty(JpaPersistenceProvider.NON_JTA_DATA_SOURCE_PROPERTY);
+        String name = getProperty(Provider.NON_JTA_DATA_SOURCE_PROPERTY);
         return getJpaDataSourceFactory().getNonJtaDataSource(name, this);
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaUnitClassLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaUnitClassLoader.java?view=diff&rev=503210&r1=503209&r2=503210
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaUnitClassLoader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaUnitClassLoader.java
Sat Feb  3 06:21:26 2007
@@ -17,7 +17,6 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.jpa;
 
 import java.io.ByteArrayOutputStream;
@@ -31,9 +30,9 @@
  * 
  * @author Andrus Adamchik
  */
-public class JpaUnitClassLoader extends SecureClassLoader {
+class JpaUnitClassLoader extends SecureClassLoader {
 
-    public JpaUnitClassLoader(ClassLoader parent) {
+    JpaUnitClassLoader(ClassLoader parent) {
         super(parent);
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/Provider.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/Provider.java?view=diff&rev=503210&r1=503209&r2=503210
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/Provider.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/Provider.java
Sat Feb  3 06:21:26 2007
@@ -26,8 +26,10 @@
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Map;
+import java.util.Properties;
 
 import javax.persistence.EntityManagerFactory;
+import javax.persistence.spi.PersistenceProvider;
 import javax.persistence.spi.PersistenceUnitInfo;
 import javax.persistence.spi.PersistenceUnitTransactionType;
 import javax.sql.DataSource;
@@ -40,10 +42,13 @@
 import org.apache.cayenne.dba.AutoAdapter;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.enhancer.Enhancer;
+import org.apache.cayenne.instrument.InstrumentUtil;
 import org.apache.cayenne.jpa.bridge.DataMapConverter;
 import org.apache.cayenne.jpa.conf.EntityMapLoader;
 import org.apache.cayenne.jpa.conf.EntityMapLoaderContext;
+import org.apache.cayenne.jpa.conf.UnitLoader;
 import org.apache.cayenne.jpa.enhancer.JpaEnhancerVisitorFactory;
+import org.apache.cayenne.jpa.instrument.InstrumentingUnitFactory;
 import org.apache.cayenne.jpa.instrument.UnitClassTranformer;
 import org.apache.cayenne.jpa.map.JpaClassDescriptor;
 import org.apache.cayenne.jpa.reflect.JpaClassDescriptorFactory;
@@ -63,10 +68,25 @@
  * 
  * @author Andrus Adamchik
  */
-public class Provider extends JpaPersistenceProvider {
+public class Provider implements PersistenceProvider {
 
+    // common properties
+    public static final String PROVIDER_PROPERTY = "javax.persistence.provider";
+    public static final String TRANSACTION_TYPE_PROPERTY = "javax.persistence.transactionType";
+    public static final String JTA_DATA_SOURCE_PROPERTY = "javax.persistence.jtaDataSource";
+    public static final String NON_JTA_DATA_SOURCE_PROPERTY = "javax.persistence.nonJtaDataSource";
+
+    // provider-specific properties
+    public static final String DATA_SOURCE_FACTORY_PROPERTY = "org.apache.cayenne.jpa.jpaDataSourceFactory";
+    public static final String UNIT_FACTORY_PROPERTY = "org.apache.cayenne.jpa.jpaUnitFactory";
+
+    public static final String INSTRUMENTING_FACTORY_CLASS = InstrumentingUnitFactory.class
+            .getName();
     public static final String CREATE_SCHEMA_PROPERTY = "cayenne.schema.create";
 
+    protected boolean validateDescriptors;
+    protected UnitLoader unitLoader;
+    protected Properties defaultProperties;
     protected Configuration configuration;
     protected Log logger;
 
@@ -79,7 +99,11 @@
     }
 
     public Provider(boolean validateDescriptors) {
-        super(validateDescriptors);
+        this.validateDescriptors = validateDescriptors;
+        this.defaultProperties = new Properties();
+
+        configureEnvironmentProperties();
+        configureDefaultProperties();
 
         this.logger = LogFactory.getLog(getClass());
         this.configuration = new LazyConfiguration();
@@ -88,6 +112,30 @@
         Configuration.initializeSharedConfiguration(configuration);
     }
 
+    /**
+     * Loads default properties from the Java environment.
+     */
+    protected void configureEnvironmentProperties() {
+        String dsFactory = System.getProperty(DATA_SOURCE_FACTORY_PROPERTY);
+        if (dsFactory != null) {
+            defaultProperties.put(DATA_SOURCE_FACTORY_PROPERTY, dsFactory);
+        }
+
+        String transactionType = System.getProperty(TRANSACTION_TYPE_PROPERTY);
+        if (transactionType != null) {
+            defaultProperties.put(TRANSACTION_TYPE_PROPERTY, transactionType);
+        }
+
+        String unitFactory = System.getProperty(UNIT_FACTORY_PROPERTY);
+        if (unitFactory == null && InstrumentUtil.isAgentLoaded()) {
+            unitFactory = INSTRUMENTING_FACTORY_CLASS;
+        }
+
+        if (unitFactory != null) {
+            defaultProperties.put(UNIT_FACTORY_PROPERTY, unitFactory);
+        }
+    }
+
     protected void configureDefaultProperties() {
         if (!defaultProperties.containsKey(DATA_SOURCE_FACTORY_PROPERTY)) {
             defaultProperties.put(
@@ -102,11 +150,42 @@
         }
     }
 
+    public EntityManagerFactory createEntityManagerFactory(String emName, Map map) {
+
+        // TODO: Andrus, 2/11/2006 - cache loaded units (or factories)...
+
+        JpaUnit ui = getUnitLoader().loadUnit(emName);
+
+        if (ui == null) {
+            return null;
+        }
+
+        // override properties
+        if (map != null) {
+            ui.addProperties(map);
+        }
+
+        // set default properties if they are not set explicitly
+        Properties properties = ui.getProperties();
+        for (Map.Entry property : defaultProperties.entrySet()) {
+            if (!properties.containsKey(property.getKey())) {
+                properties.put(property.getKey(), property.getValue());
+            }
+        }
+
+        // check if we are allowed to handle this unit (JPA Spec, 7.2)
+        String provider = ui.getPersistenceProviderClassName();
+        if (provider != null && !provider.equals(this.getClass().getName())) {
+            return null;
+        }
+
+        return createContainerEntityManagerFactory(ui, map);
+    }
+
     /**
      * Maps PersistenceUnitInfo to Cayenne DataDomain and returns a
-     * {@link CjpaEntityManagerFactory} which is a DataDomain wrapper.
+     * {@link EntityManagerFactory} which is a DataDomain wrapper.
      */
-    @Override
     // TODO: andrus, 07/24/2006 - extract properties from the second map parameter as well
     // as PUI.
     public synchronized EntityManagerFactory createContainerEntityManagerFactory(
@@ -293,6 +372,37 @@
         return configuration;
     }
 
+    /**
+     * Returns unit loader, lazily creating it on first invocation.
+     */
+    protected UnitLoader getUnitLoader() {
+        if (unitLoader == null) {
+
+            JpaUnitFactory factory = null;
+
+            String unitFactoryName = getDefaultProperty(UNIT_FACTORY_PROPERTY);
+            if (unitFactoryName != null) {
+
+                try {
+                    Class factoryClass = Class.forName(unitFactoryName, true, Thread
+                            .currentThread()
+                            .getContextClassLoader());
+
+                    factory = (JpaUnitFactory) factoryClass.newInstance();
+                }
+                catch (Exception e) {
+                    throw new JpaProviderException("Error loading unit infor factory '"
+                            + unitFactoryName
+                            + "'", e);
+                }
+            }
+
+            this.unitLoader = new UnitLoader(factory, validateDescriptors);
+        }
+
+        return unitLoader;
+    }
+
     // TODO: andrus, 4/29/2006 - this is copied from non-public conf.NodeDataSource. In
     // Cayenne > 1.2 make it public.
     class NodeDataSource implements DataSource {
@@ -327,6 +437,10 @@
         public int getLoginTimeout() throws SQLException {
             return node.getDataSource().getLoginTimeout();
         }
+    }
+
+    protected String getDefaultProperty(String key) {
+        return defaultProperties.getProperty(key);
     }
 
     class LazyConfiguration extends Configuration {

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/UnitDescriptorParser.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/UnitDescriptorParser.java?view=diff&rev=503210&r1=503209&r2=503210
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/UnitDescriptorParser.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/UnitDescriptorParser.java
Sat Feb  3 06:21:26 2007
@@ -33,9 +33,9 @@
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
 
-import org.apache.cayenne.jpa.JpaPersistenceProvider;
 import org.apache.cayenne.jpa.JpaUnit;
 import org.apache.cayenne.jpa.JpaUnitFactory;
+import org.apache.cayenne.jpa.Provider;
 import org.xml.sax.Attributes;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
@@ -142,7 +142,7 @@
 
                     if (transactionType != null) {
                         unit.putProperty(
-                                JpaPersistenceProvider.TRANSACTION_TYPE_PROPERTY,
+                                Provider.TRANSACTION_TYPE_PROPERTY,
                                 transactionType);
                     }
                 }
@@ -177,9 +177,7 @@
                             unit.addManagedClassName(string);
                         }
                         else if (PROVIDER.equals(qName)) {
-                            unit.putProperty(
-                                    JpaPersistenceProvider.PROVIDER_PROPERTY,
-                                    string);
+                            unit.putProperty(Provider.PROVIDER_PROPERTY, string);
                         }
                         else if (JAR_FILE.equals(qName)) {
                             unit.addJarFileUrl(string);
@@ -188,13 +186,11 @@
                             unit.addMappingFileName(string);
                         }
                         else if (JTA_DATASOURCE.equals(qName)) {
-                            unit.putProperty(
-                                    JpaPersistenceProvider.JTA_DATA_SOURCE_PROPERTY,
-                                    string);
+                            unit.putProperty(Provider.JTA_DATA_SOURCE_PROPERTY, string);
                         }
                         else if (NON_JTA_DATASOURCE.equals(qName)) {
                             unit.putProperty(
-                                    JpaPersistenceProvider.NON_JTA_DATA_SOURCE_PROPERTY,
+                                    Provider.NON_JTA_DATA_SOURCE_PROPERTY,
                                     string);
                         }
                         else if (DESCRIPTION.equals(qName)) {

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/JpaUnitTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/JpaUnitTest.java?view=diff&rev=503210&r1=503209&r2=503210
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/JpaUnitTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/JpaUnitTest.java
Sat Feb  3 06:21:26 2007
@@ -17,7 +17,6 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.jpa;
 
 import java.net.URL;
@@ -26,9 +25,6 @@
 
 import javax.persistence.spi.PersistenceUnitTransactionType;
 
-import org.apache.cayenne.jpa.JpaPersistenceProvider;
-import org.apache.cayenne.jpa.JpaUnit;
-
 import junit.framework.TestCase;
 
 public class JpaUnitTest extends TestCase {
@@ -37,12 +33,12 @@
 
         JpaUnit info = new JpaUnit();
         info.addProperties(Collections.singletonMap(
-                JpaPersistenceProvider.TRANSACTION_TYPE_PROPERTY,
+                Provider.TRANSACTION_TYPE_PROPERTY,
                 PersistenceUnitTransactionType.JTA.name()));
         assertSame(PersistenceUnitTransactionType.JTA, info.getTransactionType());
 
         info.addProperties(Collections.singletonMap(
-                JpaPersistenceProvider.TRANSACTION_TYPE_PROPERTY,
+                Provider.TRANSACTION_TYPE_PROPERTY,
                 PersistenceUnitTransactionType.RESOURCE_LOCAL.name()));
         assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, info
                 .getTransactionType());

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/conf/UnitDescriptorParserTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/conf/UnitDescriptorParserTest.java?view=diff&rev=503210&r1=503209&r2=503210
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/conf/UnitDescriptorParserTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/conf/UnitDescriptorParserTest.java
Sat Feb  3 06:21:26 2007
@@ -17,7 +17,6 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.jpa.conf;
 
 import java.io.InputStream;
@@ -29,8 +28,8 @@
 
 import junit.framework.TestCase;
 
-import org.apache.cayenne.jpa.JpaPersistenceProvider;
 import org.apache.cayenne.jpa.JpaUnit;
+import org.apache.cayenne.jpa.Provider;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXParseException;
 
@@ -57,7 +56,7 @@
         assertEquals(root, info.getPersistenceUnitRootUrl());
         assertTrue(info.getDescription().startsWith("This unit manages orders"));
         assertEquals("jdbc/MyOrderDB", info.getProperties().getProperty(
-                JpaPersistenceProvider.JTA_DATA_SOURCE_PROPERTY));
+                Provider.JTA_DATA_SOURCE_PROPERTY));
 
         assertEquals(1, info.getMappingFileNames().size());
         assertTrue(info.getMappingFileNames().contains("ormap.xml"));
@@ -88,7 +87,7 @@
         assertEquals(root, info.getPersistenceUnitRootUrl());
         assertTrue(info.getDescription().startsWith("This unit manages orders"));
         assertEquals("jdbc/MyOrderDB", info.getProperties().getProperty(
-                JpaPersistenceProvider.JTA_DATA_SOURCE_PROPERTY));
+                Provider.JTA_DATA_SOURCE_PROPERTY));
 
         assertEquals(1, info.getMappingFileNames().size());
         assertTrue(info.getMappingFileNames().contains("ormap.xml"));
@@ -143,7 +142,7 @@
                 .getTransactionType());
         assertEquals(root, info.getPersistenceUnitRootUrl());
         assertEquals("jdbc/MyDB", info.getProperties().getProperty(
-                JpaPersistenceProvider.NON_JTA_DATA_SOURCE_PROPERTY));
+                Provider.NON_JTA_DATA_SOURCE_PROPERTY));
 
         assertEquals(1, info.getMappingFileNames().size());
         assertTrue(info.getMappingFileNames().contains("order-mappings.xml"));
@@ -177,7 +176,7 @@
         assertEquals(root, info.getPersistenceUnitRootUrl());
         assertTrue(info.getDescription().startsWith("This unit manages orders"));
         assertEquals("jdbc/MyOrderDB", info.getProperties().getProperty(
-                JpaPersistenceProvider.JTA_DATA_SOURCE_PROPERTY));
+                Provider.JTA_DATA_SOURCE_PROPERTY));
 
         assertEquals(1, info.getMappingFileNames().size());
         assertTrue(info.getMappingFileNames().contains("ormap.xml"));



Mime
View raw message