Search in sources :

Example 1 with BroadleafClassTransformer

use of org.broadleafcommerce.common.extensibility.jpa.convert.BroadleafClassTransformer in project BroadleafCommerce by BroadleafCommerce.

the class MergePersistenceUnitManager method addTransformersToPersistenceUnits.

/**
 * Adds all of the configured {@link #classTransformers} to all of the persistence units
 * @return whether or not there was a LoadTimeWeaver registered
 * @throws Exception if there was an undetectable problem during transformer addition
 */
protected boolean addTransformersToPersistenceUnits() throws Exception {
    boolean weaverRegistered = true;
    for (PersistenceUnitInfo pui : mergedPus.values()) {
        if (pui.getProperties().containsKey(AvailableSettings.USE_CLASS_ENHANCER) && "true".equalsIgnoreCase(pui.getProperties().getProperty(AvailableSettings.USE_CLASS_ENHANCER))) {
            pui.addTransformer(new InterceptFieldClassFileTransformer(pui.getManagedClassNames()));
        }
        for (BroadleafClassTransformer transformer : classTransformers) {
            try {
                boolean isTransformerQualified = !(transformer instanceof NullClassTransformer) && (pui.getPersistenceUnitName().equals("blPU") && !(transformer instanceof BroadleafPersistenceUnitDeclaringClassTransformer)) || ((transformer instanceof BroadleafPersistenceUnitDeclaringClassTransformer) && pui.getPersistenceUnitName().equals(((BroadleafPersistenceUnitDeclaringClassTransformer) transformer).getPersistenceUnitName()));
                if (isTransformerQualified) {
                    pui.addTransformer(transformer);
                }
            } catch (Exception e) {
                weaverRegistered = handleClassTransformerRegistrationProblem(transformer, e);
            }
        }
    }
    weaverRegistered = addNamedQueriesToPersistenceUnits(weaverRegistered);
    return weaverRegistered;
}
Also used : BroadleafClassTransformer(org.broadleafcommerce.common.extensibility.jpa.convert.BroadleafClassTransformer) NullClassTransformer(org.broadleafcommerce.common.extensibility.jpa.copy.NullClassTransformer) BroadleafPersistenceUnitDeclaringClassTransformer(org.broadleafcommerce.common.extensibility.jpa.convert.BroadleafPersistenceUnitDeclaringClassTransformer) InterceptFieldClassFileTransformer(org.hibernate.ejb.instrument.InterceptFieldClassFileTransformer) MutablePersistenceUnitInfo(org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo)

Example 2 with BroadleafClassTransformer

use of org.broadleafcommerce.common.extensibility.jpa.convert.BroadleafClassTransformer in project BroadleafCommerce by BroadleafCommerce.

the class MergePersistenceUnitManager method exceptionIfEntityMarkerNotFound.

/**
 * Detects the presence of the {@link EntityMarkerClassTransformer} and throws an exception if this is misconfigured. If there
 * are no class transformes within {@link #mergedClassTransformers} then this does nothing
 */
