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;
});
}
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;
}
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();
}
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));
}
Aggregations