use of org.apache.felix.ipojo.parser.ParseException 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.parser.ParseException 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.parser.ParseException in project felix by apache.
the class ProvidedServiceHandler method checkServiceSpecification.
/**
* Check composite requirement against service specification requirement is available.
* @param svc : the provided service to check
* @throws CompositionException : occurs if the specification field of the service specification cannot be analyzed correctly.
*/
private void checkServiceSpecification(ProvidedService svc) throws CompositionException {
try {
Class spec = m_context.getBundle().loadClass(svc.getSpecification());
Field specField = spec.getField("specification");
Object object = specField.get(null);
if (object instanceof String) {
Element specification = ManifestMetadataParser.parse((String) object);
Element[] reqs = specification.getElements("requires");
for (int j = 0; reqs != null && j < reqs.length; j++) {
ServiceImporter imp = getAttachedRequirement(reqs[j]);
if (imp != null) {
// Fix service-level dependency flag
imp.setServiceLevelDependency();
}
checkRequirement(imp, reqs[j]);
}
} else {
error("[" + getCompositeManager().getInstanceName() + "] The specification field of the service specification " + svc.getSpecification() + " needs to be a String");
throw new CompositionException("Service Specification checking failed : The specification field of the service specification " + svc.getSpecification() + " needs to be a String");
}
} catch (NoSuchFieldException e) {
// No specification field
return;
} catch (ClassNotFoundException e) {
error("[" + getCompositeManager().getInstanceName() + "] The service specification " + svc.getSpecification() + " cannot be load");
throw new CompositionException("The service specification " + svc.getSpecification() + " cannot be loaded", e);
} catch (IllegalArgumentException e) {
error("[" + getCompositeManager().getInstanceName() + "] The field 'specification' of the service specification " + svc.getSpecification() + " is not accessible : " + e.getMessage());
throw new CompositionException("The field 'specification' of the service specification " + svc.getSpecification() + " is not accessible", e);
} catch (IllegalAccessException e) {
error("[" + getCompositeManager().getInstanceName() + "] The field 'specification' of the service specification " + svc.getSpecification() + " is not accessible : " + e.getMessage());
throw new CompositionException("The field 'specification' of the service specification " + svc.getSpecification() + " is not accessible", e);
} catch (ParseException e) {
error("[" + getCompositeManager().getInstanceName() + "] The field 'specification' of the service specification " + svc.getSpecification() + " does not contain a valid String : " + e.getMessage());
throw new CompositionException("The field 'specification' of the service specification " + svc.getSpecification() + " does not contain a valid String", e);
}
}
use of org.apache.felix.ipojo.parser.ParseException in project felix by apache.
the class TestInstantiate method getInstanceMetadata.
/**
* Returns the instance metadatas of the component with the given name,
* defined in the given bundle.
*
* @param bundle the bundle from which the component is defined.
* @param component the name of the defined component.
* @return the list of instance metadata of the component with the given name,
* defined in the given bundle, or {@code null} if not found.
*/
public static Element[] getInstanceMetadata(Bundle bundle, String component) {
// Retrieves the component description from the bundle's manifest.
String elem = (String) bundle.getHeaders().get("iPOJO-Components");
if (elem == null) {
throw new IllegalArgumentException("Cannot find iPOJO-Components descriptor in the specified bundle (" + bundle.getSymbolicName() + "). Not an iPOJO bundle.");
}
// Parses the retrieved description and find the component with the
// given name.
List<Element> list = new ArrayList<Element>();
try {
Element element = ManifestMetadataParser.parseHeaderMetadata(elem);
Element[] childs = element.getElements("instance");
for (int i = 0; i < childs.length; i++) {
String name = childs[i].getAttribute("component");
if (name != null && name.equalsIgnoreCase(component)) {
list.add(childs[i]);
}
}
if (list.isEmpty()) {
// Component not found...
return null;
} else {
return (Element[]) list.toArray(new Element[list.size()]);
}
} catch (ParseException e) {
throw new IllegalStateException("Cannot parse the components from specified bundle (" + bundle.getSymbolicName() + "): " + e.getMessage());
}
}
use of org.apache.felix.ipojo.parser.ParseException in project felix by apache.
the class TestBadLFCCallback method getManipulationForComponent.
private Element getManipulationForComponent() {
// On KF we must cast the result.
String header = (String) getTestBundle().getHeaders().get("iPOJO-Components");
Element elem = null;
try {
elem = ManifestMetadataParser.parseHeaderMetadata(header);
} catch (ParseException e) {
fail("Parse Exception when parsing iPOJO-Component");
}
assertNotNull("Check elem not null", elem);
Element manip = getManipulationForComponent(elem, type);
assertNotNull("Check manipulation metadata not null for " + type, manip);
return manip;
}
Aggregations