Search in sources :

Example 6 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project activemq-artemis by apache.

the class StringRefAddrReferenceTest method getObject.

private <T> T getObject(Reference reference, Class<T> tClass) throws Exception {
    String factoryName = reference.getFactoryClassName();
    Class<?> factoryClass = Class.forName(factoryName);
    ObjectFactory factory = (ObjectFactory) factoryClass.newInstance();
    Object o = factory.getObjectInstance(reference, null, null, null);
    if (tClass.isAssignableFrom(tClass)) {
        return tClass.cast(o);
    } else {
        throw new IllegalStateException("Expected class, " + tClass.getName());
    }
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory)

Example 7 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project activemq-artemis by apache.

the class ReferenceableTest method testReferenceCF.

@Test
public void testReferenceCF() throws Exception {
    Reference cfRef = ((Referenceable) cf).getReference();
    String factoryName = cfRef.getFactoryClassName();
    Class<?> factoryClass = Class.forName(factoryName);
    ObjectFactory factory = (ObjectFactory) factoryClass.newInstance();
    Object instance = factory.getObjectInstance(cfRef, null, null, null);
    ProxyAssertSupport.assertTrue(instance instanceof ActiveMQConnectionFactory);
    ActiveMQJMSConnectionFactory cf2 = (ActiveMQJMSConnectionFactory) instance;
    simpleSendReceive(cf2, queue1);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) Referenceable(javax.naming.Referenceable) ObjectFactory(javax.naming.spi.ObjectFactory) Reference(javax.naming.Reference) ActiveMQJMSConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory) Test(org.junit.Test)

Example 8 with ObjectFactory

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

the class EjbFactory method getObjectInstance.

// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
 * Create a new EJB instance.
 *
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if (obj instanceof EjbRef) {
        Reference ref = (Reference) obj;
        // If ejb-link has been specified, resolving the link using JNDI
        RefAddr linkRefAddr = ref.get(EjbRef.LINK);
        if (linkRefAddr != null) {
            // Retrieving the EJB link
            String ejbLink = linkRefAddr.getContent().toString();
            Object beanObj = (new InitialContext()).lookup(ejbLink);
            /*
                String homeClassName = ref.getClassName();
                try {
                    Class home = Class.forName(homeClassName);
                    if (home.isInstance(beanObj)) {
                        System.out.println("Bean of type " 
                                           + beanObj.getClass().getName() 
                                           + " implements home interface " 
                                           + home.getName());
                    } else {
                        System.out.println("Bean of type " 
                                           + beanObj.getClass().getName() 
                                           + " doesn't implement home interface " 
                                           + home.getName());
                        throw new NamingException
                            ("Bean of type " + beanObj.getClass().getName() 
                             + " doesn't implement home interface " 
                             + home.getName());
                    }
                } catch (ClassNotFoundException e) {
                    System.out.println("Couldn't load home interface "
                                       + homeClassName);
                }
                */
            return beanObj;
        }
        ObjectFactory factory = null;
        RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
        if (factoryRefAddr != null) {
            // Using the specified factory
            String factoryClassName = factoryRefAddr.getContent().toString();
            // Loading factory
            ClassLoader tcl = Thread.currentThread().getContextClassLoader();
            Class<?> factoryClass = null;
            if (tcl != null) {
                try {
                    factoryClass = tcl.loadClass(factoryClassName);
                } catch (ClassNotFoundException e) {
                    NamingException ex = new NamingException("Could not load resource factory class");
                    ex.initCause(e);
                    throw ex;
                }
            } else {
                try {
                    factoryClass = Class.forName(factoryClassName);
                } catch (ClassNotFoundException e) {
                    NamingException ex = new NamingException("Could not load resource factory class");
                    ex.initCause(e);
                    throw ex;
                }
            }
            if (factoryClass != null) {
                try {
                    factory = (ObjectFactory) factoryClass.newInstance();
                } catch (Throwable t) {
                    NamingException ex = new NamingException("Could not load resource factory class");
                    ex.initCause(t);
                    throw ex;
                }
            }
        } else {
            String javaxEjbFactoryClassName = System.getProperty("javax.ejb.Factory", Constants.OPENEJB_EJB_FACTORY);
            try {
                factory = (ObjectFactory) Class.forName(javaxEjbFactoryClassName).newInstance();
            } catch (Throwable t) {
                if (t instanceof NamingException)
                    throw (NamingException) t;
                NamingException ex = new NamingException("Could not create resource factory instance");
                ex.initCause(t);
                throw ex;
            }
        }
        if (factory != null) {
            return factory.getObjectInstance(obj, name, nameCtx, environment);
        } else {
            throw new NamingException("Cannot create resource instance");
        }
    }
    return null;
}
Also used : RefAddr(javax.naming.RefAddr) ObjectFactory(javax.naming.spi.ObjectFactory) Reference(javax.naming.Reference) EjbRef(org.apache.naming.EjbRef) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 9 with ObjectFactory

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

the class ResourceFactory method getObjectInstance.

// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
 * Crete a new DataSource instance.
 *
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if (obj instanceof ResourceRef) {
        Reference ref = (Reference) obj;
        ObjectFactory factory = null;
        RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
        if (factoryRefAddr != null) {
            // Using the specified factory
            String factoryClassName = factoryRefAddr.getContent().toString();
            // Loading factory
            ClassLoader tcl = Thread.currentThread().getContextClassLoader();
            Class<?> factoryClass = null;
            if (tcl != null) {
                try {
                    factoryClass = tcl.loadClass(factoryClassName);
                } catch (ClassNotFoundException e) {
                    NamingException ex = new NamingException("Could not load resource factory class");
                    ex.initCause(e);
                    throw ex;
                }
            } else {
                try {
                    factoryClass = Class.forName(factoryClassName);
                } catch (ClassNotFoundException e) {
                    NamingException ex = new NamingException("Could not load resource factory class");
                    ex.initCause(e);
                    throw ex;
                }
            }
            if (factoryClass != null) {
                try {
                    factory = (ObjectFactory) factoryClass.newInstance();
                } catch (Exception e) {
                    if (e instanceof NamingException)
                        throw (NamingException) e;
                    NamingException ex = new NamingException("Could not create resource factory instance");
                    ex.initCause(e);
                    throw ex;
                }
            }
        } else {
            if (ref.getClassName().equals("javax.sql.DataSource")) {
                String javaxSqlDataSourceFactoryClassName = System.getProperty("javax.sql.DataSource.Factory", Constants.DBCP_DATASOURCE_FACTORY);
                try {
                    factory = (ObjectFactory) Class.forName(javaxSqlDataSourceFactoryClassName).newInstance();
                } catch (Exception e) {
                    NamingException ex = new NamingException("Could not create resource factory instance");
                    ex.initCause(e);
                    throw ex;
                }
            } else if (ref.getClassName().equals("javax.mail.Session")) {
                String javaxMailSessionFactoryClassName = System.getProperty("javax.mail.Session.Factory", "org.apache.naming.factory.MailSessionFactory");
                try {
                    factory = (ObjectFactory) Class.forName(javaxMailSessionFactoryClassName).newInstance();
                } catch (Throwable t) {
                    NamingException ex = new NamingException("Could not create resource factory instance");
                    ex.initCause(t);
                    throw ex;
                }
            }
        }
        if (factory != null) {
            return factory.getObjectInstance(obj, name, nameCtx, environment);
        } else {
            throw new NamingException("Cannot create resource instance");
        }
    }
    return null;
}
Also used : RefAddr(javax.naming.RefAddr) ObjectFactory(javax.naming.spi.ObjectFactory) Reference(javax.naming.Reference) ResourceRef(org.apache.naming.ResourceRef) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException)

Example 10 with ObjectFactory

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

the class NamingBindingAdd method createObjectFactory.

private ObjectFactory createObjectFactory(OperationContext context, ModelNode model) throws OperationFailedException {
    final ModuleIdentifier moduleID = ModuleIdentifier.fromString(NamingBindingResourceDefinition.MODULE.resolveModelAttribute(context, model).asString());
    final String className = NamingBindingResourceDefinition.CLASS.resolveModelAttribute(context, model).asString();
    final Module module;
    try {
        module = Module.getBootModuleLoader().loadModule(moduleID);
    } catch (ModuleNotFoundException e) {
        throw NamingLogger.ROOT_LOGGER.moduleNotFound(moduleID, e.getMessage());
    } catch (ModuleLoadException e) {
        throw NamingLogger.ROOT_LOGGER.couldNotLoadModule(moduleID);
    }
    final ObjectFactory objectFactoryClassInstance;
    final ClassLoader cl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
    try {
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(module.getClassLoader());
        final Class<?> clazz = module.getClassLoader().loadClass(className);
        objectFactoryClassInstance = (ObjectFactory) clazz.newInstance();
    } catch (ClassNotFoundException e) {
        throw NamingLogger.ROOT_LOGGER.couldNotLoadClassFromModule(className, moduleID);
    } catch (InstantiationException e) {
        throw NamingLogger.ROOT_LOGGER.couldNotInstantiateClassInstanceFromModule(className, moduleID);
    } catch (IllegalAccessException e) {
        throw NamingLogger.ROOT_LOGGER.couldNotInstantiateClassInstanceFromModule(className, moduleID);
    } catch (ClassCastException e) {
        throw NamingLogger.ROOT_LOGGER.notAnInstanceOfObjectFactory(className, moduleID);
    } finally {
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(cl);
    }
    return objectFactoryClassInstance;
}
Also used : ModuleLoadException(org.jboss.modules.ModuleLoadException) ModuleNotFoundException(org.jboss.modules.ModuleNotFoundException) ObjectFactory(javax.naming.spi.ObjectFactory) ExternalContextObjectFactory(org.jboss.as.naming.ExternalContextObjectFactory) ModuleIdentifier(org.jboss.modules.ModuleIdentifier) Module(org.jboss.modules.Module)

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