use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.
the class InstanceHandler method configure.
/**
* Configure method.
* @param metadata : component type metadata.
* @param configuration : instance configuration.
* @throws ConfigurationException : occurs an instance cannot be parsed correctly.
* @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 {
m_scope = getCompositeManager().getServiceContext();
// Prepare the configuration to append.
Properties toAppend = new Properties();
Enumeration keys = configuration.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
if (!(key.equals("instance.name") || key.equals("component"))) {
// Remove instance.name and component
toAppend.put(key, configuration.get(key));
}
}
Element[] instances = metadata.getElements("instance");
m_configurations = new ManagedConfiguration[instances.length];
for (int i = 0; i < instances.length; i++) {
Properties conf = null;
try {
conf = parseInstance(instances[i]);
} catch (ParseException e) {
error("An instance cannot be parsed correctly", e);
throw new ConfigurationException("An instance cannot be parsed correctly", e);
}
Properties instanceConfiguration = new Properties();
instanceConfiguration.putAll(conf);
instanceConfiguration.putAll(toAppend);
m_configurations[i] = new ManagedConfiguration(instanceConfiguration);
}
m_description = new InstanceHandlerDescription(this, m_configurations);
}
use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.
the class ServiceDependencyHandler method createServiceInstance.
/**
* Create a Service instance object form the given Element.
* This method parse the given element and configure the service instance object.
* @param service : the Element describing the service instance
* @param conf : the configuration from the composite instance
* @throws ConfigurationException : the service instance cannot be created correctly
*/
private void createServiceInstance(Element service, Dictionary conf) throws ConfigurationException {
// Prepare the configuration to append.
Properties toAppend = new Properties();
Enumeration keys = conf.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
if (!(key.equals("instance.name") || key.equals("component"))) {
// Remove instance.name and component
toAppend.put(key, conf.get(key));
}
}
String spec = service.getAttribute("specification");
if (spec == null) {
throw new ConfigurationException("Malformed service : the specification attribute is mandatory");
}
// Cannot reinstantiate yourself
String filter = "(&(!(factory.name=" + getCompositeManager().getFactory().getComponentDescription().getName() + "))(factory.state=1))";
String givenFilter = service.getAttribute("filter");
if (givenFilter != null) {
// NOPMD
filter = "(&" + filter + givenFilter + ")";
}
Filter fil;
try {
fil = getCompositeManager().getGlobalContext().createFilter(filter);
} catch (InvalidSyntaxException e) {
throw new ConfigurationException("Malformed filter " + filter, e);
}
Properties prop = new Properties();
Element[] props = service.getElements("property");
for (int k = 0; props != null && k < props.length; k++) {
try {
InstanceHandler.parseProperty(props[k], prop);
} catch (ParseException e) {
throw new ConfigurationException("An instance configuration is invalid", e);
}
}
Properties instanceConfiguration = new Properties();
instanceConfiguration.putAll(prop);
instanceConfiguration.putAll(toAppend);
String aggregate = service.getAttribute("aggregate");
boolean agg = aggregate != null && aggregate.equalsIgnoreCase("true");
String optional = service.getAttribute("optional");
boolean opt = optional != null && optional.equalsIgnoreCase("true");
int policy = DependencyMetadataHelper.getPolicy(service);
Comparator cmp = DependencyMetadataHelper.getComparator(service, getCompositeManager().getGlobalContext());
SvcInstance inst = new SvcInstance(this, spec, instanceConfiguration, agg, opt, fil, cmp, policy);
m_instances.add(inst);
String sources = service.getAttribute("context-source");
if (sources != null) {
SourceManager source = new SourceManager(sources, filter, inst, getCompositeManager());
if (m_sources == null) {
m_sources = new ArrayList(1);
}
m_sources.add(source);
}
}
use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.
the class ServiceDependencyHandler method configure.
/**
* Configure the handler.
* @param metadata : the metadata of the component
* @param conf : the instance configuration
* @throws ConfigurationException : the specification attribute is missing
* @see CompositeHandler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
*/
public void configure(Element metadata, Dictionary conf) throws ConfigurationException {
Element[] services = metadata.getElements("subservice");
// Get instance filters
Dictionary confFilter = null;
if (conf.get("requires.filters") != null) {
confFilter = (Dictionary) conf.get("requires.filters");
}
for (int i = 0; i < services.length; i++) {
String action = services[i].getAttribute("action");
if (action == null) {
throw new ConfigurationException("The action attribute must be set to 'instantiate' or 'import'");
} else if ("instantiate".equalsIgnoreCase(action)) {
createServiceInstance(services[i], conf);
} else if ("import".equalsIgnoreCase(action)) {
createServiceImport(services[i], confFilter);
} else {
throw new ConfigurationException("Unknown action : " + action);
}
}
m_description = new ServiceInstantiatorDescription(this, m_instances, m_importers);
}
use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.
the class ServiceDependencyHandler method createServiceImport.
/**
* Create a Service importer object from the given Element.
* This method parse the given element and configure the service importer object.
* @param imp : Element describing the import
* @param confFilter : instance filter customization
* @throws ConfigurationException : the service importer cannot be created correctly
*/
private void createServiceImport(Element imp, Dictionary confFilter) throws ConfigurationException {
boolean optional = false;
boolean aggregate = false;
String specification = imp.getAttribute("specification");
if (specification == null) {
// Malformed import
error("Malformed import: the specification attribute is mandatory");
throw new ConfigurationException("Malformed import : the specification attribute is mandatory");
} else {
String opt = imp.getAttribute("optional");
optional = opt != null && opt.equalsIgnoreCase("true");
String agg = imp.getAttribute("aggregate");
aggregate = agg != null && agg.equalsIgnoreCase("true");
// Cannot import yourself
String original = "(&(objectClass=" + specification + ")(!(instance.name=" + getCompositeManager().getInstanceName() + ")))";
String filter = original;
String givenFilter = imp.getAttribute("filter");
if (givenFilter != null) {
// NOPMD
filter = "(&" + filter + givenFilter + ")";
}
String identitity = imp.getAttribute("id");
String scope = imp.getAttribute("scope");
// Get the default bundle context.
BundleContext context = getCompositeManager().getGlobalContext();
if (scope != null) {
if (scope.equalsIgnoreCase("global")) {
context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.GLOBAL);
} else if (scope.equalsIgnoreCase("composite")) {
context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.LOCAL);
} else if (scope.equalsIgnoreCase("composite+global")) {
context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.LOCAL_AND_GLOBAL);
}
}
// Configure instance filter if available
if (confFilter != null && identitity != null && confFilter.get(identitity) != null) {
filter = "(&" + original + (String) confFilter.get(identitity) + ")";
}
Filter fil = null;
if (filter != null) {
try {
fil = getCompositeManager().getGlobalContext().createFilter(filter);
} catch (InvalidSyntaxException e) {
throw new ConfigurationException("A required filter " + filter + " is malformed", e);
}
}
Comparator cmp = DependencyMetadataHelper.getComparator(imp, getCompositeManager().getGlobalContext());
Class spec = DependencyMetadataHelper.loadSpecification(specification, getCompositeManager().getGlobalContext());
int policy = DependencyMetadataHelper.getPolicy(imp);
ServiceImporter importer = new ServiceImporter(spec, fil, aggregate, optional, cmp, policy, context, identitity, this);
m_importers.add(importer);
String sources = imp.getAttribute("context-source");
if (sources != null) {
SourceManager source = new SourceManager(sources, filter, importer, getCompositeManager());
if (m_sources == null) {
m_sources = new ArrayList(1);
}
m_sources.add(source);
}
}
}
use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.
the class TemporalHandler method configure.
/**
* Configure method. Creates managed dependencies.
*
* @param meta the component type metadata.
* @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);
}
// Get instance filters.
Dictionary filtersConfiguration = getRequiresFilters(dictionary.get("temporal.filters"));
if (filtersConfiguration == null || filtersConfiguration.isEmpty()) {
// Fall back on the Requires handler configuration, if any
filtersConfiguration = getRequiresFilters(dictionary.get("requires.filters"));
}
// Get from filters if any.
Dictionary fromConfiguration = getRequiresFilters(dictionary.get("temporal.from"));
if (fromConfiguration == null || fromConfiguration.isEmpty()) {
// Fall back on the Requires handler configuration, if any
fromConfiguration = getRequiresFilters(dictionary.get("requires.from"));
}
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");
String id = field;
if (deps[i].containsAttribute("id")) {
id = deps[i].getAttribute("id");
}
FieldMetadata fieldmeta = manipulation.getField(field);
if (fieldmeta == null) {
error("The field " + field + " does not exist in the class " + getInstanceManager().getClassName());
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");
}
}
// Determine the filter
String fil = deps[i].getAttribute("filter");
// Override the filter if filter configuration if available in the instance configuration
if (filtersConfiguration != null && id != null && filtersConfiguration.get(id) != null) {
fil = (String) filtersConfiguration.get(id);
}
// Check the from attribute
String from = deps[i].getAttribute("from");
if (fromConfiguration != null && id != null && fromConfiguration.get(id) != null) {
from = (String) fromConfiguration.get(id);
}
if (from != null) {
String fromFilter = "(|(instance.name=" + from + ")(service.pid=" + from + "))";
if (agg) {
warn("The 'from' attribute is incompatible with aggregate requirements: only one provider will " + "match : " + fromFilter);
}
if (fil != null) {
// Append the two filters
fil = "(&" + fromFilter + fil + ")";
} else {
fil = fromFilter;
}
}
Filter filter = null;
if (fil != null) {
try {
filter = getInstanceManager().getContext().createFilter(fil);
} catch (InvalidSyntaxException e) {
throw new ConfigurationException("A requirement filter is invalid : " + filter, e);
}
}
String prox = deps[i].getAttribute("proxy");
// 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")) {
// Infinite wait time ...
timeout = Long.MAX_VALUE;
} 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 = DependencyMetadataHelper.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);
}
}
getInstanceManager().register(fieldmeta, dep);
}
}
Aggregations