Search in sources :

Example 6 with PersistenceUnitMetadataHolder

use of org.jboss.as.jpa.config.PersistenceUnitMetadataHolder in project wildfly by wildfly.

the class PersistenceUnitParseProcessor method normalize.

/**
     * Eliminate duplicate PU definitions from clustering the deployment (first definition will win)
     * <p/>
     * JPA 8.2  A persistence unit must have a name. Only one persistence unit of any given name must be defined
     * within a single EJB-JAR file, within a single WAR file, within a single application client jar, or within
     * an EAR. See Section 8.2.2, “Persistence Unit Scope”.
     *
     * @param listPUHolders
     * @return
     */
private PersistenceUnitMetadataHolder normalize(List<PersistenceUnitMetadataHolder> listPUHolders) {
    // eliminate duplicates (keeping the first instance of each PU by name)
    Map<String, PersistenceUnitMetadata> flattened = new HashMap<String, PersistenceUnitMetadata>();
    for (PersistenceUnitMetadataHolder puHolder : listPUHolders) {
        for (PersistenceUnitMetadata pu : puHolder.getPersistenceUnits()) {
            if (!flattened.containsKey(pu.getPersistenceUnitName())) {
                flattened.put(pu.getPersistenceUnitName(), pu);
            } else {
                PersistenceUnitMetadata first = flattened.get(pu.getPersistenceUnitName());
                PersistenceUnitMetadata duplicate = pu;
                ROOT_LOGGER.duplicatePersistenceUnitDefinition(duplicate.getPersistenceUnitName(), first.getScopedPersistenceUnitName(), duplicate.getScopedPersistenceUnitName());
            }
        }
    }
    PersistenceUnitMetadataHolder holder = new PersistenceUnitMetadataHolder(new ArrayList<PersistenceUnitMetadata>(flattened.values()));
    return holder;
}
Also used : PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) HashMap(java.util.HashMap) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata)

Example 7 with PersistenceUnitMetadataHolder

use of org.jboss.as.jpa.config.PersistenceUnitMetadataHolder in project wildfly by wildfly.

the class PersistenceUnitParseProcessor method handleWarDeployment.

private void handleWarDeployment(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (!appClientContainerMode && isWarDeployment(deploymentUnit)) {
        int puCount;
        // ordered list of PUs
        List<PersistenceUnitMetadataHolder> listPUHolders = new ArrayList<PersistenceUnitMetadataHolder>(1);
        // handle WEB-INF/classes/META-INF/persistence.xml
        final ResourceRoot deploymentRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
        VirtualFile persistence_xml = deploymentRoot.getRoot().getChild(WEB_PERSISTENCE_XML);
        parse(persistence_xml, listPUHolders, deploymentUnit);
        PersistenceUnitMetadataHolder holder = normalize(listPUHolders);
        deploymentRoot.putAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS, holder);
        addApplicationDependenciesOnProvider(deploymentUnit, holder);
        markDU(holder, deploymentUnit);
        puCount = holder.getPersistenceUnits().size();
        // look for persistence.xml in jar files in the META-INF/persistence.xml directory (these are not currently
        // handled as subdeployments)
        List<ResourceRoot> resourceRoots = deploymentUnit.getAttachmentList(Attachments.RESOURCE_ROOTS);
        for (ResourceRoot resourceRoot : resourceRoots) {
            if (resourceRoot.getRoot().getName().toLowerCase(Locale.ENGLISH).endsWith(JAR_FILE_EXTENSION)) {
                listPUHolders = new ArrayList<PersistenceUnitMetadataHolder>(1);
                persistence_xml = resourceRoot.getRoot().getChild(META_INF_PERSISTENCE_XML);
                parse(persistence_xml, listPUHolders, deploymentUnit);
                holder = normalize(listPUHolders);
                resourceRoot.putAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS, holder);
                addApplicationDependenciesOnProvider(deploymentUnit, holder);
                markDU(holder, deploymentUnit);
                puCount += holder.getPersistenceUnits().size();
            }
        }
        ROOT_LOGGER.tracef("parsed persistence unit definitions for war %s", deploymentRoot.getRootName());
        incrementPersistenceUnitCount(deploymentUnit, puCount);
    }
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) ArrayList(java.util.ArrayList) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit)

Example 8 with PersistenceUnitMetadataHolder

use of org.jboss.as.jpa.config.PersistenceUnitMetadataHolder in project wildfly by wildfly.

the class PersistenceUnitParseProcessor method parse.

