use of javax.naming.spi.InitialContextFactory in project spring-framework by spring-projects.
the class SimpleNamingContextTests method testNamingContextBuilder.
@Test
public void testNamingContextBuilder() throws NamingException {
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
InitialContextFactory factory = builder.createInitialContextFactory(null);
DataSource ds = new StubDataSource();
builder.bind("java:comp/env/jdbc/myds", ds);
Object obj = new Object();
builder.bind("myobject", obj);
Context context1 = factory.getInitialContext(null);
assertTrue("Correct DataSource registered", context1.lookup("java:comp/env/jdbc/myds") == ds);
assertTrue("Correct Object registered", context1.lookup("myobject") == obj);
Hashtable<String, String> env2 = new Hashtable<>();
env2.put("key1", "value1");
Context context2 = factory.getInitialContext(env2);
assertTrue("Correct DataSource registered", context2.lookup("java:comp/env/jdbc/myds") == ds);
assertTrue("Correct Object registered", context2.lookup("myobject") == obj);
assertTrue("Correct environment", context2.getEnvironment() != env2);
assertTrue("Correct key1", "value1".equals(context2.getEnvironment().get("key1")));
Integer i = new Integer(0);
context1.rebind("myinteger", i);
String s = "";
context2.bind("mystring", s);
Context context3 = (Context) context2.lookup("");
context3.rename("java:comp/env/jdbc/myds", "jdbc/myds");
context3.unbind("myobject");
assertTrue("Correct environment", context3.getEnvironment() != context2.getEnvironment());
context3.addToEnvironment("key2", "value2");
assertTrue("key2 added", "value2".equals(context3.getEnvironment().get("key2")));
context3.removeFromEnvironment("key1");
assertTrue("key1 removed", context3.getEnvironment().get("key1") == null);
assertTrue("Correct DataSource registered", context1.lookup("jdbc/myds") == ds);
try {
context1.lookup("myobject");
fail("Should have thrown NameNotFoundException");
} catch (NameNotFoundException ex) {
// expected
}
assertTrue("Correct Integer registered", context1.lookup("myinteger") == i);
assertTrue("Correct String registered", context1.lookup("mystring") == s);
assertTrue("Correct DataSource registered", context2.lookup("jdbc/myds") == ds);
try {
context2.lookup("myobject");
fail("Should have thrown NameNotFoundException");
} catch (NameNotFoundException ex) {
// expected
}
assertTrue("Correct Integer registered", context2.lookup("myinteger") == i);
assertTrue("Correct String registered", context2.lookup("mystring") == s);
assertTrue("Correct DataSource registered", context3.lookup("jdbc/myds") == ds);
try {
context3.lookup("myobject");
fail("Should have thrown NameNotFoundException");
} catch (NameNotFoundException ex) {
// expected
}
assertTrue("Correct Integer registered", context3.lookup("myinteger") == i);
assertTrue("Correct String registered", context3.lookup("mystring") == s);
Map<String, Binding> bindingMap = new HashMap<>();
NamingEnumeration<?> bindingEnum = context3.listBindings("");
while (bindingEnum.hasMoreElements()) {
Binding binding = (Binding) bindingEnum.nextElement();
bindingMap.put(binding.getName(), binding);
}
assertTrue("Correct jdbc subcontext", bindingMap.get("jdbc").getObject() instanceof Context);
assertTrue("Correct jdbc subcontext", SimpleNamingContext.class.getName().equals(bindingMap.get("jdbc").getClassName()));
Context jdbcContext = (Context) context3.lookup("jdbc");
jdbcContext.bind("mydsX", ds);
Map<String, Binding> subBindingMap = new HashMap<>();
NamingEnumeration<?> subBindingEnum = jdbcContext.listBindings("");
while (subBindingEnum.hasMoreElements()) {
Binding binding = (Binding) subBindingEnum.nextElement();
subBindingMap.put(binding.getName(), binding);
}
assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("myds").getObject()));
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("myds").getClassName()));
assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("mydsX").getObject()));
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("mydsX").getClassName()));
assertTrue("Correct Integer registered", i.equals(bindingMap.get("myinteger").getObject()));
assertTrue("Correct Integer registered", Integer.class.getName().equals(bindingMap.get("myinteger").getClassName()));
assertTrue("Correct String registered", s.equals(bindingMap.get("mystring").getObject()));
assertTrue("Correct String registered", String.class.getName().equals(bindingMap.get("mystring").getClassName()));
context1.createSubcontext("jdbc").bind("sub/subds", ds);
Map<String, String> pairMap = new HashMap<>();
NamingEnumeration<?> pairEnum = context2.list("jdbc");
while (pairEnum.hasMore()) {
NameClassPair pair = (NameClassPair) pairEnum.next();
pairMap.put(pair.getName(), pair.getClassName());
}
assertTrue("Correct sub subcontext", SimpleNamingContext.class.getName().equals(pairMap.get("sub")));
Context subContext = (Context) context2.lookup("jdbc/sub");
Map<String, String> subPairMap = new HashMap<>();
NamingEnumeration<?> subPairEnum = subContext.list("");
while (subPairEnum.hasMoreElements()) {
NameClassPair pair = (NameClassPair) subPairEnum.next();
subPairMap.put(pair.getName(), pair.getClassName());
}
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subPairMap.get("subds")));
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
pairMap.clear();
pairEnum = context1.list("jdbc/");
while (pairEnum.hasMore()) {
NameClassPair pair = (NameClassPair) pairEnum.next();
pairMap.put(pair.getName(), pair.getClassName());
}
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
}
use of javax.naming.spi.InitialContextFactory 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));
}
use of javax.naming.spi.InitialContextFactory in project aries by apache.
the class ContextHelper method getInitialContextUsingBuilder.
private static Optional<ContextProvider> getInitialContextUsingBuilder(BundleContext context, Hashtable<?, ?> environment) throws NamingException {
for (ServiceReference<InitialContextFactoryBuilder> ref : Activator.getInitialContextFactoryBuilderServices()) {
InitialContextFactoryBuilder builder = Activator.getService(context, ref);
InitialContextFactory factory = null;
try {
factory = builder.createInitialContextFactory(environment);
} catch (NamingException ne) {
// ignore this, if the builder fails we want to move onto the next one
logger.log(Level.FINE, "Exception caught", ne);
} catch (NullPointerException npe) {
logger.log(Level.SEVERE, "NPE caught in ContextHelper.getInitialContextUsingBuilder. context=" + context + " ref=" + ref);
throw npe;
}
if (factory != null) {
return Optional.of(new SingleContextProvider(context, ref, factory.getInitialContext(environment)));
}
}
return Optional.empty();
}
use of javax.naming.spi.InitialContextFactory in project Payara by payara.
the class ExternalJndiResourceDeployer method installExternalJndiResource.
/**
* Installs the given external jndi resource. This method gets called
* during server initialization and from external jndi resource
* deployer to handle resource events.
*
* @param extJndiRes external jndi resource
* @param resourceInfo Information about the resource
*/
public void installExternalJndiResource(org.glassfish.resources.beans.ExternalJndiResource extJndiRes, ResourceInfo resourceInfo) {
try {
// create the external JNDI factory, its initial context and
// pass them as references.
String factoryClass = extJndiRes.getFactoryClass();
String jndiLookupName = extJndiRes.getJndiLookupName();
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "installExternalJndiResources resourceName " + resourceInfo + " factoryClass " + factoryClass + " jndiLookupName = " + jndiLookupName);
}
Object factory = ResourceUtil.loadObject(factoryClass);
if (factory == null) {
_logger.log(Level.WARNING, "jndi.factory_load_error", factoryClass);
return;
} else if (!(factory instanceof javax.naming.spi.InitialContextFactory)) {
_logger.log(Level.WARNING, "jndi.factory_class_unexpected", factoryClass);
return;
}
// Get properties to create the initial naming context
// for the target JNDI factory
Hashtable env = new Hashtable();
for (Iterator props = extJndiRes.getProperties().iterator(); props.hasNext(); ) {
ResourceProperty prop = (ResourceProperty) props.next();
env.put(prop.getName(), prop.getValue());
}
Context context = null;
try {
context = ((InitialContextFactory) factory).getInitialContext(env);
} catch (NamingException ne) {
_logger.log(Level.SEVERE, "jndi.initial_context_error", factoryClass);
_logger.log(Level.SEVERE, "jndi.initial_context_error_excp", ne.getMessage());
}
if (context == null) {
_logger.log(Level.SEVERE, "jndi.factory_create_error", factoryClass);
return;
}
// Bind a Reference to the proxy object factory; set the
// initial context factory.
// JndiProxyObjectFactory.setInitialContext(bindName, context);
Reference ref = new Reference(extJndiRes.getResType(), "org.glassfish.resources.naming.JndiProxyObjectFactory", null);
// unique JNDI name within server runtime
ref.add(new SerializableObjectRefAddr("resourceInfo", resourceInfo));
// target JNDI name
ref.add(new StringRefAddr("jndiLookupName", jndiLookupName));
// target JNDI factory class
ref.add(new StringRefAddr("jndiFactoryClass", factoryClass));
// add Context info as a reference address
ref.add(new ProxyRefAddr(extJndiRes.getResourceInfo().getName(), env));
// Publish the reference
namingService.publishObject(resourceInfo, ref, true);
} catch (Exception ex) {
_logger.log(Level.SEVERE, "customrsrc.create_ref_error", resourceInfo);
_logger.log(Level.SEVERE, "customrsrc.create_ref_error_excp", ex);
}
}
use of javax.naming.spi.InitialContextFactory in project activemq-artemis by apache.
the class JNDITestSupport method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
configureEnvironment();
InitialContextFactory factory = new ActiveMQInitialContextFactory();
context = factory.getInitialContext(environment);
assertTrue("No context created", context != null);
}
Aggregations