Search in sources :

Example 1 with ServiceImporter

use of org.apache.felix.ipojo.composite.service.instantiator.ServiceImporter in project felix by apache.

the class ProvidedServiceHandler method computeAvailableServices.

/**
 * Build the list of available specifications.
 */
private void computeAvailableServices() {
    // Get instantiated services :
    ServiceDependencyHandler handler = (ServiceDependencyHandler) getHandler(HandlerFactory.IPOJO_NAMESPACE + ":subservice");
    for (int i = 0; handler != null && i < handler.getInstances().size(); i++) {
        SvcInstance svc = (SvcInstance) handler.getInstances().get(i);
        String itf = svc.getServiceSpecification();
        boolean agg = svc.isAggregate();
        boolean opt = svc.isOptional();
        SpecificationMetadata specMeta = new SpecificationMetadata(itf, m_context, agg, opt, this);
        m_services.add(specMeta);
    }
    for (int i = 0; handler != null && i < handler.getRequirements().size(); i++) {
        ServiceImporter imp = (ServiceImporter) handler.getRequirements().get(i);
        String itf = imp.getSpecification().getName();
        boolean agg = imp.isAggregate();
        boolean opt = imp.isOptional();
        SpecificationMetadata specMeta = new SpecificationMetadata(itf, m_context, agg, opt, this);
        m_services.add(specMeta);
    }
}
Also used : SvcInstance(org.apache.felix.ipojo.composite.service.instantiator.SvcInstance) ServiceImporter(org.apache.felix.ipojo.composite.service.instantiator.ServiceImporter) ServiceDependencyHandler(org.apache.felix.ipojo.composite.service.instantiator.ServiceDependencyHandler)

Example 2 with ServiceImporter

use of org.apache.felix.ipojo.composite.service.instantiator.ServiceImporter in project felix by apache.

the class ProvidedServiceHandler method checkServiceSpecification.

/**
 * Check composite requirement against service specification requirement is available.
 * @param svc : the provided service to check
 * @throws CompositionException : occurs if the specification field of the service specification cannot be analyzed correctly.
 */
private void checkServiceSpecification(ProvidedService svc) throws CompositionException {
    try {
        Class spec = m_context.getBundle().loadClass(svc.getSpecification());
        Field specField = spec.getField("specification");
        Object object = specField.get(null);
        if (object instanceof String) {
            Element specification = ManifestMetadataParser.parse((String) object);
            Element[] reqs = specification.getElements("requires");
            for (int j = 0; reqs != null && j < reqs.length; j++) {
                ServiceImporter imp = getAttachedRequirement(reqs[j]);
                if (imp != null) {
                    // Fix service-level dependency flag
                    imp.setServiceLevelDependency();
                }
                checkRequirement(imp, reqs[j]);
            }
        } else {
            error("[" + getCompositeManager().getInstanceName() + "] The specification field of the service specification " + svc.getSpecification() + " needs to be a String");
            throw new CompositionException("Service Specification checking failed : The specification field of the service specification " + svc.getSpecification() + " needs to be a String");
        }
    } catch (NoSuchFieldException e) {
        // No specification field
        return;
    } catch (ClassNotFoundException e) {
        error("[" + getCompositeManager().getInstanceName() + "] The service specification " + svc.getSpecification() + " cannot be load");
        throw new CompositionException("The service specification " + svc.getSpecification() + " cannot be loaded", e);
    } catch (IllegalArgumentException e) {
        error("[" + getCompositeManager().getInstanceName() + "] The field 'specification' of the service specification " + svc.getSpecification() + " is not accessible : " + e.getMessage());
        throw new CompositionException("The field 'specification' of the service specification " + svc.getSpecification() + " is not accessible", e);
    } catch (IllegalAccessException e) {
        error("[" + getCompositeManager().getInstanceName() + "] The field 'specification' of the service specification " + svc.getSpecification() + " is not accessible : " + e.getMessage());
        throw new CompositionException("The field 'specification' of the service specification " + svc.getSpecification() + " is not accessible", e);
    } catch (ParseException e) {
        error("[" + getCompositeManager().getInstanceName() + "] The field 'specification' of the service specification " + svc.getSpecification() + " does not contain a valid String : " + e.getMessage());
        throw new CompositionException("The field 'specification' of the service specification " + svc.getSpecification() + " does not contain a valid String", e);
    }
}
Also used : Element(org.apache.felix.ipojo.metadata.Element) ServiceImporter(org.apache.felix.ipojo.composite.service.instantiator.ServiceImporter) Field(java.lang.reflect.Field) ParseException(org.apache.felix.ipojo.parser.ParseException)

Example 3 with ServiceImporter

use of org.apache.felix.ipojo.composite.service.instantiator.ServiceImporter 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)

Example 4 with ServiceImporter

use of org.apache.felix.ipojo.composite.service.instantiator.ServiceImporter in project felix by apache.

the class ProvidedServiceHandler method getAttachedRequirement.

/**
 * Look for the implementation (i.e. composite) requirement for the given service-level requirement metadata.
 * @param element : the service-level requirement metadata
 * @return the ServiceImporter object, null if not found or if the DependencyHandler is not plugged to the instance
 */
private ServiceImporter getAttachedRequirement(Element element) {
    ServiceDependencyHandler handler = (ServiceDependencyHandler) getHandler(HandlerFactory.IPOJO_NAMESPACE + ":subservice");
    if (handler == null) {
        return null;
    }
    String identity = element.getAttribute("id");
    if (identity != null) {
        // Look for dependency Id
        for (int i = 0; i < handler.getRequirements().size(); i++) {
            ServiceImporter imp = (ServiceImporter) handler.getRequirements().get(i);
            if (imp.getId().equals(identity)) {
                return imp;
            }
        }
    }
    // If not found or no id, look for a dependency with the same specification
    String requirement = element.getAttribute("specification");
    for (int i = 0; i < handler.getRequirements().size(); i++) {
        ServiceImporter imp = (ServiceImporter) handler.getRequirements().get(i);
        if (imp.getId().equals(requirement) || imp.getSpecification().getName().equals(requirement)) {
            return imp;
        }
    }
    return null;
}
Also used : ServiceImporter(org.apache.felix.ipojo.composite.service.instantiator.ServiceImporter) ServiceDependencyHandler(org.apache.felix.ipojo.composite.service.instantiator.ServiceDependencyHandler)

Aggregations

ServiceImporter (org.apache.felix.ipojo.composite.service.instantiator.ServiceImporter)4 ServiceDependencyHandler (org.apache.felix.ipojo.composite.service.instantiator.ServiceDependencyHandler)3 Element (org.apache.felix.ipojo.metadata.Element)2 Field (java.lang.reflect.Field)1 Properties (java.util.Properties)1 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)1 Factory (org.apache.felix.ipojo.Factory)1 HandlerFactory (org.apache.felix.ipojo.HandlerFactory)1 HandlerManager (org.apache.felix.ipojo.HandlerManager)1 MissingHandlerException (org.apache.felix.ipojo.MissingHandlerException)1 PolicyServiceContext (org.apache.felix.ipojo.PolicyServiceContext)1 UnacceptableConfiguration (org.apache.felix.ipojo.UnacceptableConfiguration)1 SvcInstance (org.apache.felix.ipojo.composite.service.instantiator.SvcInstance)1 ParseException (org.apache.felix.ipojo.parser.ParseException)1 BundleContext (org.osgi.framework.BundleContext)1 Filter (org.osgi.framework.Filter)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1