use of org.apache.felix.ipojo.parser.MethodMetadata in project felix by apache.
the class DependencyConfigurationChecker method ensureThatTheConstructorParameterIsCoherent.
/**
* Checks whether the constructor parameter injection is suitable. this check verified that the constructor has
* enough parameter.
* @param dependency the dependency
* @param manipulation the manipulation metadata
* @throws ConfigurationException if the constructor is not suitable
*/
private static void ensureThatTheConstructorParameterIsCoherent(Dependency dependency, PojoMetadata manipulation) throws ConfigurationException {
if (dependency.getConstructorParameterIndex() != -1) {
MethodMetadata[] constructors = manipulation.getConstructors();
if (constructors == null || constructors.length == 0) {
throw new ConfigurationException("The constructor parameter attribute of " + DependencyHandler.getDependencyIdentifier(dependency) + " is inconsistent - reason: there is no constructor in" + " the component class (" + dependency.getHandler().getInstanceManager().getClassName() + ")");
}
// TODO Consider only the first constructor. This is a limitation we should think about,
// how to determine which constructor to use. Only one constructor should have annotations,
// it could be use as hint.
MethodMetadata constructor = constructors[0];
if (!(constructor.getMethodArguments().length > dependency.getConstructorParameterIndex())) {
throw new ConfigurationException("The constructor parameter attribute of " + DependencyHandler.getDependencyIdentifier(dependency) + " is inconsistent - reason: the constructor with the " + "signature " + Arrays.toString(constructor.getMethodArguments()) + " has not enough " + "parameters");
}
}
}
use of org.apache.felix.ipojo.parser.MethodMetadata in project felix by apache.
the class DependencyHandler method configure.
/**
* Configure the handler.
*
* @param componentMetadata : the component type metadata
* @param configuration : the instance configuration
* @throws ConfigurationException : one dependency metadata is not correct.
* @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
*/
public void configure(Element componentMetadata, Dictionary configuration) throws ConfigurationException {
PojoMetadata manipulation = getFactory().getPojoMetadata();
boolean atLeastOneField = false;
// Create the dependency according to the component metadata
Element[] deps = componentMetadata.getElements("Requires");
// Get instance filters.
Dictionary filtersConfiguration = getRequiresFilters(configuration.get("requires.filters"));
Dictionary fromConfiguration = (Dictionary) configuration.get("requires.from");
for (int i = 0; deps != null && i < deps.length; i++) {
// Create the dependency metadata
final Element dependencyElement = deps[i];
String field = dependencyElement.getAttribute("field");
String serviceSpecification = getServiceSpecificationAttribute(dependencyElement);
String opt = dependencyElement.getAttribute("optional");
boolean optional = opt != null && opt.equalsIgnoreCase("true");
String defaultImpl = dependencyElement.getAttribute("default-implementation");
String exception = dependencyElement.getAttribute("exception");
String to = dependencyElement.getAttribute("timeout");
int timeout = 0;
if (to != null) {
timeout = Integer.parseInt(to);
}
String agg = dependencyElement.getAttribute("aggregate");
boolean aggregate = agg != null && agg.equalsIgnoreCase("true");
String identity = dependencyElement.getAttribute("id");
String nul = dependencyElement.getAttribute("nullable");
boolean nullable = nul == null || nul.equalsIgnoreCase("true");
boolean isProxy = isProxy(dependencyElement);
BundleContext context = getFacetedBundleContext(dependencyElement);
String filter = computeFilter(dependencyElement, filtersConfiguration, fromConfiguration, aggregate, identity);
Filter fil = createAndCheckFilter(filter);
Class spec = null;
if (serviceSpecification != null) {
spec = DependencyMetadataHelper.loadSpecification(serviceSpecification, getInstanceManager().getContext());
}
int policy = DependencyMetadataHelper.getPolicy(dependencyElement);
Comparator cmp = DependencyMetadataHelper.getComparator(dependencyElement, getInstanceManager().getGlobalContext());
Dependency dep = new Dependency(this, field, spec, fil, optional, aggregate, nullable, isProxy, identity, context, policy, cmp, defaultImpl, exception);
dep.setTimeout(timeout);
// Look for dependency callback :
addCallbacksToDependency(dependencyElement, dep);
// Add the constructor parameter if needed
String paramIndex = dependencyElement.getAttribute("constructor-parameter");
if (paramIndex != null) {
int index = Integer.parseInt(paramIndex);
dep.addConstructorInjection(index);
}
// Check the dependency, throws an exception on error.
DependencyConfigurationChecker.ensure(dep, dependencyElement, manipulation);
m_dependencies.add(dep);
if (dep.getField() != null) {
getInstanceManager().register(manipulation.getField(dep.getField()), dep);
atLeastOneField = true;
}
}
if (atLeastOneField) {
// Does register only if we have fields
MethodMetadata[] methods = manipulation.getMethods();
for (MethodMetadata method : methods) {
for (Dependency dep : m_dependencies) {
getInstanceManager().register(method, dep);
}
}
// Also track the inner class methods
for (String inner : manipulation.getInnerClasses()) {
MethodMetadata[] meths = manipulation.getMethodsFromInnerClass(inner);
if (meths != null) {
for (MethodMetadata method : meths) {
for (Dependency dep : m_dependencies) {
getInstanceManager().register(method, inner, dep);
}
}
}
}
}
// Initialize the description.
m_description = new DependencyHandlerDescription(this, getDependencies());
manageContextSources(configuration);
}
use of org.apache.felix.ipojo.parser.MethodMetadata 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);
}
}
Aggregations