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());
}
}
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);
}
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;
}
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;
}
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;
}
Aggregations