Example usage for org.springframework.orm.jpa.persistenceunit MutablePersistenceUnitInfo getPersistenceUnitRootUrl

List of usage examples for org.springframework.orm.jpa.persistenceunit MutablePersistenceUnitInfo getPersistenceUnitRootUrl

Introduction

In this page you can find the example usage for org.springframework.orm.jpa.persistenceunit MutablePersistenceUnitInfo getPersistenceUnitRootUrl.

Prototype

@Override
    @Nullable
    public URL getPersistenceUnitRootUrl() 

Source Link

Usage

From source file:org.soulspace.base.infrastructure.spring.jpa.MergingPersistenceUnitPostProcessor.java

public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
    System.out.println("Processing: " + pui.getPersistenceUnitRootUrl() + " - " + pui.getPersistenceUnitName());
    Set<String> classes = puiClasses.get(pui.getPersistenceUnitName());

    if (classes == null) {
        classes = new HashSet<String>();
        puiClasses.put(pui.getPersistenceUnitName(), classes);
    }// w w  w. jav a  2s. com
    for (String className : classes) {
        if (!pui.getManagedClassNames().contains(className)) {
            pui.getManagedClassNames().add(className);
        }
    }
    classes.addAll(pui.getManagedClassNames());

    Set<String> mappingFiles = puiMappingFiles.get(pui.getPersistenceUnitName());
    if (mappingFiles == null) {
        mappingFiles = new HashSet<String>();
        puiMappingFiles.put(pui.getPersistenceUnitName(), mappingFiles);

    }
    for (String mappingFileName : mappingFiles) {
        if (!pui.getMappingFileNames().contains(mappingFileName)) {
            pui.getMappingFileNames().add(mappingFileName);
        }
    }
    mappingFiles.addAll(pui.getMappingFileNames());

    System.out.println("Managed classes: " + pui.getManagedClassNames());
    System.out.println("Mapping files: " + pui.getMappingFileNames());
}

From source file:com.hmed.config.MultiConfigAwarePersistenceUnitManager.java

protected void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
    super.postProcessPersistenceUnitInfo(pui);

    // This our template persistence unit that we are post-processing
    // so let's just skip.
    if (isApplicationPersistenceUnit(pui)) {
        return;/*ww w .  j  ava  2  s. c om*/
    }

    final MutablePersistenceUnitInfo mainPui = getMainPersistenceUnitInfo(pui);

    if (strict) {
        pui.addJarFileUrl(pui.getPersistenceUnitRootUrl());
    }

    if (mainPui != null) {
        if (strict) {
            if (logger.isDebugEnabled()) {
                logger.debug("Merging existing jar file urls " + mainPui.getJarFileUrls()
                        + " to persistence unit [" + pui.getPersistenceUnitName() + "]");
            }
            copyPersistenceUnit(mainPui, pui);
            if (logger.isDebugEnabled()) {
                logger.debug("Persistence unit[" + pui.getPersistenceUnitName() + "] has now "
                        + "the following jar file urls " + pui.getJarFileUrls() + "");
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Merging information from [" + pui.getPersistenceUnitName() + "] " + "to ["
                        + mainPui.getPersistenceUnitName() + "]");
            }
            mergePersistenceUnit(pui, mainPui);
            if (logger.isDebugEnabled()) {
                logger.debug("Persistence unit[" + mainPui.getPersistenceUnitName() + "] has now "
                        + "the following jar file urls " + mainPui.getJarFileUrls() + "");
            }
        }
    } else if (!pui.getPersistenceUnitName().equals(persistenceUnitName)) {
        logger.debug("Adding persistence unit [" + pui.getPersistenceUnitName() + "] as is (no merging)");
    }
}

From source file:net.ggtools.maven.ddlgenerator.MultiConfigAwarePersistenceUnitManager.java

/**
 * {@inheritDoc}./*from  w w w . j  a  v a2s . c om*/
 */
@Override
protected void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
    super.postProcessPersistenceUnitInfo(pui);

    // This our template persistence unit that we are post-processing
    // so let's just skip.
    if (isApplicationPersistenceUnit(pui)) {
        return;
    }

    final MutablePersistenceUnitInfo mainPui = getMainPersistenceUnitInfo(pui);

    if (strict) {
        pui.addJarFileUrl(pui.getPersistenceUnitRootUrl());
    }

    if (mainPui != null) {
        if (strict) {
            if (log.isDebugEnabled()) {
                log.debug("Merging existing jar file urls " + mainPui.getJarFileUrls()
                        + " to persistence unit [" + pui.getPersistenceUnitName() + "]");
            }
            copyPersistenceUnit(mainPui, pui);
            if (log.isDebugEnabled()) {
                log.debug("Persistence unit[" + pui.getPersistenceUnitName() + "] has now "
                        + "the following jar file urls " + pui.getJarFileUrls() + "");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Merging information from [" + pui.getPersistenceUnitName() + "] " + "to ["
                        + mainPui.getPersistenceUnitName() + "]");
            }
            mergePersistenceUnit(pui, mainPui);
            if (log.isDebugEnabled()) {
                log.debug("Persistence unit[" + mainPui.getPersistenceUnitName() + "] has now "
                        + "the following jar file urls " + mainPui.getJarFileUrls());
            }
        }
    } else if (!pui.getPersistenceUnitName().equals(persistenceUnitName)) {
        log.debug("Adding persistence unit [" + pui.getPersistenceUnitName() + "] as is (no merging)");
    }
}

