@Test(expected = IllegalArgumentException.class) public void testCreateProxyNoBundle() throws Exception { BundleContext bundleContext = mock(BundleContext.class); ServiceReference reference = mock(ServiceReference.class); OsgiDefaultProxyCreator creator = new OsgiDefaultProxyCreator(); creator.createProxy(bundleContext, reference, getClass().getClassLoader()); }
@Override @SuppressWarnings("unchecked") public <T> T createProxy(BundleContext bundleContext, ServiceReference reference, ClassLoader classLoader) { Bundle exportingBundle = reference.getBundle(); if(exportingBundle == null) { throw new IllegalArgumentException(String.format("Service [%s] has been unregistered", reference)); } InvocationHandler handler = new OsgiInvocationHandler(bundleContext, reference); String[] classNames = (String[]) reference.getProperty(Constants.OBJECTCLASS); List<Class<?>> classes = new ArrayList<Class<?>>(classNames.length); for(String className : classNames) { try { Class<?> clazz = classLoader.loadClass(className); if (clazz.isInterface()) { classes.add(clazz); } } catch (ClassNotFoundException e) { throw new IllegalArgumentException( String.format("Unable to find class [%s] with classloader [%s]", className, classLoader)); } } classes.add(OsgiProxy.class); classes.add(ServiceReference.class); return (T) Proxy.newProxyInstance(classLoader, classes.toArray(new Class<?>[classes.size()]), handler); }
OsgiDefaultProxyCreator implements OsgiProxyCreator { @Override @SuppressWarnings("unchecked") public <T> T createProxy(BundleContext bundleContext, ServiceReference reference, ClassLoader classLoader) { Bundle exportingBundle = reference.getBundle(); if(exportingBundle == null) { throw new IllegalArgumentException(String.format("Service [%s] has been unregistered", reference)); } InvocationHandler handler = new OsgiInvocationHandler(bundleContext, reference); String[] classNames = (String[]) reference.getProperty(Constants.OBJECTCLASS); List<Class<?>> classes = new ArrayList<Class<?>>(classNames.length); for(String className : classNames) { try { Class<?> clazz = classLoader.loadClass(className); if (clazz.isInterface()) { classes.add(clazz); } } catch (ClassNotFoundException e) { throw new IllegalArgumentException( String.format("Unable to find class [%s] with classloader [%s]", className, classLoader)); } } classes.add(OsgiProxy.class); classes.add(ServiceReference.class); return (T) Proxy.newProxyInstance(classLoader, classes.toArray(new Class<?>[classes.size()]), handler); } }
OsgiDefaultProxyCreator implements OsgiProxyCreator { @Override @SuppressWarnings("unchecked") public <T> T createProxy(BundleContext bundleContext, ServiceReference reference, ClassLoader classLoader) { Bundle exportingBundle = reference.getBundle(); if(exportingBundle == null) { throw new IllegalArgumentException(String.format("Service [%s] has been unregistered", reference)); } InvocationHandler handler = new OsgiInvocationHandler(bundleContext, reference); String[] classNames = (String[]) reference.getProperty(Constants.OBJECTCLASS); List<Class<?>> classes = new ArrayList<Class<?>>(classNames.length); for(String className : classNames) { try { Class<?> clazz = classLoader.loadClass(className); if (clazz.isInterface()) { classes.add(clazz); } } catch (ClassNotFoundException e) { throw new IllegalArgumentException( String.format("Unable to find class [%s] with classloader [%s]", className, classLoader)); } } classes.add(OsgiProxy.class); classes.add(ServiceReference.class); return (T) Proxy.newProxyInstance(classLoader, classes.toArray(new Class<?>[classes.size()]), handler); }  }
OsgiDefaultProxyCreator implements OsgiProxyCreator { @Override @SuppressWarnings("unchecked") public <T> T createProxy(BundleContext bundleContext, ServiceReference reference, ClassLoader classLoader) { Bundle exportingBundle = reference.getBundle(); if(exportingBundle == null) { throw new IllegalArgumentException(String.format("Service [%s] has been unregistered", reference)); } InvocationHandler handler = new OsgiInvocationHandler(bundleContext, reference); String[] classNames = (String[]) reference.getProperty(Constants.OBJECTCLASS); List<Class<?>> classes = new ArrayList<Class<?>>(classNames.length); for(String className : classNames) { try { Class<?> clazz = classLoader.loadClass(className); if (clazz.isInterface()) { classes.add(clazz); } } catch (ClassNotFoundException e) { throw new IllegalArgumentException( String.format("Unable to find class [%s] with classloader [%s]", className, classLoader)); } } classes.add(OsgiProxy.class); classes.add(ServiceReference.class); return (T) Proxy.newProxyInstance(classLoader, classes.toArray(new Class<?>[classes.size()]), handler); }  @Override @SuppressWarnings("unchecked") T createProxy(BundleContext bundleContext, ServiceReference reference, ClassLoader classLoader); }
OsgiDefaultProxyCreator implements OsgiProxyCreator { @Override @SuppressWarnings("unchecked") public <T> T createProxy(BundleContext bundleContext, ServiceReference reference, ClassLoader classLoader) { Bundle exportingBundle = reference.getBundle(); if(exportingBundle == null) { throw new IllegalArgumentException(String.format("Service [%s] has been unregistered", reference)); } InvocationHandler handler = new OsgiInvocationHandler(bundleContext, reference); String[] classNames = (String[]) reference.getProperty(Constants.OBJECTCLASS); List<Class<?>> classes = new ArrayList<Class<?>>(classNames.length); for(String className : classNames) { try { Class<?> clazz = classLoader.loadClass(className); if (clazz.isInterface()) { classes.add(clazz); } } catch (ClassNotFoundException e) { throw new IllegalArgumentException( String.format("Unable to find class [%s] with classloader [%s]", className, classLoader)); } } classes.add(OsgiProxy.class); classes.add(ServiceReference.class); return (T) Proxy.newProxyInstance(classLoader, classes.toArray(new Class<?>[classes.size()]), handler); }  @Override @SuppressWarnings("unchecked") T createProxy(BundleContext bundleContext, ServiceReference reference, ClassLoader classLoader);  }