Search in sources :

Example 1 with SourceManager

use of org.apache.felix.ipojo.composite.util.SourceManager in project felix by apache.

the class ServiceDependencyHandler method start.

/**
 * Start the service instantiator handler.
 * Start all created service instance.
 * @see org.apache.felix.ipojo.composite.CompositeHandler#start()
 */
public void start() {
    for (int i = 0; m_sources != null && i < m_sources.size(); i++) {
        SourceManager source = (SourceManager) m_sources.get(i);
        source.start();
    }
    for (int i = 0; i < m_importers.size(); i++) {
        ServiceImporter imp = (ServiceImporter) m_importers.get(i);
        imp.start();
    }
    for (int i = 0; i < m_instances.size(); i++) {
        SvcInstance inst = (SvcInstance) m_instances.get(i);
        inst.start();
    }
    isHandlerValid();
    m_isStarted = true;
}
Also used : SourceManager(org.apache.felix.ipojo.composite.util.SourceManager)

Example 2 with SourceManager

use of org.apache.felix.ipojo.composite.util.SourceManager in project felix by apache.

the class ServiceDependencyHandler method createServiceInstance.

/**
 * Create a Service instance object form the given Element.
 * This method parse the given element and configure the service instance object.
 * @param service : the Element describing the service instance
 * @param conf : the configuration from the composite instance
 * @throws ConfigurationException : the service instance cannot be created correctly
 */
private void createServiceInstance(Element service, Dictionary conf) throws ConfigurationException {
    // Prepare the configuration to append.
    Properties toAppend = new Properties();
    Enumeration keys = conf.keys();
    while (keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        if (!(key.equals("instance.name") || key.equals("component"))) {
            // Remove instance.name and component
            toAppend.put(key, conf.get(key));
        }
    }
    String spec = service.getAttribute("specification");
    if (spec == null) {
        throw new ConfigurationException("Malformed service : the specification attribute is mandatory");
    }
    // Cannot reinstantiate yourself
    String filter = "(&(!(factory.name=" + getCompositeManager().getFactory().getComponentDescription().getName() + "))(factory.state=1))";
    String givenFilter = service.getAttribute("filter");
    if (givenFilter != null) {
        // NOPMD
        filter = "(&" + filter + givenFilter + ")";
    }
    Filter fil;
    try {
        fil = getCompositeManager().getGlobalContext().createFilter(filter);
    } catch (InvalidSyntaxException e) {
        throw new ConfigurationException("Malformed filter " + filter, e);
    }
    Properties prop = new Properties();
    Element[] props = service.getElements("property");
    for (int k = 0; props != null && k < props.length; k++) {
        try {
            InstanceHandler.parseProperty(props[k], prop);
        } catch (ParseException e) {
            throw new ConfigurationException("An instance configuration is invalid", e);
        }
    }
    Properties instanceConfiguration = new Properties();
    instanceConfiguration.putAll(prop);
    instanceConfiguration.putAll(toAppend);
    String aggregate = service.getAttribute("aggregate");
    boolean agg = aggregate != null && aggregate.equalsIgnoreCase("true");
    String optional = service.getAttribute("optional");
    boolean opt = optional != null && optional.equalsIgnoreCase("true");
    int policy = DependencyMetadataHelper.getPolicy(service);
    Comparator cmp = DependencyMetadataHelper.getComparator(service, getCompositeManager().getGlobalContext());
    SvcInstance inst = new SvcInstance(this, spec, instanceConfiguration, agg, opt, fil, cmp, policy);
    m_instances.add(inst);
    String sources = service.getAttribute("context-source");
    if (sources != null) {
        SourceManager source = new SourceManager(sources, filter, inst, getCompositeManager());
        if (m_sources == null) {
            m_sources = new ArrayList(1);
        }
        m_sources.add(source);
    }
}
Also used : Enumeration(java.util.Enumeration) Element(org.apache.felix.ipojo.metadata.Element) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Comparator(java.util.Comparator) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Filter(org.osgi.framework.Filter) SourceManager(org.apache.felix.ipojo.composite.util.SourceManager) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ParseException(org.apache.felix.ipojo.parser.ParseException)

Example 3 with SourceManager

use of org.apache.felix.ipojo.composite.util.SourceManager in project felix by apache.

the class ServiceDependencyHandler method createServiceImport.

/**
 * Create a Service importer object from the given Element.
 * This method parse the given element and configure the service importer object.
 * @param imp : Element describing the import
 * @param confFilter : instance filter customization
 * @throws ConfigurationException : the service importer cannot be created correctly
 */
