diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java new file mode 100644 index 000000000..3bd4782e6 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java @@ -0,0 +1,110 @@ +/* + * 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.puconf; + +import javax.persistence.EntityManagerFactory; + +import org.apache.openjpa.persistence.OpenJPAPersistence; +import org.apache.openjpa.persistence.test.PersistenceTestCase; + +public class TestPersistenceUnitConfig extends PersistenceTestCase { + private String persistenceXmlResource; + + public TestPersistenceUnitConfig() { + persistenceXmlResource = getClass().getPackage().getName().replaceAll("\\.", "/") + + "/META-INF/persistence.xml"; + } + + public EntityManagerFactory createEmf(String unitName) { + return OpenJPAPersistence.createEntityManagerFactory(unitName, persistenceXmlResource); + } + + public void testCreateEMFWithGoodPU() { + EntityManagerFactory emf = null; + try { + emf = createEmf("PUTest-Good"); + assertNotNull("Assert emf was successfully created.", emf); + } finally { + if (emf != null) { + try { + emf.close(); + } catch (Throwable t) { + // Swallow Exception + } + } + } + } + + public void testCreateEMFWithBadJarFileElement() { + EntityManagerFactory emf = null; + try { + // Create EMF, expecting no problems. + emf = createEmf("PUTest-Good"); + } finally { + if (emf != null) { + try { + emf.close(); + } catch (Throwable t) { + // Swallow Exception + } + } + } + } + + public void testCreateEMFWithNonOpenJPAProvider() { + EntityManagerFactory emf = null; + try { + emf = createEmf("PUTest-NonOpenJPAProvider"); + + // Did not catch the expected MissingResourceException Exception + fail("The createEntityManager() operation did not throw any Exception."); + } catch (java.util.MissingResourceException mre) { + // Caught the expected PersistenceException + } finally { + if (emf != null) { + try { + emf.close(); + } catch (Throwable t) { + // Swallow Exception + } + } + } + } + + public void testCreateEMFWithBadJarFileElementAndNonOpenJPAProvider() { + EntityManagerFactory emf = null; + try { + emf = createEmf("PUTest-BadJarFile-NonOpenJPAProvider"); + + // Did not catch the expected MissingResourceException Exception + fail("The createEntityManager() operation did not throw any Exception."); + } catch (java.util.MissingResourceException mre) { + // Caught the expected PersistenceException + } finally { + if (emf != null) { + try { + emf.close(); + } catch (Throwable t) { + // Swallow Exception + } + } + } + } + +} diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml new file mode 100644 index 000000000..3d7c33463 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml @@ -0,0 +1,51 @@ + + + + + org.apache.openjpa.persistence.common.apps.Address + org.apache.openjpa.persistence.common.apps.CompUser + + + + org.apache.openjpa.persistence.PersistenceProviderImpl + IDoNotExist.jar + + org.apache.openjpa.persistence.common.apps.Address + org.apache.openjpa.persistence.common.apps.CompUser + + + + a.bogus.provider + + org.apache.openjpa.persistence.common.apps.Address + org.apache.openjpa.persistence.common.apps.CompUser + + + + a.bogus.provider + IDoNotExist.jar + + org.apache.openjpa.persistence.common.apps.Address + org.apache.openjpa.persistence.common.apps.CompUser + + diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java index 9c74950e9..887c3dcc6 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java @@ -533,6 +533,10 @@ public class PersistenceProductDerivation rsrc, name, pinfo.getPersistenceProviderClassName()). getMessage(), getClass().getName(), rsrc); } + + // Process jar-file references after confirming OpenJPA is the desired JPA provider. + pinfo.processJarFileNames(); + cp.addProperties(pinfo.toOpenJPAProperties()); cp.setSource(pinfo.getPersistenceXmlFileUrl().toString()); return Boolean.TRUE; diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java index ee1fd6af3..fe592f2b1 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java @@ -81,6 +81,7 @@ public class PersistenceUnitInfoImpl private List _mappingFileNames; private List _entityClassNames; private List _jarFiles; + private List _jarFileNames; private String _jtaDataSourceName; private DataSource _jtaDataSource; private String _nonJtaDataSourceName; @@ -219,6 +220,30 @@ public class PersistenceUnitInfoImpl } public void addJarFileName(String name) { + // Defer searching the classpath for jar files referenced by the jar-file element until after + // the XML has been parsed and it has been confirmed that OpenJPA is the desired JPA provider. + + if (_jarFileNames == null) { + _jarFileNames = new ArrayList(); + } + _jarFileNames.add(name); + } + + /** + * Process jar-file elements. An IllegalArgumentException may be thrown if the jar file does not exist in the + * classpath. + */ + public void processJarFileNames() { + if (_jarFileNames != null) { + for (String name : _jarFileNames) { + validateJarFileName(name); + } + + _jarFileNames.clear(); + } + } + + public void validateJarFileName(String name) { MultiClassLoader loader = AccessController .doPrivileged(J2DoPrivHelper.newMultiClassLoaderAction()); loader.addClassLoader(getClass().getClassLoader());