Search in sources :

Example 16 with ManagedReferenceFactory

use of org.jboss.as.naming.ManagedReferenceFactory in project wildfly by wildfly.

the class ResourceReferenceProcessor method getResourceRefEntries.

private List<BindingConfiguration> getResourceRefEntries(final DeploymentUnit deploymentUnit, DeploymentDescriptorEnvironment environment, ClassLoader classLoader, DeploymentReflectionIndex deploymentReflectionIndex, ResourceInjectionTarget resourceInjectionTarget) throws DeploymentUnitProcessingException {
    List<BindingConfiguration> bindings = new ArrayList<BindingConfiguration>();
    final EEResourceReferenceProcessorRegistry registry = deploymentUnit.getAttachment(Attachments.RESOURCE_REFERENCE_PROCESSOR_REGISTRY);
    final ResourceReferencesMetaData resourceRefs = environment.getEnvironment().getResourceReferences();
    if (resourceRefs == null) {
        return bindings;
    }
    for (final ResourceReferenceMetaData resourceRef : resourceRefs) {
        final String name;
        if (resourceRef.getName().startsWith("java:")) {
            name = resourceRef.getName();
        } else {
            name = environment.getDefaultContext() + resourceRef.getName();
        }
        Class<?> classType = null;
        if (resourceRef.getType() != null) {
            try {
                classType = classLoader.loadClass(resourceRef.getType());
            } catch (ClassNotFoundException e) {
                throw EeLogger.ROOT_LOGGER.cannotLoad(e, resourceRef.getType());
            }
        }
        // our injection (source) comes from the local (ENC) lookup, no matter what.
        InjectionSource injectionSource = new LookupInjectionSource(name);
        classType = processInjectionTargets(resourceInjectionTarget, injectionSource, classLoader, deploymentReflectionIndex, resourceRef, classType);
        if (!isEmpty(resourceRef.getLookupName())) {
            injectionSource = new LookupInjectionSource(resourceRef.getLookupName(), classType != null && JAVAX_NAMING_CONTEXT.equals(classType.getName()));
        } else if (!isEmpty(resourceRef.getResUrl())) {
            final String url = resourceRef.getResUrl();
            if (classType != null && classType.equals(URI.class)) {
                try {
                    //we need a newURI every time
                    injectionSource = new FixedInjectionSource(new ManagedReferenceFactory() {

                        @Override
                        public ManagedReference getReference() {
                            try {
                                return new ImmediateManagedReference(new URI(url));
                            } catch (URISyntaxException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }, new URI(url));
                } catch (URISyntaxException e) {
                    throw EeLogger.ROOT_LOGGER.cannotParseResourceRefUri(e, resourceRef.getResUrl());
                }
            } else {
                try {
                    injectionSource = new FixedInjectionSource(new ManagedReferenceFactory() {

                        @Override
                        public ManagedReference getReference() {
                            try {
                                return new ImmediateManagedReference(new URL(url));
                            } catch (MalformedURLException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }, new URL(url));
                } catch (MalformedURLException e) {
                    throw EeLogger.ROOT_LOGGER.cannotParseResourceRefUri(e, resourceRef.getResUrl());
                }
            }
        } else {
            if (classType == null) {
                throw EeLogger.ROOT_LOGGER.cannotDetermineType(name);
            }
            //check if it is a well known type
            final String lookup = ResourceInjectionAnnotationParsingProcessor.FIXED_LOCATIONS.get(classType.getName());
            if (lookup != null) {
                injectionSource = new LookupInjectionSource(lookup);
            } else {
                final EEResourceReferenceProcessor resourceReferenceProcessor = registry.getResourceReferenceProcessor(classType.getName());
                if (resourceReferenceProcessor != null) {
                    injectionSource = resourceReferenceProcessor.getResourceReferenceBindingSource();
                } else if (!resourceRef.getResourceRefName().startsWith("java:")) {
                    injectionSource = new LookupInjectionSource("java:jboss/resources/" + resourceRef.getResourceRefName());
                } else {
                    //if we cannot resolve it just log
                    ROOT_LOGGER.cannotResolve("resource-env-ref", name);
                    continue;
                }
            }
        }
        bindings.add(new BindingConfiguration(name, injectionSource));
    }
    return bindings;
}
Also used : ResourceReferencesMetaData(org.jboss.metadata.javaee.spec.ResourceReferencesMetaData) MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) ManagedReference(org.jboss.as.naming.ManagedReference) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource) InjectionSource(org.jboss.as.ee.component.InjectionSource) FixedInjectionSource(org.jboss.as.ee.component.FixedInjectionSource) EnvEntryInjectionSource(org.jboss.as.ee.component.EnvEntryInjectionSource) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) ResourceReferenceMetaData(org.jboss.metadata.javaee.spec.ResourceReferenceMetaData) FixedInjectionSource(org.jboss.as.ee.component.FixedInjectionSource) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) BindingConfiguration(org.jboss.as.ee.component.BindingConfiguration) LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource)

