use of org.apache.felix.ipojo.configuration.Instance in project felix by apache.
the class ConfigurationProcessor method instantiateAndDeclareInstances.
private void instantiateAndDeclareInstances(Bundle bundle, String resource, ClassLoader classLoader) {
String classname = getClassNameFromResource(resource);
List<Instance> instances = new ArrayList<Instance>();
try {
Class clazz = classLoader.loadClass(classname);
Object configuration = clazz.newInstance();
// Collect fields
Map<Field, Instance> fields = fields().ofType(Instance.class).in(configuration).map();
for (Map.Entry<Field, Instance> entry : fields.entrySet()) {
Instance instance = entry.getValue();
instance.nameIfUnnamed(entry.getKey().getName()).with("instance.bundle.context").setto(bundle.getBundleContext());
instances.add(instance);
}
// Collect methods with Bundle Context as argument
Map<Method, InvocationResult<Instance>> methods = methods().in(configuration).ofReturnType(Instance.class).withParameter(BundleContext.class).map(bundle.getBundleContext());
// Collect methods without arguments
methods.putAll(methods().in(configuration).ofReturnType(Instance.class).map());
for (Map.Entry<Method, InvocationResult<Instance>> entry : methods.entrySet()) {
Instance instance = entry.getValue().get();
if (instance == null) {
m_logger.log(Log.ERROR, "The Instance declaration creation failed because the method " + entry.getKey().getName() + " of class " + entry.getKey().getDeclaringClass() + " threw an " + "exception", entry.getValue().error());
} else {
instance.nameIfUnnamed(entry.getKey().getName()).with("instance.bundle.context").setto(bundle.getBundleContext());
instances.add(instance);
}
}
} catch (ClassNotFoundException e) {
m_logger.log(Log.ERROR, "Cannot load class " + classname + " despite it was considered as a " + "configuration", e);
return;
} catch (InstantiationException e) {
m_logger.log(Log.ERROR, "Cannot instantiate class " + classname + " despite it was considered as a " + "configuration", e);
return;
} catch (IllegalAccessException e) {
m_logger.log(Log.ERROR, "Cannot instantiate class " + classname + " despite it was considered as a " + "configuration", e);
return;
}
m_logger.log(Log.WARNING, instances.size() + " instances found in class " + classname);
// Build and enqueue declaration
for (Instance instance : instances) {
DefaultInstanceDeclaration declaration = new DefaultInstanceDeclaration(bundle.getBundleContext(), instance.factory(), instance.configuration());
declaration.start();
getComponentsAndInstances(bundle).m_instances.add(declaration);
}
}
Aggregations