ServiceRegistryContext extends AbstractServiceRegistryContext implements Context { public Object lookup(Name name) throws NamingException { Object result; OsgiName validName = convert(name); String pathFragment = validName.getSchemePath(); String schemeName = validName.getScheme(); if (validName.hasInterface()) { if (OsgiName.FRAMEWORK_PATH.equals(pathFragment) && "bundleContext".equals(validName.getServiceName())) { try { SecurityManager sm = System.getSecurityManager(); if (sm != null) { AdminPermission adminPermission = new AdminPermission(callerContext.getBundle(), AdminPermission.CONTEXT); sm.checkPermission(adminPermission); } } catch (AccessControlException accessControlException) { NamingException namingException = new NameNotFoundException("The Caller does not have permissions to get the BundleContext."); namingException.setRootCause(accessControlException); throw namingException; } return callerContext; } else if ((OsgiName.SERVICE_PATH.equals(pathFragment) && OsgiName.OSGI_SCHEME.equals(schemeName)) || (OsgiName.SERVICES_PATH.equals(pathFragment) && OsgiName.ARIES_SCHEME.equals(schemeName))) { result = ServiceHelper.getService(callerContext, validName, null, true, env, OsgiName.OSGI_SCHEME.equals(schemeName)); } else if (OsgiName.SERVICE_LIST_PATH.equals(pathFragment)) { result = new ServiceRegistryListContext(callerContext, env, validName); } else { result = null; } } else { result = new ServiceRegistryContext(callerContext, validName, env); } if (result == null) { throw new NameNotFoundException(name.toString()); } return result; } ServiceRegistryContext(BundleContext callerContext, Hashtable<?, ?> environment);  ServiceRegistryContext(BundleContext callerContext, OsgiName validName, Map<String, Object> env); NamingEnumeration<NameClassPair> list(final Name name); NamingEnumeration<NameClassPair> list(String name); NamingEnumeration<Binding> listBindings(final Name name); NamingEnumeration<Binding> listBindings(String name); Object lookup(Name name); Object lookup(String name);  }
@Test public void testBaseLookup() throws NamingException { BundleMock mock = new BundleMock("scooby.doo", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); InitialContext ctx = new InitialContext(); Context ctx2 = (Context) ctx.lookup("osgi:service"); Runnable r1 = (Runnable) ctx2.lookup("java.lang.Runnable"); assertNotNull(r1); assertTrue("expected proxied service class", r1 != service); Runnable r2 = (Runnable) ctx.lookup("aries:services/java.lang.Runnable"); assertNotNull(r2); assertTrue("expected non-proxied service class", r2 == service); }
@Test public void testLookupWithPause() throws NamingException { BundleMock mock = new BundleMock("scooby.doo", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); Hashtable<Object, Object> env = new Hashtable<Object, Object>(); env.put(JNDIConstants.REBIND_TIMEOUT, 1000); InitialContext ctx = new InitialContext(env); Context ctx2 = (Context) ctx.lookup("osgi:service"); Runnable r1 = (Runnable) ctx2.lookup("java.lang.Runnable"); reg.unregister(); long startTime = System.currentTimeMillis(); try { r1.run(); fail("Should have received an exception"); } catch (ServiceException e) { long endTime = System.currentTimeMillis(); long diff = endTime - startTime; assertTrue("The run method did not fail in the expected time (1s): " + diff, diff >= 1000); } }
@Test public void jndiLookupServiceNameTest() throws NamingException, SQLException { InitialContext ctx = new InitialContext(new Hashtable<Object, Object>()); BundleMock mock = new BundleMock("scooby.doo", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); DataSource first = Skeleton.newMock(DataSource.class); DataSource second = Skeleton.newMock(DataSource.class); Hashtable<String, String> properties = new Hashtable<String, String>(); properties.put("osgi.jndi.service.name", "jdbc/myDataSource"); bc.registerService(DataSource.class.getName(), first, properties); properties = new Hashtable<String, String>(); properties.put("osgi.jndi.service.name", "jdbc/myDataSource2"); bc.registerService(DataSource.class.getName(), second, properties); DataSource s = (DataSource) ctx.lookup("osgi:service/jdbc/myDataSource"); assertNotNull(s); s = (DataSource) ctx.lookup("osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/myDataSource2)"); assertNotNull(s); s.isWrapperFor(DataSource.class); Skeleton.getSkeleton(second).assertCalled(new MethodCall(DataSource.class, "isWrapperFor", Class.class)); }
@Test public void simpleJNDILookup() throws NamingException { System.setProperty(Context.URL_PKG_PREFIXES, "helloMatey"); InitialContext ctx = new InitialContext(new Hashtable<Object, Object>()); BundleMock mock = new BundleMock("scooby.doo.1", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); Runnable s = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable"); assertNotNull("We didn't get a service back from our lookup :(", s); s.run(); Skeleton.getSkeleton(service).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1); Skeleton skel = Skeleton.getSkeleton(mock.getBundleContext()); skel.assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", null)); ctx = new InitialContext(new Hashtable<Object, Object>()); mock = new BundleMock("scooby.doo.2", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); s = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable"); String packages = System.getProperty(Context.URL_PKG_PREFIXES, null); assertTrue(ctx.getEnvironment().containsValue(packages)); assertNotNull("We didn't get a service back from our lookup :(", s); s.run(); Skeleton.getSkeleton(service).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 2); skel = Skeleton.getSkeleton(mock.getBundleContext()); skel.assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", null)); }
@Test public void jndiLookupWithFilter() throws NamingException { BundleMock mock = new BundleMock("scooby.doo", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); InitialContext ctx = new InitialContext(); Object s = ctx.lookup("osgi:service/java.lang.Runnable/(rubbish=smelly)"); assertNotNull("We didn't get a service back from our lookup :(", s); service.run(); Skeleton.getSkeleton(service).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1); Skeleton.getSkeleton(mock.getBundleContext()).assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", "(rubbish=smelly)")); }
@Test(expected=NameNotFoundException.class) public void testLookupWhenServiceHasBeenRemoved() throws NamingException { reg.unregister(); BundleMock mock = new BundleMock("scooby.doo", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); InitialContext ctx = new InitialContext(); ctx.lookup("osgi:service/java.lang.Runnable"); }
@Test(expected=NameNotFoundException.class) public void testLookupForServiceWeNeverHad() throws NamingException { BundleMock mock = new BundleMock("scooby.doo", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); InitialContext ctx = new InitialContext(); ctx.lookup("osgi:service/java.lang.Integer"); }
@Test public void checkProxyDynamism() throws NamingException { BundleMock mock = new BundleMock("scooby.doo", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); InitialContext ctx = new InitialContext(); String className = Runnable.class.getName(); Runnable t = Skeleton.newMock(Runnable.class); Runnable t2 = Skeleton.newMock(Runnable.class); reg.unregister(); ServiceRegistration reg = bc.registerService(className, t, null); bc.registerService(className, t2, null); Runnable r = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable"); r.run(); Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1); Skeleton.getSkeleton(t2).assertNotCalled(new MethodCall(Runnable.class, "run")); reg.unregister(); r.run(); Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1); Skeleton.getSkeleton(t2).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1); }
@Test public void checkServiceListLookup() throws NamingException { BundleMock mock = new BundleMock("scooby.doo", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); InitialContext ctx = new InitialContext(); String className = Runnable.class.getName(); Runnable t = Skeleton.newMock(Runnable.class); reg.unregister(); ServiceRegistration reg = bc.registerService(className, t, null); ServiceRegistration reg2 = bc.registerService("java.lang.Thread", new Thread(), null); Context ctx2 = (Context) ctx.lookup("osgi:servicelist/java.lang.Runnable"); Runnable r = (Runnable) ctx2.lookup(String.valueOf(reg.getReference().getProperty(Constants.SERVICE_ID))); r.run(); Skeleton.getSkeleton(t).assertCalled(new MethodCall(Runnable.class, "run")); reg.unregister(); try { r.run(); fail("Should have received a ServiceException"); } catch (ServiceException e) { assertEquals("service exception has the wrong type", ServiceException.UNREGISTERED, e.getType()); } try { ctx2.lookup(String.valueOf(reg2.getReference().getProperty(Constants.SERVICE_ID))); fail("Expected a NameNotFoundException"); } catch (NameNotFoundException e) { } }
@Test(expected=ServiceException.class) public void checkProxyWhenServiceGoes() throws ServiceException, NamingException { BundleMock mock = new BundleMock("scooby.doo", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); InitialContext ctx = new InitialContext(); Runnable r = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable"); r.run(); Skeleton.getSkeleton(service).assertCalled(new MethodCall(Runnable.class, "run")); reg.unregister(); r.run(); }
@Test public void checkServiceOrderObserved() throws NamingException { BundleMock mock = new BundleMock("scooby.doo", new Properties()); Thread.currentThread().setContextClassLoader(mock.getClassLoader()); InitialContext ctx = new InitialContext(); String className = Runnable.class.getName(); Runnable t = Skeleton.newMock(Runnable.class); Runnable t2 = Skeleton.newMock(Runnable.class); reg.unregister(); ServiceRegistration reg = bc.registerService(className, t, null); ServiceRegistration reg2 = bc.registerService(className, t2, null); Runnable r = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable"); r.run(); Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1); Skeleton.getSkeleton(t2).assertNotCalled(new MethodCall(Runnable.class, "run")); reg.unregister(); reg2.unregister(); Hashtable<String, Object> props = new Hashtable<String, Object>(); props.put(Constants.SERVICE_RANKING, 55); t = Skeleton.newMock(Runnable.class); t2 = Skeleton.newMock(Runnable.class); bc.registerService(className, t, null); bc.registerService(className, t2, props); r = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable"); r.run(); Skeleton.getSkeleton(t).assertNotCalled(new MethodCall(Runnable.class, "run")); Skeleton.getSkeleton(t2).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1); }