Search in sources :

Example 1 with DirObjectFactory

use of javax.naming.spi.DirObjectFactory in project aries by apache.

the class DirObjectFactoryHelper method getObjectInstanceUsingObjectFactoryBuilders.

private Object getObjectInstanceUsingObjectFactoryBuilders(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
    ObjectFactory factory = null;
    ServiceReference[] refs = Utils.getReferencesPrivileged(callerContext, ObjectFactoryBuilder.class);
    if (refs != null) {
        Arrays.sort(refs, Utils.SERVICE_REFERENCE_COMPARATOR);
        for (ServiceReference ref : refs) {
            ObjectFactoryBuilder builder = (ObjectFactoryBuilder) Utils.getServicePrivileged(callerContext, ref);
            try {
                factory = builder.createObjectFactory(obj, environment);
            } catch (NamingException e) {
            // TODO: log it
            } finally {
                callerContext.ungetService(ref);
            }
            if (factory != null) {
                break;
            }
        }
    }
    Object result = null;
    if (factory != null) {
        if (factory instanceof DirObjectFactory) {
            result = ((DirObjectFactory) factory).getObjectInstance(obj, name, nameCtx, environment, attrs);
        } else {
            result = factory.getObjectInstance(obj, name, nameCtx, environment);
        }
    }
    return (result == null) ? obj : result;
}
Also used : DirObjectFactory(javax.naming.spi.DirObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory) ObjectFactoryBuilder(javax.naming.spi.ObjectFactoryBuilder) NamingException(javax.naming.NamingException) DirObjectFactory(javax.naming.spi.DirObjectFactory) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with DirObjectFactory

use of javax.naming.spi.DirObjectFactory 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 3 with DirObjectFactory

use of javax.naming.spi.DirObjectFactory in project aries by apache.

the class DirObjectFactoryHelper method getObjectInstanceUsingObjectFactories.

private Object getObjectInstanceUsingObjectFactories(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
    Object result = null;
    ServiceReference[] refs = Utils.getReferencesPrivileged(callerContext, DirObjectFactory.class);
    if (refs != null) {
        Arrays.sort(refs, Utils.SERVICE_REFERENCE_COMPARATOR);
        for (ServiceReference ref : refs) {
            if (canCallObjectFactory(obj, ref)) {
                DirObjectFactory factory = (DirObjectFactory) Utils.getServicePrivileged(callerContext, ref);
                try {
                    result = factory.getObjectInstance(obj, name, nameCtx, environment, attrs);
                } finally {
                    callerContext.ungetService(ref);
                }
                // loop we are in.
                if (result != null && result != obj) {
                    break;
                }
            }
        }
    }
    if (result == null) {
        result = getObjectInstanceUsingObjectFactories(obj, name, nameCtx, environment);
    }
    return (result == null) ? obj : result;
}
Also used : DirObjectFactory(javax.naming.spi.DirObjectFactory) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

DirObjectFactory (javax.naming.spi.DirObjectFactory)3 ObjectFactory (javax.naming.spi.ObjectFactory)2 ServiceReference (org.osgi.framework.ServiceReference)2 NamingException (javax.naming.NamingException)1 ObjectFactoryBuilder (javax.naming.spi.ObjectFactoryBuilder)1 ServiceAwareObjectFactory (org.jboss.as.naming.ServiceAwareObjectFactory)1