Search in sources :

Example 1 with InjectionSource

use of org.jboss.as.ee.component.InjectionSource 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 InjectionSource

use of org.jboss.as.ee.component.InjectionSource in project wildfly by wildfly.

the class JPAAnnotationProcessor method bindClassSources.

private void bindClassSources(final DeploymentUnit deploymentUnit, final AnnotationInstance annotation, final EEModuleClassDescription classDescription) throws DeploymentUnitProcessingException {
    // handle PersistenceContext and PersistenceUnit annotations
    if (isPersistenceContext(annotation) || isPersistenceUnit(annotation)) {
        String injectionTypeName = getClassLevelInjectionType(annotation);
        InjectionSource injectionSource = getBindingSource(deploymentUnit, annotation, injectionTypeName, classDescription);
        if (injectionSource != null) {
            final AnnotationValue nameValue = annotation.value("name");
            if (nameValue == null || nameValue.asString().isEmpty()) {
                classDescription.setInvalid(JpaLogger.ROOT_LOGGER.classLevelAnnotationParameterRequired(annotation.name().toString(), classDescription.getClassName(), "name"));
                return;
            }
            final String name = nameValue.asString();
            final BindingConfiguration bindingConfiguration = new BindingConfiguration(name, injectionSource);
            classDescription.getBindingConfigurations().add(bindingConfiguration);
        }
    } else if (isPersistenceUnits(annotation)) {
        // handle PersistenceUnits (array of PersistenceUnit)
        AnnotationValue containedPersistenceUnits = annotation.value("value");
        AnnotationInstance[] arrayPersistenceUnits;
        if (containedPersistenceUnits != null && (arrayPersistenceUnits = containedPersistenceUnits.asNestedArray()) != null) {
            for (int source = 0; source < arrayPersistenceUnits.length; source++) {
                String injectionTypeName = getClassLevelInjectionType(arrayPersistenceUnits[source]);
                InjectionSource injectionSource = getBindingSource(deploymentUnit, arrayPersistenceUnits[source], injectionTypeName, classDescription);
                if (injectionSource != null) {
                    final AnnotationValue nameValue = arrayPersistenceUnits[source].value("name");
                    if (nameValue == null || nameValue.asString().isEmpty()) {
                        classDescription.setInvalid(JpaLogger.ROOT_LOGGER.classLevelAnnotationParameterRequired(arrayPersistenceUnits[source].name().toString(), classDescription.getClassName(), "name"));
                        return;
                    }
                    final String name = nameValue.asString();
                    final BindingConfiguration bindingConfiguration = new BindingConfiguration(name, injectionSource);
                    classDescription.getBindingConfigurations().add(bindingConfiguration);
                }
            }
        }
    } else if (isPersistenceContexts(annotation)) {
        // handle PersistenceContexts (array of PersistenceContext)
        AnnotationValue containedPersistenceContexts = annotation.value("value");
        AnnotationInstance[] arrayPersistenceContexts;
        if (containedPersistenceContexts != null && (arrayPersistenceContexts = containedPersistenceContexts.asNestedArray()) != null) {
            for (int source = 0; source < arrayPersistenceContexts.length; source++) {
                String injectionTypeName = getClassLevelInjectionType(arrayPersistenceContexts[source]);
                InjectionSource injectionSource = getBindingSource(deploymentUnit, arrayPersistenceContexts[source], injectionTypeName, classDescription);
                if (injectionSource != null) {
                    final AnnotationValue nameValue = arrayPersistenceContexts[source].value("name");
                    if (nameValue == null || nameValue.asString().isEmpty()) {
                        classDescription.setInvalid(JpaLogger.ROOT_LOGGER.classLevelAnnotationParameterRequired(arrayPersistenceContexts[source].name().toString(), classDescription.getClassName(), "name"));
                        return;
                    }
                    final String name = nameValue.asString();
                    final BindingConfiguration bindingConfiguration = new BindingConfiguration(name, injectionSource);
                    classDescription.getBindingConfigurations().add(bindingConfiguration);
                }
            }
        }
    }
}
Also used : 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) AnnotationValue(org.jboss.jandex.AnnotationValue) BindingConfiguration(org.jboss.as.ee.component.BindingConfiguration) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 3 with InjectionSource

use of org.jboss.as.ee.component.InjectionSource in project wildfly by wildfly.

the class WSRefAnnotationProcessor method processRef.

