mirror of
https://github.com/apache/openjpa.git
synced 2025-03-06 08:29:08 +00:00
Merge pull request #80 from jeanouii/OPENJPA-2875_filterJPAProvider
OPENJPA-2875 make sure to avoid doing things when another persistence provider is specified.
This commit is contained in:
commit
722eb579ea
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.test;
|
||||
|
||||
import org.apache.openjpa.persistence.PersistenceProviderImpl;
|
||||
import org.apache.openjpa.persistence.entity.EntityA;
|
||||
import org.apache.openjpa.persistence.entity.EntityB;
|
||||
import org.apache.openjpa.persistence.entity.EntityC;
|
||||
import org.apache.openjpa.util.UserException;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.spi.PersistenceProvider;
|
||||
import javax.persistence.spi.PersistenceUnitInfo;
|
||||
import javax.persistence.spi.ProviderUtil;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/*
|
||||
* This tests that OpenJPA should not be doing anything if another provider is specified.
|
||||
*/
|
||||
public class TestPersistenceProviderFilteringTestCase extends SQLListenerTestCase {
|
||||
|
||||
private final String persistenceUnitName = "test";
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
setUp(DROP_TABLES, EntityA.class, EntityB.class, EntityC.class);
|
||||
|
||||
}
|
||||
|
||||
public void testGenerateSchemaNoProvider() {
|
||||
final PersistenceProviderImpl ppi = new PersistenceProviderImpl();
|
||||
final Map<Object, Object> map = new HashMap<>();
|
||||
assertTrue(ppi.generateSchema(persistenceUnitName, map));
|
||||
}
|
||||
|
||||
public void testGenerateSchemaOpenJPAProvider() {
|
||||
final PersistenceProviderImpl ppi = new PersistenceProviderImpl();
|
||||
final Map<Object, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.provider", PersistenceProviderImpl.class.getName());
|
||||
assertTrue(ppi.generateSchema(persistenceUnitName, map));
|
||||
}
|
||||
|
||||
public void testGenerateSchemaEclipseProvider() {
|
||||
final PersistenceProviderImpl ppi = new PersistenceProviderImpl();
|
||||
final Map<Object, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.provider", "org.eclipse.persistence.jpa.PersistenceProvider");
|
||||
assertFalse(ppi.generateSchema(persistenceUnitName, map));
|
||||
}
|
||||
|
||||
public void testGenerateSchemaFakeProviderClass() {
|
||||
final PersistenceProviderImpl ppi = new PersistenceProviderImpl();
|
||||
final Map<Object, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.provider", FakeProvider.class);
|
||||
assertFalse(ppi.generateSchema(persistenceUnitName, map));
|
||||
}
|
||||
|
||||
|
||||
public static final class FakeProvider implements PersistenceProvider {
|
||||
|
||||
@Override public EntityManagerFactory createEntityManagerFactory(final String s, final Map map) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public EntityManagerFactory createContainerEntityManagerFactory(
|
||||
final PersistenceUnitInfo persistenceUnitInfo, final Map map) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public void generateSchema(final PersistenceUnitInfo persistenceUnitInfo, final Map map) {
|
||||
|
||||
}
|
||||
|
||||
@Override public boolean generateSchema(final String s, final Map map) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override public ProviderUtil getProviderUtil() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -18,21 +18,6 @@
|
||||
*/
|
||||
package org.apache.openjpa.persistence;
|
||||
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.instrument.IllegalClassFormatException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.ProtectionDomain;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.spi.ClassTransformer;
|
||||
import javax.persistence.spi.LoadState;
|
||||
import javax.persistence.spi.PersistenceProvider;
|
||||
import javax.persistence.spi.PersistenceUnitInfo;
|
||||
import javax.persistence.spi.ProviderUtil;
|
||||
|
||||
import org.apache.openjpa.conf.BrokerValue;
|
||||
import org.apache.openjpa.conf.OpenJPAConfiguration;
|
||||
import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
|
||||
@ -55,6 +40,20 @@ import org.apache.openjpa.persistence.osgi.BundleUtils;
|
||||
import org.apache.openjpa.persistence.validation.ValidationUtils;
|
||||
import org.apache.openjpa.util.ClassResolver;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.spi.ClassTransformer;
|
||||
import javax.persistence.spi.LoadState;
|
||||
import javax.persistence.spi.PersistenceProvider;
|
||||
import javax.persistence.spi.PersistenceUnitInfo;
|
||||
import javax.persistence.spi.ProviderUtil;
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.instrument.IllegalClassFormatException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.ProtectionDomain;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* Bootstrapping class that allows the creation of a stand-alone
|
||||
@ -228,6 +227,11 @@ public class PersistenceProviderImpl
|
||||
@Override
|
||||
public void generateSchema(final PersistenceUnitInfo info, final Map map) {
|
||||
final Map runMap = map == null ? new HashMap<>() : new HashMap<>(map);
|
||||
|
||||
if (!acceptProvider(runMap)) {
|
||||
return;
|
||||
}
|
||||
|
||||
runMap.put("javax.persistence.schema-generation.database.action", "create");
|
||||
final OpenJPAEntityManagerFactory factory = createContainerEntityManagerFactory(info, runMap);
|
||||
try {
|
||||
@ -240,6 +244,11 @@ public class PersistenceProviderImpl
|
||||
@Override
|
||||
public boolean generateSchema(final String persistenceUnitName, final Map map) {
|
||||
final Map runMap = map == null ? new HashMap<>() : new HashMap<>(map);
|
||||
|
||||
if (!acceptProvider(runMap)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
runMap.put("javax.persistence.schema-generation.database.action", "create");
|
||||
final OpenJPAEntityManagerFactory factory = createEntityManagerFactory(persistenceUnitName, runMap);
|
||||
try {
|
||||
@ -250,6 +259,32 @@ public class PersistenceProviderImpl
|
||||
}
|
||||
}
|
||||
|
||||
// if persistence provider is specific, don't do anything
|
||||
// only allowed to process if persistence provider matches or if not provider is specified
|
||||
public boolean acceptProvider(final Map properties) {
|
||||
Object provider = properties.get("javax.persistence.provider");
|
||||
|
||||
// provider is specified, so it has to match
|
||||
if (provider != null) {
|
||||
if (provider instanceof Class) {
|
||||
provider = ((Class) provider).getName();
|
||||
}
|
||||
try {
|
||||
if (!((String) provider).equals(org.apache.openjpa.persistence.PersistenceProviderImpl.class.getName())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (final ClassCastException e) {
|
||||
return false;
|
||||
// not a recognized provider property value so must be another provider.
|
||||
}
|
||||
}
|
||||
|
||||
// no provider specified
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
private Object synchronizeMappings(final OpenJPAEntityManagerFactory factory) {
|
||||
if (EntityManagerFactoryImpl.class.isInstance(factory)) {
|
||||
final EntityManagerFactoryImpl entityManagerFactory = EntityManagerFactoryImpl.class.cast(factory);
|
||||
|
Loading…
x
Reference in New Issue
Block a user