* @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.InstanceManager, org.apache.felix.ipojo.metadata.Element,
* java.util.Dictionary)
*/
public void configure(Element componentMetadata, Dictionary configuration) throws ConfigurationException {
// getPojoMetadata();
PojoMetadata manipulation = getFactory().getPojoMetadata();
boolean atLeastOneField = false;
// Create the dependency according to the component metadata
Element[] deps = componentMetadata.getElements("Requires");
// Get instance filters.
Dictionary filtersConfiguration = getRequiresFilters(configuration.get("requires.filters"));
Dictionary fromConfiguration = (Dictionary) configuration.get("requires.from");
for (int i = 0; deps != null && i < deps.length; i++) {
// Create the dependency metadata
String field = deps[i].getAttribute("field");
String serviceSpecification = deps[i].getAttribute("interface");
// the 'interface' attribute is deprecated
if (serviceSpecification != null) {
warn("The 'interface' attribute is deprecated, use the 'specification' attribute instead");
} else {
serviceSpecification = deps[i].getAttribute("specification");
}
String filter = deps[i].getAttribute("filter");
String opt = deps[i].getAttribute("optional");
boolean optional = opt != null && opt.equalsIgnoreCase("true");
String defaultImplem = deps[i].getAttribute("default-implementation");
String agg = deps[i].getAttribute("aggregate");
boolean aggregate = agg != null && agg.equalsIgnoreCase("true");
String identitity = deps[i].getAttribute("id");
String nul = deps[i].getAttribute("nullable");
boolean nullable = nul == null || nul.equalsIgnoreCase("true");
boolean isProxy = true;
// Detect proxy default value.
String setting = getInstanceManager().getContext().getProperty(PROXY_SETTINGS_PROPERTY);
if (setting == null || PROXY_ENABLED.equals(setting)) { // If not set => Enabled
isProxy = true;
} else if (setting != null && PROXY_DISABLED.equals(setting)) {
isProxy = false;
}
String proxy = deps[i].getAttribute("proxy");
// If proxy == null, use default value
if (proxy != null) {
if (proxy.equals("false")) {
isProxy = false;
} else if (proxy.equals("true")) {
if (! isProxy) { // The configuration overrides the system setting
warn("The configuration of a service dependency overrides the proxy mode");
}
isProxy = true;
}
}
String scope = deps[i].getAttribute("scope");
BundleContext context = getInstanceManager().getContext(); // Get the default bundle context.
if (scope != null) {
// If we are not in a composite, the policy is set to global.
if (scope.equalsIgnoreCase("global") || ((((IPojoContext) getInstanceManager().getContext()).getServiceContext()) == null)) {
context =
new PolicyServiceContext(getInstanceManager().getGlobalContext(), getInstanceManager().getLocalServiceContext(),
PolicyServiceContext.GLOBAL);
} else if (scope.equalsIgnoreCase("composite")) {
context =
new PolicyServiceContext(getInstanceManager().getGlobalContext(), getInstanceManager().getLocalServiceContext(),
PolicyServiceContext.LOCAL);
} else if (scope.equalsIgnoreCase("composite+global")) {
context =
new PolicyServiceContext(getInstanceManager().getGlobalContext(), getInstanceManager().getLocalServiceContext(),
PolicyServiceContext.LOCAL_AND_GLOBAL);
}
}
// Get instance filter if available
if (filtersConfiguration != null && identitity != null && filtersConfiguration.get(identitity) != null) {
filter = (String) filtersConfiguration.get(identitity);
}
// Compute the 'from' attribute
String from = deps[i].getAttribute("from");
if (fromConfiguration != null && identitity != null && fromConfiguration.get(identitity) != null) {
from = (String) fromConfiguration.get(identitity);
}
if (from != null) {
String fromFilter = "(|(instance.name=" + from + ")(service.pid=" + from + "))";
if (aggregate) {
warn("The 'from' attribute is incompatible with aggregate requirements: only one provider will match : " + fromFilter);
}
if (filter != null) {
filter = "(&" + fromFilter + filter + ")"; // Append the two filters
} else {
filter = fromFilter;
}
}
Filter fil = null;
if (filter != null) {
try {
fil = getInstanceManager().getContext().createFilter(filter);
} catch (InvalidSyntaxException e) {
throw new ConfigurationException("A requirement filter is invalid : " + filter + " - " + e.getMessage());
}
}
Class spec = null;
if (serviceSpecification != null) {
spec = DependencyModel.loadSpecification(serviceSpecification, getInstanceManager().getContext());
}
int policy = DependencyModel.getPolicy(deps[i]);
Comparator cmp = DependencyModel.getComparator(deps[i], getInstanceManager().getGlobalContext());
Dependency dep = new Dependency(this, field, spec, fil, optional, aggregate, nullable, isProxy, identitity, context, policy, cmp, defaultImplem);
// Look for dependency callback :
Element[] cbs = deps[i].getElements("Callback");
for (int j = 0; cbs != null && j < cbs.length; j++) {
if (!cbs[j].containsAttribute("method") && cbs[j].containsAttribute("type")) {
throw new ConfigurationException("Requirement Callback : a dependency callback must contain a method and a type (bind or unbind) attribute");
}
String method = cbs[j].getAttribute("method");
String type = cbs[j].getAttribute("type");
int methodType = 0;
if ("bind".equalsIgnoreCase(type)) {
methodType = DependencyCallback.BIND;
} else if ("modified".equalsIgnoreCase(type)) {
methodType = DependencyCallback.MODIFIED;
} else {
methodType = DependencyCallback.UNBIND;
}
DependencyCallback callback = new DependencyCallback(dep, method, methodType);
dep.addDependencyCallback(callback);
}
// Add the constructor parameter if needed
String paramIndex = deps[i].getAttribute("constructor-parameter");
if (paramIndex != null) {
int index = Integer.parseInt(paramIndex);
dep.addConstructorInjection(index);
}
// Check the dependency :
if (checkDependency(dep, manipulation)) {
addDependency(dep);
if (dep.getField() != null) {
getInstanceManager().register(manipulation.getField(dep.getField()), dep);
atLeastOneField = true;
}
}
}
if (atLeastOneField) { // Does register only if we have fields
MethodMetadata[] methods = manipulation.getMethods();
for (int i = 0; i < methods.length; i++) {
for (int j = 0; j < m_dependencies.length; j++) {
getInstanceManager().register(methods[i], m_dependencies[j]);
}
}