You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mi...@apache.org on 2009/06/16 19:35:57 UTC

svn commit: r785312 - in /openjpa/branches/1.3.x/openjpa-persistence: ./ src/main/java/org/apache/openjpa/persistence/ src/main/resources/org/apache/openjpa/persistence/ src/test/java/org/apache/openjpa/persistence/ src/test/resources/ src/test/resourc...

Author: mikedd
Date: Tue Jun 16 17:35:57 2009
New Revision: 785312

URL: http://svn.apache.org/viewvc?rev=785312&view=rev
Log:
OPENJPA-932 Committing code and tests contributed by Rick Curtis.
Merging revision 769901 originally committed by Jeremy Bauer.

(cherry picked from commit bd50e177bb770396c10cec7ee8f85d0a01044ac4)

Conflicts:

	openjpa-persistence/pom.xml
	openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
	openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties

	modified:   openjpa-persistence/pom.xml
	modified:   openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
	modified:   openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
	modified:   openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties
	new file:   openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
	new file:   openjpa-persistence/src/test/resources/META-INF/persistence.xml
	new file:   openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml

Added:
    openjpa/branches/1.3.x/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
    openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/
    openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/META-INF/
    openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/META-INF/persistence.xml
    openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/second-persistence/
    openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/second-persistence/META-INF/
    openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml
Modified:
    openjpa/branches/1.3.x/openjpa-persistence/pom.xml
    openjpa/branches/1.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
    openjpa/branches/1.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
    openjpa/branches/1.3.x/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties

Modified: openjpa/branches/1.3.x/openjpa-persistence/pom.xml
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence/pom.xml?rev=785312&r1=785311&r2=785312&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence/pom.xml (original)
+++ openjpa/branches/1.3.x/openjpa-persistence/pom.xml Tue Jun 16 17:35:57 2009
@@ -44,4 +44,116 @@
             <artifactId>geronimo-jpa_3.0_spec</artifactId>
         </dependency>
     </dependencies>
+    
+    <profiles>
+  		<profile>
+  		<!-- =============================================================== -->
+  		<!-- Compiling with JDK5 compiler excludes classes that explicitly   -->
+  		<!-- import and use JDK6 classes/packages for annotation processing  -->
+  		<!-- =============================================================== -->
+  		    <id>jdk5-compiler</id>
+            <build>
+              <plugins>
+					<plugin>
+						<groupId>org.apache.maven.plugins</groupId>
+						<artifactId>maven-surefire-plugin</artifactId>
+						<configuration>
+							<additionalClasspathElements>
+								<additionalClasspathElement>${basedir}/target/test-classes/second-persistence.jar</additionalClasspathElement>
+							</additionalClasspathElements>
+						</configuration>
+					</plugin>
+					<plugin>
+						<artifactId>maven-antrun-plugin</artifactId>
+						<executions>
+							<execution>
+								<phase>test-compile</phase>
+								<configuration>
+									<tasks>
+										<jar destfile="${basedir}/target/test-classes/second-persistence.jar"
+											basedir="${basedir}/src/test/resources/second-persistence" />
+									</tasks>
+								</configuration>
+								<goals>
+									<goal>run</goal>
+								</goals>
+							</execution>
+						</executions>
+					</plugin>
+		        <plugin>
+		    		<groupId>org.apache.maven.plugins</groupId>
+		    		<artifactId>maven-compiler-plugin</artifactId>
+		            <configuration>
+                      <excludes>
+                        <exclude>**/AnnotationProcessor6.java</exclude>
+                        <exclude>**/SourceAnnotationHandler.java</exclude>
+                      </excludes>
+		            </configuration>
+		        </plugin>
+		      </plugins>
+		   </build>
+		   <activation>
+		      <jdk>1.5</jdk>
+		   </activation>
+  		</profile>
+  		<profile>
+  			<!-- =========================================================== -->
+  			<!-- Compiling with JDK6 compiler packages a META-INF/services   -->
+  			<!-- file for our annotation processor                           -->
+  			<!-- =========================================================== -->
+  		    <id>jdk6-compiler</id>
+            <build>
+              <plugins>
+					<plugin>
+						<groupId>org.apache.maven.plugins</groupId>
+						<artifactId>maven-surefire-plugin</artifactId>
+						<configuration>
+							<additionalClasspathElements>
+								<additionalClasspathElement>${basedir}/target/test-classes/second-persistence.jar</additionalClasspathElement>
+							</additionalClasspathElements>
+						</configuration>
+					</plugin>
+					<plugin>
+						<artifactId>maven-antrun-plugin</artifactId>
+						<executions>
+							<execution>
+								<phase>test-compile</phase>
+								<configuration>
+									<tasks>
+										<jar destfile="${basedir}/target/test-classes/second-persistence.jar"
+											basedir="${basedir}/src/test/resources/second-persistence" />
+									</tasks>
+								</configuration>
+								<goals>
+									<goal>run</goal>
+								</goals>
+							</execution>
+						</executions>
+					</plugin>
+		        <plugin>
+		    		<groupId>org.apache.maven.plugins</groupId>
+		    		<artifactId>maven-antrun-plugin</artifactId>
+		            <executions>
+		            <execution>
+		            <phase>process-classes</phase>
+		            <configuration>
+		            <tasks>
+		            <!-- echo file="${basedir}/src/main/resources/META-INF/services/javax.annotation.processing.Processor"
+		                  message="org.apache.openjpa.persistence.meta.AnnotationProcessor6"/ -->
+		            </tasks>
+		            </configuration>
+                        <goals>
+                            <goal>run</goal>
+                        </goals>
+		            </execution>
+		            </executions>
+		        </plugin>
+		      </plugins>
+		   </build>
+		   <activation>
+		      <jdk>1.6</jdk>
+		   </activation>
+  		</profile>
+  		
+	</profiles>
 </project>

