Search in sources :

Example 6 with ConfigurationException

use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.

the class BundleContextHandler method getBundleContextForConfiguration.

private BundleContext getBundleContextForConfiguration(Element element) throws ConfigurationException {
    String type = element.getAttribute("value");
    if (type == null) {
        // XML case.
        type = element.getAttribute("context");
    }
    BundleContext context;
    if ("INSTANCE".equalsIgnoreCase(type)) {
        context = getInstanceBundleContext();
    } else if (type == null || "COMPONENT".equalsIgnoreCase(type)) {
        context = getComponentBundleContext();
    } else {
        throw new ConfigurationException("Not supported bundle context source : " + type);
    }
    return context;
}
Also used : ConfigurationException(org.apache.felix.ipojo.ConfigurationException) BundleContext(org.osgi.framework.BundleContext)

Example 7 with ConfigurationException

use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.

the class DependencyConfigurationChecker method deduceAggregationFromTheInjectionPoints.

/**
 * Determines if the dependency is aggregate from the field or constructor parameter used to inject the dependency.
 * If the dependency just uses methods, this method does nothing. This method also check that dependencies set to
 * aggregate have a valid injection type.
 * @param dependency the dependency
 * @param manipulation the manipulation metadata
 * @throws ConfigurationException if the type of the field or constructor parameter used to inject the dependency
 * is not suitable for aggregate dependencies.
 */
private static void deduceAggregationFromTheInjectionPoints(Dependency dependency, PojoMetadata manipulation) throws ConfigurationException {
    if (dependency.getField() != null) {
        FieldMetadata field = manipulation.getField(dependency.getField());
        String type = field.getFieldType();
        if (type.endsWith("[]")) {
            dependency.setAggregateType(AggregateDependencyInjectionType.ARRAY);
        } else if (Collection.class.getName().equals(type) || List.class.getName().equals(type)) {
            dependency.setAggregateType(AggregateDependencyInjectionType.LIST);
        } else if (Set.class.getName().equals(type)) {
            dependency.setAggregateType(AggregateDependencyInjectionType.SET);
        } else if (Vector.class.getName().equals(type)) {
            dependency.setAggregateType(AggregateDependencyInjectionType.VECTOR);
        } else if (dependency.isAggregate()) {
            // Something wrong. The dependency has a field that is not suitable for aggregate dependencies
            throw new ConfigurationException("The dependency " + DependencyHandler.getDependencyIdentifier(dependency) + " cannot be an aggregate dependency - reason: the type " + field.getFieldType() + " of the field " + field.getFieldName() + " is not suitable for aggregate " + "dependencies. Compatible types are array, vector, list, set and collection.");
        }
    }
    if (dependency.getConstructorParameterIndex() != -1) {
        String type = manipulation.getConstructors()[0].getMethodArguments()[dependency.getConstructorParameterIndex()];
        if (type.endsWith("[]")) {
            dependency.setAggregateType(AggregateDependencyInjectionType.ARRAY);
        } else if (Collection.class.getName().equals(type) || List.class.getName().equals(type)) {
            dependency.setAggregateType(AggregateDependencyInjectionType.LIST);
        } else if (Set.class.getName().equals(type)) {
            dependency.setAggregateType(AggregateDependencyInjectionType.SET);
        } else if (Vector.class.getName().equals(type)) {
            dependency.setAggregateType(AggregateDependencyInjectionType.VECTOR);
        } else if (dependency.isAggregate()) {
            // Something wrong. The dependency has a field that is not suitable for aggregate dependencies
            throw new ConfigurationException("The dependency " + DependencyHandler.getDependencyIdentifier(dependency) + " cannot be an aggregate dependency - reason: the type " + type + " of the constructor parameter " + dependency.getConstructorParameterIndex() + " is not suitable for aggregate " + "dependencies. Compatible types are array, vector, list, set and collection.");
        }
    }
// TODO We may not cover some cases such as inconsistency between the constructor and the field. However this
// should be very rare.
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) ConfigurationException(org.apache.felix.ipojo.ConfigurationException)

Example 8 with ConfigurationException

use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.

the class ControllerHandler method initializeComponentFactory.