private static void processRef(final DeploymentUnit unit, final String type, final WSRefAnnotationWrapper annotation, final ClassInfo classInfo, final InjectionTarget injectionTarget, final String bindingName) throws DeploymentUnitProcessingException {
    final EEModuleDescription moduleDescription = unit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
    final AnnotatedElement target = createAnnotatedElement(unit, classInfo, injectionTarget);
    final String componentClassName = classInfo.name().toString();
    final Map<String, String> bindingMap = new HashMap<String, String>();
    boolean isEJB = false;
    for (final ComponentDescription componentDescription : moduleDescription.getComponentsByClassName(componentClassName)) {
        if (componentDescription instanceof SessionBeanComponentDescription) {
            isEJB = true;
            bindingMap.put(componentDescription.getComponentName() + "/" + bindingName, bindingName);
        }
    }
    if (!isEJB) {
        bindingMap.put(bindingName, bindingName);
    }
    for (String refKey : bindingMap.keySet()) {
        String refName = bindingMap.get(refKey);
        ManagedReferenceFactory factory = WebServiceReferences.createWebServiceFactory(unit, type, annotation, target, refName, refKey);
        final EEModuleClassDescription classDescription = moduleDescription.addOrGetLocalClassDescription(classInfo.name().toString());
        // Create the binding from whence our injection comes.
        final InjectionSource serviceRefSource = new FixedInjectionSource(factory, factory);
        final BindingConfiguration bindingConfiguration = new BindingConfiguration(refName, serviceRefSource);
        classDescription.getBindingConfigurations().add(bindingConfiguration);
        // our injection comes from the local lookup, no matter what.
        final ResourceInjectionConfiguration injectionConfiguration = injectionTarget != null ? new ResourceInjectionConfiguration(injectionTarget, new LookupInjectionSource(refName)) : null;
        if (injectionConfiguration != null) {
            classDescription.addResourceInjection(injectionConfiguration);
        }
    }
}
Also used : SessionBeanComponentDescription(org.jboss.as.ejb3.component.session.SessionBeanComponentDescription) ComponentDescription(org.jboss.as.ee.component.ComponentDescription) HashMap(java.util.HashMap) AnnotatedElement(java.lang.reflect.AnnotatedElement) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource) InjectionSource(org.jboss.as.ee.component.InjectionSource) FixedInjectionSource(org.jboss.as.ee.component.FixedInjectionSource) FixedInjectionSource(org.jboss.as.ee.component.FixedInjectionSource) EEModuleClassDescription(org.jboss.as.ee.component.EEModuleClassDescription) SessionBeanComponentDescription(org.jboss.as.ejb3.component.session.SessionBeanComponentDescription) BindingConfiguration(org.jboss.as.ee.component.BindingConfiguration) LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource) ResourceInjectionConfiguration(org.jboss.as.ee.component.ResourceInjectionConfiguration)

Example 4 with InjectionSource

use of org.jboss.as.ee.component.InjectionSource in project wildfly by wildfly.

the class ResourceInjectionAnnotationParsingProcessor method process.

