in utils/src/main/java/org/jclouds/karaf/utils/ServiceHelper.java [161:183]
public static <T> Context toContext(T service) {
Context ctx = null;
Class c = service.getClass();
try {
//Ugly way to get the Context, but there doesn't seem to be a better one.
Method m = c.getMethod("getContext");
Object obj = m.invoke(service);
if (Context.class.isAssignableFrom(obj.getClass())) {
ctx = (Context) obj;
} else if (View.class.isAssignableFrom(obj.getClass())) {
ctx = ((View) obj).unwrap();
} else {
throw new IllegalArgumentException("Service doesn't have a context.");
}
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(e);
} catch (InvocationTargetException e) {
throw new IllegalArgumentException(e);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
return ctx;
}