use of org.apache.felix.ipojo.FieldInterceptor in project felix by apache.
the class BundleContextHandler method configure.
/**
* Configures the handler.
* This method collects all `context` element.
*
* @param metadata the metadata of the component
* @param configuration the instance configuration
* @throws org.apache.felix.ipojo.ConfigurationException if the metadata are not correct.
*/
@Override
public void configure(Element metadata, Dictionary configuration) throws ConfigurationException {
Element[] contexts = metadata.getElements("context");
for (Element element : contexts) {
BundleContext bc = getBundleContextForConfiguration(element);
if (element.containsAttribute("constructor-parameter")) {
String idx = element.getAttribute("constructor-parameter");
int index = Integer.parseInt(idx);
final BundleContext injected = bc;
getLogger().log(Log.DEBUG, "Registering bundle context injection for index " + index + " for instance" + " " + getInstanceManager().getInstanceName());
getInstanceManager().register(index, new ConstructorInjector() {
public Object getConstructorParameter(int index) {
return injected;
}
public Class getConstructorParameterType(int index) {
return BundleContext.class;
}
});
} else if (element.containsAttribute("field")) {
String field = element.getAttribute("field");
final BundleContext injected = bc;
FieldMetadata fm = getFactory().getPojoMetadata().getField(field);
if (fm == null) {
throw new ConfigurationException("Cannot inject the bundle context in the field " + field + " - " + "reason: the field does not exist in " + getInstanceManager().getClassName());
}
if (!BundleContext.class.getName().equals(fm.getFieldType())) {
throw new ConfigurationException("Cannot inject the bundle context in the field " + field + " - " + "reason: the field " + field + " from " + getInstanceManager().getClassName() + " is not " + "from the BundleContext type");
}
getInstanceManager().register(fm, new FieldInterceptor() {
public void onSet(Object pojo, String fieldName, Object value) {
// Do nothing.
}
public Object onGet(Object pojo, String fieldName, Object value) {
return injected;
}
});
} else if (element.containsAttribute("method")) {
String method = element.getAttribute("method");
MethodMetadata mm = getFactory().getPojoMetadata().getMethod(method, new String[] { BundleContext.class.getName() });
if (mm == null) {
getLogger().log(Log.WARNING, "Cannot find the method " + method + " in the class " + getInstanceManager().getClassName() + ", super classes lookup will be attempted");
}
Callback callback = new Callback(method, new Class[] { BundleContext.class }, false, getInstanceManager());
m_methods.add(new BundleCallback(callback, bc));
}
}
}
Aggregations