protected void process(final DeploymentPhaseContext phaseContext, final EEModuleClassDescription classDescription, final AnnotationInstance annotation, final String injectionType, final String localContextName, final InjectionTarget targetDescription, final EEModuleDescription eeModuleDescription, final Module module, final EEApplicationClasses applicationClasses, final PropertyReplacer replacer) throws DeploymentUnitProcessingException {
    final EEResourceReferenceProcessorRegistry registry = phaseContext.getDeploymentUnit().getAttachment(Attachments.RESOURCE_REFERENCE_PROCESSOR_REGISTRY);
    final AnnotationValue lookupAnnotation = annotation.value("lookup");
    String lookup = (lookupAnnotation == null) ? null : replacer.replaceProperties(lookupAnnotation.asString());
    // if "lookup" hasn't been specified then fallback on "mappedName" which we treat the same as "lookup"
    if (isEmpty(lookup)) {
        final AnnotationValue mappedNameAnnotationValue = annotation.value("mappedName");
        lookup = (mappedNameAnnotationValue == null) ? null : replacer.replaceProperties(mappedNameAnnotationValue.asString());
    }
    if (isEmpty(lookup) && FIXED_LOCATIONS.containsKey(injectionType)) {
        lookup = FIXED_LOCATIONS.get(injectionType);
    }
    InjectionSource valueSource = null;
    final boolean isEnvEntryType = this.isEnvEntryType(injectionType, module);
    if (!isEmpty(lookup)) {
        valueSource = new LookupInjectionSource(lookup, JAVAX_NAMING_CONTEXT.equals(injectionType));
    } else if (isEnvEntryType) {
    // if it's an env-entry type then we do *not* create a BindingConfiguration to bind to the ENC
    // since the binding (value) for env-entry is always driven from a deployment descriptor.
    // The deployment descriptor processing and subsequent binding in the ENC is taken care off by a
    // different Deployment unit processor. If the value isn't specified in the deployment descriptor,
    // then there will be no binding the ENC and that's what is expected by the Java EE 6 spec. Furthermore,
    // if the @Resource is an env-entry binding then the injection target will be optional since in the absence of
    // an env-entry-value, there won't be a binding and effectively no injection. This again is as expected by spec.
    } else {
        //otherwise we just try and handle it
        //if we don't have a value source we will try and inject from a lookup
        //and the user has to configure the value in a deployment descriptor
        final EEResourceReferenceProcessor resourceReferenceProcessor = registry.getResourceReferenceProcessor(injectionType);
        if (resourceReferenceProcessor != null) {
            valueSource = resourceReferenceProcessor.getResourceReferenceBindingSource();
        }
    }
    if (valueSource == null) {
        // the ResourceInjectionConfiguration is created by LazyResourceInjection
        if (targetDescription != null) {
            final LookupInjectionSource optionalInjection = new LookupInjectionSource(localContextName, true);
            final ResourceInjectionConfiguration injectionConfiguration = new ResourceInjectionConfiguration(targetDescription, optionalInjection, true);
            classDescription.addResourceInjection(injectionConfiguration);
        }
    } else {
        // our injection comes from the local lookup, no matter what.
        final InjectionSource injectionSource = new LookupInjectionSource(localContextName);
        final ResourceInjectionConfiguration injectionConfiguration = targetDescription != null ? new ResourceInjectionConfiguration(targetDescription, injectionSource) : null;
        final BindingConfiguration bindingConfiguration = new BindingConfiguration(localContextName, valueSource);
        classDescription.getBindingConfigurations().add(bindingConfiguration);
        if (injectionConfiguration != null) {
            classDescription.addResourceInjection(injectionConfiguration);
        }
    }
}
Also used : LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource) InjectionSource(org.jboss.as.ee.component.InjectionSource) AnnotationValue(org.jboss.jandex.AnnotationValue) LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource) BindingConfiguration(org.jboss.as.ee.component.BindingConfiguration) ResourceInjectionConfiguration(org.jboss.as.ee.component.ResourceInjectionConfiguration)

Example 5 with InjectionSource

use of org.jboss.as.ee.component.InjectionSource in project wildfly by wildfly.

the class ManagedBeanResourceReferenceProcessor method getResourceReferenceBindingSource.

@Override
public InjectionSource getResourceReferenceBindingSource() throws DeploymentUnitProcessingException {
    ROOT_LOGGER.debugf("Processing @Resource of type: %s", this.managedBeanClassName);
    // ComponentType binding source for managed beans
    final InjectionSource bindingSource = new ComponentTypeInjectionSource(this.managedBeanClassName);
    return bindingSource;
}
Also used : InjectionSource(org.jboss.as.ee.component.InjectionSource) ComponentTypeInjectionSource(org.jboss.as.ee.component.ComponentTypeInjectionSource) ComponentTypeInjectionSource(org.jboss.as.ee.component.ComponentTypeInjectionSource)

Aggregations

InjectionSource (org.jboss.as.ee.component.InjectionSource)12 BindingConfiguration (org.jboss.as.ee.component.BindingConfiguration)11 LookupInjectionSource (org.jboss.as.ee.component.LookupInjectionSource)10 ResourceInjectionConfiguration (org.jboss.as.ee.component.ResourceInjectionConfiguration)5 PersistenceContextInjectionSource (org.jboss.as.jpa.injectors.PersistenceContextInjectionSource)5 PersistenceUnitInjectionSource (org.jboss.as.jpa.injectors.PersistenceUnitInjectionSource)5 ArrayList (java.util.ArrayList)4 AnnotationValue (org.jboss.jandex.AnnotationValue)4 FixedInjectionSource (org.jboss.as.ee.component.FixedInjectionSource)3 ManagedReferenceFactory (org.jboss.as.naming.ManagedReferenceFactory)3 HashMap (java.util.HashMap)2 EEModuleClassDescription (org.jboss.as.ee.component.EEModuleClassDescription)2 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)2 EnvEntryInjectionSource (org.jboss.as.ee.component.EnvEntryInjectionSource)2 FieldInjectionTarget (org.jboss.as.ee.component.FieldInjectionTarget)2 InjectionTarget (org.jboss.as.ee.component.InjectionTarget)2 MethodInjectionTarget (org.jboss.as.ee.component.MethodInjectionTarget)2 ManagedReference (org.jboss.as.naming.ManagedReference)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 MalformedURLException (java.net.MalformedURLException)1