use of org.apache.felix.ipojo.util.Callback in project felix by apache.
the class ConfigurationHandler method configure.
/**
* Configures the handler.
* Access to field does not require synchronization as this method is executed
* before any thread access to this object.
*
* @param metadata the metadata of the component
* @param configuration the instance configuration
* @throws ConfigurationException one property metadata is not correct
* @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
*/
public void configure(Element metadata, Dictionary configuration) throws ConfigurationException {
// Build the map
Element[] confs = metadata.getElements("Properties", "");
Element[] configurables = confs[0].getElements("Property");
// Check if the component is dynamically configurable
// Propagation enabled by default.
m_mustPropagate = true;
// We must create a copy as the Config Admin dictionary has some limitations
m_toPropagate = new Hashtable<String, Object>();
if (configuration != null) {
Enumeration keys = configuration.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
// we don't propagate properties starting with .
if (!excluded(key)) {
m_toPropagate.put(key, configuration.get(key));
}
}
}
String propa = confs[0].getAttribute("propagation");
if (propa != null && propa.equalsIgnoreCase("false")) {
m_mustPropagate = false;
m_toPropagate = null;
}
// Check if the component support ConfigurationADmin reconfiguration
// Look inside the component type description
m_managedServicePID = confs[0].getAttribute("pid");
// Look inside the instance configuration.
String instanceMSPID = (String) configuration.get(MANAGED_SERVICE_PID);
if (instanceMSPID != null) {
m_managedServicePID = instanceMSPID;
}
// updated method
String upd = confs[0].getAttribute("updated");
if (upd != null) {
MethodMetadata method = getPojoMetadata().getMethod(upd);
if (method == null) {
throw new ConfigurationException("The updated method is not found in the class " + getInstanceManager().getClassName());
} else if (method.getMethodArguments().length == 0) {
m_updated = new Callback(upd, new Class[0], false, getInstanceManager());
} else if (method.getMethodArguments().length == 1 && method.getMethodArguments()[0].equals(Dictionary.class.getName())) {
m_updated = new Callback(upd, new Class[] { Dictionary.class }, false, getInstanceManager());
} else {
throw new ConfigurationException("The updated method is found in the class " + getInstanceManager().getClassName() + " must have either no argument or a Dictionary");
}
}
for (int i = 0; configurables != null && i < configurables.length; i++) {
String fieldName = configurables[i].getAttribute("field");
String methodName = configurables[i].getAttribute("method");
String paramIndex = configurables[i].getAttribute("constructor-parameter");
int index = -1;
// The initialize method has fixed the property name.
String name = configurables[i].getAttribute("name");
String value = configurables[i].getAttribute("value");
// The initialize method has fixed the property name.
String type = configurables[i].getAttribute("type");
Property prop;
if (paramIndex == null) {
prop = new Property(name, fieldName, methodName, value, type, getInstanceManager(), this);
} else {
index = Integer.parseInt(paramIndex);
prop = new Property(name, fieldName, methodName, index, value, type, getInstanceManager(), this);
}
addProperty(prop);
// Check if the instance configuration contains value for the current property :
if (configuration.get(name) == null) {
if (fieldName != null && configuration.get(fieldName) != null) {
prop.setValue(configuration.get(fieldName));
}
} else {
prop.setValue(configuration.get(name));
}
if (fieldName != null) {
FieldMetadata field = new FieldMetadata(fieldName, type);
getInstanceManager().register(field, prop);
}
if (index != -1) {
getInstanceManager().register(index, prop);
}
}
m_description = new ConfigurationHandlerDescription(this, m_configurableProperties, m_managedServicePID);
}
use of org.apache.felix.ipojo.util.Callback 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