From source file:com.hmed.config.MultiConfigAwarePersistenceUnitManager.java

/**
 * Merges a persistence unit to another one. Takes care of handling both
 * managed classes and urls. If the persistence unit has managed classes,
 * only merge these and prevents scanning. If no managed classes are
 * available, add the url of the module for entity scanning.
 *
 * @param from the persistence unit to handle
 * @param to the target (merged) persistence unit
 *//*from w ww.java2s  .  c  om*/
protected void mergePersistenceUnit(MutablePersistenceUnitInfo from, MutablePersistenceUnitInfo to) {
    if (from.getManagedClassNames().size() != 0) {
        for (String s : from.getManagedClassNames()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Adding entity [" + s + "]");
            }
            to.addManagedClassName(s);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Added [" + from.getManagedClassNames().size() + "] managed classes to "
                    + "persistence unit[" + to.getPersistenceUnitName() + "]");
        }
    } else {
        to.addJarFileUrl(from.getPersistenceUnitRootUrl());
        if (logger.isDebugEnabled()) {
            logger.debug("Added [" + from.getPersistenceUnitRootUrl() + "] for entity scanning "
                    + "to persistence unit[" + to.getPersistenceUnitName() + "]");
        }
    }
}

From source file:fr.paris.lutece.util.jpa.JPAPersistenceUnitPostProcessor.java

/**
 * Show PUI infos//w w w  .  j a va 2  s.  c o  m
 * @param pui PersistenceUnitInfo
 */
private void dumpPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
    _Log.debug("Dumping content for PersistenceUnitInfo of " + pui.getPersistenceUnitName());

    _Log.debug("** getTransactionType : " + pui.getTransactionType());
    _Log.debug("** getPersistenceProviderClassName : " + pui.getPersistenceProviderClassName());
    _Log.debug("** getPersistenceProviderPackageName : " + pui.getPersistenceProviderPackageName());
    _Log.debug("** getPersistenceUnitName : " + pui.getPersistenceUnitName());
    _Log.debug("** getPersistenceXMLSchemaVersion : " + pui.getPersistenceXMLSchemaVersion());
    _Log.debug("** getJtaDataSource : " + pui.getJtaDataSource());
    _Log.debug("** getManagedClassNames : " + pui.getManagedClassNames());
    _Log.debug("** getMappingFileNames : " + pui.getMappingFileNames());
    _Log.debug("** getNonJtaDataSource : " + pui.getNonJtaDataSource());
    _Log.debug("** getPersistenceUnitRootUrl :" + pui.getPersistenceUnitRootUrl());
    _Log.debug("** getProperties : " + pui.getProperties());
}

From source file:net.ggtools.maven.ddlgenerator.MultiConfigAwarePersistenceUnitManager.java

/**
 * Merges a persistence unit to another one. Takes care of handling both
 * managed classes and urls. If the persistence unit has managed classes,
 * only merge these and prevents scanning. If no managed classes are
 * available, add the url of the module for entity scanning.
 *
 * @param from the persistence unit to handle
 * @param to   the target (merged) persistence unit
 *///from www  .jav  a  2s.  c o m
protected void mergePersistenceUnit(MutablePersistenceUnitInfo from, MutablePersistenceUnitInfo to) {
    if (from.getMappingFileNames().size() != 0) {
        for (String s : from.getMappingFileNames()) {
            if (log.isDebugEnabled()) {
                log.debug("Adding entity [" + s + "]");
            }
            to.addMappingFileName(s);
        }
        if (log.isDebugEnabled()) {
            log.debug("Added [" + from.getMappingFileNames().size() + "] mapping file to " + "persistence unit["
                    + to.getPersistenceUnitName() + "]");
        }
    } else if (from.getManagedClassNames().size() != 0) {
        for (String s : from.getManagedClassNames()) {
            if (log.isDebugEnabled()) {
                log.debug("Adding entity [" + s + "]");
            }
            to.addManagedClassName(s);
        }
        if (log.isDebugEnabled()) {
            log.debug("Added [" + from.getManagedClassNames().size() + "] managed classes to "
                    + "persistence unit[" + to.getPersistenceUnitName() + "]");
        }
    } else {
        to.addJarFileUrl(from.getPersistenceUnitRootUrl());
        if (log.isDebugEnabled()) {
            log.debug("Added [" + from.getPersistenceUnitRootUrl() + "] for entity scanning "
                    + "to persistence unit[" + to.getPersistenceUnitName() + "]");
        }
    }
}

