use of org.apache.felix.ipojo.parser.ParseException in project felix by apache.
the class TestBadConfigurations method setUp.
/**
* Initialization before test cases.
* <p/>
* Create all the instances
*/
@Before
public void setUp() {
m_utils = new EahTestUtils(bc, ipojoHelper);
/**
* Get the list of available components.
*/
try {
String header = (String) getTestBundle().getHeaders().get("iPOJO-Components");
m_components = ManifestMetadataParser.parseHeaderMetadata(header).getElements("component");
} catch (ParseException e) {
fail("Parse Exception when parsing iPOJO-Component");
}
/**
* Initialize the standard publishing component (based on the
* asynchronous donut provider).
*/
m_provider = new Element("component", "");
m_provider.addAttribute(new Attribute("className", "org.apache.felix.ipojo.handler.eventadmin.test.donut.DonutProviderImpl"));
m_provider.addAttribute(new Attribute("name", "standard donut provider for bad tests"));
// The provided service of the publisher
Element providesDonutProvider = new Element("provides", "");
providesDonutProvider.addAttribute(new Attribute("interface", "org.apache.felix.ipojo.handler.eventadmin.test.donut.DonutProvider"));
Element providesDonutProviderProperty = new Element("property", "");
providesDonutProviderProperty.addAttribute(new Attribute("name", "name"));
providesDonutProviderProperty.addAttribute(new Attribute("field", "m_name"));
providesDonutProviderProperty.addAttribute(new Attribute("value", "Unknown donut vendor"));
providesDonutProvider.addElement(providesDonutProviderProperty);
m_provider.addElement(providesDonutProvider);
// The event publisher, corresponding to the following description :
// <ev:publisher name="donut-publisher" field="m_publisher"
// topics="food/donuts" data-key="food" synchronous="false"/>
m_publisher = new Element("publisher", NAMESPACE);
m_publisherName = new Attribute("name", "donut-publisher");
m_publisherField = new Attribute("field", "m_publisher");
m_publisherTopics = new Attribute("topics", "food/donuts");
m_publisherDataKey = new Attribute("data-key", "food");
m_publisherSynchronous = new Attribute("synchronous", "false");
m_publisher.addAttribute(m_publisherName);
m_publisher.addAttribute(m_publisherField);
m_publisher.addAttribute(m_publisherTopics);
m_publisher.addAttribute(m_publisherDataKey);
m_publisher.addAttribute(m_publisherSynchronous);
m_provider.addElement(m_publisher);
m_provider.addElement(getManipulationForComponent("donut-provider"));
/**
* Initialize the standard subscribing component (based on the donut
* consumer).
*/
m_consumer = new Element("component", "");
m_consumer.addAttribute(new Attribute("className", "org.apache.felix.ipojo.handler.eventadmin.test.donut.DonutConsumerImpl"));
m_consumer.addAttribute(new Attribute("name", "standard donut consumer for bad tests"));
// The provided service of the publisher
Element providesDonutConsumer = new Element("provides", "");
providesDonutConsumer.addAttribute(new Attribute("interface", "org.apache.felix.ipojo.handler.eventadmin.test.donut.DonutConsumer"));
Element providesDonutConsumerNameProperty = new Element("property", "");
providesDonutConsumerNameProperty.addAttribute(new Attribute("name", "name"));
providesDonutConsumerNameProperty.addAttribute(new Attribute("field", "m_name"));
providesDonutConsumerNameProperty.addAttribute(new Attribute("value", "Unknown donut consumer"));
providesDonutConsumer.addElement(providesDonutConsumerNameProperty);
Element providesDonutConsumerSlowProperty = new Element("property", "");
providesDonutConsumerSlowProperty.addAttribute(new Attribute("name", "slow"));
providesDonutConsumerSlowProperty.addAttribute(new Attribute("field", "m_isSlow"));
providesDonutConsumerSlowProperty.addAttribute(new Attribute("value", "false"));
providesDonutConsumer.addElement(providesDonutConsumerSlowProperty);
m_consumer.addElement(providesDonutConsumer);
// The event publisher, corresponding to the following description :
// <ev:subscriber name="donut-subscriber" callback="receiveDonut"
// topics="food/donuts" data-key="food"
// data-type="org.apache.felix.ipojo.handler.eventadmin.test.donut.Donut"/>
m_subscriber = new Element("subscriber", NAMESPACE);
m_subscriberName = new Attribute("name", "donut-subscriber");
m_subscriberCallback = new Attribute("callback", "receiveDonut");
m_subscriberTopics = new Attribute("topics", "food/donuts");
m_subscriberDataKey = new Attribute("data-key", "food");
m_subscriberDataType = new Attribute("data-type", "org.apache.felix.ipojo.handler.eventadmin.test.donut.Donut");
m_subscriber.addAttribute(m_subscriberName);
m_subscriber.addAttribute(m_subscriberCallback);
m_subscriber.addAttribute(m_subscriberTopics);
m_subscriber.addAttribute(m_subscriberDataKey);
m_subscriber.addAttribute(m_subscriberDataType);
m_consumer.addElement(m_subscriber);
m_consumer.addElement(getManipulationForComponent("donut-consumer"));
}
use of org.apache.felix.ipojo.parser.ParseException in project felix by apache.
the class IPOJOHelper method getMetadata.
/**
* Returns the metadata description 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 metadata description of the component with the given name,
* defined in the given bundle, or {@code null} if not found.
*/
public static Element getMetadata(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.");
}
// given name.
try {
Element element = ManifestMetadataParser.parseHeaderMetadata(elem);
Element[] childs = element.getElements("component");
for (int i = 0; i < childs.length; i++) {
String name = childs[i].getAttribute("name");
String clazz = childs[i].getAttribute("classname");
if (name != null && name.equalsIgnoreCase(component)) {
return childs[i];
}
if (clazz.equalsIgnoreCase(component)) {
return childs[i];
}
}
// Component not found...
return null;
} 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 TestManipulationMetadata method getManipulationForComponent.
private Element getManipulationForComponent(String comp_name) {
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, comp_name);
assertNotNull("Check manipulation metadata not null for " + comp_name, manip);
return manip;
}
use of org.apache.felix.ipojo.parser.ParseException in project felix by apache.
the class ProvidedServiceHandler method checkProvidedService.
/**
* Check the provided service given in argument in the sense that the metadata are consistent.
* @param svc : the provided service to check.
* @return true if the provided service is correct
* @throws ConfigurationException : the checked provided service is not correct.
*/
private boolean checkProvidedService(ProvidedService svc) throws ConfigurationException {
Set<ClassLoader> classloaders = new LinkedHashSet<ClassLoader>();
for (int i = 0; i < svc.getServiceSpecifications().length; i++) {
String specName = svc.getServiceSpecifications()[i];
// Check service level dependencies
try {
Class spec = load(specName, classloaders);
classloaders.add(spec.getClassLoader());
Field specField = spec.getField("specification");
Object specif = specField.get(null);
if (specif instanceof String) {
Element specification = ManifestMetadataParser.parse((String) specif);
Element[] deps = specification.getElements("requires");
for (int j = 0; deps != null && j < deps.length; j++) {
Dependency dep = getAttachedDependency(deps[j]);
if (dep != null) {
// Fix service-level dependency flag
dep.setServiceLevelDependency();
}
isDependencyCorrect(dep, deps[j]);
}
} else {
throw new ConfigurationException("Service Providing: The specification field of the service specification " + svc.getServiceSpecifications()[i] + " needs to be a String");
}
} catch (NoSuchFieldException e) {
// Ignore it, keep and going.
} catch (ClassNotFoundException e) {
throw new ConfigurationException("Service Providing: The service specification " + svc.getServiceSpecifications()[i] + " cannot be loaded", e);
} catch (IllegalArgumentException e) {
throw new ConfigurationException("Service Providing: The field 'specification' of the service specification " + svc.getServiceSpecifications()[i] + " is not accessible", e);
} catch (IllegalAccessException e) {
throw new ConfigurationException("Service Providing: The field 'specification' of the service specification " + svc.getServiceSpecifications()[i] + " is not accessible", e);
} catch (ParseException e) {
throw new ConfigurationException("Service Providing: The field 'specification' of the service specification " + svc.getServiceSpecifications()[i] + " does not contain a valid String", e);
}
}
return true;
}
Aggregations