Java 类javax.naming.spi.InitialContextFactoryBuilder 实例源码

项目:carbon-jndi    文件:JNDITest.java   
/**
 * In this method we are trying to create an InitialContext from a non-existent InitialContextFactory called
 * FooInitialContextFactory. But we are registering an InitialContextFactoryBuilder service before that.
 * Therefore this InitialContextFactoryBuilder service should create an InitialContext according the OSGi JNDI
 * specification.
 */
@Test(dependsOnMethods = "testCustomInitialContextFactoryWithException")
public void testJNDIContextManagerWithEnvironmentContextFactoryBuilder() throws NamingException {
    Dictionary<String, Object> propertyMap = new Hashtable<>();
    propertyMap.put("service.ranking", 10);
    ServiceRegistration<InitialContextFactoryBuilder> abcICFBServiceRef = bundleContext.registerService(
            InitialContextFactoryBuilder.class, new ABCContextFactoryBuilder(), propertyMap);

    Map<String, String> environment = new HashMap<String, String>(1);
    environment.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
            FooInitialContextFactory.class.getName());

    Context initialContext = jndiContextManager.newInitialContext(environment);

    initialContext.bind("contextFactoryBuilderClass", "EMPTY");
    String contextFactoryBuilderClass = (String) initialContext.lookup("contextFactoryBuilderClass");

    assertEquals(contextFactoryBuilderClass, ABCContextFactoryBuilder.class.getName(),
            "Specified InitialContextFactory has not been picked up to create the requested initial context.");

    abcICFBServiceRef.unregister();
}
项目:carbon-jndi    文件:JNDIUtils.java   
/**
 * @param builderOptional an {@code Optional} describing InitialContextFactoryBuilder instance.
 * @param environment     The possibly null environment
 *                        specifying information to be used in the creation
 *                        of the initial context.
 * @return an {@code Optional} describing the created InitialContextFactory instance.
 */
public static Optional<InitialContextFactory> getContextFactory(
        Optional<InitialContextFactoryBuilder> builderOptional,
        Hashtable<?, ?> environment) {

    return builderOptional.map(builder -> {
        try {
            return builder.createInitialContextFactory(environment);
        } catch (NamingException ignored) {
            // According to the OSGi JNDI service specification this exception should not thrown to the caller.
            logger.debug(ignored.getMessage(), ignored);
            return null;
        }
    });
}
项目:carbon-jndi    文件:JNDIUtils.java   
/**
     * @param bundleContext        caller BundleContext.
     * @param serviceRefCollection Collection of {@code ServiceReference} objects of InitialContextFactoryBuilders.
     * @param environment          The possibly null environment
     *                             specifying information to be used in the creation
     *                             of the initial context.
     * @return an {@code Optional} describing created initial context from the builder.
     * @throws NamingException upon any error that occurs during context creation
     */
    public static Optional<Context> getInitialContextFromBuilder(
            BundleContext bundleContext,
            Collection<ServiceReference<InitialContextFactoryBuilder>> serviceRefCollection,
            Hashtable<?, ?> environment) throws NamingException {

        return serviceRefCollection
                .stream()
//                .sorted(new ServiceRankComparator())
                .map(serviceReference -> getService(bundleContext, serviceReference))
                .map(builderOptional -> getContextFactory(builderOptional, environment))
                .flatMap(factoryOptional -> factoryOptional.map(Stream::of).orElseGet(Stream::empty))
                .map(rethrowFunction(factory -> factory.getInitialContext(environment)))
                .findFirst();
    }
项目:cn1    文件:NamingManagerBuilderTest.java   
/**
 * Set the initial context factory builder to a mock instance.
 * 
 */
