Search in sources :

Example 1 with ImmediateManagedReference

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

the class SFSBCreateInterceptor method processInvocation.

@Override
public Object processInvocation(InterceptorContext interceptorContext) throws Exception {
    ComponentInstance componentInstance = interceptorContext.getPrivateData(ComponentInstance.class);
    Map<String, ExtendedEntityManager> entityManagers = null;
    if (componentInstance.getInstanceData(SFSBInvocationInterceptor.CONTEXT_KEY) == null) {
        // Get all of the extended persistence contexts in use by the bean (some of which may of been inherited from
        // other beans).
        entityManagers = new HashMap<String, ExtendedEntityManager>();
        componentInstance.setInstanceData(SFSBInvocationInterceptor.CONTEXT_KEY, new ImmediateManagedReference(entityManagers));
    } else {
        ManagedReference entityManagerRef = (ManagedReference) componentInstance.getInstanceData(SFSBInvocationInterceptor.CONTEXT_KEY);
        entityManagers = (Map<String, ExtendedEntityManager>) entityManagerRef.getInstance();
    }
    final ExtendedEntityManager[] ems = CreatedEntityManagers.getDeferredEntityManagers();
    for (ExtendedEntityManager e : ems) {
        entityManagers.put(e.getScopedPuName(), e);
    }
    return interceptorContext.proceed();
}
Also used : ComponentInstance(org.jboss.as.ee.component.ComponentInstance) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) ManagedReference(org.jboss.as.naming.ManagedReference) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) ExtendedEntityManager(org.jboss.as.jpa.container.ExtendedEntityManager)

Example 2 with ImmediateManagedReference

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

the class LookupInjectionSource method getResourceValue.

/**
     * {@inheritDoc}
     */
public void getResourceValue(final ResolutionContext resolutionContext, final ServiceBuilder<?> serviceBuilder, final DeploymentPhaseContext phaseContext, final Injector<ManagedReferenceFactory> injector) {
    final String applicationName = resolutionContext.getApplicationName();
    final String moduleName = resolutionContext.getModuleName();
    final String componentName = resolutionContext.getComponentName();
    final boolean compUsesModule = resolutionContext.isCompUsesModule();
    final String scheme = org.jboss.as.naming.InitialContext.getURLScheme(lookupName);
    if (scheme == null) {
        // relative name, build absolute name and setup normal lookup injection
        if (componentName != null && !compUsesModule) {
            ContextNames.bindInfoFor(applicationName, moduleName, componentName, "java:comp/env/" + lookupName).setupLookupInjection(serviceBuilder, injector, phaseContext.getDeploymentUnit(), optional);
        } else if (compUsesModule) {
            ContextNames.bindInfoFor(applicationName, moduleName, componentName, "java:module/env/" + lookupName).setupLookupInjection(serviceBuilder, injector, phaseContext.getDeploymentUnit(), optional);
        } else {
            ContextNames.bindInfoFor(applicationName, moduleName, componentName, "java:jboss/env/" + lookupName).setupLookupInjection(serviceBuilder, injector, phaseContext.getDeploymentUnit(), optional);
        }
    } else {
        if (scheme.equals("java")) {
            // an absolute java name, setup normal lookup injection
            if (compUsesModule && lookupName.startsWith("java:comp/")) {
                // switch "comp" with "module"
                ContextNames.bindInfoFor(applicationName, moduleName, componentName, "java:module/" + lookupName.substring(10)).setupLookupInjection(serviceBuilder, injector, phaseContext.getDeploymentUnit(), optional);
            } else {
                ContextNames.bindInfoFor(applicationName, moduleName, componentName, lookupName).setupLookupInjection(serviceBuilder, injector, phaseContext.getDeploymentUnit(), optional);
            }
        } else {
            // an absolute non java name
            final ManagedReferenceFactory managedReferenceFactory;
            if (URL_SCHEMES.contains(scheme)) {
                // a Java EE Standard Resource Manager Connection Factory for URLs, using lookup to define value of URL, inject factory that creates URL instances
                managedReferenceFactory = new ManagedReferenceFactory() {

                    @Override
                    public ManagedReference getReference() {
                        try {
                            return new ImmediateManagedReference(new URL(lookupName));
                        } catch (MalformedURLException e) {
                            throw new RuntimeException(e);
                        }
                    }
                };
            } else {
                // lookup for a non java jndi resource, inject factory which does a true jndi lookup
                managedReferenceFactory = new ManagedReferenceFactory() {

                    @Override
                    public ManagedReference getReference() {
                        try {
                            return new ImmediateManagedReference(new InitialContext().lookup(lookupName));
                        } catch (NamingException e) {
                            EeLogger.ROOT_LOGGER.tracef(e, "failed to lookup %s", lookupName);
                            return null;
                        }
                    }
                };
            }
            injector.inject(managedReferenceFactory);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) ManagedReference(org.jboss.as.naming.ManagedReference) NamingException(javax.naming.NamingException) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) URL(java.net.URL) InitialContext(javax.naming.InitialContext)

Example 3 with ImmediateManagedReference

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

the class BasicComponent method createInstance.

/**
     * Wraps an existing object instance in a ComponentInstance, and run the post construct interceptor chain on it.
     *
     * @param instance The instance to wrap
     * @return The new ComponentInstance
     */
public ComponentInstance createInstance(Object instance) {
    BasicComponentInstance obj = constructComponentInstance(new ImmediateManagedReference(instance), true);
    obj.constructionFinished();
    return obj;
}
Also used : ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference)

