use of javax.naming.Context in project jetty.project by eclipse.
the class Transaction method bindToComp.
/**
* Insist on the java:comp/UserTransaction binding
* @throws NamingException
*/
private void bindToComp() throws NamingException {
//ignore the name, it is always bound to java:comp
InitialContext ic = new InitialContext();
Context env = (Context) ic.lookup("java:comp");
__log.debug("Binding java:comp/" + getJndiName() + " to " + _objectNameString);
NamingUtil.bind(env, getJndiName(), new LinkRef(_objectNameString));
}
use of javax.naming.Context in project jetty.project by eclipse.
the class localContextRoot method unbind.
/**
*
*
* @see javax.naming.Context#unbind(javax.naming.Name)
*/
public void unbind(Name name) throws NamingException {
synchronized (__root) {
if (name.size() == 0)
return;
if (__root.isLocked())
throw new NamingException("This context is immutable");
Name cname = __root.toCanonicalName(name);
if (cname == null)
throw new NamingException("Name is null");
if (cname.size() == 0)
throw new NamingException("Name is empty");
//if no subcontexts, just unbind it
if (cname.size() == 1) {
__root.removeBinding(cname);
} else {
//walk down the subcontext hierarchy
if (__log.isDebugEnabled())
__log.debug("Checking for existing binding for name=" + cname + " for first element of name=" + cname.get(0));
String firstComponent = cname.get(0);
Object ctx = null;
if (firstComponent.equals(""))
ctx = this;
else {
Binding binding = __root.getBinding(name.get(0));
if (binding == null)
throw new NameNotFoundException(name.get(0) + " is not bound");
ctx = binding.getObject();
if (ctx instanceof Reference) {
//deference the object
try {
ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), __root, _env);
} catch (NamingException e) {
throw e;
} catch (Exception e) {
__log.warn("", e);
throw new NamingException(e.getMessage());
}
}
}
if (ctx instanceof Context) {
((Context) ctx).unbind(cname.getSuffix(1));
} else
throw new NotContextException("Object bound at " + firstComponent + " is not a Context");
}
}
}
use of javax.naming.Context in project jetty.project by eclipse.
the class localContextRoot method list.
/**
*
*
* @see javax.naming.Context#list(javax.naming.Name)
*/
public NamingEnumeration list(Name name) throws NamingException {
synchronized (__root) {
//return __root.list(getSuffix(name));
Name cname = __root.toCanonicalName(name);
if (cname == null) {
List<Binding> empty = Collections.emptyList();
return new NameEnumeration(empty.iterator());
}
if (cname.size() == 0) {
return new NameEnumeration(__root.getBindings().values().iterator());
}
//multipart name
String firstComponent = cname.get(0);
Object ctx = null;
if (firstComponent.equals(""))
ctx = this;
else {
Binding binding = __root.getBinding(firstComponent);
if (binding == null)
throw new NameNotFoundException();
ctx = binding.getObject();
if (ctx instanceof Reference) {
//deference the object
if (__log.isDebugEnabled())
__log.debug("Dereferencing Reference for " + name.get(0));
try {
ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), __root, _env);
} catch (NamingException e) {
throw e;
} catch (Exception e) {
__log.warn("", e);
throw new NamingException(e.getMessage());
}
}
}
if (!(ctx instanceof Context))
throw new NotContextException();
return ((Context) ctx).list(cname.getSuffix(1));
}
}
use of javax.naming.Context in project jetty.project by eclipse.
the class localContextRoot method bind.
/**
*
*
* @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
*/
public void bind(Name name, Object obj) throws NamingException {
synchronized (__root) {
if (__root.isLocked())
throw new NamingException("This context is immutable");
Name cname = __root.toCanonicalName(name);
if (cname == null)
throw new NamingException("Name is null");
if (cname.size() == 0)
throw new NamingException("Name is empty");
//if no subcontexts, just bind it
if (cname.size() == 1) {
//get the object to be bound
Object objToBind = NamingManager.getStateToBind(obj, name, this, _env);
// Check for Referenceable
if (objToBind instanceof Referenceable) {
objToBind = ((Referenceable) objToBind).getReference();
}
//anything else we should be able to bind directly
__root.addBinding(cname, objToBind);
} else {
if (__log.isDebugEnabled())
__log.debug("Checking for existing binding for name=" + cname + " for first element of name=" + cname.get(0));
//walk down the subcontext hierarchy
//need to ignore trailing empty "" name components
String firstComponent = cname.get(0);
Object ctx = null;
if (firstComponent.equals(""))
ctx = this;
else {
Binding binding = __root.getBinding(firstComponent);
if (binding == null)
throw new NameNotFoundException(firstComponent + " is not bound");
ctx = binding.getObject();
if (ctx instanceof Reference) {
//deference the object
try {
ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), this, _env);
} catch (NamingException e) {
throw e;
} catch (Exception e) {
__log.warn("", e);
throw new NamingException(e.getMessage());
}
}
}
if (ctx instanceof Context) {
((Context) ctx).bind(cname.getSuffix(1), obj);
} else
throw new NotContextException("Object bound at " + firstComponent + " is not a Context");
}
}
}
use of javax.naming.Context in project jetty.project by eclipse.
the class TestMailSessionReference method testMailSessionReference.
@Test
public void testMailSessionReference() throws Exception {
InitialContext icontext = new InitialContext();
MailSessionReference sref = new MailSessionReference();
sref.setUser("janb");
sref.setPassword("OBF:1xmk1w261z0f1w1c1xmq");
Properties props = new Properties();
props.put("mail.smtp.host", "xxx");
props.put("mail.debug", "true");
sref.setProperties(props);
NamingUtil.bind(icontext, "mail/Session", sref);
Object x = icontext.lookup("mail/Session");
assertNotNull(x);
assertTrue(x instanceof javax.mail.Session);
javax.mail.Session session = (javax.mail.Session) x;
Properties sessionProps = session.getProperties();
assertEquals(props, sessionProps);
assertTrue(session.getDebug());
Context foo = icontext.createSubcontext("foo");
NameParser parser = icontext.getNameParser("");
Name objectNameInNamespace = parser.parse(icontext.getNameInNamespace());
objectNameInNamespace.addAll(parser.parse("mail/Session"));
NamingUtil.bind(foo, "mail/Session", new LinkRef(objectNameInNamespace.toString()));
Object o = foo.lookup("mail/Session");
assertNotNull(o);
Session fooSession = (Session) o;
assertEquals(props, fooSession.getProperties());
assertTrue(fooSession.getDebug());
icontext.destroySubcontext("mail");
icontext.destroySubcontext("foo");
}
Aggregations