* @param dictionary the instance configuration.
* @throws ConfigurationException if the dependency is not configured correctly
* @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
*/
public void configure(Element meta, Dictionary dictionary) throws ConfigurationException {
PojoMetadata manipulation = getFactory().getPojoMetadata();
Element[] deps = meta.getElements("requires", NAMESPACE);
// Also check with temporal is no requires.
if (deps == null || deps.length == 0) {
deps = meta.getElements("temporal", NAMESPACE);
}
for (int i = 0; i < deps.length; i++) {
if (!deps[i].containsAttribute("field") || m_dependencies.contains(deps[i].getAttribute("field"))) {
error("One temporal dependency must be attached to a field or the field is already used");
return;
}
String field = deps[i].getAttribute("field");
FieldMetadata fieldmeta = manipulation.getField(field);
if (fieldmeta == null) {
error("The field " + field + " does not exist in the class " + getInstanceManager().getClassName());
return;
}
String fil = deps[i].getAttribute("filter");
Filter filter = null;
if (fil != null) {
try {
filter = getInstanceManager().getContext().createFilter(fil);
} catch (InvalidSyntaxException e) {
error("Cannot create the field from " + fil + ": " + e.getMessage());
return;
}
}
boolean agg = false;
boolean collection = false;
String spec = fieldmeta.getFieldType();
if (spec.endsWith("[]")) {
agg = true;
spec = spec.substring(0, spec.length() - 2);
} else if (Collection.class.getName().equals(spec)) {
agg = true;
collection = true;
// Collection detected. Check for the specification attribute
spec = deps[i].getAttribute("specification");
if (spec == null) {
error("A dependency injected inside a Collection must contain the 'specification' attribute");
}
}
String prox = deps[i].getAttribute("proxy");
//boolean proxy = prox != null && prox.equals("true");
// Use proxy by default except for array:
boolean proxy = prox == null || prox.equals("true");
if (prox == null && proxy) { // Proxy set because of the default.
if (agg && ! collection) { // Aggregate and array
proxy = false;
}
}
if (proxy && agg) {
if (! collection) {
error("Proxied aggregate temporal dependencies cannot be an array. Only collections are supported");
}
}
long timeout = DEFAULT_TIMEOUT;
if (deps[i].containsAttribute("timeout")) {
String to = deps[i].getAttribute("timeout");
if (to.equalsIgnoreCase("infinite") || to.equalsIgnoreCase("-1")) {
timeout = Long.MAX_VALUE; // Infinite wait time ...
} else {
timeout = new Long(deps[i].getAttribute("timeout")).longValue();
}
}
int policy = NO_POLICY;
String di = null;
String onTimeout = deps[i].getAttribute("onTimeout");
if (onTimeout != null) {
if (onTimeout.equalsIgnoreCase("nullable")) {
policy = NULLABLE;
} else if (onTimeout.equalsIgnoreCase("empty-array") || onTimeout.equalsIgnoreCase("empty")) {
policy = EMPTY;
if (! agg) {
// The empty array policy can only be used on aggregate dependencies
error("Cannot use the empty array policy for " + field + " : non aggregate dependency.");
}
} else if (onTimeout.equalsIgnoreCase("null")) {
policy = NULL;
} else if (onTimeout.length() > 0) {
di = onTimeout;
policy = DEFAULT_IMPLEMENTATION;
}
}
Class specification = DependencyModel.loadSpecification(spec, getInstanceManager().getContext());
TemporalDependency dep = new TemporalDependency(specification, agg, collection, proxy, filter, getInstanceManager().getContext(), timeout, policy, di, this);
m_dependencies.add(dep);
if (! proxy) { // Register method interceptor only if are not a proxy
MethodMetadata[] methods = manipulation.getMethods();
for (int k = 0; k < methods.length; k++) {
getInstanceManager().register(methods[k], dep);
}
}