Search in sources :

Example 1 with InitialContextFactoryBuilder

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;
}
Also used : InitialContextFactoryBuilder(javax.naming.spi.InitialContextFactoryBuilder) NamingException(javax.naming.NamingException) InitialContextFactory(javax.naming.spi.InitialContextFactory) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with InitialContextFactoryBuilder

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));
}
Also used : InitialContextFactoryBuilder(javax.naming.spi.InitialContextFactoryBuilder) Hashtable(java.util.Hashtable) ExtendedRequest(javax.naming.ldap.ExtendedRequest) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Properties(java.util.Properties) InitialContextFactory(javax.naming.spi.InitialContextFactory) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext) Test(org.junit.Test)

Aggregations

InitialContextFactory (javax.naming.spi.InitialContextFactory)2 InitialContextFactoryBuilder (javax.naming.spi.InitialContextFactoryBuilder)2 Hashtable (java.util.Hashtable)1 Properties (java.util.Properties)1 NamingException (javax.naming.NamingException)1 ExtendedRequest (javax.naming.ldap.ExtendedRequest)1 InitialLdapContext (javax.naming.ldap.InitialLdapContext)1 LdapContext (javax.naming.ldap.LdapContext)1 MethodCall (org.apache.aries.unittest.mocks.MethodCall)1 Test (org.junit.Test)1 ServiceReference (org.osgi.framework.ServiceReference)1