From source file:org.broadleafcommerce.common.extensibility.jpa.MergePersistenceUnitManager.java

@Override
@SuppressWarnings({ "unchecked", "ToArrayCallWithZeroLengthArrayArgument" })
public void preparePersistenceUnitInfos() {
    //Need to use reflection to try and execute the logic in the DefaultPersistenceUnitManager
    //SpringSource added a block of code in version 3.1 to "protect" the user from having more than one PU with
    //the same name.  Of course, in our case, this happens before a merge occurs.  They have added
    //a block of code to throw an exception if more than one PU has the same name.  We want to
    //use the logic of the DefaultPersistenceUnitManager without the exception in the case of
    //a duplicate name. This will require reflection in order to do what we need.
    try {// w  w  w .  j  a va  2 s  .c  o  m
        Set<String> persistenceUnitInfoNames = null;
        Map<String, PersistenceUnitInfo> persistenceUnitInfos = null;
        ResourcePatternResolver resourcePatternResolver = null;
        Field[] fields = getClass().getSuperclass().getDeclaredFields();
        for (Field field : fields) {
            if ("persistenceUnitInfoNames".equals(field.getName())) {
                field.setAccessible(true);
                persistenceUnitInfoNames = (Set<String>) field.get(this);
            } else if ("persistenceUnitInfos".equals(field.getName())) {
                field.setAccessible(true);
                persistenceUnitInfos = (Map<String, PersistenceUnitInfo>) field.get(this);
            } else if ("resourcePatternResolver".equals(field.getName())) {
                field.setAccessible(true);
                resourcePatternResolver = (ResourcePatternResolver) field.get(this);
            }
        }

        persistenceUnitInfoNames.clear();
        persistenceUnitInfos.clear();

        Method readPersistenceUnitInfos = getClass().getSuperclass()
                .getDeclaredMethod("readPersistenceUnitInfos");
        readPersistenceUnitInfos.setAccessible(true);

        //In Spring 3.0 this returns an array
        //In Spring 3.1 this returns a List
        Object pInfosObject = readPersistenceUnitInfos.invoke(this);
        Object[] puis;
        if (pInfosObject.getClass().isArray()) {
            puis = (Object[]) pInfosObject;
        } else {
            puis = ((Collection) pInfosObject).toArray();
        }

        for (Object pui : puis) {
            MutablePersistenceUnitInfo mPui = (MutablePersistenceUnitInfo) pui;
            if (mPui.getPersistenceUnitRootUrl() == null) {
                Method determineDefaultPersistenceUnitRootUrl = getClass().getSuperclass()
                        .getDeclaredMethod("determineDefaultPersistenceUnitRootUrl");
                determineDefaultPersistenceUnitRootUrl.setAccessible(true);
                mPui.setPersistenceUnitRootUrl((URL) determineDefaultPersistenceUnitRootUrl.invoke(this));
            }
            ConfigurationOnlyState state = ConfigurationOnlyState.getState();
            if ((state == null || !state.isConfigurationOnly()) && mPui.getNonJtaDataSource() == null) {
                mPui.setNonJtaDataSource(getDefaultDataSource());
            }
            if (super.getLoadTimeWeaver() != null) {
                Method puiInitMethod = mPui.getClass().getDeclaredMethod("init", LoadTimeWeaver.class);
                puiInitMethod.setAccessible(true);
                puiInitMethod.invoke(pui, getLoadTimeWeaver());
            } else {
                Method puiInitMethod = mPui.getClass().getDeclaredMethod("init", ClassLoader.class);
                puiInitMethod.setAccessible(true);
                puiInitMethod.invoke(pui, resourcePatternResolver.getClassLoader());
            }
            postProcessPersistenceUnitInfo((MutablePersistenceUnitInfo) pui);
            String name = mPui.getPersistenceUnitName();
            persistenceUnitInfoNames.add(name);

            persistenceUnitInfos.put(name, mPui);
        }
    } catch (Exception e) {
        throw new RuntimeException("An error occured reflectively invoking methods on " + "class: "
                + getClass().getSuperclass().getName(), e);
    }

    try {
        List<String> managedClassNames = new ArrayList<String>();

        boolean weaverRegistered = true;
        for (PersistenceUnitInfo pui : mergedPus.values()) {
            for (BroadleafClassTransformer transformer : classTransformers) {
                try {
                    if (!(transformer instanceof NullClassTransformer)
                            && pui.getPersistenceUnitName().equals("blPU")) {
                        pui.addTransformer(transformer);
                    }
                } catch (Exception e) {
                    Exception refined = ExceptionHelper.refineException(IllegalStateException.class,
                            RuntimeException.class, e);
                    if (refined instanceof IllegalStateException) {
                        LOG.warn(
                                "A BroadleafClassTransformer is configured for this persistence unit, but Spring "
                                        + "reported a problem (likely that a LoadTimeWeaver is not registered). As a result, "
                                        + "the Broadleaf Commerce ClassTransformer ("
                                        + transformer.getClass().getName() + ") is "
                                        + "not being registered with the persistence unit.");
                        weaverRegistered = false;
                    } else {
                        throw refined;
                    }
                }
            }
        }

        // Only validate transformation results if there was a LoadTimeWeaver registered in the first place
        if (weaverRegistered) {
            for (PersistenceUnitInfo pui : mergedPus.values()) {
                for (String managedClassName : pui.getManagedClassNames()) {
                    if (!managedClassNames.contains(managedClassName)) {
                        // Force-load this class so that we are able to ensure our instrumentation happens globally.
                        // If transformation is happening, it should be tracked in EntityMarkerClassTransformer
                        Class.forName(managedClassName, true, getClass().getClassLoader());
                        managedClassNames.add(managedClassName);
                    }
                }
            }

            // If a class happened to be loaded by the ClassLoader before we had a chance to set up our instrumentation,
            // it may not be in a consistent state. This verifies with the EntityMarkerClassTransformer that it
            // actually saw the classes loaded by the above process
            List<String> nonTransformedClasses = new ArrayList<String>();
            for (PersistenceUnitInfo pui : mergedPus.values()) {
                for (String managedClassName : pui.getManagedClassNames()) {
                    // We came across a class that is not a real persistence class (doesn't have the right annotations)
                    // but is still being transformed/loaded by
                    // the persistence unit. This might have unexpected results downstream, but it could also be benign
                    // so just output a warning
                    if (entityMarkerClassTransformer.getTransformedNonEntityClassNames()
                            .contains(managedClassName)) {
                        LOG.warn("The class " + managedClassName
                                + " is marked as a managed class within the MergePersistenceUnitManager"
                                + " but is not annotated with @Entity, @MappedSuperclass or @Embeddable."
                                + " This class is still referenced in a persistence.xml and is being transformed by"
                                + " PersistenceUnit ClassTransformers which may result in problems downstream"
                                + " and represents a potential misconfiguration. This class should be removed from"
                                + " your persistence.xml");
                    } else if (!entityMarkerClassTransformer.getTransformedEntityClassNames()
                            .contains(managedClassName)) {
                        // This means the class not in the 'warning' list, but it is also not in the list that we would
                        // expect it to be in of valid entity classes that were transformed. This means that we
                        // never got the chance to transform the class AT ALL even though it is a valid entity class
                        nonTransformedClasses.add(managedClassName);
                    }
                }
            }

            if (CollectionUtils.isNotEmpty(nonTransformedClasses)) {
                String message = "The classes\n" + Arrays.toString(nonTransformedClasses.toArray())
                        + "\nare managed classes within the MergePersistenceUnitManager"
                        + "\nbut were not detected as being transformed by the EntityMarkerClassTransformer. These"
                        + "\nclasses are likely loaded earlier in the application startup lifecyle by the servlet"
                        + "\ncontainer. Verify that an empty <absolute-ordering /> element is contained in your"
                        + "\nweb.xml to disable scanning for ServletContainerInitializer classes by your servlet"
                        + "\ncontainer which can trigger early class loading. If the problem persists, ensure that"
                        + "\nthere are no bean references to your entity class anywhere else in your Spring applicationContext"
                        + "\nand consult the documentation for your servlet container to determine if classes are loaded"
                        + "\nprior to the Spring context initialization. Finally, ensure that Session Persistence is"
                        + "\nalso disabled by your Servlet Container. To do this in Tomcat, add <Manager pathname=\"\" />"
                        + "\ninside of the <Context> element in context.xml in your app's META-INF folder or your server's conf folder";
                LOG.error(message);
                throw new IllegalStateException(message);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.broadleafcommerce.common.extensibility.jpa.MergePersistenceUnitManager.java

@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);
        }//  ww  w .j  a va  2 s  .  c  om
    }
    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()) {
        // Avoid duplicate class scanning by Ejb3Configuration. Do not re-add the URL to the list of jars for this
        // 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);
                    }
                }
            }
        }
    }
    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());
    }
}