Example 17 with ManagedReferenceFactory

use of org.jboss.as.naming.ManagedReferenceFactory in project wildfly by wildfly.

the class ServiceInjectionSource method getResourceValue.

/**
     * {@inheritDoc}
     */
public void getResourceValue(final ResolutionContext context, final ServiceBuilder<?> serviceBuilder, final DeploymentPhaseContext phaseContext, final Injector<ManagedReferenceFactory> injector) {
    Injector inject = ManagedReferenceFactory.class.isAssignableFrom(serviceValueType) ? injector : new ManagedReferenceInjector(injector);
    serviceBuilder.addDependency(serviceName, serviceValueType, inject);
}
Also used : ManagedReferenceInjector(org.jboss.as.naming.ManagedReferenceInjector) ManagedReferenceInjector(org.jboss.as.naming.ManagedReferenceInjector) Injector(org.jboss.msc.inject.Injector) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory)

Example 18 with ManagedReferenceFactory

use of org.jboss.as.naming.ManagedReferenceFactory in project wildfly by wildfly.

the class AbstractComponentConfigurator method mergeInjectionsForClass.

/**
     * Sets up all resource injections for a class. This takes into account injections that have been specified in the module and component deployment descriptors
     * <p/>
     * Note that this does not take superclasses into consideration, only injections on the current class
     *
     * @param clazz             The class or superclass to perform injection for
     * @param actualClass       The actual component or interceptor class
     * @param classDescription  The class description, may be null
     * @param moduleDescription The module description
     * @param description       The component description
     * @param configuration     The component configuration
     * @param context           The phase context
     * @param injectors         The list of injectors for the current component
     * @param instanceKey       The key that identifies the instance to inject in the interceptor context
     * @param uninjectors       The list of uninjections for the current component
     * @throws org.jboss.as.server.deployment.DeploymentUnitProcessingException
     *
     */
