Search in sources :

Example 11 with PersistenceUnitMetadataHolder

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

the class PersistenceUnitServiceHandler method handleEarDeployment.

private static void handleEarDeployment(DeploymentPhaseContext phaseContext, boolean startEarly, Platform platform) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (isEarDeployment(deploymentUnit) && JPADeploymentMarker.isJPADeployment(deploymentUnit)) {
        // handle META-INF/persistence.xml
        final List<ResourceRoot> deploymentRoots = DeploymentUtils.allResourceRoots(deploymentUnit);
        for (final ResourceRoot root : deploymentRoots) {
            if (!SubDeploymentMarker.isSubDeployment(root)) {
                PersistenceUnitMetadataHolder holder;
                ArrayList<PersistenceUnitMetadataHolder> puList = new ArrayList<PersistenceUnitMetadataHolder>(1);
                if (root != null && (holder = root.getAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS)) != null && holder.getPersistenceUnits().size() > 0) {
                    // assemble and install the PU service
                    puList.add(holder);
                }
                ROOT_LOGGER.tracef("install persistence unit definitions for ear %s", root.getRootName());
                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)

Example 12 with PersistenceUnitMetadataHolder

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

the class HibernateSearchProcessor method addSearchDependency.

private void addSearchDependency(ModuleSpecification moduleSpecification, ModuleLoader moduleLoader, DeploymentUnit deploymentUnit) throws DeploymentUnitProcessingException {
    String searchModuleName = null;
    PersistenceUnitsInApplication persistenceUnitsInApplication = DeploymentUtils.getTopDeploymentUnit(deploymentUnit).getAttachment(PersistenceUnitsInApplication.PERSISTENCE_UNITS_IN_APPLICATION);
    for (PersistenceUnitMetadataHolder holder : persistenceUnitsInApplication.getPersistenceUnitHolders()) {
        for (PersistenceUnitMetadata pu : holder.getPersistenceUnits()) {
            String providerModule = pu.getProperties().getProperty(Configuration.HIBERNATE_SEARCH_MODULE);
            if (providerModule != null) {
                // one persistence unit specifying the Hibernate search module is allowed
                if (searchModuleName == null) {
                    searchModuleName = providerModule;
                } else // more than one persistence unit specifying different Hibernate search module names is not allowed
                if (!providerModule.equals(searchModuleName)) {
                    throw JpaLogger.ROOT_LOGGER.differentSearchModuleDependencies(deploymentUnit.getName(), searchModuleName, providerModule);
                }
            }
        }
    }
    if (NONE.equals(searchModuleName)) {
        // Hibernate Search module will not be added to deployment
        ROOT_LOGGER.debugf("Not adding Hibernate Search dependency to deployment %s", deploymentUnit.getName());
        return;
    }
    // use Search module name specified in persistence unit definition
    if (searchModuleName != null && !IGNORE.equals(searchModuleName)) {
        ModuleIdentifier moduleIdentifier = ModuleIdentifier.fromString(searchModuleName);
        moduleSpecification.addSystemDependency(new ModuleDependency(moduleLoader, moduleIdentifier, false, true, true, false));
        ROOT_LOGGER.debugf("added %s dependency to %s", moduleIdentifier, deploymentUnit.getName());
    } else {
        // add Hibernate Search module dependency if application is using the Hibernate Search Indexed annotation
        final CompositeIndex index = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.COMPOSITE_ANNOTATION_INDEX);
        List<AnnotationInstance> annotations = index.getAnnotations(SEARCH_INDEXED_ANNOTATION_NAME);
        if (annotations != null && annotations.size() > 0) {
            moduleSpecification.addSystemDependency(new ModuleDependency(moduleLoader, defaultSearchModule, false, true, true, false));
            ROOT_LOGGER.debugf("deployment %s contains %s annotation, added %s dependency", deploymentUnit.getName(), SEARCH_INDEXED_ANNOTATION_NAME, defaultSearchModule);
        }
    }
}
Also used : ModuleDependency(org.jboss.as.server.deployment.module.ModuleDependency) PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) PersistenceUnitsInApplication(org.jboss.as.jpa.config.PersistenceUnitsInApplication) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) ModuleIdentifier(org.jboss.modules.ModuleIdentifier) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 13 with PersistenceUnitMetadataHolder

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

the class JpaDependenciesProvider method getDependencies.

