use of javax.naming.Name in project jetty.project by eclipse.
the class NamingContext method rebind.
/*------------------------------------------------*/
/**
* Overwrite or create a binding
*
* @param name a <code>Name</code> value
* @param obj an <code>Object</code> value
* @exception NamingException if an error occurs
*/
public void rebind(Name name, Object obj) throws NamingException {
if (isLocked())
throw new NamingException("This context is immutable");
Name cname = 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) {
//check if it is a Referenceable
Object objToBind = NamingManager.getStateToBind(obj, name, this, _env);
if (objToBind instanceof Referenceable) {
objToBind = ((Referenceable) objToBind).getReference();
}
removeBinding(cname);
addBinding(cname, objToBind);
} 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 = 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), this, _env);
} catch (NamingException e) {
throw e;
} catch (Exception e) {
__log.warn("", e);
throw new NamingException(e.getMessage());
}
}
}
if (ctx instanceof Context) {
((Context) ctx).rebind(cname.getSuffix(1), obj);
} else
throw new NotContextException("Object bound at " + firstComponent + " is not a Context");
}
}
use of javax.naming.Name in project jetty.project by eclipse.
the class NamingUtil method bind.
/* ------------------------------------------------------------ */
/**
* Bind an object to a context ensuring all sub-contexts
* are created if necessary
*
* @param ctx the context into which to bind
* @param nameStr the name relative to context to bind
* @param obj the object to be bound
* @return the bound context
* @exception NamingException if an error occurs
*/
public static Context bind(Context ctx, String nameStr, Object obj) throws NamingException {
Name name = ctx.getNameParser("").parse(nameStr);
//no name, nothing to do
if (name.size() == 0)
return null;
Context subCtx = ctx;
//last component of the name will be the name to bind
for (int i = 0; i < name.size() - 1; i++) {
try {
subCtx = (Context) subCtx.lookup(name.get(i));
if (__log.isDebugEnabled())
__log.debug("Subcontext " + name.get(i) + " already exists");
} catch (NameNotFoundException e) {
subCtx = subCtx.createSubcontext(name.get(i));
if (__log.isDebugEnabled())
__log.debug("Subcontext " + name.get(i) + " created");
}
}
subCtx.rebind(name.get(name.size() - 1), obj);
if (__log.isDebugEnabled())
__log.debug("Bound object to " + name.get(name.size() - 1));
return subCtx;
}
use of javax.naming.Name in project jetty.project by eclipse.
the class localContextRoot method lookup.
/**
*
*
* @see javax.naming.Context#lookup(javax.naming.Name)
*/
public Object lookup(Name name) throws NamingException {
synchronized (__root) {
if (__log.isDebugEnabled())
__log.debug("Looking up name=\"" + name + "\"");
Name cname = __root.toCanonicalName(name);
if ((cname == null) || (cname.size() == 0)) {
__log.debug("Null or empty name, returning copy of this context");
NamingContext ctx = new NamingContext(_env, null, null, __root.getNameParser(""));
ctx.setBindings(__root.getBindings());
return ctx;
}
if (cname.size() == 1) {
Binding binding = __root.getBinding(cname);
if (binding == null) {
NameNotFoundException nnfe = new NameNotFoundException();
nnfe.setRemainingName(cname);
throw nnfe;
}
Object o = binding.getObject();
//handle links by looking up the link
if (o instanceof LinkRef) {
//if link name starts with ./ it is relative to current context
String linkName = ((LinkRef) o).getLinkName();
if (linkName.startsWith("./"))
return lookup(linkName.substring(2));
else {
//link name is absolute
InitialContext ictx = new InitialContext();
return ictx.lookup(linkName);
}
} else if (o instanceof Reference) {
//deference the object
try {
return NamingManager.getObjectInstance(o, cname, __root, _env);
} catch (NamingException e) {
throw e;
} catch (final Exception e) {
throw new NamingException(e.getMessage()) {
{
initCause(e);
}
};
}
} else
return o;
}
//it is a multipart name, get the first subcontext
String firstComponent = cname.get(0);
Object ctx = null;
if (firstComponent.equals(""))
ctx = this;
else {
Binding binding = __root.getBinding(firstComponent);
if (binding == null) {
NameNotFoundException nnfe = new NameNotFoundException();
nnfe.setRemainingName(cname);
throw nnfe;
}
//as we have bound a reference to an object factory
//for the component specific contexts
//at "comp" we need to resolve the reference
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))
throw new NotContextException();
return ((Context) ctx).lookup(cname.getSuffix(1));
}
}
use of javax.naming.Name in project jetty.project by eclipse.
the class EnvConfiguration method bindEnvEntries.
/**
* Bind all EnvEntries that have been declared, so that the processing of the
* web.xml file can potentially override them.
*
* We first bind EnvEntries declared in Server scope, then WebAppContext scope.
* @param context the context to use for the object scope
* @throws NamingException if unable to bind env entries
*/
public void bindEnvEntries(WebAppContext context) throws NamingException {
LOG.debug("Binding env entries from the jvm scope");
InitialContext ic = new InitialContext();
Context envCtx = (Context) ic.lookup("java:comp/env");
Object scope = null;
List<Object> list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
Iterator<Object> itor = list.iterator();
while (itor.hasNext()) {
EnvEntry ee = (EnvEntry) itor.next();
ee.bindToENC(ee.getJndiName());
Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
//also save the EnvEntry in the context so we can check it later
NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
}
LOG.debug("Binding env entries from the server scope");
scope = context.getServer();
list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
itor = list.iterator();
while (itor.hasNext()) {
EnvEntry ee = (EnvEntry) itor.next();
ee.bindToENC(ee.getJndiName());
Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
//also save the EnvEntry in the context so we can check it later
NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
}
LOG.debug("Binding env entries from the context scope");
scope = context;
list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
itor = list.iterator();
while (itor.hasNext()) {
EnvEntry ee = (EnvEntry) itor.next();
ee.bindToENC(ee.getJndiName());
Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
//also save the EnvEntry in the context so we can check it later
NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
}
}
use of javax.naming.Name in project jetty.project by eclipse.
the class TestNamingEntryUtil method testMakeNamingEntryName.
@Test
public void testMakeNamingEntryName() throws Exception {
Name name = NamingEntryUtil.makeNamingEntryName(null, "fee/fi/fo/fum");
assertNotNull(name);
assertEquals(NamingEntry.__contextName + "/fee/fi/fo/fum", name.toString());
}
Aggregations