/**
 * Initialize the component factory.
 * The controller field is checked to avoid configure check.
 * @param desc : component description
 * @param metadata : component type metadata
 * @throws ConfigurationException : occurs if the controller field is not in the POJO class or is not a boolean.
 * @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 {
    String field = null;
    Element[] controller = metadata.getElements("controller");
    // Use only the first controller
    field = controller[0].getAttribute("field");
    if (field == null) {
        throw new ConfigurationException("Lifecycle controller : the controller element needs to contain a field attribute");
    }
    PojoMetadata method = getFactory().getPojoMetadata();
    FieldMetadata fieldMetadata = method.getField(field);
    if (fieldMetadata == null) {
        throw new ConfigurationException("Lifecycle controller : The field " + field + " does not exist in the implementation class");
    }
    if (!fieldMetadata.getFieldType().equalsIgnoreCase("boolean")) {
        throw new ConfigurationException("Lifecycle controller : The field " + field + " must be a boolean (" + fieldMetadata.getFieldType() + " found)");
    }
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Element(org.apache.felix.ipojo.metadata.Element) PojoMetadata(org.apache.felix.ipojo.parser.PojoMetadata)

Example 9 with ConfigurationException

use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.

the class ProvidedServiceHandler method configure.

/**
 * Configure the handler.
 * @param componentMetadata : the component type metadata
 * @param configuration : the instance configuration
 * @throws ConfigurationException : the metadata are 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 {
    m_providedServices.clear();
    // Create the dependency according to the component metadata
    Element[] providedServices = componentMetadata.getElements("Provides");
    for (Element providedService : providedServices) {
        // Set by the initialize component factory.
        String[] serviceSpecifications = ParseUtils.parseArrays(providedService.getAttribute("specifications"));
        // Get the factory policy
        int factory = ProvidedService.SINGLETON_STRATEGY;
        Class custom = null;
        String strategy = providedService.getAttribute("strategy");
        if (strategy == null) {
            strategy = providedService.getAttribute("factory");
        }
        if (strategy != null) {
            if ("singleton".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.SINGLETON_STRATEGY;
            } else if ("service".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.SERVICE_STRATEGY;
            } else if ("method".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.STATIC_STRATEGY;
            } else if ("instance".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.INSTANCE_STRATEGY;
            } else {
                // Customized policy
                try {
                    custom = getInstanceManager().getContext().getBundle().loadClass(strategy);
                    if (!CreationStrategy.class.isAssignableFrom(custom)) {
                        throw new ConfigurationException("The custom creation policy class " + custom.getName() + " does not implement " + CreationStrategy.class.getName());
                    }
                } catch (ClassNotFoundException e) {
                    throw new ConfigurationException("The custom creation policy class " + strategy + " cannot be loaded ", e);
                }
            }
        }
        // Then create the provided service
        ProvidedService svc = new ProvidedService(this, serviceSpecifications, factory, custom, configuration);
        // Post-Registration callback
        String post = providedService.getAttribute("post-registration");
        if (post != null) {
            Callback cb = new Callback(post, new Class[] { ServiceReference.class }, false, getInstanceManager());
            svc.setPostRegistrationCallback(cb);
        }
        post = providedService.getAttribute("post-unregistration");
        if (post != null) {
            // TODO Can we really send the service reference here ?
            Callback cb = new Callback(post, new Class[] { ServiceReference.class }, false, getInstanceManager());
            svc.setPostUnregistrationCallback(cb);
        }
        Element[] props = providedService.getElements("Property");
        if (props != null) {
            // Property[] properties = new Property[props.length];
            Property[] properties = new Property[props.length];
            for (int j = 0; 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");
                Property prop = new Property(name, field, null, value, type, getInstanceManager(), this);
                properties[j] = prop;
                // Check if the instance configuration has a value for this property
                Object object = configuration.get(prop.getName());
                if (object != null) {
                    prop.setValue(object);
                }
                if (field != null) {
                    getInstanceManager().register(new FieldMetadata(field, type), this);
                // Cannot register the property as the interception is necessary
                // to deal with registration update.
                }
            }
            // Attach to properties to the provided service
            svc.setProperties(properties);
        }
        Element[] controllers = providedService.getElements("Controller");
        if (controllers != null) {
            for (Element controller : controllers) {
                String field = controller.getAttribute("field");
                if (field == null) {
                    throw new ConfigurationException("The field attribute of a controller is mandatory");
                }
                String v = controller.getAttribute("value");
                boolean value = !(v != null && v.equalsIgnoreCase("false"));
                String s = controller.getAttribute("specification");
                if (s == null) {
                    s = "ALL";
                }
                svc.setController(field, value, s);
                getInstanceManager().register(new FieldMetadata(field, "boolean"), this);
            }
        }
        if (checkProvidedService(svc)) {
            m_providedServices.add(svc);
        } else {
            StringBuilder itfs = new StringBuilder();
            for (String serviceSpecification : serviceSpecifications) {
                itfs.append(' ');
                itfs.append(serviceSpecification);
            }
            throw new ConfigurationException("The provided service" + itfs + " is not valid");
        }
        // Initialize the description.
        m_description = new ProvidedServiceHandlerDescription(this, getProvidedServices());
    }
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) Element(org.apache.felix.ipojo.metadata.Element) Callback(org.apache.felix.ipojo.util.Callback) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Property(org.apache.felix.ipojo.util.Property)

Example 10 with ConfigurationException

use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.

the class CompositeComponentType method createFactory.

/**
 * Creates the component factory.
 */
private void createFactory() {
    ensureValidity();
    m_metadata = generateComponentMetadata();
    try {
        m_factory = new CompositeFactory(m_context, m_metadata);
        m_factory.start();
    } catch (ConfigurationException e) {
        throw new IllegalStateException("An exception occurs during factory initialization", e);
    }
}
Also used : CompositeFactory(org.apache.felix.ipojo.composite.CompositeFactory) ConfigurationException(org.apache.felix.ipojo.ConfigurationException)

Aggregations

ConfigurationException (org.apache.felix.ipojo.ConfigurationException)34 Element (org.apache.felix.ipojo.metadata.Element)22 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)9 MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)6 PojoMetadata (org.apache.felix.ipojo.parser.PojoMetadata)6 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 Properties (java.util.Properties)5 Filter (org.osgi.framework.Filter)5 MissingHandlerException (org.apache.felix.ipojo.MissingHandlerException)4 UnacceptableConfiguration (org.apache.felix.ipojo.UnacceptableConfiguration)4 BundleContext (org.osgi.framework.BundleContext)4 ArrayList (java.util.ArrayList)3 Comparator (java.util.Comparator)3 Dictionary (java.util.Dictionary)3 Enumeration (java.util.Enumeration)3 PropertyDescription (org.apache.felix.ipojo.architecture.PropertyDescription)3 ParseException (org.apache.felix.ipojo.parser.ParseException)3 Callback (org.apache.felix.ipojo.util.Callback)3 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)2 Factory (org.apache.felix.ipojo.Factory)2