use of javax.naming.spi.ObjectFactory in project aries by apache.
the class ObjectFactoryTest method testSpecifiedFactoryWithMatchingFactory.
@Test
public void testSpecifiedFactoryWithMatchingFactory() throws Exception {
String testObject = "Test object";
ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
Reference ref = new Reference("dummy.class.name", factory.getClass().getName(), "");
bc.registerService(new String[] { ObjectFactory.class.getName(), factory.getClass().getName() }, factory, null);
Object obj = NamingManager.getObjectInstance(ref, null, null, env);
assertEquals("The naming manager should have returned the test object", testObject, obj);
}
use of javax.naming.spi.ObjectFactory in project aries by apache.
the class ObjectFactoryTest method testObjectFactoryThatThrowsIsIgnored.
@Test
public void testObjectFactoryThatThrowsIsIgnored() throws Exception {
final ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
bc.registerService(ObjectFactory.class.getName(), factory, null);
final Skeleton skeleton = Skeleton.getSkeleton(factory);
final MethodCall getObjectInstance = new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class);
skeleton.setThrows(getObjectInstance, new Exception());
Object original = new Object() {
};
Object processed = NamingManager.getObjectInstance(original, null, null, env);
skeleton.assertCalled(getObjectInstance);
assertEquals("The original object should be returned despite the object factory throwing an exception", original, processed);
}
use of javax.naming.spi.ObjectFactory in project aries by apache.
the class InitialContextTest method registerURLObjectFactory.
/**
* Registers an ObjectFactory to be used for creating URLContexts for the given scheme
* @param of
* @param scheme
*/
private void registerURLObjectFactory(ObjectFactory of, String scheme) {
Properties props = new Properties();
props.setProperty(JNDIConstants.JNDI_URLSCHEME, "test");
bc.registerService(ObjectFactory.class.getName(), of, (Dictionary) props);
}
use of javax.naming.spi.ObjectFactory in project aries by apache.
the class InitialContextTest method testURLContextErrorPropagation.
@Test
public void testURLContextErrorPropagation() throws Exception {
ObjectFactory of = new ObjectFactory() {
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
throw new Exception("doh");
}
};
registerURLObjectFactory(of, "test");
ic = initialContext();
try {
ic.lookup("test:something");
Assert.fail("Expected NamingException");
} catch (NamingException ne) {
assertNotNull(ne.getCause());
assertEquals("doh", ne.getCause().getMessage());
}
}
use of javax.naming.spi.ObjectFactory in project tomcat by apache.
the class ResourceFactory method getDefaultFactory.
@Override
protected ObjectFactory getDefaultFactory(Reference ref) throws NamingException {
ObjectFactory factory = null;
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).getConstructor().newInstance();
} catch (Exception e) {
NamingException ex = new NamingException(sm.getString("resourceFactory.factoryCreationError"));
ex.initCause(e);
throw ex;
}
} else if (ref.getClassName().equals("jakarta.mail.Session")) {
String javaxMailSessionFactoryClassName = System.getProperty("jakarta.mail.Session.Factory", "org.apache.naming.factory.MailSessionFactory");
try {
factory = (ObjectFactory) Class.forName(javaxMailSessionFactoryClassName).getConstructor().newInstance();
} catch (Throwable t) {
if (t instanceof NamingException) {
throw (NamingException) t;
}
if (t instanceof ThreadDeath) {
throw (ThreadDeath) t;
}
if (t instanceof VirtualMachineError) {
throw (VirtualMachineError) t;
}
NamingException ex = new NamingException(sm.getString("resourceFactory.factoryCreationError"));
ex.initCause(t);
throw ex;
}
}
return factory;
}
Aggregations