protected void mergeInjectionsForClass(final Class<?> clazz, final Class<?> actualClass, final EEModuleClassDescription classDescription, final EEModuleDescription moduleDescription, final DeploymentReflectionIndex deploymentReflectionIndex, final ComponentDescription description, final ComponentConfiguration configuration, final DeploymentPhaseContext context, final Deque<InterceptorFactory> injectors, final Object instanceKey, final Deque<InterceptorFactory> uninjectors, boolean metadataComplete) throws DeploymentUnitProcessingException {
    final Map<InjectionTarget, ResourceInjectionConfiguration> mergedInjections = new HashMap<InjectionTarget, ResourceInjectionConfiguration>();
    if (classDescription != null && !metadataComplete) {
        mergedInjections.putAll(classDescription.getInjectionConfigurations());
    }
    mergedInjections.putAll(moduleDescription.getResourceInjections(clazz.getName()));
    mergedInjections.putAll(description.getResourceInjections(clazz.getName()));
    for (final ResourceInjectionConfiguration injectionConfiguration : mergedInjections.values()) {
        if (!moduleDescription.isAppClient() && injectionConfiguration.getTarget().isStatic(context.getDeploymentUnit())) {
            ROOT_LOGGER.debugf("Injection for a member with static modifier is only acceptable on application clients, ignoring injection for target %s", injectionConfiguration.getTarget());
            continue;
        }
        if (injectionConfiguration.getTarget() instanceof MethodInjectionTarget) {
            //we need to make sure that if this is a method injection it has not been overriden
            final MethodInjectionTarget mt = (MethodInjectionTarget) injectionConfiguration.getTarget();
            Method method = mt.getMethod(deploymentReflectionIndex, clazz);
            if (!isNotOverriden(clazz, method, actualClass, deploymentReflectionIndex)) {
                continue;
            }
        }
        final Object valueContextKey = new Object();
        final InjectedValue<ManagedReferenceFactory> managedReferenceFactoryValue = new InjectedValue<ManagedReferenceFactory>();
        configuration.getStartDependencies().add(new ComponentDescription.InjectedConfigurator(injectionConfiguration, configuration, context, managedReferenceFactoryValue));
        injectors.addFirst(injectionConfiguration.getTarget().createInjectionInterceptorFactory(instanceKey, valueContextKey, managedReferenceFactoryValue, context.getDeploymentUnit(), injectionConfiguration.isOptional()));
        uninjectors.addLast(new ImmediateInterceptorFactory(new ManagedReferenceReleaseInterceptor(valueContextKey)));
    }
}
Also used : InjectedValue(org.jboss.msc.value.InjectedValue) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) ImmediateInterceptorFactory(org.jboss.invocation.ImmediateInterceptorFactory)

Example 19 with ManagedReferenceFactory

use of org.jboss.as.naming.ManagedReferenceFactory in project wildfly by wildfly.

the class NamingBindingAdd method doRebind.

void doRebind(OperationContext context, ModelNode model, BinderService service) throws OperationFailedException {
    ManagedReferenceFactory ref = service.getManagedObjectInjector().getValue();
    if (ref instanceof MutableManagedReferenceFactory) {
        MutableManagedReferenceFactory factory = (MutableManagedReferenceFactory) ref;
        final BindingType type = BindingType.forName(NamingBindingResourceDefinition.BINDING_TYPE.resolveModelAttribute(context, model).asString());
        if (type == BindingType.SIMPLE) {
            Object bindValue = createSimpleBinding(context, model);
            factory.setFactory(new ValueManagedReferenceFactory(new ImmediateValue<Object>(bindValue)));
            service.setSource(bindValue);
        } else if (type == BindingType.OBJECT_FACTORY) {
            final ObjectFactory objectFactoryClassInstance = createObjectFactory(context, model);
            final Hashtable<String, String> environment = getObjectFactoryEnvironment(context, model);
            factory.setFactory(new ObjectFactoryManagedReference(objectFactoryClassInstance, service.getName(), environment));
            service.setSource(objectFactoryClassInstance);
        } else if (type == BindingType.LOOKUP) {
            final String lookup = NamingBindingResourceDefinition.LOOKUP.resolveModelAttribute(context, model).asString();
            factory.setFactory(new LookupManagedReferenceFactory(lookup));
            service.setSource(null);
        } else if (type == BindingType.EXTERNAL_CONTEXT) {
            throw NamingLogger.ROOT_LOGGER.cannotRebindExternalContext();
        } else {
            throw NamingLogger.ROOT_LOGGER.unknownBindingType(type.toString());
        }
    } else {
        throw NamingLogger.ROOT_LOGGER.cannotRebindExternalContext();
    }
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory) ExternalContextObjectFactory(org.jboss.as.naming.ExternalContextObjectFactory) ValueManagedReferenceFactory(org.jboss.as.naming.ValueManagedReferenceFactory) ContextListAndJndiViewManagedReferenceFactory(org.jboss.as.naming.ContextListAndJndiViewManagedReferenceFactory) ContextListManagedReferenceFactory(org.jboss.as.naming.ContextListManagedReferenceFactory) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) ValueManagedReferenceFactory(org.jboss.as.naming.ValueManagedReferenceFactory) Hashtable(java.util.Hashtable) ImmediateValue(org.jboss.msc.value.ImmediateValue)

Example 20 with ManagedReferenceFactory