private void createServiceImport(Element imp, Dictionary confFilter) throws ConfigurationException {
    boolean optional = false;
    boolean aggregate = false;
    String specification = imp.getAttribute("specification");
    if (specification == null) {
        // Malformed import
        error("Malformed import: the specification attribute is mandatory");
        throw new ConfigurationException("Malformed import : the specification attribute is mandatory");
    } else {
        String opt = imp.getAttribute("optional");
        optional = opt != null && opt.equalsIgnoreCase("true");
        String agg = imp.getAttribute("aggregate");
        aggregate = agg != null && agg.equalsIgnoreCase("true");
        // Cannot import yourself
        String original = "(&(objectClass=" + specification + ")(!(instance.name=" + getCompositeManager().getInstanceName() + ")))";
        String filter = original;
        String givenFilter = imp.getAttribute("filter");
        if (givenFilter != null) {
            // NOPMD
            filter = "(&" + filter + givenFilter + ")";
        }
        String identitity = imp.getAttribute("id");
        String scope = imp.getAttribute("scope");
        // Get the default bundle context.
        BundleContext context = getCompositeManager().getGlobalContext();
        if (scope != null) {
            if (scope.equalsIgnoreCase("global")) {
                context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.GLOBAL);
            } else if (scope.equalsIgnoreCase("composite")) {
                context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.LOCAL);
            } else if (scope.equalsIgnoreCase("composite+global")) {
                context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.LOCAL_AND_GLOBAL);
            }
        }
        // Configure instance filter if available
        if (confFilter != null && identitity != null && confFilter.get(identitity) != null) {
            filter = "(&" + original + (String) confFilter.get(identitity) + ")";
        }
        Filter fil = null;
        if (filter != null) {
            try {
                fil = getCompositeManager().getGlobalContext().createFilter(filter);
            } catch (InvalidSyntaxException e) {
                throw new ConfigurationException("A required filter " + filter + " is malformed", e);
            }
        }
        Comparator cmp = DependencyMetadataHelper.getComparator(imp, getCompositeManager().getGlobalContext());
        Class spec = DependencyMetadataHelper.loadSpecification(specification, getCompositeManager().getGlobalContext());
        int policy = DependencyMetadataHelper.getPolicy(imp);
        ServiceImporter importer = new ServiceImporter(spec, fil, aggregate, optional, cmp, policy, context, identitity, this);
        m_importers.add(importer);
        String sources = imp.getAttribute("context-source");
        if (sources != null) {
            SourceManager source = new SourceManager(sources, filter, importer, getCompositeManager());
            if (m_sources == null) {
                m_sources = new ArrayList(1);
            }
            m_sources.add(source);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) PolicyServiceContext(org.apache.felix.ipojo.PolicyServiceContext) Comparator(java.util.Comparator) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Filter(org.osgi.framework.Filter) SourceManager(org.apache.felix.ipojo.composite.util.SourceManager) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext)

Example 4 with SourceManager

use of org.apache.felix.ipojo.composite.util.SourceManager in project felix by apache.

the class ServiceDependencyHandler method stop.

/**
 * Handler stop method.
 * Stop all created service instance.
 * @see org.apache.felix.ipojo.composite.CompositeHandler#stop()
 */
public void stop() {
    for (int i = 0; m_sources != null && i < m_sources.size(); i++) {
        SourceManager source = (SourceManager) m_sources.get(i);
        source.stop();
    }
    for (int i = 0; i < m_instances.size(); i++) {
        SvcInstance inst = (SvcInstance) m_instances.get(i);
        inst.stop();
    }
    for (int i = 0; i < m_importers.size(); i++) {
        ServiceImporter imp = (ServiceImporter) m_importers.get(i);
        imp.stop();
    }
    m_isStarted = false;
}
Also used : SourceManager(org.apache.felix.ipojo.composite.util.SourceManager)

Aggregations

SourceManager (org.apache.felix.ipojo.composite.util.SourceManager)4 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)2 Filter (org.osgi.framework.Filter)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 Enumeration (java.util.Enumeration)1 Properties (java.util.Properties)1 PolicyServiceContext (org.apache.felix.ipojo.PolicyServiceContext)1 Element (org.apache.felix.ipojo.metadata.Element)1 ParseException (org.apache.felix.ipojo.parser.ParseException)1 BundleContext (org.osgi.framework.BundleContext)1