Search in sources :

Example 6 with InitialContextFactory

use of javax.naming.spi.InitialContextFactory in project sqlg by pietermartin.

the class TestJNDIInitialization method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
    configuration = new PropertiesConfiguration(sqlProperties);
    if (!configuration.containsKey("jdbc.url")) {
        throw new IllegalArgumentException(String.format("SqlGraph configuration requires that the %s be set", "jdbc.url"));
    }
    String url = configuration.getString("jdbc.url");
    // obtain the connection that we will later supply from JNDI
    SqlgPlugin p = findSqlgPlugin(url);
    Assert.assertNotNull(p);
    ds = new C3p0DataSourceFactory().setup(p.getDriverFor(url), configuration).getDatasource();
    // change the connection url to be a JNDI one
    configuration.setProperty("jdbc.url", "jndi:testConnection");
    // set up the initial context
    NamingManager.setInitialContextFactoryBuilder(environment -> {
        InitialContextFactory mockFactory = mock(InitialContextFactory.class);
        Context mockContext = mock(Context.class);
        when(mockFactory.getInitialContext(any())).thenReturn(mockContext);
        when(mockContext.lookup("testConnection")).thenReturn(ds);
        return mockFactory;
    });
}
Also used : Context(javax.naming.Context) C3p0DataSourceFactory(org.umlg.sqlg.structure.ds.C3p0DataSourceFactory) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) SqlgPlugin(org.umlg.sqlg.SqlgPlugin) InitialContextFactory(javax.naming.spi.InitialContextFactory) URL(java.net.URL) BeforeClass(org.junit.BeforeClass)

Example 7 with InitialContextFactory

use of javax.naming.spi.InitialContextFactory in project aries by apache.

the class ContextHelper method getContextProvider.

public static ContextProvider getContextProvider(BundleContext context, Hashtable<?, ?> environment) throws NamingException {
    ContextProvider provider = null;
    String contextFactoryClass = (String) environment.get(Context.INITIAL_CONTEXT_FACTORY);
    if (contextFactoryClass == null) {
        // 1. get ContextFactory using builder
        provider = getInitialContextUsingBuilder(context, environment).orElseGet(() -> getInitialContextUsingFactoryServices(context, environment).orElse(null));
    } else {
        // 1. lookup using specified InitialContextFactory
        ServiceReference<InitialContextFactory> ref = Activator.getInitialContextFactory(contextFactoryClass);
        if (ref != null) {
            InitialContextFactory factory = Activator.getService(context, ref);
            if (factory != null) {
                Context initialContext = factory.getInitialContext(environment);
                provider = new SingleContextProvider(context, ref, initialContext);
            }
        }
        // 2. get ContextFactory using builder
        if (provider == null) {
            provider = getInitialContextUsingBuilder(context, environment).orElse(null);
        }
    }
    return provider;
}
Also used : BundleContext(org.osgi.framework.BundleContext) Context(javax.naming.Context) InitialContextFactory(javax.naming.spi.InitialContextFactory)

Example 8 with InitialContextFactory

use of javax.naming.spi.InitialContextFactory in project aries by apache.

the class ContextHelper method getInitialContextUsingFactoryServices.

private static Optional<ContextProvider> getInitialContextUsingFactoryServices(BundleContext context, Hashtable<?, ?> environment) {
    for (ServiceReference<InitialContextFactory> reference : Activator.getInitialContextFactoryServices()) {
        try {
            InitialContextFactory factory = Activator.getService(context, reference);
            Context initialContext = factory.getInitialContext(environment);
            if (initialContext != null) {
                return Optional.of(new SingleContextProvider(context, reference, initialContext));
            }
        } catch (NamingException e) {
            // ignore this, if the builder fails we want to move onto the next one
            logger.log(Level.FINE, "Exception caught", e);
        }
    }
    return Optional.empty();
}
Also used : BundleContext(org.osgi.framework.BundleContext) Context(javax.naming.Context) NamingException(javax.naming.NamingException) InitialContextFactory(javax.naming.spi.InitialContextFactory)

Example 9 with InitialContextFactory

use of javax.naming.spi.InitialContextFactory in project aries by apache.

the class InitialContextTest method testLookupWithICF.

@Test
public void testLookupWithICF() throws NamingException {
    InitialContextFactory icf = Skeleton.newMock(InitialContextFactory.class);
    bc.registerService(new String[] { InitialContextFactory.class.getName(), icf.getClass().getName() }, icf, (Dictionary) new Properties());
    Skeleton.getSkeleton(icf).setReturnValue(new MethodCall(Context.class, "lookup", "/"), Skeleton.newMock(Context.class));
    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, icf.getClass().getName());
    props.put(JNDIConstants.BUNDLE_CONTEXT, bc);
    InitialContext ctx = new InitialContext(props);
    Context namingCtx = (Context) ctx.lookup("/");
    assertTrue("Context returned isn't the raw naming context: " + namingCtx, Skeleton.isSkeleton(namingCtx));
}
Also used : InitialLdapContext(javax.naming.ldap.InitialLdapContext) BundleContext(org.osgi.framework.BundleContext) LdapContext(javax.naming.ldap.LdapContext) Properties(java.util.Properties) InitialContextFactory(javax.naming.spi.InitialContextFactory) MethodCall(org.apache.aries.unittest.mocks.MethodCall) Test(org.junit.Test)

Aggregations

InitialContextFactory (javax.naming.spi.InitialContextFactory)9 Context (javax.naming.Context)5 Hashtable (java.util.Hashtable)3 NamingException (javax.naming.NamingException)3 Test (org.junit.Test)3 BundleContext (org.osgi.framework.BundleContext)3 Properties (java.util.Properties)2 InitialLdapContext (javax.naming.ldap.InitialLdapContext)2 LdapContext (javax.naming.ldap.LdapContext)2 InitialContextFactoryBuilder (javax.naming.spi.InitialContextFactoryBuilder)2 MethodCall (org.apache.aries.unittest.mocks.MethodCall)2 ResourceProperty (com.sun.enterprise.repository.ResourceProperty)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Binding (javax.naming.Binding)1 InitialContext (javax.naming.InitialContext)1 NameClassPair (javax.naming.NameClassPair)1 NameNotFoundException (javax.naming.NameNotFoundException)1 Reference (javax.naming.Reference)1