@Override
public Set<ServiceName> getDependencies(DeploymentUnit deploymentUnit) {
    Set<ServiceName> dependencies = new HashSet<>();
    for (ResourceRoot root : DeploymentUtils.allResourceRoots(deploymentUnit)) {
        final PersistenceUnitMetadataHolder persistenceUnits = root.getAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS);
        if (persistenceUnits != null && persistenceUnits.getPersistenceUnits() != null) {
            for (final PersistenceUnitMetadata pu : persistenceUnits.getPersistenceUnits()) {
                final Properties properties = pu.getProperties();
                final String jpaContainerManaged = properties.getProperty(Configuration.JPA_CONTAINER_MANAGED);
                final boolean deployPU = (jpaContainerManaged == null || Boolean.parseBoolean(jpaContainerManaged));
                if (deployPU) {
                    final ServiceName serviceName = PersistenceUnitServiceImpl.getPUServiceName(pu);
                    dependencies.add(serviceName);
                }
            }
        }
    }
    return dependencies;
}
Also used : ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) ServiceName(org.jboss.msc.service.ServiceName) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) Properties(java.util.Properties) HashSet(java.util.HashSet)

Example 14 with PersistenceUnitMetadataHolder

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

the class PersistenceUnitServiceHandler method addPuService.

/**
     * Add one PU service per top level deployment that represents
     *
     *
     * @param phaseContext
     * @param puList
     * @param startEarly
     * @param platform
     * @throws DeploymentUnitProcessingException
     *
     */
private static void addPuService(final DeploymentPhaseContext phaseContext, final ArrayList<PersistenceUnitMetadataHolder> puList, final boolean startEarly, final Platform platform) throws DeploymentUnitProcessingException {
    if (puList.size() > 0) {
        final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
        final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
        final EEModuleDescription eeModuleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
        final Collection<ComponentDescription> components = eeModuleDescription.getComponentDescriptions();
        if (module == null) {
            // Unresolved OSGi bundles would not have a module attached
            ROOT_LOGGER.failedToGetModuleAttachment(deploymentUnit);
            return;
        }
        final ServiceTarget serviceTarget = phaseContext.getServiceTarget();
        final ModuleClassLoader classLoader = module.getClassLoader();
        for (PersistenceUnitMetadataHolder holder : puList) {
            setAnnotationIndexes(holder, deploymentUnit);
            for (PersistenceUnitMetadata pu : holder.getPersistenceUnits()) {
                // only start the persistence unit if JPA_CONTAINER_MANAGED is true
                String jpaContainerManaged = pu.getProperties().getProperty(Configuration.JPA_CONTAINER_MANAGED);
                boolean deployPU = (jpaContainerManaged == null ? true : Boolean.parseBoolean(jpaContainerManaged));
                if (deployPU) {
                    final PersistenceProviderDeploymentHolder persistenceProviderDeploymentHolder = getPersistenceProviderDeploymentHolder(deploymentUnit);
                    final PersistenceProvider provider = lookupProvider(pu, persistenceProviderDeploymentHolder, deploymentUnit);
                    final PersistenceProviderAdaptor adaptor = getPersistenceProviderAdaptor(pu, persistenceProviderDeploymentHolder, deploymentUnit, provider, platform);
                    final boolean twoPhaseBootStrapCapable = (adaptor instanceof TwoPhaseBootstrapCapable) && Configuration.allowTwoPhaseBootstrap(pu);
                    if (startEarly) {
                        if (twoPhaseBootStrapCapable) {
                            deployPersistenceUnitPhaseOne(phaseContext, deploymentUnit, eeModuleDescription, components, serviceTarget, classLoader, pu, adaptor);
                        } else if (false == Configuration.needClassFileTransformer(pu)) {
                            // will start later when startEarly == false
                            ROOT_LOGGER.tracef("persistence unit %s in deployment %s is configured to not need class transformer to be set, no class rewriting will be allowed", pu.getPersistenceUnitName(), deploymentUnit.getName());
                        } else {
                            // we need class file transformer to work, don't allow cdi bean manager to be access since that
                            // could cause application classes to be loaded (workaround by setting jboss.as.jpa.classtransformer to false).  WFLY-1463
                            final boolean allowCdiBeanManagerAccess = false;
                            deployPersistenceUnit(phaseContext, deploymentUnit, eeModuleDescription, components, serviceTarget, classLoader, pu, startEarly, provider, adaptor, allowCdiBeanManagerAccess);
                        }
                    } else {
                        // !startEarly
                        if (twoPhaseBootStrapCapable) {
                            deployPersistenceUnitPhaseTwo(phaseContext, deploymentUnit, eeModuleDescription, components, serviceTarget, classLoader, pu, provider, adaptor);
                        } else if (false == Configuration.needClassFileTransformer(pu)) {
                            final boolean allowCdiBeanManagerAccess = true;
                            // PUs that have Configuration.JPA_CONTAINER_CLASS_TRANSFORMER = false will start during INSTALL phase
                            deployPersistenceUnit(phaseContext, deploymentUnit, eeModuleDescription, components, serviceTarget, classLoader, pu, startEarly, provider, adaptor, allowCdiBeanManagerAccess);
                        }
                    }
                } else {
                    ROOT_LOGGER.tracef("persistence unit %s in deployment %s is not container managed (%s is set to false)", pu.getPersistenceUnitName(), deploymentUnit.getName(), Configuration.JPA_CONTAINER_MANAGED);
                }
            }
        }
    }
}
Also used : ComponentDescription(org.jboss.as.ee.component.ComponentDescription) PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) ServiceTarget(org.jboss.msc.service.ServiceTarget) PersistenceProvider(javax.persistence.spi.PersistenceProvider) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) Module(org.jboss.modules.Module) PersistenceProviderDeploymentHolder(org.jboss.as.jpa.config.PersistenceProviderDeploymentHolder) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) TwoPhaseBootstrapCapable(org.jipijapa.plugin.spi.TwoPhaseBootstrapCapable) PersistenceProviderAdaptor(org.jipijapa.plugin.spi.PersistenceProviderAdaptor)

