Author: jrbauer
Date: Wed Apr 29 19:55:39 2009
New Revision: 769901

URL: http://svn.apache.org/viewvc?rev=769901&view=rev
Log:
OPENJPA-932 Committing code and tests contributed by Rick Curtis

Added:
    
openjpa/trunk/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
   (with props)
    openjpa/trunk/openjpa-persistence/src/test/resources/
    openjpa/trunk/openjpa-persistence/src/test/resources/META-INF/
    
openjpa/trunk/openjpa-persistence/src/test/resources/META-INF/persistence.xml   
(with props)
    openjpa/trunk/openjpa-persistence/src/test/resources/second-persistence/
    
openjpa/trunk/openjpa-persistence/src/test/resources/second-persistence/META-INF/
    
openjpa/trunk/openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml
   (with props)
Modified:
    openjpa/trunk/openjpa-persistence/pom.xml
    
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
    
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
    
openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties

Modified: openjpa/trunk/openjpa-persistence/pom.xml
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/pom.xml?rev=769901&r1=769900&r2=769901&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/pom.xml (original)
+++ openjpa/trunk/openjpa-persistence/pom.xml Wed Apr 29 19:55:39 2009
@@ -54,6 +54,32 @@
                    <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>
@@ -78,6 +104,32 @@
                    <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>

Modified: 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java?rev=769901&r1=769900&r2=769901&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
 (original)
+++ 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
 Wed Apr 29 19:55:39 2009
@@ -27,9 +27,12 @@
 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;
@@ -82,6 +85,8 @@
     private static final Localizer _loc = Localizer.forPackage
         (PersistenceProductDerivation.class);
 
+    private HashMap<String, PUNameCollision> _puNameCollisions
+        = new HashMap<String,PUNameCollision>();
     public void putBrokerFactoryAliases(Map m) {
     }
 
@@ -266,15 +271,17 @@
     public List getAnchorsInResource(String resource) throws Exception {
         ConfigurationParser parser = new ConfigurationParser(null);
         try {
+               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;
@@ -318,6 +325,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;
@@ -407,11 +429,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;
             }
 
@@ -425,6 +460,9 @@
                     ojpa = pinfo;
             }
         }
+        if(result!=null){
+            return result;
+        }
         return ojpa;
     }
 
@@ -469,6 +507,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.   
      */
@@ -691,4 +739,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/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java?rev=769901&r1=769900&r2=769901&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
 (original)
+++ 
openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
 Wed Apr 29 19:55:39 2009
@@ -37,6 +37,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;
@@ -60,6 +61,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
@@ -80,6 +82,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/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties?rev=769901&r1=769900&r2=769901&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties
 (original)
+++ 
openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties
 Wed Apr 29 19:55:39 2009
@@ -179,6 +179,9 @@
 unwrap-query-invalid: Query can not be unwrapped to an instance of "{0}".
 invalid_entity_argument: Object being locked must be an valid and not detached 
\
     entity.
+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.    
 bad-lock-level: Invalid lock mode/level. Valid values are \
     "none"(0), "read"(10), "optimistic"(15), "write"(20), \
     "optimistic-force-increment"(25), \

Added: 
openjpa/trunk/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java?rev=769901&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
 Wed Apr 29 19:55:39 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);        
+    }
+}

Propchange: 
openjpa/trunk/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openjpa/trunk/openjpa-persistence/src/test/resources/META-INF/persistence.xml
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/test/resources/META-INF/persistence.xml?rev=769901&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence/src/test/resources/META-INF/persistence.xml 
(added)
+++ 
openjpa/trunk/openjpa-persistence/src/test/resources/META-INF/persistence.xml 
Wed Apr 29 19:55:39 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>

Propchange: 
openjpa/trunk/openjpa-persistence/src/test/resources/META-INF/persistence.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openjpa/trunk/openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml?rev=769901&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml
 (added)
+++ 
openjpa/trunk/openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml
 Wed Apr 29 19:55:39 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>
+

Propchange: 
openjpa/trunk/openjpa-persistence/src/test/resources/second-persistence/META-INF/persistence.xml
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to