use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.
the class ObjectFactoryBuilder method factoryFromReference.
private ObjectFactory factoryFromReference(final Reference reference, final ClassLoader classLoader, final Hashtable<?, ?> environment) throws Exception {
try {
final Class<?> factoryClass = classLoader.loadClass(reference.getFactoryClassName());
ObjectFactory factory = ObjectFactory.class.cast(factoryClass.newInstance());
if (factory instanceof ServiceAwareObjectFactory) {
((ServiceAwareObjectFactory) factory).injectServiceRegistry(currentServiceContainer());
}
return factory;
} catch (Throwable t) {
throw NamingLogger.ROOT_LOGGER.objectFactoryCreationFailure(t);
}
}
use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.
the class InitialContext method removeUrlContextFactory.
/**
* Remove an ObjectFactory from the map of registered ones. To make sure that not anybody can remove an
* ObjectFactory both the scheme as well as the actual object factory itself need to be supplied. So you
* can only remove the factory if you have the factory object.
* @param scheme The URL scheme for which the handler is registered.
* @param factory The factory object associated with the scheme
* @throws IllegalArgumentException if the requested scheme/factory combination is not registered.
*/
public static synchronized void removeUrlContextFactory(final String scheme, ObjectFactory factory) {
Map<String, ObjectFactory> factories = new HashMap<String, ObjectFactory>(urlContextFactories);
ObjectFactory f = factories.get(scheme);
if (f == factory) {
factories.remove(scheme);
urlContextFactories = Collections.unmodifiableMap(factories);
return;
} else {
throw new IllegalArgumentException();
}
}
use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.
the class InitialContextTestCase method testRegisterURLSchemeHandler.
@Test
public void testRegisterURLSchemeHandler() throws Exception {
InitialContext ictx = new InitialContext(null);
try {
ictx.lookup("foobar:something");
Assert.fail("Precondition: the foobar: scheme should not yet be registered");
} catch (NamingException ne) {
// good
}
ObjectFactory tof = new TestObjectFactory();
InitialContext.addUrlContextFactory("foobar", tof);
String something = (String) ictx.lookup("foobar:something");
Assert.assertTrue("The object should now be provided by our TestObjectFactory", something.startsWith("TestObject:"));
try {
InitialContext.removeUrlContextFactory("foobar:", new TestObjectFactory());
Assert.fail("Should throw an IllegalArgumentException since the associated factory object doesn't match the registration");
} catch (IllegalArgumentException iae) {
// good;
}
Assert.assertEquals("The foobar: scheme should still be registered", something, ictx.lookup("foobar:something"));
InitialContext.removeUrlContextFactory("foobar", tof);
try {
ictx.lookup("foobar:something");
Assert.fail("The foobar: scheme should not be registered any more");
} catch (NamingException ne) {
// good
}
}
use of javax.naming.spi.ObjectFactory in project aries by apache.
the class ObjectFactoryTest method testURLReferenceWithMatchingHandler.
@Test
public void testURLReferenceWithMatchingHandler() 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);
Properties props = new Properties();
props.setProperty("osgi.jndi.urlScheme", "wibble");
bc.registerService(ObjectFactory.class.getName(), factory, (Dictionary) props);
Reference ref = new Reference(null);
ref.add(new StringRefAddr("URL", "wibble"));
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 ObjectFactoryHelper method getObjectInstanceUsingClassName.
private Object getObjectInstanceUsingClassName(Object reference, String className, Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
Object result = null;
ObjectFactory factory = ObjectFactoryHelper.findObjectFactoryByClassName(defaultContext, className);
if (factory != null) {
result = getObjectFromFactory(reference, name, nameCtx, environment, attrs, factory);
}
return (result == null) ? obj : result;
}
Aggregations