OPENJPA-932 Committing code and tests contributed by Rick Curtis

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@769901 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jeremy Bauer 2009-04-29 19:55:39 +00:00
parent 1677b481b2
commit bd50e177bb
7 changed files with 237 additions and 3 deletions

View File

@ -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>

View File

@ -27,9 +27,12 @@ import java.security.PrivilegedActionException;
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 @@ public class PersistenceProductDerivation
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 class PersistenceProductDerivation
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 @@ public class PersistenceProductDerivation
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 @@ public class PersistenceProductDerivation
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 @@ public class PersistenceProductDerivation
ojpa = pinfo;
}
}
if(result!=null){
return result;
}
return ojpa;
}
@ -469,6 +507,16 @@ public class PersistenceProductDerivation
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 @@ public class PersistenceProductDerivation
_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);
}
}
}

View File

@ -37,6 +37,7 @@ import org.apache.openjpa.kernel.BrokerFactory;
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 @@ public class PersistenceProviderImpl
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 @@ public class PersistenceProviderImpl
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);

View File

@ -179,6 +179,9 @@ unwrap-em-invalid: EntityManager can not be unwrapped to an instance of "{0}".
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), \

View File

@ -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);
}
}

View File

@ -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>

View File

@ -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>