Search in sources :

Example 6 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