public void myTestSetInitialContextFactoryBuilder_SetNormal()
        throws NamingException {
    log.setMethod("myTestSetInitialContextFactoryBuilder_SetNormal");
    InitialContextFactoryBuilder contextFactoryBuilder = MockInitialContextFactoryBuilder
            .getInstance();
    NamingManager.setInitialContextFactoryBuilder(contextFactoryBuilder);
    assertTrue(NamingManager.hasInitialContextFactoryBuilder());
}
项目:cn1    文件:NamingManagerExploreTest.java   
public void testFactoryBuilder() throws IllegalStateException,
        SecurityException, NamingException {
    // log.setMethod("testFactoryBuilder");

    if (!NamingManager.hasInitialContextFactoryBuilder()) {
        InitialContextFactoryBuilder contextFactoryBuilder = MockInitialContextFactoryBuilder
                .getInstance();
        NamingManager
                .setInitialContextFactoryBuilder(contextFactoryBuilder);
    }

    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    env.put(Context.URL_PKG_PREFIXES, "org.apache.harmony.jndi.tests.javax.naming.spi.mock");

    MyInitialContext context = new MyInitialContext(env);
    // log.log(context.getEnvironment().toString());
    // log.log("DefaultContext:" +
    // context.getDefaultContext().getClass().getName());
    //
    Context urlContext = NamingManager.getURLContext("http", env);
    assertEquals("http", urlContext.getEnvironment().get("url.schema"));

    String name = "http://www.apache.org";
    String obj = "String object";
    context.bind(name, obj);

    assertNull(InvokeRecord.getLatestUrlSchema());
}
项目:cn1    文件:NamingManagerExploreTest.java   
public void testFactoryBuilder_name() throws IllegalStateException,
        SecurityException, NamingException {
    // log.setMethod("testFactoryBuilder_name");

    if (!NamingManager.hasInitialContextFactoryBuilder()) {
        InitialContextFactoryBuilder contextFactoryBuilder = MockInitialContextFactoryBuilder
                .getInstance();
        NamingManager
                .setInitialContextFactoryBuilder(contextFactoryBuilder);
    }

    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    env.put(Context.URL_PKG_PREFIXES, "org.apache.harmony.jndi.tests.javax.naming.spi.mock");

    MyInitialContext context = new MyInitialContext(env);
    // log.log(context.getEnvironment().toString());
    // log.log("DefaultContext:" +
    // context.getDefaultContext().getClass().getName());
    //
    Context urlContext = NamingManager.getURLContext("http", env);
    assertEquals("http", urlContext.getEnvironment().get("url.schema"));

    Name name = new CompositeName("http://www.apache.org");
    String obj = "Name object";
    context.bind(name, obj);

    assertNull(InvokeRecord.getLatestUrlSchema());
}
项目:freeVM    文件:NamingManagerBuilderTest.java   
/**
 * Set the initial context factory builder to a mock instance.
 * 
 */