use of org.jboss.as.naming.ManagedReferenceFactory in project wildfly by wildfly.

the class NamingBindingResourceDefinition method registerOperations.

@Override
public void registerOperations(ManagementResourceRegistration resourceRegistration) {
    super.registerOperations(resourceRegistration);
    SimpleOperationDefinitionBuilder builder = new SimpleOperationDefinitionBuilder(NamingSubsystemModel.REBIND, getResourceDescriptionResolver()).addParameter(BINDING_TYPE).addParameter(TYPE).addParameter(VALUE).addParameter(CLASS).addParameter(MODULE).addParameter(LOOKUP).addParameter(ENVIRONMENT);
    resourceRegistration.registerOperationHandler(builder.build(), new OperationStepHandler() {

        @Override
        public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
            context.addStep(new OperationStepHandler() {

                @Override
                public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
                    validateResourceModel(operation, false);
                    Resource resource = context.readResourceForUpdate(PathAddress.EMPTY_ADDRESS);
                    ModelNode model = resource.getModel();
                    for (AttributeDefinition attr : ATTRIBUTES) {
                        attr.validateAndSet(operation, model);
                    }
                    context.addStep(new OperationStepHandler() {

                        @Override
                        public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
                            final String name = context.getCurrentAddressValue();
                            final ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(name);
                            ServiceController<ManagedReferenceFactory> service = (ServiceController<ManagedReferenceFactory>) context.getServiceRegistry(false).getService(bindInfo.getBinderServiceName());
                            if (service == null) {
                                context.reloadRequired();
                                return;
                            }
                            NamingBindingAdd.INSTANCE.doRebind(context, operation, (BinderService) service.getService());
                        }
                    }, OperationContext.Stage.RUNTIME);
                }
            }, OperationContext.Stage.MODEL);
        }
    });
}
Also used : OperationContext(org.jboss.as.controller.OperationContext) SimpleOperationDefinitionBuilder(org.jboss.as.controller.SimpleOperationDefinitionBuilder) OperationStepHandler(org.jboss.as.controller.OperationStepHandler) OperationFailedException(org.jboss.as.controller.OperationFailedException) Resource(org.jboss.as.controller.registry.Resource) AttributeDefinition(org.jboss.as.controller.AttributeDefinition) SimpleAttributeDefinition(org.jboss.as.controller.SimpleAttributeDefinition) PropertiesAttributeDefinition(org.jboss.as.controller.PropertiesAttributeDefinition) BinderService(org.jboss.as.naming.service.BinderService) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) ServiceController(org.jboss.msc.service.ServiceController) ModelNode(org.jboss.dmr.ModelNode)

Aggregations

ManagedReferenceFactory (org.jboss.as.naming.ManagedReferenceFactory)29 BinderService (org.jboss.as.naming.service.BinderService)12 ContextNames (org.jboss.as.naming.deployment.ContextNames)9 ManagedReference (org.jboss.as.naming.ManagedReference)7 ServiceName (org.jboss.msc.service.ServiceName)6 BindingConfiguration (org.jboss.as.ee.component.BindingConfiguration)5 InjectionSource (org.jboss.as.ee.component.InjectionSource)5 ContextListManagedReferenceFactory (org.jboss.as.naming.ContextListManagedReferenceFactory)5 ValueManagedReferenceFactory (org.jboss.as.naming.ValueManagedReferenceFactory)5 ServiceController (org.jboss.msc.service.ServiceController)5 HashMap (java.util.HashMap)3 NamingException (javax.naming.NamingException)3 Injector (org.jboss.msc.inject.Injector)3 ListenerInfo (io.undertow.servlet.api.ListenerInfo)2 HashSet (java.util.HashSet)2 InitialContext (javax.naming.InitialContext)2 OperationContext (org.jboss.as.controller.OperationContext)2 Resource (org.jboss.as.controller.registry.Resource)2 EEModuleClassDescription (org.jboss.as.ee.component.EEModuleClassDescription)2 ContextListAndJndiViewManagedReferenceFactory (org.jboss.as.naming.ContextListAndJndiViewManagedReferenceFactory)2