Example 15 with PersistenceUnitMetadataHolder

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

the class PersistenceUnitXmlParser method parse.

public static PersistenceUnitMetadataHolder parse(final XMLStreamReader reader, final PropertyReplacer propertyReplacer) throws XMLStreamException {
    // check for a bogus document and throw error
    reader.require(START_DOCUMENT, null, null);
    // Read until the first start element
    Version version = null;
    while (reader.hasNext() && reader.next() != START_ELEMENT) {
        if (reader.getEventType() == DTD) {
            final String dtdLocation = readDTDLocation(reader);
            if (dtdLocation != null) {
                version = Version.forLocation(dtdLocation);
            }
        }
    }
    final String schemaLocation = readSchemaLocation(reader);
    if (schemaLocation != null) {
        version = Version.forLocation(schemaLocation);
    }
    if (version == null || Version.UNKNOWN.equals(version)) {
        // Look at the version attribute
        String versionString = null;
        final int count = reader.getAttributeCount();
        for (int i = 0; i < count; i++) {
            final String attributeNamespace = reader.getAttributeNamespace(i);
            if (attributeNamespace != null && !attributeNamespace.isEmpty()) {
                continue;
            }
            final Attribute attribute = Attribute.forName(reader.getAttributeLocalName(i));
            if (attribute == Attribute.VERSION) {
                versionString = reader.getAttributeValue(i);
            }
        }
        if ("1.0".equals(versionString)) {
            version = Version.JPA_1_0;
        } else if ("1".equals(versionString)) {
            version = Version.JPA_1_0;
        } else if ("2.0".equals(versionString)) {
            version = Version.JPA_2_0;
        } else if ("2.1".equals(versionString)) {
            version = Version.JPA_2_1;
        } else if ("2".equals(versionString)) {
            version = Version.JPA_2_0;
        } else {
            version = Version.JPA_2_1;
        }
    }
    final int count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
        final String attributeNamespace = reader.getAttributeNamespace(i);
        if (attributeNamespace != null && !attributeNamespace.isEmpty()) {
            continue;
        }
        final Attribute attribute = Attribute.forName(reader.getAttributeLocalName(i));
        switch(attribute) {
            case VERSION:
                // TODO:  handle version
                break;
            default:
                throw unexpectedAttribute(reader, i);
        }
    }
    final List<PersistenceUnitMetadata> PUs = new ArrayList<PersistenceUnitMetadata>();
    // until the ending PERSISTENCE tag
    while (reader.hasNext() && reader.nextTag() != END_ELEMENT) {
        final Element element = Element.forName(reader.getLocalName());
        switch(element) {
            case PERSISTENCEUNIT:
                PersistenceUnitMetadata pu = parsePU(reader, version, propertyReplacer);
                PUs.add(pu);
                ROOT_LOGGER.readingPersistenceXml(pu.getPersistenceUnitName());
                break;
            default:
                throw unexpectedElement(reader);
        }
    }
    PersistenceUnitMetadataHolder result = new PersistenceUnitMetadataHolder(PUs);
    if (ROOT_LOGGER.isTraceEnabled())
        ROOT_LOGGER.trace(result.toString());
    return result;
}
Also used : PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) ArrayList(java.util.ArrayList) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata)

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