Search in sources :

Example 21 with ConfigurationException

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

the class InstanceHandler method createInstance.

/**
 * Create an instance using the given factory and the given configuration.
 *
 * @param fact : the factory name to used.
 * @param config : the configuration.
 */
private void createInstance(Factory fact, ManagedConfiguration config) {
    Dictionary conf = config.getConfiguration();
    try {
        config.setInstance(fact.createComponentInstance(conf, m_scope));
        config.setFactory(fact.getName());
        config.getInstance().addInstanceStateListener(this);
    } catch (UnacceptableConfiguration e) {
        error("A factory is available for the configuration but the configuration is not acceptable", e);
    } catch (MissingHandlerException e) {
        error("The instance creation has failed, at least one handler is missing", e);
    } catch (ConfigurationException e) {
        error("The instance creation has failed, an error during the configuration has occured", e);
    }
}
Also used : Dictionary(java.util.Dictionary) MissingHandlerException(org.apache.felix.ipojo.MissingHandlerException) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) UnacceptableConfiguration(org.apache.felix.ipojo.UnacceptableConfiguration)

Example 22 with ConfigurationException

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

the class SvcInstance method onServiceArrival.

/**
 * A new service is injected.
 * This method create the sub-service instance in the composite.
 * @param ref : service reference.
 * @see org.apache.felix.ipojo.util.DependencyModel#onServiceArrival(org.osgi.framework.ServiceReference)
 */
public void onServiceArrival(ServiceReference ref) {
    // The given factory matches.
    try {
        Factory fact = (Factory) getService(ref);
        if (m_factories.get(ref) == null) {
            ComponentInstance instance = createInstance(fact);
            m_factories.put(ref, instance);
        } else {
            m_handler.info("An arriving factory is already used, ignore the creation : " + fact.getName());
        }
    } catch (UnacceptableConfiguration e) {
        m_handler.error("A matching factory refuses the actual configuration : " + e.getMessage());
        m_handler.getCompositeManager().stop();
    } catch (MissingHandlerException e) {
        m_handler.error("A matching factory is no more valid : " + e.getMessage());
        m_handler.getCompositeManager().stop();
    } catch (ConfigurationException e) {
        m_handler.error("A matching configuration is refused by the instance : " + e.getMessage());
        m_handler.getCompositeManager().stop();
    }
}
Also used : MissingHandlerException(org.apache.felix.ipojo.MissingHandlerException) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) UnacceptableConfiguration(org.apache.felix.ipojo.UnacceptableConfiguration)

Example 23 with ConfigurationException

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

the class ProvidedService method register.

/**
 * Register the exposed service.
 */
public void register() {
    Properties props = new Properties();
    props.put("instance.name", m_instanceName);
    List fields = m_composition.getFieldList();
    for (int i = 0; i < fields.size(); i++) {
        FieldMetadata field = (FieldMetadata) fields.get(i);
        if (field.isUseful() && !field.getSpecification().isInterface()) {
            String type = field.getSpecification().getComponentType();
            Object pojo = getObjectByType(type);
            props.put(field.getName(), pojo);
        }
    }
    if (m_instance == null) {
        // Else we have to create the instance
        try {
            m_instance = m_factory.createComponentInstance(props, m_manager.getServiceContext());
        } catch (UnacceptableConfiguration e) {
            throw new IllegalStateException("Cannot create the service implementation", e);
        } catch (MissingHandlerException e) {
            throw new IllegalStateException("Cannot create the service implementation", e);
        } catch (ConfigurationException e) {
            throw new IllegalStateException("Cannot create the service implementation", e);
        }
    } else {
        // We have to reconfigure the instance in order to inject up to date glue component instance.
        m_instance.reconfigure(props);
    }
    m_exports.start();
}
Also used : MissingHandlerException(org.apache.felix.ipojo.MissingHandlerException) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) List(java.util.List) Properties(java.util.Properties) UnacceptableConfiguration(org.apache.felix.ipojo.UnacceptableConfiguration)

Example 24 with ConfigurationException

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

the class ProvidedService method start.

/**
 * Start method.
 * Build service implementation type, factory and instance.
 * @throws CompositionException if a consistent mapping cannot be discovered.
 */
public void start() throws CompositionException {
    m_composition.buildMapping();
    m_instanceName = m_composition.getSpecificationMetadata().getName() + "Provider-Gen";
    byte[] clazz = m_composition.buildPOJO();
    Element metadata = m_composition.buildMetadata(m_instanceName);
    // Create the factory
    try {
        m_factory = new ComponentFactory(m_context, clazz, metadata);
        m_factory.setUseFactoryClassloader(true);
        m_factory.start();
    } catch (ConfigurationException e) {
        // Should not happen.
        m_manager.getFactory().getLogger().log(Logger.ERROR, "A factory cannot be created", e);
    }
    try {
        Class spec = DependencyMetadataHelper.loadSpecification(m_composition.getSpecificationMetadata().getName(), m_context);
        Filter filter = m_context.createFilter("(instance.name=" + m_instanceName + ")");
        // Create the exports
        m_exports = new ServiceExporter(spec, filter, false, false, null, DependencyModel.DYNAMIC_BINDING_POLICY, m_scope, m_context, this, m_manager);
    } catch (InvalidSyntaxException e) {
        throw new CompositionException("A provided service filter is invalid", e);
    } catch (ConfigurationException e) {
        throw new CompositionException("The class " + m_composition.getSpecificationMetadata().getName() + " cannot be loaded", e);
    }
}
Also used : ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Filter(org.osgi.framework.Filter) Element(org.apache.felix.ipojo.metadata.Element) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 25 with ConfigurationException

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

