BlueprintURLContext implements Context { @Override public Object lookup(Name name) throws NamingException, ServiceUnavailableException { ServiceReference<BlueprintContainer> blueprintContainerRef = getBlueprintContainerRef(_callersBundle); Object result; try { BlueprintContainer blueprintContainer = _callersBundle.getBundleContext().getService(blueprintContainerRef); BlueprintName bpName; if (name instanceof BlueprintName) { bpName = (BlueprintName) name; } else if (_parentName != null) { bpName = new BlueprintName(_parentName.toString() + "/" + name.toString()); } else { bpName = (BlueprintName) _parser.parse(name.toString()); } if (bpName.hasComponent()) { String componentId = bpName.getComponentId(); try { result = blueprintContainer.getComponentInstance(componentId); } catch (NoSuchComponentException nsce) { throw new NameNotFoundException(nsce.getMessage()); } } else { result = new BlueprintURLContext(_callersBundle, bpName, _env); } } finally { _callersBundle.getBundleContext().ungetService(blueprintContainerRef); } return result; } @SuppressWarnings("unchecked")  BlueprintURLContext(Bundle callersBundle, Hashtable<?, ?> env); private  BlueprintURLContext(Bundle callersBundle, BlueprintName parentName, Map<String, Object> env); static int getGracePeriod(Bundle b); @Override Object addToEnvironment(String propName, Object propVal); @Override void bind(Name n, Object o); @Override void bind(String s, Object o); @Override void close(); @Override Name composeName(Name name, Name prefix); @Override String composeName(String name, String prefix); @Override Context createSubcontext(Name n); @Override Context createSubcontext(String s); @Override void destroySubcontext(Name n); @Override void destroySubcontext(String s); @Override Hashtable<?, ?> getEnvironment(); @Override String getNameInNamespace(); @Override NameParser getNameParser(Name n); @Override NameParser getNameParser(String s); @Override NamingEnumeration<NameClassPair> list(Name name); @Override NamingEnumeration<NameClassPair> list(String s); @Override NamingEnumeration<Binding> listBindings(Name name); @Override NamingEnumeration<Binding> listBindings(String name); @Override Object lookup(Name name); @Override Object lookup(String name); @Override Object lookupLink(Name n); @Override Object lookupLink(String s); @Override void rebind(Name n, Object o); @Override void rebind(String s, Object o); @Override Object removeFromEnvironment(String propName); @Override void rename(Name nOld, Name nNew); @Override void rename(String sOld, String sNew); @Override void unbind(Name n); @Override void unbind(String s);  }
@Test public void testSimpleComponentLookup() throws Exception { BlueprintURLContext bpURLc = new BlueprintURLContext (bundle, new Hashtable<String, String>()); SimpleComponent sc = (SimpleComponent) bpURLc.lookup("blueprint:comp/comp1"); assertNotNull (sc); String msg = sc.getIdMessage(); assertEquals ("comp1 message wrong", "comp1_message", msg); }
@Test public void testTwoLevelComponentLookup() throws Exception { InitialContext ctx = new InitialContext(); Context ctx2 = (Context) ctx.lookup("blueprint:comp"); SimpleComponent sc = (SimpleComponent) ctx2.lookup("comp2"); assertNotNull (sc); String msg = sc.getIdMessage(); assertEquals ("comp2 message wrong", "AnotherComponent with id comp2", msg); }
@Test(expected=NameNotFoundException.class) public void testLookupForServiceWeNeverHad() throws NamingException { InitialContext ctx = new InitialContext(); ctx.lookup("blueprint:comp/this.is.not.a.component"); }