Search in sources :

Example 11 with ObjectFactory

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

the class ObjectFactoryBuilder method getObjectInstance.

/**
 * Create an object instance.
 *
 * @param ref         Object containing reference information
 * @param name        The name relative to nameCtx
 * @param nameCtx     The naming context
 * @param environment The environment information
 * @param attributes  The directory attributes
 * @return The object
 * @throws Exception If any error occur
 */
public Object getObjectInstance(final Object ref, final Name name, final Context nameCtx, final Hashtable<?, ?> environment, final Attributes attributes) throws Exception {
    final ClassLoader classLoader = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
    if (classLoader == null) {
        return ref;
    }
    final String factoriesProp = (String) environment.get(Context.OBJECT_FACTORIES);
    if (factoriesProp != null) {
        final String[] classes = factoriesProp.split(":");
        for (String className : classes) {
            try {
                final Class<?> factoryClass = classLoader.loadClass(className);
                final ObjectFactory objectFactory = ObjectFactory.class.cast(factoryClass.newInstance());
                final Object result;
                if (objectFactory instanceof DirObjectFactory) {
                    result = DirObjectFactory.class.cast(objectFactory).getObjectInstance(ref, name, nameCtx, environment, attributes);
                } else {
                    result = objectFactory.getObjectInstance(ref, name, nameCtx, environment);
                }
                if (result != null) {
                    return result;
                }
            } catch (Throwable ignored) {
            }
        }
    }
    return ref;
}
Also used : DirObjectFactory(javax.naming.spi.DirObjectFactory) ServiceAwareObjectFactory(org.jboss.as.naming.ServiceAwareObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory) DirObjectFactory(javax.naming.spi.DirObjectFactory)

Example 12 with ObjectFactory

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

the class ObjectFactoryBuilder method lookForURLs.

static ObjectFactory lookForURLs(Reference ref, Hashtable environment) throws NamingException {
    for (int i = 0; i < ref.size(); i++) {
        RefAddr addr = ref.get(i);
        if (addr instanceof StringRefAddr && addr.getType().equalsIgnoreCase("URL")) {
            String url = (String) addr.getContent();
            ObjectFactory answer = processURL(url, environment);
            if (answer != null) {
                return answer;
            }
        }
    }
    return null;
}
Also used : RefAddr(javax.naming.RefAddr) StringRefAddr(javax.naming.StringRefAddr) DirObjectFactory(javax.naming.spi.DirObjectFactory) ServiceAwareObjectFactory(org.jboss.as.naming.ServiceAwareObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory) StringRefAddr(javax.naming.StringRefAddr)

Example 13 with ObjectFactory

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

the class ObjectFactoryBuilder method getObjectInstance.

/**
 * Create an object instance.
 *
 * @param ref         Object containing reference information
 * @param name        The name relative to nameCtx
 * @param nameCtx     The naming context
 * @param environment The environment information
 * @return The object
 * @throws Exception If any error occur
 */
public Object getObjectInstance(final Object ref, final Name name, final Context nameCtx, final Hashtable<?, ?> environment) throws Exception {
    final ClassLoader classLoader = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
    if (classLoader == null) {
        return ref;
    }
    final String factoriesProp = (String) environment.get(Context.OBJECT_FACTORIES);
    if (factoriesProp != null) {
        final String[] classes = factoriesProp.split(":");
        for (String className : classes) {
            try {
                final Class<?> factoryClass = classLoader.loadClass(className);
                final ObjectFactory objectFactory = ObjectFactory.class.cast(factoryClass.newInstance());
                final Object result = objectFactory.getObjectInstance(ref, name, nameCtx, environment);
                if (result != null) {
                    return result;
                }
            } catch (Throwable ignored) {
            }
        }
    }
    return ref;
}
Also used : DirObjectFactory(javax.naming.spi.DirObjectFactory) ServiceAwareObjectFactory(org.jboss.as.naming.ServiceAwareObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory)

Example 14 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project tomee by apache.

the class IvmContext method getFederatedFactories.

public static ObjectFactory[] getFederatedFactories() throws NamingException {
    if (federatedFactories == null) {
        final Set<ObjectFactory> factories = new HashSet<>();
        final String urlPackagePrefixes = getUrlPackagePrefixes();
        if (urlPackagePrefixes == null) {
            return new ObjectFactory[0];
        }
        for (final StringTokenizer tokenizer = new StringTokenizer(urlPackagePrefixes, ":"); tokenizer.hasMoreTokens(); ) {
            final String urlPackagePrefix = tokenizer.nextToken();
            final String className = urlPackagePrefix + ".java.javaURLContextFactory";
            if (className.equals("org.apache.openejb.core.ivm.naming.java.javaURLContextFactory")) {
                continue;
            }
            try {
                final ClassLoader cl = ClassLoaderUtil.getContextClassLoader();
                final Class factoryClass = Class.forName(className, true, cl);
                final ObjectFactory factoryInstance = (ObjectFactory) factoryClass.newInstance();
                factories.add(factoryInstance);
            } catch (final ClassNotFoundException cnfe) {
            // no-op
            } catch (final Throwable e) {
                final NamingException ne = new NamingException("Federation failed: Cannot instantiate " + className);
                ne.setRootCause(e);
                throw ne;
            }
        }
        final Object[] temp = factories.toArray();
        federatedFactories = new ObjectFactory[temp.length];
        System.arraycopy(temp, 0, federatedFactories, 0, federatedFactories.length);
    }
    return federatedFactories;
}
Also used : StringTokenizer(java.util.StringTokenizer) ObjectFactory(javax.naming.spi.ObjectFactory) NamingException(javax.naming.NamingException) HashSet(java.util.HashSet)

Example 15 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project tomee by apache.

the class IvmContext method federate.

protected Object federate(final String compositName) throws NamingException {
    final ObjectFactory[] factories = getFederatedFactories();
    for (final ObjectFactory factory : factories) {
        try {
            final CompositeName name = new CompositeName(compositName);
            final Object obj = factory.getObjectInstance(null, name, null, null);
            if (obj instanceof Context) {
                return ((Context) obj).lookup(compositName);
            } else if (obj != null) {
                return obj;
            }
        } catch (final Exception doNothing) {
        // no-op
        }
    }
    throw new NameNotFoundException("Name \"" + compositName + "\" not found.");
}
Also used : Context(javax.naming.Context) ObjectFactory(javax.naming.spi.ObjectFactory) NameNotFoundException(javax.naming.NameNotFoundException) CompositeName(javax.naming.CompositeName) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) FileNotFoundException(java.io.FileNotFoundException) ObjectStreamException(java.io.ObjectStreamException) OperationNotSupportedException(javax.naming.OperationNotSupportedException)

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