Search in sources :

Example 1 with PersistenceContextType

use of javax.persistence.PersistenceContextType in project wildfly by wildfly.

the class PersistenceRefProcessor method getPersistenceContextRefs.

/**
 * Resolves persistence-unit-ref
 *
 * @param environment               The environment to resolve the elements for
 * @param classLoader               The deployment class loader
 * @param deploymentReflectionIndex The reflection index
 * @return The bindings for the environment entries
 */
private List<BindingConfiguration> getPersistenceContextRefs(DeploymentUnit deploymentUnit, DeploymentDescriptorEnvironment environment, ClassLoader classLoader, DeploymentReflectionIndex deploymentReflectionIndex, ResourceInjectionTarget resourceInjectionTarget) throws DeploymentUnitProcessingException {
    List<BindingConfiguration> bindingConfigurations = new ArrayList<BindingConfiguration>();
    final RemoteEnvironment remoteEnvironment = environment.getEnvironment();
    if (remoteEnvironment == null) {
        return bindingConfigurations;
    }
    if (remoteEnvironment instanceof Environment) {
        PersistenceContextReferencesMetaData persistenceUnitRefs = ((Environment) remoteEnvironment).getPersistenceContextRefs();
        if (persistenceUnitRefs != null) {
            for (PersistenceContextReferenceMetaData puRef : persistenceUnitRefs) {
                String name = puRef.getName();
                String persistenceUnitName = puRef.getPersistenceUnitName();
                String lookup = puRef.getLookupName();
                if (!isEmpty(lookup) && !isEmpty(persistenceUnitName)) {
                    throw JpaLogger.ROOT_LOGGER.cannotSpecifyBoth("<lookup-name>", lookup, "persistence-unit-name", persistenceUnitName, "<persistence-context-ref/>", resourceInjectionTarget);
                }
                if (!name.startsWith("java:")) {
                    name = environment.getDefaultContext() + name;
                }
                // our injection (source) comes from the local (ENC) lookup, no matter what.
                LookupInjectionSource injectionSource = new LookupInjectionSource(name);
                // add any injection targets
                processInjectionTargets(resourceInjectionTarget, injectionSource, classLoader, deploymentReflectionIndex, puRef, EntityManager.class);
                BindingConfiguration bindingConfiguration = null;
                if (!isEmpty(lookup)) {
                    bindingConfiguration = new BindingConfiguration(name, new LookupInjectionSource(lookup));
                } else {
                    PropertiesMetaData properties = puRef.getProperties();
                    Map<String, String> map = new HashMap<>();
                    if (properties != null) {
                        for (PropertyMetaData prop : properties) {
                            map.put(prop.getKey(), prop.getValue());
                        }
                    }
                    PersistenceContextType type = (puRef.getPersistenceContextType() == null || puRef.getPersistenceContextType() == PersistenceContextTypeDescription.TRANSACTION) ? PersistenceContextType.TRANSACTION : PersistenceContextType.EXTENDED;
                    SynchronizationType synchronizationType = (puRef.getPersistenceContextSynchronization() == null || PersistenceContextSynchronizationType.Synchronized.equals(puRef.getPersistenceContextSynchronization())) ? SynchronizationType.SYNCHRONIZED : SynchronizationType.UNSYNCHRONIZED;
                    InjectionSource pcBindingSource = this.getPersistenceContextBindingSource(deploymentUnit, persistenceUnitName, type, synchronizationType, map);
                    bindingConfiguration = new BindingConfiguration(name, pcBindingSource);
                }
                bindingConfigurations.add(bindingConfiguration);
            }
        }
    }
    return bindingConfigurations;
}
Also used : RemoteEnvironment(org.jboss.metadata.javaee.spec.RemoteEnvironment) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PersistenceContextType(javax.persistence.PersistenceContextType) PropertyMetaData(org.jboss.metadata.javaee.spec.PropertyMetaData) PropertiesMetaData(org.jboss.metadata.javaee.spec.PropertiesMetaData) PersistenceContextSynchronizationType(org.jboss.metadata.javaee.spec.PersistenceContextSynchronizationType) SynchronizationType(javax.persistence.SynchronizationType) PersistenceContextReferenceMetaData(org.jboss.metadata.javaee.spec.PersistenceContextReferenceMetaData) LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource) InjectionSource(org.jboss.as.ee.component.InjectionSource) PersistenceContextInjectionSource(org.jboss.as.jpa.injectors.PersistenceContextInjectionSource) PersistenceUnitInjectionSource(org.jboss.as.jpa.injectors.PersistenceUnitInjectionSource) RemoteEnvironment(org.jboss.metadata.javaee.spec.RemoteEnvironment) DeploymentDescriptorEnvironment(org.jboss.as.ee.component.DeploymentDescriptorEnvironment) Environment(org.jboss.metadata.javaee.spec.Environment) BindingConfiguration(org.jboss.as.ee.component.BindingConfiguration) PersistenceContextReferencesMetaData(org.jboss.metadata.javaee.spec.PersistenceContextReferencesMetaData) LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource)

