use of javax.naming.Binding in project aries by apache.
the class BlueprintURLContextTest method testListBindings.
/**
* Test BlueprintURLContext.listBindings()
* This returns an enumeration of component id -> component pairs
*/
@Test
public void testListBindings() throws Exception {
InitialContext ctx = new InitialContext();
NamingEnumeration<Binding> bindings = ctx.listBindings("blueprint:comp");
Set<String> expectedCompIds = new BlueprintContainerStub().getComponentIds();
while (bindings.hasMore()) {
Binding b = bindings.next();
String compId = b.getName();
Object component = b.getObject();
if (compId.equals("comp1")) {
SimpleComponent sc = (SimpleComponent) component;
assertEquals("comp1 message wrong", "comp1_message", sc.getIdMessage());
} else if (compId.equals("comp2")) {
AnotherComponent ac = (AnotherComponent) component;
assertEquals("comp2 message wrong", "AnotherComponent with id comp2", ac.getIdMessage());
}
expectedCompIds.remove(compId);
}
assertEquals("Not all expected components were found", expectedCompIds.size(), 0);
}
use of javax.naming.Binding in project aries by apache.
the class ServiceRegistryContextTest method checkThreadRetrievedViaListBindingsMethod.
/**
* Check that the NamingEnumeration passed in has another element, which represents a java.lang.Thread
* @param serviceList
* @return the object in the registry
* @throws NamingException
*/
private Object checkThreadRetrievedViaListBindingsMethod(NamingEnumeration<Binding> serviceList) throws NamingException {
assertTrue("The repository was empty", serviceList.hasMoreElements());
Binding binding = serviceList.nextElement();
assertNotNull("We didn't get a service back from our lookup :(", binding);
assertNotNull("The object from the SR was null", binding.getObject());
assertTrue("The service retrieved was not of the correct type", binding.getObject() instanceof Thread);
assertEquals("osgi:service/java.lang.Runnable/(rubbish=smelly)", binding.getName().toString());
return binding.getObject();
}
use of javax.naming.Binding in project aries by apache.
the class ServiceRegistryContextTest method checkServiceListListBindings.
@Test
public void checkServiceListListBindings() throws NamingException {
BundleMock mock = new BundleMock("scooby.doo", new Properties());
Thread.currentThread().setContextClassLoader(mock.getClassLoader());
InitialContext ctx = new InitialContext();
String className = Runnable.class.getName();
MethodCall run = new MethodCall(Runnable.class, "run");
Runnable t = Skeleton.newMock(Runnable.class);
Runnable t2 = Skeleton.newMock(Runnable.class);
// we don't want the default service
reg.unregister();
ServiceRegistration reg = bc.registerService(className, t, null);
ServiceRegistration reg2 = bc.registerService(className, t2, null);
NamingEnumeration<Binding> ne = ctx.listBindings("osgi:servicelist/" + className);
assertTrue(ne.hasMoreElements());
Binding bnd = ne.nextElement();
assertEquals(String.valueOf(reg.getReference().getProperty(Constants.SERVICE_ID)), bnd.getName());
assertTrue("Class name not correct. Was: " + bnd.getClassName(), bnd.getClassName().contains("Proxy") || bnd.getClassName().contains("EnhancerByCGLIB"));
Runnable r = (Runnable) bnd.getObject();
assertNotNull(r);
r.run();
Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(run, 1);
Skeleton.getSkeleton(t2).assertNotCalled(run);
assertTrue(ne.hasMoreElements());
bnd = ne.nextElement();
assertEquals(String.valueOf(reg2.getReference().getProperty(Constants.SERVICE_ID)), bnd.getName());
assertTrue("Class name not correct. Was: " + bnd.getClassName(), bnd.getClassName().contains("Proxy") || bnd.getClassName().contains("EnhancerByCGLIB"));
r = (Runnable) bnd.getObject();
assertNotNull(r);
r.run();
Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(run, 1);
Skeleton.getSkeleton(t2).assertCalledExactNumberOfTimes(run, 1);
assertFalse(ne.hasMoreElements());
}
use of javax.naming.Binding in project tomcat by apache.
the class ManagerServlet method printResources.
/**
* List the resources of the given context.
* @param writer Writer to render to
* @param prefix Path for recursion
* @param namingContext The naming context for lookups
* @param type Fully qualified class name of the resource type of interest,
* or <code>null</code> to list resources of all types
* @param smClient i18n support for current client's locale
*/
protected void printResources(PrintWriter writer, String prefix, javax.naming.Context namingContext, String type, StringManager smClient) {
try {
NamingEnumeration<Binding> items = namingContext.listBindings("");
while (items.hasMore()) {
Binding item = items.next();
Object obj = item.getObject();
if (obj instanceof javax.naming.Context) {
printResources(writer, prefix + item.getName() + "/", (javax.naming.Context) obj, type, smClient);
} else {
if (type != null && (obj == null || !IntrospectionUtils.isInstance(obj.getClass(), type))) {
continue;
}
writer.print(prefix + item.getName());
writer.print(':');
writer.print(item.getClassName());
// Do we want a description if available?
writer.println();
}
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
log(sm.getString("managerServlet.error.resources", type), t);
writer.println(smClient.getString("managerServlet.exception", t.toString()));
}
}
use of javax.naming.Binding in project jetty.project by eclipse.
the class localContextRoot method createSubcontext.
/**
*
*
* @see javax.naming.Context#createSubcontext(javax.naming.Name)
*/
public Context createSubcontext(Name name) throws NamingException {
synchronized (__root) {
if (__root.isLocked()) {
NamingException ne = new NamingException("This context is immutable");
ne.setRemainingName(name);
throw ne;
}
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 (cname.size() == 1) {
//not permitted to bind if something already bound at that name
Binding binding = __root.getBinding(cname);
if (binding != null)
throw new NameAlreadyBoundException(cname.toString());
//make a new naming context with the root as the parent
Context ctx = new NamingContext((Hashtable) _env.clone(), cname.get(0), __root, __root.getNameParser(""));
__root.addBinding(cname, ctx);
return ctx;
}
//If the name has multiple subcontexts, walk the hierarchy by
//fetching the first one. All intermediate subcontexts in the
//name must already exist.
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
if (__log.isDebugEnabled())
__log.debug("Object bound at " + firstComponent + " is a Reference");
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) {
return ((Context) ctx).createSubcontext(cname.getSuffix(1));
} else
throw new NotContextException(firstComponent + " is not a Context");
}
}
Aggregations