public void myTestSetInitialContextFactoryBuilder_SetNormal()
        throws NamingException {
    log.setMethod("myTestSetInitialContextFactoryBuilder_SetNormal");
    InitialContextFactoryBuilder contextFactoryBuilder = MockInitialContextFactoryBuilder
            .getInstance();
    NamingManager.setInitialContextFactoryBuilder(contextFactoryBuilder);
    assertTrue(NamingManager.hasInitialContextFactoryBuilder());
}
项目:freeVM    文件:NamingManagerExploreTest.java   
public void testFactoryBuilder() throws IllegalStateException,
        SecurityException, NamingException {
    // log.setMethod("testFactoryBuilder");

    if (!NamingManager.hasInitialContextFactoryBuilder()) {
        InitialContextFactoryBuilder contextFactoryBuilder = MockInitialContextFactoryBuilder
                .getInstance();
        NamingManager
                .setInitialContextFactoryBuilder(contextFactoryBuilder);
    }

    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    env.put(Context.URL_PKG_PREFIXES, "org.apache.harmony.jndi.tests.javax.naming.spi.mock");

    MyInitialContext context = new MyInitialContext(env);
    // log.log(context.getEnvironment().toString());
    // log.log("DefaultContext:" +
    // context.getDefaultContext().getClass().getName());
    //
    Context urlContext = NamingManager.getURLContext("http", env);
    assertEquals("http", urlContext.getEnvironment().get("url.schema"));

    String name = "http://www.apache.org";
    String obj = "String object";
    context.bind(name, obj);

    assertNull(InvokeRecord.getLatestUrlSchema());
}
项目:freeVM    文件:NamingManagerExploreTest.java   
public void testFactoryBuilder_name() throws IllegalStateException,
        SecurityException, NamingException {
    // log.setMethod("testFactoryBuilder_name");

    if (!NamingManager.hasInitialContextFactoryBuilder()) {
        InitialContextFactoryBuilder contextFactoryBuilder = MockInitialContextFactoryBuilder
                .getInstance();
        NamingManager
                .setInitialContextFactoryBuilder(contextFactoryBuilder);
    }

    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    env.put(Context.URL_PKG_PREFIXES, "org.apache.harmony.jndi.tests.javax.naming.spi.mock");

    MyInitialContext context = new MyInitialContext(env);
    // log.log(context.getEnvironment().toString());
    // log.log("DefaultContext:" +
    // context.getDefaultContext().getClass().getName());
    //
    Context urlContext = NamingManager.getURLContext("http", env);
    assertEquals("http", urlContext.getEnvironment().get("url.schema"));

    Name name = new CompositeName("http://www.apache.org");
    String obj = "Name object";
    context.bind(name, obj);

    assertNull(InvokeRecord.getLatestUrlSchema());
}
项目:freeVM    文件:NamingManagerBuilderTest.java   
/**
 * Set the initial context factory builder to a mock instance.
 * 
 */
public void myTestSetInitialContextFactoryBuilder_SetNormal()
        throws NamingException {
    log.setMethod("myTestSetInitialContextFactoryBuilder_SetNormal");
    InitialContextFactoryBuilder contextFactoryBuilder = MockInitialContextFactoryBuilder
            .getInstance();
    NamingManager.setInitialContextFactoryBuilder(contextFactoryBuilder);
    assertTrue(NamingManager.hasInitialContextFactoryBuilder());
}
项目:freeVM    文件:NamingManagerExploreTest.java   
public void testFactoryBuilder() throws IllegalStateException,
        SecurityException, NamingException {
    // log.setMethod("testFactoryBuilder");

    if (!NamingManager.hasInitialContextFactoryBuilder()) {
        InitialContextFactoryBuilder contextFactoryBuilder = MockInitialContextFactoryBuilder
                .getInstance();
        NamingManager
                .setInitialContextFactoryBuilder(contextFactoryBuilder);
    }

    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    env.put(Context.URL_PKG_PREFIXES, "org.apache.harmony.jndi.tests.javax.naming.spi.mock");

    MyInitialContext context = new MyInitialContext(env);
    // log.log(context.getEnvironment().toString());
    // log.log("DefaultContext:" +
    // context.getDefaultContext().getClass().getName());
    //
    Context urlContext = NamingManager.getURLContext("http", env);
    assertEquals("http", urlContext.getEnvironment().get("url.schema"));

    String name = "http://www.apache.org";
    String obj = "String object";
    context.bind(name, obj);

    assertNull(InvokeRecord.getLatestUrlSchema());
}
项目:freeVM    文件:NamingManagerExploreTest.java   
public void testFactoryBuilder_name() throws IllegalStateException,
        SecurityException, NamingException {
    // log.setMethod("testFactoryBuilder_name");

    if (!NamingManager.hasInitialContextFactoryBuilder()) {
        InitialContextFactoryBuilder contextFactoryBuilder = MockInitialContextFactoryBuilder
                .getInstance();
        NamingManager
                .setInitialContextFactoryBuilder(contextFactoryBuilder);
    }

    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    env.put(Context.URL_PKG_PREFIXES, "org.apache.harmony.jndi.tests.javax.naming.spi.mock");

    MyInitialContext context = new MyInitialContext(env);
    // log.log(context.getEnvironment().toString());
    // log.log("DefaultContext:" +
    // context.getDefaultContext().getClass().getName());
    //
    Context urlContext = NamingManager.getURLContext("http", env);
    assertEquals("http", urlContext.getEnvironment().get("url.schema"));

    Name name = new CompositeName("http://www.apache.org");
    String obj = "Name object";
    context.bind(name, obj);

    assertNull(InvokeRecord.getLatestUrlSchema());
}
项目:pentaho-kettle    文件:DatabaseConnectUnitTest.java   
@BeforeClass
public static void beforeClass() throws NamingException {
  if ( !NamingManager.hasInitialContextFactoryBuilder() ) {
    // If JNDI is not initialized, use simpleJNDI
    System.setProperty( Context.INITIAL_CONTEXT_FACTORY, "org.osjava.sj.memory.MemoryContextFactory" ); // pentaho#simple-jndi;1.0.0
    System.setProperty( "org.osjava.sj.jndi.shared", "true" );
    InitialContextFactoryBuilder simpleBuilder = new SimpleNamingContextBuilder();
    NamingManager.setInitialContextFactoryBuilder( simpleBuilder );
  }
}
项目:carbon-jndi    文件:JNDITest.java   
/**
 * In this method we are testing the functionality of the JNDIContextManager when there are multiple
 * InitialContextFactoryBuilder services with different service.ranking property value.
 * <p>
 * We also test the situation with an InitialContextFactoryBuilder which returns a null context.
 */
