Skip to content

Commit

Permalink
Continue refactoring and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
arcane86 authored and alesj committed Oct 12, 2012
1 parent 647d7ba commit 58db0c5
Show file tree
Hide file tree
Showing 7 changed files with 607 additions and 582 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.environment.osgi.impl.extension;
package org.jboss.weld.environment.osgi.impl.annotation;

import org.jboss.weld.environment.osgi.api.annotation.OSGiService;

import javax.enterprise.util.AnnotationLiteral;

public class OSGiServiceQualifier
extends AnnotationLiteral<OSGiService>
implements OSGiService {
/**
* This is a wrapper for the annotation {@link OSGiService}.
* <p/>
* @author Mathieu ANCELIN - SERLI (mathieu.ancelin@serli.com)
* @author Matthieu CLOCHARD - SERLI (matthieu.clochard@serli.com)
*/
public class OSGiServiceAnnotation extends AnnotationLiteral<OSGiService>
implements OSGiService {

private final int timeout;

public OSGiServiceQualifier(int timeout) {
public OSGiServiceAnnotation(int timeout) {
this.timeout = timeout;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.environment.osgi.impl.extension.beans;
package org.jboss.weld.environment.osgi.impl.extension;

import org.jboss.weld.environment.osgi.api.annotation.Filter;
import org.jboss.weld.environment.osgi.impl.extension.service.WeldOSGiExtension;
Expand All @@ -29,13 +29,14 @@
import java.util.Set;

/**
* Handler for OSGi dynamic service in use by
* {@link org.osgi.cdi.impl.extension.OSGiServiceBean}.
*
* Handler for proxy used by {@link OSGiServiceBean}. Dynamicaly lookup for a
* matching OSGi service at method call.
* <p/>
* @author Mathieu ANCELIN - SERLI (mathieu.ancelin@serli.com)
* @author Matthieu CLOCHARD - SERLI (matthieu.clochard@serli.com)
*/
public class DynamicServiceHandler implements InvocationHandler {

private static Logger logger = LoggerFactory.getLogger(
DynamicServiceHandler.class);

Expand All @@ -44,7 +45,8 @@ public class DynamicServiceHandler implements InvocationHandler {
private final String name;

private Filter filter;
// private final ServiceTracker tracker;

/*private final ServiceTracker tracker;*/

private final long timeout;

Expand All @@ -57,36 +59,36 @@ public DynamicServiceHandler(Bundle bundle,
Filter filter,
Set<Annotation> qualifiers,
long timeout) {
logger.debug("Creation of a new DynamicServiceHandler for bundle {} "
+ "as a {} with filter {}",
new Object[]
{
bundle, name, filter.value()
});
logger.trace("Entering DynamicServiceHandler : "
+ "DynamicServiceHandler() with parameter {} | {} | {} | {} | {}",
new Object[] {bundle, name, filter, qualifiers, timeout});
this.bundle = bundle;
this.name = name;
this.filter = filter;
this.timeout = timeout;
this.qualifiers = qualifiers;
// try {
// if (filter != null && filter.value() != null && filter.value().length() > 0) {
// this.tracker = new ServiceTracker(bundle.getBundleContext(),
// bundle.getBundleContext().createFilter(
// "(&(objectClass=" + name + ")" + filter.value() + ")"),
// null);
// } else {
// this.tracker = new ServiceTracker(bundle.getBundleContext(), name, null);
// }
// } catch (Exception e) {
// logger.error("Unable to create the DynamicServiceHandler.",e);
// throw new RuntimeException(e);
// }
// this.tracker.open();
/* ServiceTracker usage, currently fails
try {
if (filter != null && filter.value() != null && filter.value().length() > 0) {
this.tracker = new ServiceTracker(bundle.getBundleContext(),
bundle.getBundleContext().createFilter(
"(&(objectClass=" + name + ")" + filter.value() + ")"),
null);
} else {
this.tracker = new ServiceTracker(bundle.getBundleContext(), name, null);
}
} catch (Exception e) {
logger.error("Unable to create the DynamicServiceHandler.",e);
throw new RuntimeException(e);
}
this.tracker.open();
*/
logger.debug("New DynamicServiceHandler constructed {}", this);
}

public void closeHandler() {
// this.tracker.close();
}
/*public void closeHandler() {
this.tracker.close();
}*/

public void setStored(boolean stored) {
this.stored = stored;
Expand All @@ -99,46 +101,54 @@ public Object invoke(Object proxy, Method method, Object[] args)
this,
method);
WeldOSGiExtension.currentBundle.set(bundle.getBundleId());
if (!stored && method.getName().equals("hashCode")) { //intercept hashCode method
//intercept HashCode method when the handler is not allready registered
//map.put() need a correct hashCode() method to use
//see OSGiServiceBean
if (!stored && method.getName().equals("hashCode")) {
int result = name.hashCode();
result = 31 * result + filter.value().hashCode();
result = 31 * result + qualifiers.hashCode();
result = 31 * result + (int) timeout;
return result;
}
ServiceReference reference = null;
if (filter != null && filter.value() != null && filter.value().length() > 0) {
if (filter != null &&
filter.value() != null &&
filter.value().length() > 0) {
ServiceReference[] refs =
bundle.getBundleContext()
.getServiceReferences(name, filter.value());
bundle.getBundleContext().getServiceReferences(name, filter.value());
if (refs != null && refs.length > 0) {
reference = refs[0];
}
} else {
}
else {
reference = bundle.getBundleContext().getServiceReference(name);
}
if (reference == null) {
throw new IllegalStateException("Can't call service "
+ name
+ ". No matching service found.");
+ name
+ ". No matching service found.");
}
Object instanceToUse = bundle.getBundleContext().getService(reference);
try {
return method.invoke(instanceToUse, args);
} catch (Throwable t) {
}
catch(Throwable t) {
throw new RuntimeException(t);
} finally {
}
finally {
bundle.getBundleContext().ungetService(reference);
WeldOSGiExtension.currentBundle.remove();
}
// Object instanceToUse = this.tracker.waitForService(timeout);
// try {
// return method.invoke(instanceToUse, args);
// } catch(Throwable t) {
// logger.error("Unable to find a matching service for {} with filter {} due to {}", new Object[] {name, filter.value(), t});
// throw new RuntimeException(t);
// } finally {
// WeldOSGiExtension.currentBundle.remove();
// }
/*Object instanceToUse = this.tracker.waitForService(timeout);
try {
return method.invoke(instanceToUse, args);
} catch(Throwable t) {
logger.error("Unable to find a matching service for {} with filter {} due to {}", new Object[] {name, filter.value(), t});
throw new RuntimeException(t);
} finally {
WeldOSGiExtension.currentBundle.remove();
}*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.jboss.weld.environment.osgi.impl.extension;

import org.jboss.weld.environment.osgi.impl.annotation.OSGiServiceAnnotation;
import org.jboss.weld.environment.osgi.api.annotation.Filter;
import org.jboss.weld.environment.osgi.api.annotation.OSGiService;
import org.jboss.weld.environment.osgi.api.annotation.Required;
Expand Down Expand Up @@ -55,7 +56,7 @@ public OSGIServiceAnnotatedParameter(AnnotatedParameter parameter) {
filter = FilterGenerator.makeFilter(parameter.getAnnotations());
annotations.add(filter);
//annotations.add(new AnnotationLiteral<OSGiService>() {});
annotations.add(new OSGiServiceQualifier(
annotations.add(new OSGiServiceAnnotation(
parameter.getAnnotation(OSGiService.class).value()));
if (parameter.getAnnotation(Required.class) != null) {
annotations.add(new AnnotationLiteral<Required>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.jboss.weld.environment.osgi.impl.extension;

import org.jboss.weld.environment.osgi.impl.annotation.OSGiServiceAnnotation;
import org.jboss.weld.environment.osgi.api.annotation.Filter;
import org.jboss.weld.environment.osgi.api.annotation.OSGiService;
import org.jboss.weld.environment.osgi.api.annotation.Required;
Expand Down Expand Up @@ -55,7 +56,7 @@ public OSGiServiceAnnotatedField(final AnnotatedField<? super T> field) {
filter = FilterGenerator.makeFilter(filter, field.getAnnotations());
annotations.add(filter);
//annotations.add(new AnnotationLiteral<OSGiService>() {});
annotations.add(new OSGiServiceQualifier(
annotations.add(new OSGiServiceAnnotation(
field.getJavaMember().getAnnotation(OSGiService.class).value()));
if (field.getAnnotation(Required.class) != null) {
annotations.add(new AnnotationLiteral<Required>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

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;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.slf4j.Logger;
Expand Down Expand Up @@ -46,16 +45,19 @@
* This the bean class for all beans generated from a
* {@link org.osgi.cdi.api.extension.annotation.OSGiService} annotated
* {@link InjectionPoint}.
*
* <b/>
* @author Mathieu ANCELIN - SERLI (mathieu.ancelin@serli.com)
* @author Matthieu CLOCHARD - SERLI (matthieu.clochard@serli.com)
*
* @see OSGiServiceProducerBean
*/
public class OSGiServiceBean implements Bean {

private static final Logger logger =
LoggerFactory.getLogger(OSGiServiceBean.class);
LoggerFactory.getLogger(OSGiServiceBean.class);

private final Map<Object, DynamicServiceHandler> handlers =
new HashMap<Object, DynamicServiceHandler>();
new HashMap<Object, DynamicServiceHandler>();

private final InjectionPoint injectionPoint;

Expand All @@ -68,8 +70,9 @@ public class OSGiServiceBean implements Bean {
private long timeout;

public OSGiServiceBean(InjectionPoint injectionPoint) {
logger.debug("Creation of a new OSGiServiceBean for injection point: {}",
injectionPoint);
logger.trace("Entering OSGiServiceBean : "
+ "OSGiServiceBean() with parameter {}",
new Object[] {injectionPoint});
this.injectionPoint = injectionPoint;
type = injectionPoint.getType();
qualifiers = injectionPoint.getQualifiers();
Expand All @@ -80,6 +83,7 @@ public OSGiServiceBean(InjectionPoint injectionPoint) {
break;
}
}
logger.debug("New OSGiServiceBean constructed {}", this);
}

@Override
Expand Down Expand Up @@ -135,43 +139,54 @@ public Set<InjectionPoint> getInjectionPoints() {
}

@Override
public Object create(CreationalContext ctx) {
logger.debug("Instantiation of an {}", this);
public Object create(CreationalContext creationalContext) {
logger.trace("Entering OSGiServiceBean : create() with parameter {}",
new Object[] {creationalContext});
try {
Bundle bundle = FrameworkUtil.getBundle(injectionPoint.getMember()
.getDeclaringClass());
Bundle bundle =
FrameworkUtil.getBundle(injectionPoint.getMember().getDeclaringClass());
DynamicServiceHandler handler =
new DynamicServiceHandler(bundle, ((Class) type)
.getName(), filter, qualifiers, timeout);
Object proxy = Proxy.newProxyInstance(
getClass().getClassLoader(),
new Class[]
{
getBeanClass()
},
handler);
new DynamicServiceHandler(bundle,
((Class) type).getName(),
filter,
qualifiers,
timeout);
Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(),
new Class[] {getBeanClass()},
handler);
//memorize if the handler has been allready stored
if (handlers.containsKey(proxy)) {
handler.setStored(true);
} else {
}
else {
//map.put() need a correct hashCode() method to use
//see DynamicServiceHandler
handlers.put(proxy, handler);
handler.setStored(true);
}
logger.debug("New proxy for {} created {}", this, proxy);
return proxy;
} catch (Exception e) {
}
catch(Exception e) {
logger.error("Unable to instantiate {} due to {}", this, e);
throw new CreationException(e);
}
}

@Override
public void destroy(Object instance, CreationalContext creationalContext) {
logger.trace("Entering OSGiServiceBean : "
+ "destroy() with parameter {} | {}",
new Object[] {instance, creationalContext});
// Nothing to do, services are unget after each call.
DynamicServiceHandler handler = handlers.get(instance);
if (handler != null) {
handler.closeHandler();
/* ServiceTracker usage, currently fails
handler.closeHandler();*/
handlers.remove(instance);
} else {
logger.info("Can't close tracker for bean {}", this.toString());
}
else {
logger.info("Can't close handler for bean {}", this.toString());
}
}

Expand Down Expand Up @@ -210,10 +225,10 @@ public int hashCode() {
@Override
public String toString() {
return "OSGiServiceBean ["
+ ((Class) type).getSimpleName()
+ "] with qualifiers ["
+ printQualifiers()
+ "]";
+ ((Class) type).getSimpleName()
+ "] with qualifiers ["
+ printQualifiers()
+ "]";
}

public String printQualifiers() {
Expand All @@ -240,7 +255,8 @@ private String printValues(Annotation qualifier) {
if (value != null) {
result += m.getName() + "=" + value.toString();
}
} catch (Throwable t) {
}
catch(Throwable t) {
// ignore
}
}
Expand Down
Loading

0 comments on commit 58db0c5

Please sign in to comment.