Search in sources :

Example 41 with ObjectFactory

use of javax.naming.spi.ObjectFactory 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 42 with ObjectFactory

use of javax.naming.spi.ObjectFactory 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) ModuleNotFoundException(org.jboss.modules.ModuleNotFoundException) 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)

Example 43 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.

the class ObjectFactoryBuilder method processURL.

private static ObjectFactory processURL(Object refInfo, Hashtable environment) throws NamingException {
    if (refInfo instanceof String) {
        String url = (String) refInfo;
        String scheme = getURLScheme(url);
        if (scheme != null) {
            ObjectFactory answer = getURLObjectFactory(scheme, url, environment);
            if (answer != null) {
                return answer;
            }
        }
    }
    if (refInfo instanceof String[]) {
        String[] urls = (String[]) refInfo;
        for (int i = 0; i < urls.length; i++) {
            String scheme = getURLScheme(urls[i]);
            if (scheme != null) {
                ObjectFactory answer = getURLObjectFactory(scheme, urls[i], environment);
                if (answer != null) {
                    return answer;
                }
            }
        }
    }
    return null;
}
Also used : DirObjectFactory(javax.naming.spi.DirObjectFactory) ServiceAwareObjectFactory(org.jboss.as.naming.ServiceAwareObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory)

Example 44 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.

the class NamingContext method getObjectInstance.

private Object getObjectInstance(final Object object, final Name name, final Hashtable<?, ?> environment) throws NamingException {
    try {
        final ObjectFactoryBuilder factoryBuilder = ObjectFactoryBuilder.INSTANCE;
        final ObjectFactory objectFactory = factoryBuilder.createObjectFactory(object, environment);
        return objectFactory == null ? null : objectFactory.getObjectInstance(object, name, this, environment);
    } catch (NamingException e) {
        throw e;
    } catch (Throwable t) {
        throw NamingLogger.ROOT_LOGGER.cannotDeferenceObject(t);
    }
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory) ObjectFactoryBuilder(org.jboss.as.naming.context.ObjectFactoryBuilder) NamingException(javax.naming.NamingException)

Example 45 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.

the class ObjectFactoryBuilder method getURLObjectFactory.

private static ObjectFactory getURLObjectFactory(String scheme, String url, Hashtable environment) throws NamingException {
    String facProp = (String) environment.get(Context.URL_PKG_PREFIXES);
    if (facProp != null) {
        facProp += ":" + "com.sun.jndi.url";
    } else {
        facProp = "com.sun.jndi.url";
    }
    ClassLoader loader = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
    String suffix = "." + scheme + "." + scheme + "URLContextFactory";
    // Not cached; find first factory and cache
    StringTokenizer parser = new StringTokenizer(facProp, ":");
    String className;
    ObjectFactory factory = null;
    while (parser.hasMoreTokens()) {
        className = parser.nextToken() + suffix;
        try {
            Class<?> clazz;
            if (loader == null) {
                clazz = Class.forName(className);
            } else {
                clazz = Class.forName(className, true, loader);
            }
            return new ReferenceUrlContextFactoryWrapper((ObjectFactory) clazz.newInstance(), url);
        } catch (InstantiationException | IllegalAccessException e) {
            NamingException ne = new NamingException(className);
            ne.setRootCause(e);
            throw ne;
        } catch (Exception e) {
        }
    }
    return factory;
}
Also used : StringTokenizer(java.util.StringTokenizer) DirObjectFactory(javax.naming.spi.DirObjectFactory) ServiceAwareObjectFactory(org.jboss.as.naming.ServiceAwareObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException)

Aggregations

ObjectFactory (javax.naming.spi.ObjectFactory)59 Reference (javax.naming.Reference)22 NamingException (javax.naming.NamingException)21 Test (org.junit.Test)16 DirObjectFactory (javax.naming.spi.DirObjectFactory)14 RefAddr (javax.naming.RefAddr)12 Hashtable (java.util.Hashtable)10 BundleContext (org.osgi.framework.BundleContext)9 Context (javax.naming.Context)7 MethodCall (org.apache.aries.unittest.mocks.MethodCall)6 ServiceAwareObjectFactory (org.jboss.as.naming.ServiceAwareObjectFactory)6 Name (javax.naming.Name)5 Referenceable (javax.naming.Referenceable)5 ServiceReference (org.osgi.framework.ServiceReference)5 InitialContext (javax.naming.InitialContext)4 StringRefAddr (javax.naming.StringRefAddr)4 InitialLdapContext (javax.naming.ldap.InitialLdapContext)4 LdapContext (javax.naming.ldap.LdapContext)4 Properties (java.util.Properties)3 ObjectFactoryBuilder (javax.naming.spi.ObjectFactoryBuilder)3