OsgiInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (Object.class.getMethod("equals", Object.class).equals(method)) { return proxy == args[0]; } if (Object.class.getMethod("hashCode").equals(method)) { return System.identityHashCode(proxy); } if (OsgiProxy.class.getMethod("getReference").equals(method)) { return getReference(); } if (method.getDeclaringClass() == ServiceReference.class) { return method.invoke(reference, args); } Object service = bundleContext.getService(reference); if(service == null) { throw new IllegalStateException( String.format("There is no service registered for reference [%s]", reference)); } try { return method.invoke(service, args); } finally { bundleContext.ungetService(reference); } } OsgiInvocationHandler(BundleContext bundleContext, ServiceReference reference); @Override Object invoke(Object proxy, Method method, Object[] args); BundleContext getBundleContext(); ServiceReference getReference();  }
@Test public void testInvoke() throws Throwable { List<?> service = new ArrayList<Object>(); ServiceReference reference = mock(ServiceReference.class); BundleContext bundleContext = mock(BundleContext.class); when(bundleContext.getService(same(reference))).thenReturn(service); OsgiInvocationHandler handler = new OsgiInvocationHandler(bundleContext, reference); handler.invoke(reference, List.class.getMethod("add", Object.class), new Object[] {"test"}); verify(bundleContext).getService(same(reference)); verify(bundleContext).ungetService(same(reference)); assertThat((String) service.get(0), equalTo("test")); }
@Test public void testInvokeEquals() throws Throwable { List<?> service = new ArrayList<Object>(); ServiceReference reference = mock(ServiceReference.class); BundleContext bundleContext = mock(BundleContext.class); when(bundleContext.getService(same(reference))).thenReturn(service); OsgiInvocationHandler handler = new OsgiInvocationHandler(bundleContext, reference); Object answer = handler.invoke(reference, Object.class.getMethod("equals", Object.class), new Object[]{reference}); assertThat((Boolean) answer, equalTo(true)); }
@Test public void testInvokeHashCode() throws Throwable { List<?> service = new ArrayList<Object>(); ServiceReference reference = mock(ServiceReference.class); BundleContext bundleContext = mock(BundleContext.class); when(bundleContext.getService(same(reference))).thenReturn(service); OsgiInvocationHandler handler = new OsgiInvocationHandler(bundleContext, reference); Object answer = handler.invoke(reference, Object.class.getMethod("hashCode"), new Object[]{reference}); assertThat((Integer) answer, equalTo(reference.hashCode())); }
@Test public void testInvokeGetReference() throws Throwable { List<?> service = new ArrayList<Object>(); ServiceReference reference = mock(ServiceReference.class); BundleContext bundleContext = mock(BundleContext.class); when(bundleContext.getService(same(reference))).thenReturn(service); OsgiInvocationHandler handler = new OsgiInvocationHandler(bundleContext, reference); Object answer = handler.invoke(reference, OsgiProxy.class.getMethod("getReference"), new Object[0]); assertThat((ServiceReference) answer, sameInstance(reference)); }
@Test(expected = IllegalStateException.class) public void testInvokeNoService() throws Throwable { ServiceReference reference = mock(ServiceReference.class); BundleContext bundleContext = mock(BundleContext.class); OsgiInvocationHandler handler = new OsgiInvocationHandler(bundleContext, reference); handler.invoke(reference, List.class.getMethod("add", Object.class), new Object[]{"test"}); }