use of javax.naming.spi.InitialContextFactoryBuilder in project aries by apache.
the class ContextHelper method getInitialContextUsingBuilder.
private static ContextProvider getInitialContextUsingBuilder(BundleContext context, Hashtable<?, ?> environment) throws NamingException {
ContextProvider provider = null;
ServiceReference[] refs = AccessController.doPrivileged(new PrivilegedAction<ServiceReference[]>() {
public ServiceReference[] run() {
return Activator.getInitialContextFactoryBuilderServices();
}
});
if (refs != null) {
InitialContextFactory factory = null;
for (ServiceReference ref : refs) {
InitialContextFactoryBuilder builder = (InitialContextFactoryBuilder) Utils.getServicePrivileged(context, ref);
try {
factory = builder.createInitialContextFactory(environment);
} catch (NamingException ne) {
// TODO: log
// ignore this, if the builder fails we want to move onto the next one
} catch (NullPointerException npe) {
logger.log(Level.SEVERE, "NPE caught in ContextHelper.getInitialContextUsingBuilder. context=" + context + " ref=" + ref);
throw npe;
}
if (factory != null) {
try {
provider = new SingleContextProvider(context, ref, factory.getInitialContext(environment));
} finally {
// we didn't get something back, so this was no good.
if (provider == null)
context.ungetService(ref);
}
break;
} else {
// we didn't get something back, so this was no good.
context.ungetService(ref);
}
}
}
return provider;
}
use of javax.naming.spi.InitialContextFactoryBuilder in project aries by apache.
the class InitialContextTest method testLookFromLdapICF.
@Test
public void testLookFromLdapICF() throws Exception {
InitialContextFactoryBuilder icf = Skeleton.newMock(InitialContextFactoryBuilder.class);
bc.registerService(new String[] { InitialContextFactoryBuilder.class.getName(), icf.getClass().getName() }, icf, (Dictionary) new Properties());
LdapContext backCtx = Skeleton.newMock(LdapContext.class);
InitialContextFactory fac = Skeleton.newMock(InitialContextFactory.class);
Skeleton.getSkeleton(fac).setReturnValue(new MethodCall(InitialContextFactory.class, "getInitialContext", Hashtable.class), backCtx);
Skeleton.getSkeleton(icf).setReturnValue(new MethodCall(InitialContextFactoryBuilder.class, "createInitialContextFactory", Hashtable.class), fac);
Properties props = new Properties();
props.put(JNDIConstants.BUNDLE_CONTEXT, bc);
props.put(Context.INITIAL_CONTEXT_FACTORY, "dummy.factory");
InitialLdapContext ilc = new InitialLdapContext(props, new Control[0]);
ExtendedRequest req = Skeleton.newMock(ExtendedRequest.class);
ilc.extendedOperation(req);
Skeleton.getSkeleton(backCtx).assertCalled(new MethodCall(LdapContext.class, "extendedOperation", req));
}
Aggregations