protected void exceptionIfEntityMarkerNotFound() {
    if (CollectionUtils.isNotEmpty(mergedClassTransformers)) {
        boolean foundEntityMarkerTransformer = IterableUtils.find(mergedClassTransformers, new Predicate<BroadleafClassTransformer>() {

            @Override
            public boolean evaluate(BroadleafClassTransformer object) {
                return EntityMarkerClassTransformer.class.isAssignableFrom(object.getClass());
            }
        }) != null;
        if (!foundEntityMarkerTransformer) {
            BeanDefinition transformersBeanDef = ((BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory()).getBeanDefinition("blMergedClassTransformers");
            String msg = "The EntityMarkerClassTransformer was not detected as registered in the the list of blMergedClassTransformers. This is" + " usually caused the blMergedClassTransformers being overridden in a different configuration. Without this transformer Broadleaf" + " is unable to validate whether or not class transformation happened as expected. This bean was registered as " + transformersBeanDef + " but it should have been detected as registerd in bl-common-applicationContext.xml. Change the definition in " + transformersBeanDef.getResourceDescription() + " to instead utilize the EarlyStageMergeBeanPostProcessor in XML or an @Merge(targetRef=\"blMergedClassTransformers\" early = true) in a Java configuration class";
            throw new IllegalStateException(msg);
        }
    }
}
Also used : BroadleafClassTransformer(org.broadleafcommerce.common.extensibility.jpa.convert.BroadleafClassTransformer) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Predicate(org.apache.commons.collections4.Predicate)

Example 3 with BroadleafClassTransformer

use of org.broadleafcommerce.common.extensibility.jpa.convert.BroadleafClassTransformer in project BroadleafCommerce by BroadleafCommerce.

the class MergePersistenceUnitManager method postProcessPersistenceUnitInfo.

@Override
protected void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo newPU) {
    super.postProcessPersistenceUnitInfo(newPU);
    ConfigurationOnlyState state = ConfigurationOnlyState.getState();
    String persistenceUnitName = newPU.getPersistenceUnitName();
    MutablePersistenceUnitInfo pui = getMergedUnit(persistenceUnitName, newPU);
    List<String> managedClassNames = newPU.getManagedClassNames();
    for (String managedClassName : managedClassNames) {
        if (!pui.getManagedClassNames().contains(managedClassName)) {
            pui.addManagedClassName(managedClassName);
        }
    }
    List<String> mappingFileNames = newPU.getMappingFileNames();
    for (String mappingFileName : mappingFileNames) {
        if (!pui.getMappingFileNames().contains(mappingFileName)) {
            pui.addMappingFileName(mappingFileName);
        }
    }
    pui.setExcludeUnlistedClasses(newPU.excludeUnlistedClasses());
    for (URL url : newPU.getJarFileUrls()) {
        // persistence unit or duplicate the persistence unit root URL location (both types of locations are scanned)
        if (!pui.getJarFileUrls().contains(url) && !pui.getPersistenceUnitRootUrl().equals(url)) {
            pui.addJarFileUrl(url);
        }
    }
    if (pui.getProperties() == null) {
        pui.setProperties(newPU.getProperties());
    } else {
        Properties props = newPU.getProperties();
        if (props != null) {
            for (Object key : props.keySet()) {
                pui.getProperties().put(key, props.get(key));
                for (BroadleafClassTransformer transformer : classTransformers) {
                    try {
                        transformer.compileJPAProperties(props, key);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
    disableSchemaCreateIfApplicable(persistenceUnitName, pui);
    if (state == null || !state.isConfigurationOnly()) {
        if (newPU.getJtaDataSource() != null) {
            pui.setJtaDataSource(newPU.getJtaDataSource());
        }
        if (newPU.getNonJtaDataSource() != null) {
            pui.setNonJtaDataSource(newPU.getNonJtaDataSource());
        }
    } else {
        pui.getProperties().setProperty("hibernate.hbm2ddl.auto", "none");
        pui.getProperties().setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
    }
    pui.setTransactionType(newPU.getTransactionType());
    if (newPU.getPersistenceProviderClassName() != null) {
        pui.setPersistenceProviderClassName(newPU.getPersistenceProviderClassName());
    }
    if (newPU.getPersistenceProviderPackageName() != null) {
        pui.setPersistenceProviderPackageName(newPU.getPersistenceProviderPackageName());
    }
}
Also used : BroadleafClassTransformer(org.broadleafcommerce.common.extensibility.jpa.convert.BroadleafClassTransformer) MutablePersistenceUnitInfo(org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo) Properties(java.util.Properties) URL(java.net.URL)

Aggregations

BroadleafClassTransformer (org.broadleafcommerce.common.extensibility.jpa.convert.BroadleafClassTransformer)3 MutablePersistenceUnitInfo (org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo)2 URL (java.net.URL)1 Properties (java.util.Properties)1 PersistenceUnitInfo (javax.persistence.spi.PersistenceUnitInfo)1 Predicate (org.apache.commons.collections4.Predicate)1 BroadleafPersistenceUnitDeclaringClassTransformer (org.broadleafcommerce.common.extensibility.jpa.convert.BroadleafPersistenceUnitDeclaringClassTransformer)1 NullClassTransformer (org.broadleafcommerce.common.extensibility.jpa.copy.NullClassTransformer)1 InterceptFieldClassFileTransformer (org.hibernate.ejb.instrument.InterceptFieldClassFileTransformer)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)1