Example 2 with PersistenceContextType

use of javax.persistence.PersistenceContextType in project wildfly by wildfly.

the class JPAAnnotationProcessor method getBindingSource.

private InjectionSource getBindingSource(final DeploymentUnit deploymentUnit, final AnnotationInstance annotation, String injectionTypeName, final EEModuleClassDescription classDescription) throws DeploymentUnitProcessingException {
    PersistenceUnitMetadata pu = getPersistenceUnit(deploymentUnit, annotation, classDescription);
    if (pu == null) {
        return null;
    }
    String scopedPuName = pu.getScopedPersistenceUnitName();
    ServiceName puServiceName = getPuServiceName(scopedPuName);
    if (isPersistenceContext(annotation)) {
        if (pu.getTransactionType() == PersistenceUnitTransactionType.RESOURCE_LOCAL) {
            classDescription.setInvalid(JpaLogger.ROOT_LOGGER.cannotInjectResourceLocalEntityManager());
            return null;
        }
        AnnotationValue pcType = annotation.value("type");
        PersistenceContextType type = (pcType == null || PersistenceContextType.TRANSACTION.name().equals(pcType.asString())) ? PersistenceContextType.TRANSACTION : PersistenceContextType.EXTENDED;
        AnnotationValue stType = annotation.value("synchronization");
        SynchronizationType synchronizationType = (stType == null || SynchronizationType.SYNCHRONIZED.name().equals(stType.asString())) ? SynchronizationType.SYNCHRONIZED : SynchronizationType.UNSYNCHRONIZED;
        Map<String, String> properties;
        AnnotationValue value = annotation.value("properties");
        AnnotationInstance[] props = value != null ? value.asNestedArray() : null;
        if (props != null) {
            properties = new HashMap<>();
            for (int source = 0; source < props.length; source++) {
                properties.put(props[source].value("name").asString(), props[source].value("value").asString());
            }
        } else {
            properties = null;
        }
        // get deployment settings from top level du (jboss-all.xml is only parsed at the top level).
        final JPADeploymentSettings jpaDeploymentSettings = DeploymentUtils.getTopDeploymentUnit(deploymentUnit).getAttachment(JpaAttachments.DEPLOYMENT_SETTINGS_KEY);
        return new PersistenceContextInjectionSource(type, synchronizationType, properties, puServiceName, deploymentUnit.getServiceRegistry(), scopedPuName, injectionTypeName, pu, jpaDeploymentSettings);
    } else {
        return new PersistenceUnitInjectionSource(puServiceName, deploymentUnit.getServiceRegistry(), injectionTypeName, pu);
    }
}
Also used : PersistenceContextType(javax.persistence.PersistenceContextType) SynchronizationType(javax.persistence.SynchronizationType) JPADeploymentSettings(org.jboss.as.jpa.config.JPADeploymentSettings) ServiceName(org.jboss.msc.service.ServiceName) PersistenceContextInjectionSource(org.jboss.as.jpa.injectors.PersistenceContextInjectionSource) PersistenceUnitInjectionSource(org.jboss.as.jpa.injectors.PersistenceUnitInjectionSource) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) AnnotationValue(org.jboss.jandex.AnnotationValue) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 3 with PersistenceContextType

use of javax.persistence.PersistenceContextType in project Payara by payara.

the class EntityManagerReferenceNode method writeDescriptor.

/**
 * write the descriptor class to a DOM tree and return it
 *
 * @param parent node in the DOM tree
 * @param nodeName name for the root element of this xml fragment
 * @param descriptor descriptor to write
 * @return the DOM tree top node
 */
public Node writeDescriptor(Node parent, String nodeName, EntityManagerReferenceDescriptor descriptor) {
    Node entityMgrRefNode = appendChild(parent, nodeName);
    writeLocalizedDescriptions(entityMgrRefNode, descriptor);
    appendTextChild(entityMgrRefNode, TagNames.PERSISTENCE_CONTEXT_REF_NAME, descriptor.getName());
    appendTextChild(entityMgrRefNode, TagNames.PERSISTENCE_UNIT_NAME, descriptor.getUnitName());
    PersistenceContextType contextType = descriptor.getPersistenceContextType();
    String contextTypeString = (contextType != null && PersistenceContextType.EXTENDED.equals(contextType)) ? EXTENDED : TRANSACTION;
    appendTextChild(entityMgrRefNode, TagNames.PERSISTENCE_CONTEXT_TYPE, contextTypeString);
    for (Map.Entry<String, String> property : descriptor.getProperties().entrySet()) {
        Node propertyNode = appendChild(entityMgrRefNode, TagNames.PERSISTENCE_PROPERTY);
        appendTextChild(propertyNode, TagNames.NAME_VALUE_PAIR_NAME, property.getKey());
        appendTextChild(propertyNode, TagNames.NAME_VALUE_PAIR_VALUE, property.getValue());
    }
    if (descriptor.isInjectable()) {
        InjectionTargetNode ijNode = new InjectionTargetNode();
        for (InjectionTarget target : descriptor.getInjectionTargets()) {
            ijNode.writeDescriptor(entityMgrRefNode, TagNames.INJECTION_TARGET, target);
        }
    }
    return entityMgrRefNode;
}
Also used : Node(org.w3c.dom.Node) PersistenceContextType(javax.persistence.PersistenceContextType) InjectionTarget(com.sun.enterprise.deployment.InjectionTarget) Map(java.util.Map)

