diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java index b631e3d961..c2bd7f10c6 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java @@ -20,7 +20,9 @@ import java.io.Serializable; import java.security.AccessController; +import java.security.CodeSource; import java.security.PrivilegedActionException; +import java.security.ProtectionDomain; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -57,6 +59,7 @@ import org.apache.openjpa.util.MetaDataException; import org.apache.openjpa.util.OpenJPAId; +import jag.JAGDebug; import serp.util.Strings; /** @@ -1865,9 +1868,77 @@ private void addToCollectionInternal(Map map, Class key, Class value, bool * Puts the meta class corresponding to the given entity class. */ public void setMetaModel(Class m2) { - Class cls = _factory.getManagedClass(m2); - if (cls != null) - _metamodel.put(cls, m2); + JAGDebug.beginReportTracking(true); + try { + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); + try { + rc.append("MetaDataRepository.setMetaModel() entry:"); + rc.logVariable(" this", this).append(" ").logObjectAddress(this).nl(); + rc.logVariable(" m2", m2).append(" ").logObjectAddress(m2).nl(); + + try { + if (m2 != null) { + final ProtectionDomain pd = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(m2)); +// rc.logVariable(" m2 protection domain", pd).nl(); + final CodeSource cs = pd.getCodeSource(); + rc.logVariable(" m2 CodeSource", cs).nl(); + + } + } catch (Throwable t) { + t.printStackTrace(); + } + } finally { + rc.done(); + } + + if (_metamodel != null && !_metamodel.isEmpty()) { + rc = JAGDebug.startReportChain(); + try { + rc.append("_metamodel (").append(_metamodel.size()).append(")").nl(); + for (Map.Entry, Class> entry : _metamodel.entrySet()) { + rc.append(" ").append(entry.getKey()).append(" ").logObjectAddress(entry.getKey()) + .append(" |---> ").append(entry.getValue()).append(" ").logObjectAddress(entry.getValue()) + .nl(); + final ProtectionDomain pd_key = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(entry.getKey())); + final ProtectionDomain pd_val = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(entry.getValue())); + rc.append(" key cs = ").append(pd_key.getCodeSource()).nl(); + rc.append(" val cs = ").append(pd_val.getCodeSource()).nl(); + } + } catch (Throwable t) { + t.printStackTrace(); + } finally { + rc.done(); + } + } else { + JAGDebug.report("_metamodel is empty."); + } + + Class cls = _factory.getManagedClass(m2); + + if (cls != null) { + try { + rc.append("Inserting into _metamodel: ").nl(); + rc.append(" cls (key) = ").append(cls).append(" ").logObjectAddress(cls).nl(); + rc.append(" m2 (val) = ").append(m2).append(" ").logObjectAddress(m2).nl(); + final ProtectionDomain pd_cls = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(cls)); + rc.append(" (cls CodeSource) = ").append(pd_cls.getCodeSource()).nl(); + } catch (Throwable t) { + t.printStackTrace(); + } finally { + rc.done(); + } + _metamodel.put(cls, m2); + } + } finally { + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); + rc.append("MetaDataRepository.setMetaModel() exit: "); + rc.done(); + JAGDebug.endReportTracking(); + } } /** @@ -1891,19 +1962,94 @@ public Class getMetaModel(ClassMetaData meta, boolean load) { * the meta class. */ public Class getMetaModel(Class entity, boolean load) { - if (_metamodel.containsKey(entity)) - return _metamodel.get(entity); - String m2 = _factory.getMetaModelClassName(entity.getName()); + JAGDebug.beginReportTracking(true); + Class retVal = null; try { - ClassLoader loader = AccessController.doPrivileged(J2DoPrivHelper.getClassLoaderAction(entity)); - Class m2cls = AccessController.doPrivileged(J2DoPrivHelper.getForNameAction(m2, true, loader)); - _metamodel.put(entity, m2cls); - return m2cls; - } catch (Throwable t) { - if (_log.isInfoEnabled()) - _log.warn(_loc.get("meta-no-model", m2, entity, t)); + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); + try { + rc.append("MetaDataRepository.getMetaModel() entry:").nl(); + rc.logVariable(" this", this).append(" ").logObjectAddress(this).nl(); + rc.logVariable(" entity", entity).append(" ").logObjectAddress(entity).nl(); + rc.logVariable(" load", load).append(" ").logObjectAddress(load).nl(); + + try { + if (entity != null) { + final ProtectionDomain pd = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(entity)); +// rc.logVariable("entity protection domain", pd).nl(); + final CodeSource cs = pd.getCodeSource(); + rc.logVariable(" entity CodeSource", cs).nl(); + + } + } catch (Throwable t) { + t.printStackTrace(); + } + } finally { + rc.done(); + } + + if (_metamodel != null && !_metamodel.isEmpty()) { + rc = JAGDebug.startReportChain(); + try { + rc.append("_metamodel (").append(_metamodel.size()).append(")").nl(); + for (Map.Entry, Class> entry : _metamodel.entrySet()) { + rc.append(" ").append(entry.getKey()).append(" ").logObjectAddress(entry.getKey()) + .append(" |---> ").append(entry.getValue()).append(" ").logObjectAddress(entry.getValue()) + .nl(); + final ProtectionDomain pd_key = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(entry.getKey())); + final ProtectionDomain pd_val = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(entry.getValue())); + rc.append(" key cs = ").append(pd_key.getCodeSource()).nl(); + rc.append(" val cs = ").append(pd_val.getCodeSource()).nl(); + } + } catch (Throwable t) { + t.printStackTrace(); + } finally { + rc.done(); + } + } else { + JAGDebug.report("_metamodel is empty."); + } + + if (_metamodel.containsKey(entity)) { + retVal = _metamodel.get(entity); + return retVal; + // return _metamodel.get(entity); + } + + String m2 = _factory.getMetaModelClassName(entity.getName()); + try { + ClassLoader loader = AccessController.doPrivileged(J2DoPrivHelper.getClassLoaderAction(entity)); + Class m2cls = AccessController.doPrivileged(J2DoPrivHelper.getForNameAction(m2, true, loader)); + _metamodel.put(entity, m2cls); + rc = JAGDebug.startReportChain(); + try { + rc.append("Inserting into _metamodel: ").nl(); + rc.append(" entity (key) = ").append(entity).append(" ").logObjectAddress(entity).nl(); + rc.append(" m2cls (val) = ").append(m2cls).append(" ").logObjectAddress(m2cls).nl(); + final ProtectionDomain pd_m2cls = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(m2cls)); + rc.append(" (CodeSource) = ").append(pd_m2cls.getCodeSource()).nl(); + } catch (Throwable t) { + t.printStackTrace(); + } finally { + rc.done(); + } + retVal = m2cls; + return m2cls; + } catch (Throwable t) { + if (_log.isInfoEnabled()) + _log.warn(_loc.get("meta-no-model", m2, entity, t)); + } + return null; + } finally { + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); + rc.append("MetaDataRepository.getMetaModel() exit: "); + rc.append(retVal).append(" ").logObjectAddress(retVal); + rc.done(); + JAGDebug.endReportTracking(); } - return null; } // ///////////////////////////// diff --git a/openjpa-lib/src/main/java/jag/JAGDebug.java b/openjpa-lib/src/main/java/jag/JAGDebug.java index 66159eae02..86d1124cd2 100644 --- a/openjpa-lib/src/main/java/jag/JAGDebug.java +++ b/openjpa-lib/src/main/java/jag/JAGDebug.java @@ -312,6 +312,23 @@ public ReportChain logVariableAddress(String varName, Object o) { return this; } + public ReportChain logObjectAddress(Object o) { + if (!isDone) { + append(" "); + try { + if (o != null) { + append("(").append("@"); + append(Integer.toHexString(System.identityHashCode(o))); + append(")"); + } else { + append("null"); + } + } catch (Throwable t) {} + } + + return this; + } + private void logObjectInternal(Object o, boolean runToStringOnPCClass) { if (o == null) { append(o); diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java index 0208d8ccff..73ab263d29 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java @@ -20,6 +20,9 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.security.AccessController; +import java.security.CodeSource; +import java.security.ProtectionDomain; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -28,6 +31,10 @@ import javax.persistence.Cache; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnitUtil; +import javax.persistence.metamodel.EmbeddableType; +import javax.persistence.metamodel.EntityType; +import javax.persistence.metamodel.MappedSuperclassType; +import javax.persistence.metamodel.Type; import javax.persistence.spi.LoadState; import org.apache.commons.lang.StringUtils; @@ -42,15 +49,19 @@ import org.apache.openjpa.lib.conf.Value; import org.apache.openjpa.lib.log.Log; import org.apache.openjpa.lib.util.Closeable; +import org.apache.openjpa.lib.util.J2DoPrivHelper; import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.meta.MetaDataRepository; import org.apache.openjpa.persistence.criteria.CriteriaBuilderImpl; import org.apache.openjpa.persistence.criteria.OpenJPACriteriaBuilder; import org.apache.openjpa.persistence.meta.MetamodelImpl; +import org.apache.openjpa.persistence.meta.Types; import org.apache.openjpa.persistence.query.OpenJPAQueryBuilder; import org.apache.openjpa.persistence.query.QueryBuilderImpl; import org.apache.openjpa.util.UserException; +import jag.JAGDebug; + /** * Implementation of {@link EntityManagerFactory} that acts as a * facade to a {@link BrokerFactory}. @@ -343,13 +354,34 @@ public Set getSupportedProperties() { } public MetamodelImpl getMetamodel() { - if (_metaModel == null) { - MetaDataRepository mdr = getConfiguration().getMetaDataRepositoryInstance(); - mdr.setValidate(MetaDataRepository.VALIDATE_RUNTIME, true); - mdr.setResolve(MetaDataRepository.MODE_MAPPING_INIT, true); - _metaModel = new MetamodelImpl(mdr); + JAGDebug.beginReportTracking(true); + MetamodelImpl retVal = null; + try { + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); + try { + rc.append("EntityManagerFactoryImpl.getMetamodel() entry:").nl(); + rc.logVariable(" this", this).append(" ").logObjectAddress(this).nl(); + + } finally { + rc.done(); + } + + if (_metaModel == null) { + MetaDataRepository mdr = getConfiguration().getMetaDataRepositoryInstance(); + mdr.setValidate(MetaDataRepository.VALIDATE_RUNTIME, true); + mdr.setResolve(MetaDataRepository.MODE_MAPPING_INIT, true); + _metaModel = new MetamodelImpl(mdr); + } + + retVal = _metaModel; + return _metaModel; + } finally { + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); + rc.append("EntityManagerFactoryImpl.getMetamodel() exit: ").nl(); + rc.logVariable("retVal", retVal); + rc.done(); + JAGDebug.endReportTracking(); } - return _metaModel; } public PersistenceUnitUtil getPersistenceUnitUtil() { diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java index 44eeca2cd2..cf87992311 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java @@ -20,6 +20,8 @@ import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; +import java.security.AccessController; +import java.security.CodeSource; import java.security.ProtectionDomain; import java.util.HashMap; import java.util.Map; @@ -43,6 +45,7 @@ import org.apache.openjpa.lib.conf.ConfigurationProvider; import org.apache.openjpa.lib.conf.Configurations; import org.apache.openjpa.lib.log.Log; +import org.apache.openjpa.lib.util.J2DoPrivHelper; import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.meta.AbstractCFMetaDataFactory; import org.apache.openjpa.meta.MetaDataModes; @@ -51,6 +54,8 @@ import org.apache.openjpa.persistence.validation.ValidationUtils; import org.apache.openjpa.util.ClassResolver; +import jag.JAGDebug; + /** * Bootstrapping class that allows the creation of a stand-alone @@ -80,55 +85,91 @@ public class PersistenceProviderImpl * @return EntityManagerFactory or null */ public OpenJPAEntityManagerFactory createEntityManagerFactory(String name, String resource, Map m) { - PersistenceProductDerivation pd = new PersistenceProductDerivation(); + JAGDebug.beginReportTracking(true); + OpenJPAEntityManagerFactory retVal = null; try { - Object poolValue = Configurations.removeProperty(EMF_POOL, m); - ConfigurationProvider cp = pd.load(resource, name, m); - if (cp == null) { - return null; - } - - BrokerFactory factory = getBrokerFactory(cp, poolValue, BundleUtils.getBundleClassLoader()); - OpenJPAConfiguration conf = factory.getConfiguration(); - conf.setAppClassLoader(BundleUtils.getBundleClassLoader()); - _log = conf.getLog(OpenJPAConfiguration.LOG_RUNTIME); - pd.checkPuNameCollisions(_log,name); - - // add enhancer - loadAgent(factory); - - // Create appropriate LifecycleEventManager - loadValidator(factory); - - if (conf.getConnectionRetainModeConstant() == ConnectionRetainModes.CONN_RETAIN_ALWAYS) { - // warn about EMs holding on to connections. - _log.warn(_loc.get("retain-always", conf.getId())); + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); + try { + rc.append("PersistenceProviderImpl.createEntityManagerFactory() entry:").nl(); + rc.logVariable(" this", this).append(" ").logObjectAddress(this).nl(); + rc.logVariable(" name", name).append(" ").nl(); + rc.logVariable(" resource", resource).append(" ").nl(); + rc.logVariable(" m", m).append(" ").nl(); + } finally { + rc.done(); } - OpenJPAEntityManagerFactory emf = JPAFacadeHelper.toEntityManagerFactory(factory); - if (_log.isTraceEnabled()) { - _log.trace(this + " creating " + emf + " for PU " + name + "."); - } - return emf; - } catch (Exception e) { - if (_log != null) { - _log.error(_loc.get("create-emf-error", name), e); + PersistenceProductDerivation pd = new PersistenceProductDerivation(); + try { + Object poolValue = Configurations.removeProperty(EMF_POOL, m); + ConfigurationProvider cp = pd.load(resource, name, m); + if (cp == null) { + return null; + } + + BrokerFactory factory = getBrokerFactory(cp, poolValue, BundleUtils.getBundleClassLoader()); + OpenJPAConfiguration conf = factory.getConfiguration(); + conf.setAppClassLoader(BundleUtils.getBundleClassLoader()); + _log = conf.getLog(OpenJPAConfiguration.LOG_RUNTIME); + pd.checkPuNameCollisions(_log,name); + JAGDebug.startReportChain().logVariable("conf", conf).append(" ").logObjectAddress(conf).done(); + + JAGDebug.startReportChain().logVariable("factory", factory).append(" ") + .logObjectAddress(factory).done(); + if (factory instanceof org.apache.openjpa.kernel.DelegatingBrokerFactory) { + BrokerFactory delegate = ((org.apache.openjpa.kernel.DelegatingBrokerFactory)factory).getDelegate(); + JAGDebug.startReportChain().logVariable("factory delegate", delegate) + .append(" ").logObjectAddress(delegate).done(); + } + JAGDebug.startReportChain().logVariable("mdr", conf.getMetaDataRepositoryInstance()) + .append(" ").logObjectAddress(conf.getMetaDataRepositoryInstance()).done(); + + + // add enhancer + loadAgent(factory); + + // Create appropriate LifecycleEventManager + loadValidator(factory); + + if (conf.getConnectionRetainModeConstant() == ConnectionRetainModes.CONN_RETAIN_ALWAYS) { + // warn about EMs holding on to connections. + _log.warn(_loc.get("retain-always", conf.getId())); + } + + OpenJPAEntityManagerFactory emf = JPAFacadeHelper.toEntityManagerFactory(factory); + if (_log.isTraceEnabled()) { + _log.trace(this + " creating " + emf + " for PU " + name + "."); + } + retVal = emf; + return emf; + } catch (Exception e) { + if (_log != null) { + _log.error(_loc.get("create-emf-error", name), e); + } + + /* + * + * Maintain 1.x behavior of throwing exceptions, even though + * JPA2 9.2 - createEMF "must" return null for PU it can't handle. + * + * JPA 2.0 Specification Section 9.2 states: + * "If a provider does not qualify as the provider for the named persistence unit, + * it must return null when createEntityManagerFactory is invoked on it." + * That specification compliance behavior has happened few lines above on null return. + * Throwing runtime exception in the following code is valid (and useful) behavior + * because the qualified provider has encountered an unexpected situation. + */ + throw PersistenceExceptions.toPersistenceException(e); } - - /* - * - * Maintain 1.x behavior of throwing exceptions, even though - * JPA2 9.2 - createEMF "must" return null for PU it can't handle. - * - * JPA 2.0 Specification Section 9.2 states: - * "If a provider does not qualify as the provider for the named persistence unit, - * it must return null when createEntityManagerFactory is invoked on it." - * That specification compliance behavior has happened few lines above on null return. - * Throwing runtime exception in the following code is valid (and useful) behavior - * because the qualified provider has encountered an unexpected situation. - */ - throw PersistenceExceptions.toPersistenceException(e); + } finally { + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); + rc.append("PersistenceProviderImpl.createEntityManagerFactory() exit: ").nl(); + rc.logVariable(" retVal", retVal).append(" ").logObjectAddress(retVal).nl(); + rc.done(); + JAGDebug.endReportTracking(); } + + } private BrokerFactory getBrokerFactory(ConfigurationProvider cp, @@ -155,68 +196,111 @@ public OpenJPAEntityManagerFactory createEntityManagerFactory(String name, Map m } public OpenJPAEntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo pui, Map m) { - PersistenceProductDerivation pd = new PersistenceProductDerivation(); + JAGDebug.beginReportTracking(true); + OpenJPAEntityManagerFactory retVal = null; try { - Object poolValue = Configurations.removeProperty(EMF_POOL, m); - ConfigurationProvider cp = pd.load(pui, m); - if (cp == null) - return null; - - // add enhancer - Exception transformerException = null; - String ctOpts = (String) Configurations.getProperty(CLASS_TRANSFORMER_OPTIONS, pui.getProperties()); + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); try { - pui.addTransformer(new ClassTransformerImpl(cp, ctOpts, - pui.getNewTempClassLoader(), newConfigurationImpl())); - } catch (Exception e) { - // fail gracefully - transformerException = e; + rc.append("PersistenceProviderImpl.createContainerEntityManagerFactory() entry:").nl(); + rc.logVariable(" this", this).append(" ").logObjectAddress(this).nl(); + rc.logVariable(" pui", pui).append(" ").logObjectAddress(pui).nl(); + rc.logVariable(" m", m).append(" ").nl(); + + if (pui != null) { + rc.nl(); + rc.logVariable(" pui.purootURL", pui.getPersistenceUnitRootUrl()).nl(); + java.util.List urlList = pui.getJarFileUrls(); + if (urlList != null && urlList.size() > 0) { + for (java.net.URL url : urlList) { + rc.logVariable(" pui.jarfileurl", url).nl(); + } + } + } + } finally { + rc.done(); } + + PersistenceProductDerivation pd = new PersistenceProductDerivation(); + try { + Object poolValue = Configurations.removeProperty(EMF_POOL, m); + ConfigurationProvider cp = pd.load(pui, m); + if (cp == null) + return null; - // if the BrokerImpl hasn't been specified, switch to the - // non-finalizing one, since anything claiming to be a container - // should be doing proper resource management. - if (!Configurations.containsProperty(BrokerValue.KEY, cp.getProperties())) { - cp.addProperty("openjpa." + BrokerValue.KEY, getDefaultBrokerAlias()); - } + // add enhancer + Exception transformerException = null; + String ctOpts = (String) Configurations.getProperty(CLASS_TRANSFORMER_OPTIONS, pui.getProperties()); + try { + pui.addTransformer(new ClassTransformerImpl(cp, ctOpts, + pui.getNewTempClassLoader(), newConfigurationImpl())); + } catch (Exception e) { + // fail gracefully + transformerException = e; + } - // OPENJPA-1491 If running under OSGi, use the Bundle's ClassLoader instead of the application one - BrokerFactory factory; - if (BundleUtils.runningUnderOSGi()) { - factory = getBrokerFactory(cp, poolValue, BundleUtils.getBundleClassLoader()); - } else { - factory = getBrokerFactory(cp, poolValue, pui.getClassLoader()); - } + // if the BrokerImpl hasn't been specified, switch to the + // non-finalizing one, since anything claiming to be a container + // should be doing proper resource management. + if (!Configurations.containsProperty(BrokerValue.KEY, cp.getProperties())) { + cp.addProperty("openjpa." + BrokerValue.KEY, getDefaultBrokerAlias()); + } - OpenJPAConfiguration conf = factory.getConfiguration(); - conf.setAppClassLoader(pui.getClassLoader()); - setPersistenceEnvironmentInfo(conf, pui); - _log = conf.getLog(OpenJPAConfiguration.LOG_RUNTIME); - // now we can log any transformer exceptions from above - if (transformerException != null) { - if (_log.isTraceEnabled()) { - _log.warn(_loc.get("transformer-registration-error-ex", pui), transformerException); + // OPENJPA-1491 If running under OSGi, use the Bundle's ClassLoader instead of the application one + BrokerFactory factory; + if (BundleUtils.runningUnderOSGi()) { + factory = getBrokerFactory(cp, poolValue, BundleUtils.getBundleClassLoader()); } else { - _log.warn(_loc.get("transformer-registration-error", pui)); + factory = getBrokerFactory(cp, poolValue, pui.getClassLoader()); + } + JAGDebug.startReportChain().logVariable("factory", factory).append(" ") + .logObjectAddress(factory).done(); + if (factory instanceof org.apache.openjpa.kernel.DelegatingBrokerFactory) { + BrokerFactory delegate = ((org.apache.openjpa.kernel.DelegatingBrokerFactory)factory).getDelegate(); + JAGDebug.startReportChain().logVariable("factory delegate", delegate) + .append(" ").logObjectAddress(delegate).done(); } - } - - if (conf.getConnectionRetainModeConstant() == ConnectionRetainModes.CONN_RETAIN_ALWAYS) { - // warn about container managed EMs holding on to connections. - _log.warn(_loc.get("cm-retain-always",conf.getId())); - } - // Create appropriate LifecycleEventManager - loadValidator(factory); - - OpenJPAEntityManagerFactory emf = JPAFacadeHelper.toEntityManagerFactory(factory); - if (_log.isTraceEnabled()) { - _log.trace(this + " creating container " + emf + " for PU " + pui.getPersistenceUnitName() + "."); + OpenJPAConfiguration conf = factory.getConfiguration(); + conf.setAppClassLoader(pui.getClassLoader()); + setPersistenceEnvironmentInfo(conf, pui); + _log = conf.getLog(OpenJPAConfiguration.LOG_RUNTIME); + JAGDebug.startReportChain().logVariable("conf", conf).append(" ").logObjectAddress(conf).done(); + JAGDebug.startReportChain().logVariable("mdr", conf.getMetaDataRepositoryInstance()) + .append(" ").logObjectAddress(conf.getMetaDataRepositoryInstance()).done(); + + // now we can log any transformer exceptions from above + if (transformerException != null) { + if (_log.isTraceEnabled()) { + _log.warn(_loc.get("transformer-registration-error-ex", pui), transformerException); + } else { + _log.warn(_loc.get("transformer-registration-error", pui)); + } + } + + if (conf.getConnectionRetainModeConstant() == ConnectionRetainModes.CONN_RETAIN_ALWAYS) { + // warn about container managed EMs holding on to connections. + _log.warn(_loc.get("cm-retain-always",conf.getId())); + } + + // Create appropriate LifecycleEventManager + loadValidator(factory); + + OpenJPAEntityManagerFactory emf = JPAFacadeHelper.toEntityManagerFactory(factory); + if (_log.isTraceEnabled()) { + _log.trace(this + " creating container " + emf + " for PU " + pui.getPersistenceUnitName() + "."); + } + + retVal = emf; + return emf; + } catch (Exception e) { + throw PersistenceExceptions.toPersistenceException(e); } - - return emf; - } catch (Exception e) { - throw PersistenceExceptions.toPersistenceException(e); + } finally { + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); + rc.append("PersistenceProviderImpl.createContainerEntityManagerFactory() exit: ").nl(); + rc.logVariable(" retVal", retVal).append(" ").logObjectAddress(retVal).nl(); + rc.done(); + JAGDebug.endReportTracking(); } } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java index e9920faa95..50e55ccec9 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java @@ -27,6 +27,8 @@ import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; import java.security.AccessController; +import java.security.CodeSource; +import java.security.ProtectionDomain; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -58,6 +60,8 @@ import org.apache.openjpa.persistence.meta.Members.Member; import org.apache.openjpa.util.InternalException; +import jag.JAGDebug; + /** * Adapts JPA Metamodel to OpenJPA meta-data repository. * @@ -288,38 +292,174 @@ static CollectionType categorizeCollection(Class cls) { * Populate the static fields of the canonical type. */ public void populate(AbstractManagedType type) { - Class cls = type.getJavaType(); - Class mcls = repos.getMetaModel(cls, true); - if (mcls == null) - return; - StaticMetamodel anno = mcls.getAnnotation(StaticMetamodel.class); - if (anno == null) - throw new IllegalArgumentException(_loc.get("meta-class-no-anno", - mcls.getName(), cls.getName(), StaticMetamodel.class.getName()).getMessage()); - - if (cls != anno.value()) { - throw new IllegalStateException(_loc.get("meta-class-mismatch", - mcls.getName(), cls.getName(), anno.value()).getMessage()); - } - - ParameterizedType mfType = null; - Attribute f = null; - Field[] mfields = AccessController.doPrivileged(J2DoPrivHelper.getDeclaredFieldsAction(mcls)); - for (Field mf : mfields) { + JAGDebug.beginReportTracking(true); + try { + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); try { - mfType = getParameterizedType(mf); // metamodel type - if (mfType == null) { - continue; + rc.append("MetamodelImpl.populate() entry:").nl(); + rc.logVariable(" this", this).append(" ").logObjectAddress(this).nl(); + rc.logVariable(" type (arg)", type).append(" ").logObjectAddress(type).nl(); + + rc.logVariable(" repos", repos).append(" ").logObjectAddress(repos).nl(); + + if (type != null) { + // type.JavaType + final Class jType = type.getJavaType(); + final CodeSource jcs = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(jType)).getCodeSource(); + rc.logVariable(" type.getJavaType()", jType).append(" ") + .logObjectAddress(jType).nl(); + rc.logVariable(" type.getJavaType() codesource", jcs).nl(); + + // type.model + rc.logVariable(" type.model", type.model).append(" ").logObjectAddress(type.model).nl(); + + // type.ClassMetaData + final ClassMetaData cmd = type.meta; + rc.logVariable(" type.cmd", cmd).append(" ").logObjectAddress(cmd).nl(); + final Class dType = cmd.getDescribedType(); + final CodeSource dcs = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(dType)).getCodeSource(); + rc.logVariable(" type.cmd.getDescribedType()", dType).append(" ") + .logObjectAddress(dType).nl(); + rc.logVariable(" type.cmd.getDescribedType() codesource", dcs).nl(); + } + + if (_basics != null && _basics.size() > 0) { + rc.append(" _basics (" + _basics.size() + "):").nl(); + for (Map.Entry, Type> entry : _basics.entrySet()) { + rc.append(" ").append(entry.getKey()).logObjectAddress(entry.getKey()) + .append(" |--> ").append(entry.getValue()).logObjectAddress(entry.getValue()) + .nl(); + final CodeSource cs = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(entry.getKey())).getCodeSource(); + rc.append(" ").append("key cs = ").append(cs).nl(); + } + } + if (_embeddables != null && _embeddables.size() > 0) { + rc.append(" _embeddables (" + _embeddables.size() + "):").nl(); + for (Map.Entry, EmbeddableType> entry : _embeddables.entrySet()) { + rc.append(" ").append(entry.getKey()).append(" ").logObjectAddress(entry.getKey()) + .append(" |--> ").append(entry.getValue()).append(" ").logObjectAddress(entry.getValue()) + .nl(); + final CodeSource cs = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(entry.getKey())).getCodeSource(); + rc.append(" ").append("key cs = ").append(cs).nl(); + } + } + if (_entities != null && _entities.size() > 0) { + rc.append(" _entities (" + _entities.size() + "):").nl(); + for (Map.Entry, EntityType> entry : _entities.entrySet()) { + rc.append(" ").append(entry.getKey()).append(" ").logObjectAddress(entry.getKey()) + .append(" |--> ").append(entry.getValue()).append(" ").logObjectAddress(entry.getValue()) + .nl(); + final CodeSource cs = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(entry.getKey())).getCodeSource(); + rc.append(" ").append("key cs = ").append(cs).nl(); + } } - f = type.getAttribute(mf.getName()); // persistent type + if (_mappedsupers != null && _mappedsupers.size() > 0) { + rc.append(" _mappedsupers (" + _mappedsupers.size() + "):").nl(); + for (Map.Entry, MappedSuperclassType> entry : _mappedsupers.entrySet()) { + rc.append(" ").append(entry.getKey()).append(" ").logObjectAddress(entry.getKey()) + .append(" |--> ").append(entry.getValue()).append(" ").logObjectAddress(entry.getValue()) + .nl(); + final CodeSource cs = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(entry.getKey())).getCodeSource(); + rc.append(" ").append("key cs = ").append(cs).nl(); + } + } + if (_pseudos != null && _pseudos.size() > 0) { + rc.append(" _pseudos (" + _pseudos.size() + "):").nl(); + for (Map.Entry, Types.PseudoEntity> entry : _pseudos.entrySet()) { + rc.append(" ").append(entry.getKey()).append(" ").logObjectAddress(entry.getKey()) + .append(" |--> ").append(entry.getValue()).append(" ").logObjectAddress(entry.getValue()) + .nl(); + final CodeSource cs = AccessController + .doPrivileged(J2DoPrivHelper.getProtectionDomainAction(entry.getKey())).getCodeSource(); + rc.append(" ").append("key cs = ").append(cs).nl(); + } + } + } catch (Throwable t) { + t.printStackTrace(); + } finally { + rc.done(); + } + + Class cls = type.getJavaType(); + Class mcls = repos.getMetaModel(cls, true); + JAGDebug.startReportChain().logVariable("cls", cls).done(); + JAGDebug.startReportChain().logVariable("mcls", mcls).done(); + if (mcls == null) + return; + StaticMetamodel anno = mcls.getAnnotation(StaticMetamodel.class); + JAGDebug.startReportChain().logVariable("anno", anno).done(); + if (anno != null) { + Class annoVal = anno.value(); + JAGDebug.startReportChain().logVariable("anno.value()", annoVal).logObjectAddress(annoVal).done(); + } + if (anno == null) + throw new IllegalArgumentException(_loc.get("meta-class-no-anno", + mcls.getName(), cls.getName(), StaticMetamodel.class.getName()).getMessage()); - // populate the static field with persistent type information - mf.set(null, f); - } catch (Exception e) { - throw new RuntimeException(_loc.get("meta-field-mismatch", - new Object[] { mf.getName(), mcls.getName(), toTypeName(mfType), f.getJavaType().toString() }) - .getMessage(), e); + if (cls != anno.value()) { + throw new IllegalStateException(_loc.get("meta-class-mismatch", + mcls.getName(), cls.getName(), anno.value()).getMessage()); + } + + ParameterizedType mfType = null; + Attribute f = null; + Field[] mfields = AccessController.doPrivileged(J2DoPrivHelper.getDeclaredFieldsAction(mcls)); + if (mfields != null) { + JAGDebug.startReportChain().append("mfields:").nl() + .logVariable(" mfields.size()", mfields.length).nl() + .logVariable(" mfields", mfields).done(); + } + + for (Field mf : mfields) { + try { + JAGDebug.startReportChain().logVariable(" Field mf", mf).append(" ").logObjectAddress(mf).done(); + mfType = getParameterizedType(mf); // metamodel type + JAGDebug.startReportChain().logVariable(" mfType", mfType) + .append(" ").logObjectAddress(mfType).done(); + if (mfType == null) { + continue; + } + f = type.getAttribute(mf.getName()); // persistent type + JAGDebug.startReportChain().logVariable(" f", f).append(" ").logObjectAddress(f).done(); + + // populate the static field with persistent type information + mf.set(null, f); + JAGDebug.startReportChain() + .append("Set Metamodel Static Class Field").nl() + .logVariable(" field", mf).nl() + .append(" of declaring class ").append(mf.getDeclaringClass()).append(" ") + .logObjectAddress(mf.getDeclaringClass()).nl() + .append(" with MetaModel").nl() + .logVariable(" f", f).append(" ").logObjectAddress(f).nl() + .logVariable(" f.getName()", f.getName()).nl() + .logVariable(" f.getJavaType()", f.getJavaType()).append(" ") + .logObjectAddress(f.getJavaType()).nl() + .logVariable(" f.getJavaMember()", f.getJavaMember()).nl() + .logObjectAddress(f.getJavaMember()).nl() + .logVariable(" f.getDeclaringType()", f.getDeclaringType()).nl() + .logObjectAddress(f.getDeclaringType()).nl() + .logVariable(" f.getPersistentAttributeType()", f.getPersistentAttributeType()).nl() + .logObjectAddress(f.getPersistentAttributeType()).nl() + .done(); + } catch (Exception e) { + JAGDebug.startReportChain().append(e).done(); + throw new RuntimeException(_loc.get("meta-field-mismatch", + new Object[] { mf.getName(), mcls.getName(), toTypeName(mfType), + f.getJavaType().toString() }) + .getMessage(), e); + } } + } finally { + JAGDebug.ReportChain rc = JAGDebug.startReportChain(); + rc.append("MetamodelImpl.populate() exit: "); + rc.done(); + JAGDebug.endReportTracking(); } }