use of javax.naming.spi.ObjectFactory in project aries by apache.
the class ObjectFactoryHelper method getObjectInstanceUsingRefAddress.
private Object getObjectInstanceUsingRefAddress(Enumeration<RefAddr> addresses, Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
while (addresses.hasMoreElements()) {
RefAddr address = addresses.nextElement();
if (address instanceof StringRefAddr && "URL".equals(address.getType())) {
String urlScheme = getUrlScheme((String) address.getContent());
ServicePair<ObjectFactory> factoryService = ContextHelper.getURLObjectFactory(callerContext, urlScheme, environment);
if (factoryService != null) {
ObjectFactory factory = factoryService.get();
String value = (String) address.getContent();
Object result = getObjectFromFactory(value, name, nameCtx, environment, attrs, factory);
// loop we are in.
if (result != null && result != obj) {
return result;
}
}
}
}
return obj;
}
use of javax.naming.spi.ObjectFactory in project aries by apache.
the class ObjectFactoryHelper method getObjectInstanceViaContextDotObjectFactories.
/*
* Attempt to obtain an Object instance via the java.naming.factory.object property
*/
private Object getObjectInstanceViaContextDotObjectFactories(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
Object result = null;
String factories = (String) environment.get(Context.OBJECT_FACTORIES);
if (factories != null && factories.length() > 0) {
String[] candidates = factories.split(":");
ClassLoader cl = Utils.doPrivileged(Thread.currentThread()::getContextClassLoader);
for (String cand : candidates) {
ObjectFactory factory;
try {
@SuppressWarnings("unchecked") Class<ObjectFactory> clz = (Class<ObjectFactory>) cl.loadClass(cand);
factory = clz.newInstance();
} catch (Exception e) {
if (logger.isLoggable(Level.FINE))
logger.log(Level.FINE, "Exception instantiating factory: " + e);
continue;
}
if (logger.isLoggable(Level.FINE))
logger.log(Level.FINE, "cand=" + cand + " factory=" + factory);
if (factory != null) {
result = getObjectFromFactory(obj, name, nameCtx, environment, attrs, factory);
}
if (result != null && result != obj)
break;
}
}
if (logger.isLoggable(Level.FINE))
logger.log(Level.FINE, "result = " + result);
return (result == null) ? obj : result;
}
use of javax.naming.spi.ObjectFactory in project aries by apache.
the class InitialContextTest method testURLLookup.
@Test
public void testURLLookup() throws Exception {
ObjectFactory of = new ObjectFactory() {
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
return dummyContext("result");
}
};
registerURLObjectFactory(of, "test");
ic = initialContext();
assertEquals("result", ic.lookup("test:something"));
}
use of javax.naming.spi.ObjectFactory in project aries by apache.
the class InitialContextTest method testLookupWithoutICFButWithURLLookup.
@Test
public void testLookupWithoutICFButWithURLLookup() throws NamingException {
ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
Context ctx = Skeleton.newMock(Context.class);
Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), ctx);
Skeleton.getSkeleton(ctx).setReturnValue(new MethodCall(Context.class, "lookup", String.class), "someText");
Properties props = new Properties();
props.put(JNDIConstants.JNDI_URLSCHEME, "testURL");
bc.registerService(ObjectFactory.class.getName(), factory, (Dictionary) props);
props = new Properties();
props.put(JNDIConstants.BUNDLE_CONTEXT, bc);
InitialContext initialCtx = new InitialContext(props);
Object someObject = initialCtx.lookup("testURL:somedata");
assertEquals("Expected to be given a string, but got something else.", "someText", someObject);
}
use of javax.naming.spi.ObjectFactory in project aries by apache.
the class InitialContextTest method testNoURLContextCaching.
@Test
public void testNoURLContextCaching() throws Exception {
final AtomicBoolean second = new AtomicBoolean(false);
final Context ctx = dummyContext("one");
final Context ctx2 = dummyContext("two");
ObjectFactory of = new ObjectFactory() {
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
if (second.get())
return ctx2;
else {
second.set(true);
return ctx;
}
}
};
registerURLObjectFactory(of, "test");
ic = initialContext();
assertEquals("one", ic.lookup("test:something"));
assertEquals("two", ic.lookup("test:something"));
}
Aggregations