Example 4 with PersistenceContextType

use of javax.persistence.PersistenceContextType in project Payara by payara.

the class EntityManagerReferenceNode method setElementValue.

/**
 * receives notiification of the value for a particular tag
 *
 * @param element the xml element
 * @param value it's associated value
 */
public void setElementValue(XMLElement element, String value) {
    if (TagNames.PERSISTENCE_CONTEXT_TYPE.equals(element.getQName())) {
        EntityManagerReferenceDescriptor entityMgrReferenceDescriptor = (EntityManagerReferenceDescriptor) getDescriptor();
        PersistenceContextType contextType = null;
        if (EXTENDED.equals(value)) {
            contextType = PersistenceContextType.EXTENDED;
        } else if (TRANSACTION.equals(value)) {
            contextType = PersistenceContextType.TRANSACTION;
        } else {
            throw new IllegalArgumentException(localStrings.getLocalString("enterprise.deployment.node.invalidvalue", "Invalid value for a tag under {0} : {1}", new Object[] { TagNames.PERSISTENCE_CONTEXT_TYPE, value }));
        }
        entityMgrReferenceDescriptor.setPersistenceContextType(contextType);
    } else if (TagNames.PERSISTENCE_CONTEXT_SYNCHRONIZATION_TYPE.equals(element.getQName())) {
        EntityManagerReferenceDescriptor entityMgrReferenceDescriptor = (EntityManagerReferenceDescriptor) getDescriptor();
        SynchronizationType synchronizationType;
        if (SYNCHRONIZED.equals(value)) {
            synchronizationType = SynchronizationType.SYNCHRONIZED;
        } else if (UNSYNCHRONIZED.equals(value)) {
            synchronizationType = SynchronizationType.UNSYNCHRONIZED;
        } else {
            throw new IllegalArgumentException(localStrings.getLocalString("enterprise.deployment.node.invalidvalue", "Invalid value for a tag under {0} : {1}", new Object[] { TagNames.PERSISTENCE_CONTEXT_SYNCHRONIZATION_TYPE, value }));
        }
        entityMgrReferenceDescriptor.setSynchronizationType(synchronizationType);
    } else if (TagNames.NAME_VALUE_PAIR_NAME.equals(element.getQName())) {
        propertyName = value;
    } else if (TagNames.NAME_VALUE_PAIR_VALUE.equals(element.getQName())) {
        EntityManagerReferenceDescriptor entityMgrReferenceDescriptor = (EntityManagerReferenceDescriptor) getDescriptor();
        entityMgrReferenceDescriptor.addProperty(propertyName, value);
        propertyName = null;
    } else {
        super.setElementValue(element, value);
    }
}
Also used : PersistenceContextType(javax.persistence.PersistenceContextType) EntityManagerReferenceDescriptor(com.sun.enterprise.deployment.EntityManagerReferenceDescriptor) SynchronizationType(javax.persistence.SynchronizationType)

Aggregations

PersistenceContextType (javax.persistence.PersistenceContextType)4 SynchronizationType (javax.persistence.SynchronizationType)3 PersistenceContextInjectionSource (org.jboss.as.jpa.injectors.PersistenceContextInjectionSource)2 PersistenceUnitInjectionSource (org.jboss.as.jpa.injectors.PersistenceUnitInjectionSource)2 EntityManagerReferenceDescriptor (com.sun.enterprise.deployment.EntityManagerReferenceDescriptor)1 InjectionTarget (com.sun.enterprise.deployment.InjectionTarget)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BindingConfiguration (org.jboss.as.ee.component.BindingConfiguration)1 DeploymentDescriptorEnvironment (org.jboss.as.ee.component.DeploymentDescriptorEnvironment)1 InjectionSource (org.jboss.as.ee.component.InjectionSource)1 LookupInjectionSource (org.jboss.as.ee.component.LookupInjectionSource)1 JPADeploymentSettings (org.jboss.as.jpa.config.JPADeploymentSettings)1 AnnotationInstance (org.jboss.jandex.AnnotationInstance)1 AnnotationValue (org.jboss.jandex.AnnotationValue)1 Environment (org.jboss.metadata.javaee.spec.Environment)1 PersistenceContextReferenceMetaData (org.jboss.metadata.javaee.spec.PersistenceContextReferenceMetaData)1 PersistenceContextReferencesMetaData (org.jboss.metadata.javaee.spec.PersistenceContextReferencesMetaData)1 PersistenceContextSynchronizationType (org.jboss.metadata.javaee.spec.PersistenceContextSynchronizationType)1