the class ProvidedServiceHandler method checkRequirement.

/**
 * Check the correctness of the composite requirement against the service level dependency.
 * @param imp : requirement to check
 * @param elem : service-level dependency metadata
 * @throws CompositionException : occurs if the requirement does not match with service-level specification requirement
 */
private void checkRequirement(ServiceImporter imp, Element elem) throws CompositionException {
    String optional = elem.getAttribute("optional");
    boolean opt = optional != null && optional.equalsIgnoreCase("true");
    String aggregate = elem.getAttribute("aggregate");
    boolean agg = aggregate != null && aggregate.equalsIgnoreCase("true");
    if (imp == null) {
        // Add the missing requirement
        ServiceDependencyHandler handler = (ServiceDependencyHandler) getHandler(HandlerFactory.IPOJO_NAMESPACE + ":subservice");
        if (handler == null) {
            // Look for the ServiceDependencyHandler factory
            HandlerManager handlerManager = null;
            try {
                ServiceReference[] refs = m_context.getServiceReferences(Factory.class.getName(), "(&(handler.name=subservice)(handler.namespace=" + HandlerFactory.IPOJO_NAMESPACE + ")(handler.type=composite))");
                Factory factory = (Factory) m_context.getService(refs[0]);
                handlerManager = (HandlerManager) factory.createComponentInstance(null, getCompositeManager().getServiceContext());
            } catch (InvalidSyntaxException e) {
            // Should not happen
            } catch (UnacceptableConfiguration e) {
            // Should not happen
            } catch (MissingHandlerException e) {
            // Should not happen
            } catch (ConfigurationException e) {
            // Should not happen
            }
            // Add the required handler
            try {
                handlerManager.init(getCompositeManager(), new Element("composite", ""), new Properties());
            } catch (ConfigurationException e) {
                error("Internal error : cannot configure the Import Handler : " + e.getMessage());
                throw new CompositionException("Internal error : cannot configure the Import Handler", e);
            }
            handler = (ServiceDependencyHandler) handlerManager.getHandler();
            getCompositeManager().addCompositeHandler(handlerManager);
        }
        String spec = elem.getAttribute("specification");
        // Cannot import yourself
        String filter = "(&(objectClass=" + spec + ")(!(instance.name=" + getCompositeManager().getInstanceName() + ")))";
        String givenFilter = elem.getAttribute("filter");
        if (givenFilter != null) {
            // NOPMD
            filter = "(&" + filter + givenFilter + ")";
        }
        BundleContext context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.GLOBAL);
        Filter fil = null;
        try {
            fil = getCompositeManager().getGlobalContext().createFilter(filter);
        } catch (InvalidSyntaxException e) {
            throw new CompositionException("A required filter " + filter + " is malformed", e);
        }
        Class specToImport = null;
        try {
            specToImport = getCompositeManager().getGlobalContext().getBundle().loadClass(spec);
        } catch (ClassNotFoundException e) {
            throw new CompositionException("A required specification cannot be loaded : " + spec, e);
        }
        ServiceImporter importer = new ServiceImporter(specToImport, fil, agg, opt, null, DependencyModel.DYNAMIC_BINDING_POLICY, context, null, handler);
        handler.getRequirements().add(importer);
        SpecificationMetadata specMeta = new SpecificationMetadata(spec, m_context, agg, opt, this);
        // Update the available types
        m_services.add(specMeta);
        return;
    }
    if (imp.isAggregate() && !agg) {
        error("[" + getCompositeManager().getInstanceName() + "] The requirement " + elem.getAttribute("specification") + " is aggregate in the implementation and is declared as a simple service-level requirement");
        throw new CompositionException("The requirement " + elem.getAttribute("specification") + " is aggregate in the implementation and is declared as a simple service-level requirement");
    }
    String filter = elem.getAttribute("filter");
    if (filter != null) {
        String filter2 = imp.getFilter();
        if (filter2 == null || !filter2.equalsIgnoreCase(filter)) {
            error("[" + getCompositeManager().getInstanceName() + "] The specification requirement " + elem.getAttribute("specification") + " as not the same filter as declared in the service-level requirement");
            throw new CompositionException("The specification requirement " + elem.getAttribute("specification") + " as not the same filter as declared in the service-level requirement");
        }
    }
}
Also used : Element(org.apache.felix.ipojo.metadata.Element) ServiceImporter(org.apache.felix.ipojo.composite.service.instantiator.ServiceImporter) ServiceDependencyHandler(org.apache.felix.ipojo.composite.service.instantiator.ServiceDependencyHandler) HandlerFactory(org.apache.felix.ipojo.HandlerFactory) Factory(org.apache.felix.ipojo.Factory) PolicyServiceContext(org.apache.felix.ipojo.PolicyServiceContext) Properties(java.util.Properties) UnacceptableConfiguration(org.apache.felix.ipojo.UnacceptableConfiguration) ServiceReference(org.osgi.framework.ServiceReference) MissingHandlerException(org.apache.felix.ipojo.MissingHandlerException) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Filter(org.osgi.framework.Filter) HandlerManager(org.apache.felix.ipojo.HandlerManager) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext)

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