Skip to content

Commit

Permalink
[WELD-1170]; allow non-interfaces to be exposed as injectable osgi se…
Browse files Browse the repository at this point in the history
…rvices.
  • Loading branch information
alesj committed Jan 30, 2013
1 parent 218ee50 commit 1fc59f6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
Expand Up @@ -20,6 +20,8 @@
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -35,6 +37,9 @@
import javax.enterprise.util.AnnotationLiteral;
import javax.enterprise.util.Nonbinding;

import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;
import org.jboss.weld.environment.osgi.api.annotation.Filter;
import org.jboss.weld.environment.osgi.api.annotation.OSGiService;
import org.jboss.weld.environment.osgi.impl.extension.beans.DynamicServiceHandler;
Expand All @@ -50,6 +55,7 @@
* <b/>
* @author Mathieu ANCELIN - SERLI (mathieu.ancelin@serli.com)
* @author Matthieu CLOCHARD - SERLI (matthieu.clochard@serli.com)
* @author Ales JUSTIN -- RED HAT (ales.justin@jboss.org)
*
* @see OSGiServiceProducerBean
*/
Expand Down Expand Up @@ -158,9 +164,9 @@ public Object create(CreationalContext creationalContext) {
filter,
qualifiers,
timeout);
Object proxy = Proxy.newProxyInstance(getBeanClass().getClassLoader(),
new Class[] {getBeanClass()},
handler);

Object proxy = createProxy(getBeanClass(), handler);

//memorize if the handler has been allready stored
if (handlers.containsKey(proxy)) {
handler.setStored(true);
Expand Down Expand Up @@ -270,4 +276,49 @@ private String printValues(Annotation qualifier) {
return result.equals("()") ? "" : result;
}

@SuppressWarnings("unchecked")
public static Object createProxy(Class<?> clazz, DynamicServiceHandler handler) throws Exception {
if (clazz.isInterface()) {
return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, handler);
} else {
final ProxyFactory factory = new ProxyFactory();
factory.setFilter(FINALIZE_FILTER);
factory.setSuperclass(clazz); // expose impl
// ProxyFactory already caches classes
Class<?> proxyClass = getProxyClass(factory);
ProxyObject proxyObject = (ProxyObject) proxyClass.newInstance();
proxyObject.setHandler(handler);
return proxyObject;
}
}

protected static Class<?> getProxyClass(ProxyFactory factory) {
SecurityManager sm = System.getSecurityManager();
if (sm == null)
return factory.createClass();
else
return AccessController.doPrivileged(new ClassCreator(factory));
}

/**
* Privileged class creator.
*/
protected static class ClassCreator implements PrivilegedAction<Class<?>> {
private ProxyFactory factory;

public ClassCreator(ProxyFactory factory) {
this.factory = factory;
}

public Class<?> run() {
return factory.createClass();
}
}

private static final MethodFilter FINALIZE_FILTER = new MethodFilter() {
public boolean isHandled(Method m) {
// skip finalize methods
return !("finalize".equals(m.getName()) && m.getParameterTypes().length == 0);
}
};
}
Expand Up @@ -16,19 +16,19 @@
*/
package org.jboss.weld.environment.osgi.impl.extension.beans;

import org.jboss.weld.environment.osgi.api.annotation.Filter;
import org.jboss.weld.environment.osgi.impl.extension.service.WeldOSGiExtension;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Set;

import javassist.util.proxy.MethodHandler;
import org.jboss.weld.environment.osgi.api.annotation.Filter;
import org.jboss.weld.environment.osgi.impl.extension.OSGiServiceBean;
import org.jboss.weld.environment.osgi.impl.extension.service.WeldOSGiExtension;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Handler for proxy used by {@link OSGiServiceBean}. Dynamicaly lookup for a
Expand All @@ -38,7 +38,7 @@
* @author Mathieu ANCELIN - SERLI (mathieu.ancelin@serli.com)
* @author Matthieu CLOCHARD - SERLI (matthieu.clochard@serli.com)
*/
public class DynamicServiceHandler implements InvocationHandler {
public class DynamicServiceHandler implements InvocationHandler, MethodHandler {

private static Logger logger =
LoggerFactory.getLogger(DynamicServiceHandler.class);
Expand Down Expand Up @@ -154,4 +154,7 @@ public Object invoke(Object proxy, Method method, Object[] args)
}*/
}

public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
return invoke(self, thisMethod, args);
}
}

0 comments on commit 1fc59f6

Please sign in to comment.