Modified: openjpa/branches/1.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java?rev=785312&r1=785311&r2=785312&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java (original)
+++ openjpa/branches/1.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java Tue Jun 16 17:35:57 2009
@@ -27,9 +27,13 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.MissingResourceException;
+import java.util.Set;
+
 import javax.persistence.spi.PersistenceUnitInfo;
 import javax.persistence.spi.PersistenceUnitTransactionType;
 
@@ -77,6 +81,8 @@
     private static final Localizer _loc = Localizer.forPackage
         (PersistenceProductDerivation.class);
 
+    private HashMap<String, PUNameCollision> _puNameCollisions
+        = new HashMap<String,PUNameCollision>();
     public void putBrokerFactoryAliases(Map m) {
     }
 
@@ -228,15 +234,17 @@
     public List getAnchorsInResource(String resource) throws Exception {
         ConfigurationParser parser = new ConfigurationParser(null);
         try {
-            ClassLoader loader = (ClassLoader) AccessController.doPrivileged(
+        	List results = new ArrayList();
+            ClassLoader loader = AccessController.doPrivileged(
                 J2DoPrivHelper.getContextClassLoaderAction());
             List<URL> urls = getResourceURLs(resource, loader);
             if (urls != null) {
                 for (URL url : urls) {
                     parser.parse(url);
+                    results.addAll(getUnitNames(parser));
                 }
             }
-            return getUnitNames(parser);
+            return results;
         } catch (IOException e) {
             // not all configuration files are XML; return null if unparsable
             return null;
@@ -280,6 +288,21 @@
         return null;
     }
 
+      /**
+      * This method checks to see if the provided <code>puName</code> was
+      * detected in multiple resources. If a collision is detected, a warning
+      * will be logged and this method will return <code>true</code>.
+      * <p>
+      */
+     public boolean checkPuNameCollisions(Log logger,String puName){
+         PUNameCollision p = _puNameCollisions.get(puName);
+         if(p!=null){
+             p.logCollision(logger);
+             return true;
+         }
+         return false;
+     }
+
     private static List<URL> getResourceURLs(String rsrc, ClassLoader loader)
         throws IOException {
         Enumeration<URL> urls = null;
@@ -369,11 +392,24 @@
     private PersistenceUnitInfoImpl findUnit(List<PersistenceUnitInfoImpl> 
         pinfos, String name, ClassLoader loader) {
         PersistenceUnitInfoImpl ojpa = null;
+        PersistenceUnitInfoImpl result = null;
         for (PersistenceUnitInfoImpl pinfo : pinfos) {
             // found named unit?
             if (name != null) {
-                if (name.equals(pinfo.getPersistenceUnitName()))
-                    return pinfo;
+                if (name.equals(pinfo.getPersistenceUnitName())){
+
+                    if(result!=null){
+                        this.addPuNameCollision(name,
+                            result.getPersistenceXmlFileUrl().toString(),
+                                pinfo.getPersistenceXmlFileUrl().toString());
+
+                    }else{
+                        // Grab a ref to the pinfo that matches the name we're
+                        // looking for. Keep going to look for duplicate pu
+                        // names.
+                        result = pinfo;
+                    }
+                }
                 continue;
             }
 
@@ -387,6 +423,9 @@
                     ojpa = pinfo;
             }
         }
+        if(result!=null){
+            return result;
+        }
         return ojpa;
     }
 
@@ -431,6 +470,16 @@
         System.err.println(msg);
     }
 
+    private void addPuNameCollision(String puName, String file1, String file2){
+        PUNameCollision pun = _puNameCollisions.get(puName);
+        if(pun!=null){
+            pun.addCollision(file1, file2);
+        }else{
+            _puNameCollisions.put(puName,
+            	new PUNameCollision(puName, file1, file2));
+        }
+
+    }
     /**
      * Custom configuration provider.   
      */
@@ -606,4 +655,32 @@
                 _info.setPersistenceXmlFileUrl(_source);
 		}
 	}
