use of javax.naming.StringRefAddr in project jetty.project by eclipse.
the class TestJNDI method testIt.
@Test
public void testIt() throws Exception {
//set up some classloaders
Thread currentThread = Thread.currentThread();
ClassLoader currentLoader = currentThread.getContextClassLoader();
ClassLoader childLoader1 = new URLClassLoader(new URL[0], currentLoader);
ClassLoader childLoader2 = new URLClassLoader(new URL[0], currentLoader);
try {
//Uncomment to aid with debug
/*
javaRootURLContext.getRoot().addListener(new NamingContext.Listener()
{
public void unbind(NamingContext ctx, Binding binding)
{
System.err.println("java unbind "+binding+" from "+ctx.getName());
}
public Binding bind(NamingContext ctx, Binding binding)
{
System.err.println("java bind "+binding+" to "+ctx.getName());
return binding;
}
});
localContextRoot.getRoot().addListener(new NamingContext.Listener()
{
public void unbind(NamingContext ctx, Binding binding)
{
System.err.println("local unbind "+binding+" from "+ctx.getName());
}
public Binding bind(NamingContext ctx, Binding binding)
{
System.err.println("local bind "+binding+" to "+ctx.getName());
return binding;
}
});
*/
//Set up the tccl before doing any jndi operations
currentThread.setContextClassLoader(childLoader1);
InitialContext initCtx = new InitialContext();
//Test we can lookup the root java: naming tree
Context sub0 = (Context) initCtx.lookup("java:");
assertNotNull(sub0);
//already be bound
try {
Context sub1 = sub0.createSubcontext("comp");
fail("Comp should already be bound");
} catch (NameAlreadyBoundException e) {
//expected exception
}
//check bindings at comp
Context sub1 = (Context) initCtx.lookup("java:comp");
assertNotNull(sub1);
Context sub2 = sub1.createSubcontext("env");
assertNotNull(sub2);
initCtx.bind("java:comp/env/rubbish", "abc");
assertEquals("abc", initCtx.lookup("java:comp/env/rubbish"));
//check binding LinkRefs
LinkRef link = new LinkRef("java:comp/env/rubbish");
initCtx.bind("java:comp/env/poubelle", link);
assertEquals("abc", initCtx.lookup("java:comp/env/poubelle"));
//check binding References
StringRefAddr addr = new StringRefAddr("blah", "myReferenceable");
Reference ref = new Reference(java.lang.String.class.getName(), addr, MyObjectFactory.class.getName(), null);
initCtx.bind("java:comp/env/quatsch", ref);
assertEquals(MyObjectFactory.myString, initCtx.lookup("java:comp/env/quatsch"));
//test binding something at java:
Context sub3 = initCtx.createSubcontext("java:zero");
initCtx.bind("java:zero/one", "ONE");
assertEquals("ONE", initCtx.lookup("java:zero/one"));
//change the current thread's classloader to check distinct naming
currentThread.setContextClassLoader(childLoader2);
Context otherSub1 = (Context) initCtx.lookup("java:comp");
assertTrue(!(sub1 == otherSub1));
try {
initCtx.lookup("java:comp/env/rubbish");
fail("env should not exist for this classloader");
} catch (NameNotFoundException e) {
//expected
}
//put the thread's classloader back
currentThread.setContextClassLoader(childLoader1);
//test rebind with existing binding
initCtx.rebind("java:comp/env/rubbish", "xyz");
assertEquals("xyz", initCtx.lookup("java:comp/env/rubbish"));
//test rebind with no existing binding
initCtx.rebind("java:comp/env/mullheim", "hij");
assertEquals("hij", initCtx.lookup("java:comp/env/mullheim"));
//test that the other bindings are already there
assertEquals("xyz", initCtx.lookup("java:comp/env/poubelle"));
//test java:/comp/env/stuff
assertEquals("xyz", initCtx.lookup("java:/comp/env/poubelle/"));
//test list Names
NamingEnumeration nenum = initCtx.list("java:comp/env");
HashMap results = new HashMap();
while (nenum.hasMore()) {
NameClassPair ncp = (NameClassPair) nenum.next();
results.put(ncp.getName(), ncp.getClassName());
}
assertEquals(4, results.size());
assertEquals("java.lang.String", results.get("rubbish"));
assertEquals("javax.naming.LinkRef", results.get("poubelle"));
assertEquals("java.lang.String", results.get("mullheim"));
assertEquals("javax.naming.Reference", results.get("quatsch"));
//test list Bindings
NamingEnumeration benum = initCtx.list("java:comp/env");
assertEquals(4, results.size());
//test NameInNamespace
assertEquals("comp/env", sub2.getNameInNamespace());
//test close does nothing
Context closeCtx = (Context) initCtx.lookup("java:comp/env");
closeCtx.close();
//test what happens when you close an initial context
InitialContext closeInit = new InitialContext();
closeInit.close();
//check locking the context
Context ectx = (Context) initCtx.lookup("java:comp");
ectx.bind("crud", "xxx");
ectx.addToEnvironment("org.eclipse.jndi.immutable", "TRUE");
assertEquals("xxx", initCtx.lookup("java:comp/crud"));
try {
ectx.bind("crud2", "xxx2");
} catch (NamingException ne) {
//expected failure to modify immutable context
}
initCtx.close();
} finally {
//make some effort to clean up
InitialContext ic = new InitialContext();
Context java = (Context) ic.lookup("java:");
java.destroySubcontext("zero");
java.destroySubcontext("fee");
currentThread.setContextClassLoader(childLoader1);
Context comp = (Context) ic.lookup("java:comp");
comp.destroySubcontext("env");
comp.unbind("crud");
comp.unbind("crud2");
currentThread.setContextClassLoader(currentLoader);
}
}
use of javax.naming.StringRefAddr in project jetty.project by eclipse.
the class MailSessionReference method setUser.
public void setUser(String user) {
StringRefAddr addr = (StringRefAddr) get("user");
if (addr != null) {
throw new RuntimeException("user already set on SessionReference, can't be changed");
}
add(new StringRefAddr("user", user));
}
use of javax.naming.StringRefAddr in project jetty.project by eclipse.
the class MailSessionReference method setPassword.
public void setPassword(String password) {
StringRefAddr addr = (StringRefAddr) get("pwd");
if (addr != null)
throw new RuntimeException("password already set on SessionReference, can't be changed");
add(new StringRefAddr("pwd", password));
}
use of javax.naming.StringRefAddr in project jetty.project by eclipse.
the class ContextFactory method newNamingContext.
/**
* Create a new NamingContext.
* @param obj the object to create
* @param loader the classloader for the naming context
* @param env the jndi env for the entry
* @param name the name of the entry
* @param parentCtx the parent context of the entry
* @return the newly created naming context
* @throws Exception if unable to create a new naming context
*/
public NamingContext newNamingContext(Object obj, ClassLoader loader, Hashtable env, Name name, Context parentCtx) throws Exception {
Reference ref = (Reference) obj;
StringRefAddr parserAddr = (StringRefAddr) ref.get("parser");
String parserClassName = (parserAddr == null ? null : (String) parserAddr.getContent());
NameParser parser = (NameParser) (parserClassName == null ? null : loader.loadClass(parserClassName).newInstance());
return new NamingContext(env, name.get(0), (NamingContext) parentCtx, parser);
}
use of javax.naming.StringRefAddr in project aries by apache.
the class ObjectFactoryHelper method getObjectInstanceUsingRefAddress.
private Object getObjectInstanceUsingRefAddress(Enumeration<RefAddr> addresses, Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
Object result = null;
while (addresses.hasMoreElements()) {
RefAddr address = addresses.nextElement();
if (address instanceof StringRefAddr && "URL".equals(address.getType())) {
String urlScheme = getUrlScheme((String) address.getContent());
ServicePair<ObjectFactory> factoryService = ContextHelper.getURLObjectFactory(callerContext, urlScheme, environment);
if (factoryService != null) {
ObjectFactory factory = factoryService.get();
String value = (String) address.getContent();
try {
result = factory.getObjectInstance(value, name, nameCtx, environment);
} finally {
factoryService.unget();
}
// loop we are in.
if (result != null && result != obj) {
break;
}
}
}
}
return (result == null) ? obj : result;
}
Aggregations