private void parse(final VirtualFile persistence_xml, final List<PersistenceUnitMetadataHolder> listPUHolders, final DeploymentUnit deploymentUnit) throws DeploymentUnitProcessingException {
    ROOT_LOGGER.tracef("parse checking if %s exists, result = %b", persistence_xml.toString(), persistence_xml.exists());
    if (persistence_xml.exists() && persistence_xml.isFile()) {
        InputStream is = null;
        try {
            is = persistence_xml.openStream();
            final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            inputFactory.setXMLResolver(NoopXMLResolver.create());
            XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is);
            PersistenceUnitMetadataHolder puHolder = PersistenceUnitXmlParser.parse(xmlReader, SpecDescriptorPropertyReplacement.propertyReplacer(deploymentUnit));
            postParseSteps(persistence_xml, puHolder, deploymentUnit);
            listPUHolders.add(puHolder);
        } catch (Exception e) {
            throw new DeploymentUnitProcessingException(JpaLogger.ROOT_LOGGER.failedToParse(persistence_xml), e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
            // Ignore
            }
        }
    }
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) XMLStreamReader(javax.xml.stream.XMLStreamReader) PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) InputStream(java.io.InputStream) IOException(java.io.IOException) XMLInputFactory(javax.xml.stream.XMLInputFactory) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 9 with PersistenceUnitMetadataHolder

use of org.jboss.as.jpa.config.PersistenceUnitMetadataHolder in project wildfly by wildfly.

the class PersistenceUnitServiceHandler method handleWarDeployment.

private static void handleWarDeployment(DeploymentPhaseContext phaseContext, boolean startEarly, Platform platform) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (isWarDeployment(deploymentUnit) && JPADeploymentMarker.isJPADeployment(deploymentUnit)) {
        final ResourceRoot deploymentRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
        PersistenceUnitMetadataHolder holder;
        ArrayList<PersistenceUnitMetadataHolder> puList = new ArrayList<PersistenceUnitMetadataHolder>(1);
        String deploymentRootName = null;
        // handle persistence.xml definition in the root of the war
        if (deploymentRoot != null && (holder = deploymentRoot.getAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS)) != null && holder.getPersistenceUnits().size() > 0) {
            // assemble and install the PU service
            puList.add(holder);
            deploymentRootName = deploymentRoot.getRootName();
        }
        // look for persistence.xml in war files in the META-INF/persistence.xml directory
        List<ResourceRoot> resourceRoots = deploymentUnit.getAttachmentList(Attachments.RESOURCE_ROOTS);
        for (ResourceRoot resourceRoot : resourceRoots) {
            if (resourceRoot.getRoot().getName().toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
                if ((holder = resourceRoot.getAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS)) != null && holder.getPersistenceUnits().size() > 0) {
                    // assemble and install the PU service
                    puList.add(holder);
                }
            }
        }
        if (startEarly) {
            // only add the WebNonTxEmCloserAction valve on the earlier invocation (AS7-6690).
            deploymentUnit.addToAttachmentList(org.jboss.as.ee.component.Attachments.WEB_SETUP_ACTIONS, new WebNonTxEmCloserAction());
        }
        ROOT_LOGGER.tracef("install persistence unit definitions for war %s", deploymentRootName);
        addPuService(phaseContext, puList, startEarly, platform);
    }
}
Also used : ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) WebNonTxEmCloserAction(org.jboss.as.jpa.interceptor.WebNonTxEmCloserAction) ArrayList(java.util.ArrayList) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit)

Example 10 with PersistenceUnitMetadataHolder

use of org.jboss.as.jpa.config.PersistenceUnitMetadataHolder in project wildfly by wildfly.

the class PersistenceUnitServiceHandler method handleJarDeployment.

private static void handleJarDeployment(DeploymentPhaseContext phaseContext, boolean startEarly, Platform platform) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (!isEarDeployment(deploymentUnit) && !isWarDeployment(deploymentUnit) && JPADeploymentMarker.isJPADeployment(deploymentUnit)) {
        final ResourceRoot deploymentRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
        PersistenceUnitMetadataHolder holder;
        if (deploymentRoot != null && (holder = deploymentRoot.getAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS)) != null && holder.getPersistenceUnits().size() > 0) {
            ArrayList<PersistenceUnitMetadataHolder> puList = new ArrayList<PersistenceUnitMetadataHolder>(1);
            puList.add(holder);
            ROOT_LOGGER.tracef("install persistence unit definition for jar %s", deploymentRoot.getRootName());
            // assemble and install the PU service
            addPuService(phaseContext, puList, startEarly, platform);
        }
    }
}
Also used : ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) ArrayList(java.util.ArrayList) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit)

Aggregations

PersistenceUnitMetadataHolder (org.jboss.as.jpa.config.PersistenceUnitMetadataHolder)18 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)10 PersistenceUnitMetadata (org.jipijapa.plugin.spi.PersistenceUnitMetadata)10 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)9 ArrayList (java.util.ArrayList)7 VirtualFile (org.jboss.vfs.VirtualFile)4 PersistenceUnitsInApplication (org.jboss.as.jpa.config.PersistenceUnitsInApplication)3 HashSet (java.util.HashSet)2 PersistenceProvider (javax.persistence.spi.PersistenceProvider)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 ComponentDescription (org.jboss.as.ee.component.ComponentDescription)2 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)2 PersistenceProviderDeploymentHolder (org.jboss.as.jpa.config.PersistenceProviderDeploymentHolder)2 ServiceName (org.jboss.msc.service.ServiceName)2 PersistenceProviderAdaptor (org.jipijapa.plugin.spi.PersistenceProviderAdaptor)2 TwoPhaseBootstrapCapable (org.jipijapa.plugin.spi.TwoPhaseBootstrapCapable)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1