+    /**
+     * This private class is used to hold onto information regarding
+     * PersistentUnit name collisions.
+     */
+    private static class PUNameCollision{
+        private String _puName;
+        private Set<String> _resources;
+
+        PUNameCollision(String puName, String file1, String file2) {
+            _resources = new LinkedHashSet<String>();
+            _resources.add(file1);
+            _resources.add(file2);
+
+            _puName=puName;
+        }
+        void logCollision(Log logger){
+            if(logger.isWarnEnabled()){
+                logger.warn(_loc.getFatal("dup-pu",
+                    new Object[]{_puName,_resources.toString(),
+                    	_resources.iterator().next()}));
+            }
+        }
+        void addCollision(String file1, String file2){
+            _resources.add(file1);
+            _resources.add(file2);
+        }
+
+    }
 }

Modified: openjpa/branches/1.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java?rev=785312&r1=785311&r2=785312&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java (original)
+++ openjpa/branches/1.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java Tue Jun 16 17:35:57 2009
@@ -36,6 +36,7 @@
 import org.apache.openjpa.lib.conf.Configuration;
 import org.apache.openjpa.lib.conf.ConfigurationProvider;
 import org.apache.openjpa.lib.conf.Configurations;
+import org.apache.openjpa.lib.conf.ProductDerivations;
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.MetaDataModes;
@@ -59,6 +60,7 @@
     private static final Localizer _loc = Localizer.forPackage(
         PersistenceProviderImpl.class);
 
+    private Log _log;
     /**
      * Loads the entity manager specified by <code>name</code>, applying
      * the properties in <code>m</code> as overrides to the properties defined
@@ -79,6 +81,11 @@
                 return null;
 
             BrokerFactory factory = getBrokerFactory(cp, poolValue, null);
+            _log = factory.getConfiguration()
+                .getLog(OpenJPAConfiguration.LOG_RUNTIME);
+            if(pd.checkPuNameCollisions(_log,name)==true){
+                ;//return null;
+            }
             return JPAFacadeHelper.toEntityManagerFactory(factory);
         } catch (Exception e) {
             throw PersistenceExceptions.toPersistenceException(e);

Modified: openjpa/branches/1.3.x/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties?rev=785312&r1=785311&r2=785312&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties (original)
+++ openjpa/branches/1.3.x/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties Tue Jun 16 17:35:57 2009
@@ -161,4 +161,7 @@
 	"{2}" of type "{3}", but this parameter is bound to a field of type "{4}".
 param-type-null: Parameter "{0}" declared in "{1}" is set to null, \
 	but this parameter is bound to a field of primitive type "{2}".
-	
\ No newline at end of file
+dup-pu: The persistence unit "{0}" was found multiple times in the following \
+	resources "{1}", but persistence unit names should be unique. The first \
+	persistence unit matching the provided name in "{2}" is being used.
+

Added: openjpa/branches/1.3.x/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java?rev=785312&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java Tue Jun 16 17:35:57 2009
@@ -0,0 +1,43 @@
+/*
+ * 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.openjpa.persistence;
+
+import java.util.Arrays;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+public class TestPersistenceProductDerivation extends TestCase {
+
+    /**
+     * Added for OPENJPA-932. Verifies a ppd properly loads pu's from multiple archives.
+     * 
+     * @throws Exception
+     */
+    public void testGetAnchorsInResource()throws Exception{
+        
+        List<String> expectedPUs = Arrays.asList(
+            new String[]{"pu_1","pu_2","pu_3"});
+        
+        PersistenceProductDerivation ppd = new PersistenceProductDerivation();
+        List actual = ppd.getAnchorsInResource("META-INF/persistence.xml");
+        
+        assertEquals(expectedPUs, actual);        
+    }
+}

Added: openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/META-INF/persistence.xml?rev=785312&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/META-INF/persistence.xml (added)
+++ openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/META-INF/persistence.xml Tue Jun 16 17:35:57 2009
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.   
+-->
+<persistence xmlns="http://java.sun.com/xml/ns/persistence"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    version="1.0">
+    <persistence-unit name="pu_1" transaction-type="RESOURCE_LOCAL">
+    </persistence-unit>      
+    <persistence-unit name="pu_2" transaction-type="RESOURCE_LOCAL">
+    </persistence-unit>      
+</persistence>

Added: openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml?rev=785312&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml (added)
+++ openjpa/branches/1.3.x/openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml Tue Jun 16 17:35:57 2009
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.   
+-->
+<persistence xmlns="http://java.sun.com/xml/ns/persistence"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    version="1.0">
+    <persistence-unit name="pu_3" transaction-type="RESOURCE_LOCAL">
+    </persistence-unit>       
+</persistence>
+