use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.
the class LifecycleCallbackHandler method configure.
/**
* Configure the handler.
* @param metadata : the component type metadata
* @param configuration : the instance configuration
* @throws ConfigurationException : one callback metadata is not correct (either the transition or the method are not correct).
* @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_callbacks = new LifecycleCallback[0];
String imm = metadata.getAttribute("immediate");
m_immediate = imm != null && imm.equalsIgnoreCase("true");
PojoMetadata meta = getFactory().getPojoMetadata();
Element[] hooksMetadata = metadata.getElements("callback");
for (int i = 0; hooksMetadata != null && i < hooksMetadata.length; i++) {
String method = hooksMetadata[i].getAttribute("method");
if (method == null) {
throw new ConfigurationException("Lifecycle callback : A callback needs to contain a method attribute");
}
MethodMetadata met = meta.getMethod(method, new String[0]);
int transition = -1;
String trans = hooksMetadata[i].getAttribute("transition");
if (trans == null) {
throw new ConfigurationException("Lifecycle callback : the transition attribute is missing");
} else {
if (trans.equalsIgnoreCase("validate")) {
transition = LifecycleCallback.VALIDATE;
} else if (trans.equalsIgnoreCase("invalidate")) {
transition = LifecycleCallback.INVALIDATE;
} else {
throw new ConfigurationException("Lifecycle callback : Unknown or malformed transition : " + trans);
}
}
LifecycleCallback callback = null;
if (met == null) {
callback = new LifecycleCallback(this, transition, method);
} else {
callback = new LifecycleCallback(this, transition, met);
}
addCallback(callback);
}
}
use of org.apache.felix.ipojo.ConfigurationException 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;
}
use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.
the class ProvidedServiceHandler method initializeComponentFactory.
/**
* Initialize the component type.
* @param desc : component type description to populate.
* @param metadata : component type metadata.
* @throws ConfigurationException : occurs when the POJO does not implement any interfaces.
* @see org.apache.felix.ipojo.Handler#initializeComponentFactory(org.apache.felix.ipojo.architecture.ComponentTypeDescription, org.apache.felix.ipojo.metadata.Element)
*/
public void initializeComponentFactory(ComponentTypeDescription desc, Element metadata) throws ConfigurationException {
// Change ComponentInfo
Element[] provides = metadata.getElements("provides");
PojoMetadata manipulation = getFactory().getPojoMetadata();
for (Element provide : provides) {
// First : create the serviceSpecification list
String[] serviceSpecification = manipulation.getInterfaces();
String parent = manipulation.getSuperClass();
Set<String> interfaces = new HashSet<String>();
Set<String> parentClasses = new HashSet<String>();
try {
computeInterfacesAndSuperClasses(serviceSpecification, parent, desc.getBundleContext().getBundle(), interfaces, parentClasses);
getLogger().log(Logger.INFO, "Collected interfaces from " + metadata.getAttribute("classname") + " : " + interfaces);
getLogger().log(Logger.INFO, "Collected super classes from " + metadata.getAttribute("classname") + " : " + parentClasses);
} catch (ClassNotFoundException e) {
throw new ConfigurationException("An interface or parent class cannot be loaded", e);
}
String serviceSpecificationStr = provide.getAttribute("specifications");
if (serviceSpecificationStr == null) {
serviceSpecificationStr = provide.getAttribute("interface");
if (serviceSpecificationStr != null) {
warn("The 'interface' attribute is deprecated, use the 'specifications' attribute instead of 'interface'");
}
}
if (serviceSpecificationStr != null) {
List<String> itfs = ParseUtils.parseArraysAsList(serviceSpecificationStr);
for (String itf : itfs) if (!interfaces.contains(itf) && !parentClasses.contains(itf) && !desc.getFactory().getClassName().equals(itf)) {
desc.getFactory().getLogger().log(Logger.ERROR, "The specification " + itf + " is not implemented by " + metadata.getAttribute("classname"));
}
interfaces.clear();
interfaces.addAll(itfs);
}
if (interfaces.isEmpty()) {
warn("No service interface found in the class hierarchy, use the implementation class");
interfaces.add(desc.getFactory().getClassName());
}
StringBuffer specs = null;
Set<String> set = new HashSet<String>(interfaces);
// Remove POJO.
set.remove(Pojo.class.getName());
for (String spec : set) {
desc.addProvidedServiceSpecification(spec);
if (specs == null) {
specs = new StringBuffer("{");
specs.append(spec);
} else {
specs.append(',');
specs.append(spec);
}
}
specs.append('}');
// Add interface attribute to avoid checking in the configure method
provide.addAttribute(new Attribute("specifications", specs.toString()));
Element[] props = provide.getElements("property");
for (int j = 0; props != null && j < props.length; j++) {
String name = props[j].getAttribute("name");
String value = props[j].getAttribute("value");
String type = props[j].getAttribute("type");
String field = props[j].getAttribute("field");
// Get property name :
if (field != null && name == null) {
name = field;
}
// Check type if not already set
if (type == null) {
if (field == null) {
throw new ConfigurationException("The property " + name + " has neither type nor field.");
}
FieldMetadata fieldMeta = manipulation.getField(field);
if (fieldMeta == null) {
throw new ConfigurationException("A declared property was not found in the implementation class : " + field);
}
type = fieldMeta.getFieldType();
props[j].addAttribute(new Attribute("type", type));
}
// Is the property set to immutable
boolean immutable = false;
String imm = props[j].getAttribute("immutable");
if (imm != null && imm.equalsIgnoreCase("true")) {
immutable = true;
}
PropertyDescription pd = new PropertyDescription(name, type, value, immutable);
desc.addProperty(pd);
String man = props[j].getAttribute("mandatory");
if (man != null && man.equalsIgnoreCase("true")) {
pd.setMandatory();
}
}
}
}
use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.
the class DependencyMetadataHelper method getComparator.
/**
* Helper method parsing the comparator attribute and returning the
* comparator object. If the 'comparator' attribute is not set, this method
* returns null. If the 'comparator' attribute is set to 'osgi', this method
* returns the normal OSGi comparator. In other case, it tries to create
* an instance of the declared comparator class.
*
* @param dep the Element describing the dependency
* @param context the bundle context (to load the comparator class)
* @return the comparator object, <code>null</code> if not set.
* @throws org.apache.felix.ipojo.ConfigurationException the comparator class cannot be load or the
* comparator cannot be instantiated correctly.
*/
public static Comparator getComparator(Element dep, BundleContext context) throws ConfigurationException {
Comparator cmp = null;
String comp = dep.getAttribute("comparator");
if (comp != null) {
if (comp.equalsIgnoreCase("osgi")) {
cmp = new ServiceReferenceRankingComparator();
} else {
try {
Class cla = context.getBundle().loadClass(comp);
cmp = (Comparator) cla.newInstance();
} catch (ClassNotFoundException e) {
throw new ConfigurationException("Cannot load a customized comparator", e);
} catch (IllegalAccessException e) {
throw new ConfigurationException("Cannot create a customized comparator", e);
} catch (InstantiationException e) {
throw new ConfigurationException("Cannot create a customized comparator", e);
}
}
}
return cmp;
}
Aggregations