Example 4 with ImmediateManagedReference

use of org.jboss.as.naming.ImmediateManagedReference 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 5 with ImmediateManagedReference

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

the class NamingBindingAdd method installExternalContext.

void installExternalContext(final OperationContext context, final String name, final ModelNode model) throws OperationFailedException {
    final String moduleID = NamingBindingResourceDefinition.MODULE.resolveModelAttribute(context, model).asString();
    final String className = NamingBindingResourceDefinition.CLASS.resolveModelAttribute(context, model).asString();
    final ModelNode cacheNode = NamingBindingResourceDefinition.CACHE.resolveModelAttribute(context, model);
    boolean cache = cacheNode.isDefined() ? cacheNode.asBoolean() : false;
    final ObjectFactory objectFactoryClassInstance = new ExternalContextObjectFactory();
    final ServiceTarget serviceTarget = context.getServiceTarget();
    final ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(name);
    final Hashtable<String, String> environment = getObjectFactoryEnvironment(context, model);
    environment.put(ExternalContextObjectFactory.CACHE_CONTEXT, Boolean.toString(cache));
    environment.put(ExternalContextObjectFactory.INITIAL_CONTEXT_CLASS, className);
    environment.put(ExternalContextObjectFactory.INITIAL_CONTEXT_MODULE, moduleID);
    final ExternalContextBinderService binderService = new ExternalContextBinderService(name, objectFactoryClassInstance);
    binderService.getManagedObjectInjector().inject(new ContextListAndJndiViewManagedReferenceFactory() {

        @Override
        public ManagedReference getReference() {
            try {
                final Object value = objectFactoryClassInstance.getObjectInstance(name, null, null, environment);
                return new ImmediateManagedReference(value);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public String getInstanceClassName() {
            return className;
        }

        @Override
        public String getJndiViewInstanceValue() {
            final ClassLoader cl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
            try {
                WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(objectFactoryClassInstance.getClass().getClassLoader());
                return String.valueOf(getReference().getInstance());
            } finally {
                WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(cl);
            }
        }
    });
    serviceTarget.addService(bindInfo.getBinderServiceName(), binderService).addDependency(bindInfo.getParentContextServiceName(), ServiceBasedNamingStore.class, binderService.getNamingStoreInjector()).addDependency(ExternalContextsService.SERVICE_NAME, ExternalContexts.class, binderService.getExternalContextsInjector()).install();
}
Also used : ServiceTarget(org.jboss.msc.service.ServiceTarget) ManagedReference(org.jboss.as.naming.ManagedReference) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) ContextListAndJndiViewManagedReferenceFactory(org.jboss.as.naming.ContextListAndJndiViewManagedReferenceFactory) ModuleLoadException(org.jboss.modules.ModuleLoadException) MalformedURLException(java.net.MalformedURLException) OperationFailedException(org.jboss.as.controller.OperationFailedException) ObjectFactory(javax.naming.spi.ObjectFactory) ExternalContextObjectFactory(org.jboss.as.naming.ExternalContextObjectFactory) ExternalContexts(org.jboss.as.naming.context.external.ExternalContexts) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) ModelNode(org.jboss.dmr.ModelNode) ExternalContextObjectFactory(org.jboss.as.naming.ExternalContextObjectFactory) ContextNames(org.jboss.as.naming.deployment.ContextNames) ExternalContextBinderService(org.jboss.as.naming.service.ExternalContextBinderService)

Aggregations

ImmediateManagedReference (org.jboss.as.naming.ImmediateManagedReference)6 ManagedReference (org.jboss.as.naming.ManagedReference)4 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)2 ManagedReferenceFactory (org.jboss.as.naming.ManagedReferenceFactory)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 ObjectFactory (javax.naming.spi.ObjectFactory)1 OperationFailedException (org.jboss.as.controller.OperationFailedException)1 BindingConfiguration (org.jboss.as.ee.component.BindingConfiguration)1 ComponentInstance (org.jboss.as.ee.component.ComponentInstance)1 EnvEntryInjectionSource (org.jboss.as.ee.component.EnvEntryInjectionSource)1 FixedInjectionSource (org.jboss.as.ee.component.FixedInjectionSource)1 InjectionSource (org.jboss.as.ee.component.InjectionSource)1 LookupInjectionSource (org.jboss.as.ee.component.LookupInjectionSource)1 ExtendedEntityManager (org.jboss.as.jpa.container.ExtendedEntityManager)1 ContextListAndJndiViewManagedReferenceFactory (org.jboss.as.naming.ContextListAndJndiViewManagedReferenceFactory)1