@Test(dependsOnMethods = "testJNDIContextManagerWithEnvironmentContextFactoryBuilder")
public void testCustomInitialContextFactoryBuilders() throws NamingException {
    Dictionary<String, Object> propertyMap = new Hashtable<>();
    propertyMap.put("service.ranking", 10);
    ServiceRegistration<InitialContextFactoryBuilder> abcICFBServiceRef = bundleContext.registerService(
            InitialContextFactoryBuilder.class, new ABCContextFactoryBuilder(), propertyMap);
    Context initialContext = jndiContextManager.newInitialContext();

    initialContext.bind("contextFactoryBuilderClass", "EMPTY");
    String contextFactoryBuilderClass = (String) initialContext.lookup("contextFactoryBuilderClass");

    assertEquals(contextFactoryBuilderClass, ABCContextFactoryBuilder.class.getName(),
            "Specified InitialContextFactory has not been picked up to create the requested initial context.");

    propertyMap.put("service.ranking", 30);
    ServiceRegistration<InitialContextFactoryBuilder> nullICFBServiceRef = bundleContext.registerService(
            InitialContextFactoryBuilder.class, new NullContextFactoryBuilder(), propertyMap);
    initialContext = jndiContextManager.newInitialContext();

    initialContext.bind("contextFactoryBuilderClass", "EMPTY");
    contextFactoryBuilderClass = (String) initialContext.lookup("contextFactoryBuilderClass");

    assertEquals(contextFactoryBuilderClass, ABCContextFactoryBuilder.class.getName(),
            "Specified InitialContextFactory has not been picked up to create the requested initial context.");

    propertyMap.put("service.ranking", 20);
    ServiceRegistration<InitialContextFactoryBuilder> xyzICFBServiceRef = bundleContext.registerService(
            InitialContextFactoryBuilder.class, new XYZContextFactoryBuilder(), propertyMap);
    initialContext = jndiContextManager.newInitialContext();

    initialContext.bind("contextFactoryBuilderClass", "EMPTY");
    contextFactoryBuilderClass = (String) initialContext.lookup("contextFactoryBuilderClass");

    assertEquals(contextFactoryBuilderClass, XYZContextFactoryBuilder.class.getName(),
            "Specified InitialContextFactory has not been picked up to create the requested initial context.");

    abcICFBServiceRef.unregister();
    nullICFBServiceRef.unregister();
    xyzICFBServiceRef.unregister();
}