Search in sources :

Example 96 with Factory

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

the class SvcInstance method match.

/**
 * Does the service instance match with the given factory ?
 * @param fact : the factory to test.
 * @return true if the factory match, false otherwise.
 */
public boolean match(ServiceReference fact) {
    // Check if the factory can provide the specification
    ComponentTypeDescription desc = (ComponentTypeDescription) fact.getProperty("component.description");
    if (desc == null) {
        // No component type description.
        return false;
    }
    String[] provides = desc.getprovidedServiceSpecification();
    for (int i = 0; provides != null && i < provides.length; i++) {
        if (provides[i].equals(m_specification)) {
            // Check that the factory needs every properties contained in
            // the configuration
            PropertyDescription[] props = desc.getProperties();
            Properties conf = new Properties();
            Enumeration keys = m_configuration.keys();
            while (keys.hasMoreElements()) {
                String key = (String) keys.nextElement();
                if (!containsProperty(key, props)) {
                    return false;
                }
                conf.put(key, m_configuration.get(key));
            }
            Factory factory = (Factory) getService(fact);
            return factory.isAcceptable(conf);
        }
    }
    return false;
}
Also used : PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) ComponentTypeDescription(org.apache.felix.ipojo.architecture.ComponentTypeDescription) Enumeration(java.util.Enumeration) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties)

Example 97 with Factory

use of org.apache.felix.ipojo.Factory 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 98 with Factory

use of org.apache.felix.ipojo.Factory 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 99 with Factory

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

the class TestBothProperties method setUp.

@Before
public void setUp() {
    Factory fact = ipojoHelper.getFactory("CONFIG-BothConfigurableCheckService");
    Properties props = new Properties();
    props.put("instance.name", "under-test");
    props.put("b", "1");
    props.put("s", "1");
    props.put("i", "1");
    props.put("l", "1");
    props.put("d", "1");
    props.put("f", "1");
    props.put("c", "a");
    props.put("bool", "true");
    props.put("bs", "{1,2,3}");
    props.put("ss", "{1,2,3}");
    props.put("is", "{1,2,3}");
    props.put("ls", "{1,2,3}");
    props.put("ds", "{1,2,3}");
    props.put("fs", "{1,2,3}");
    props.put("cs", "{a,b,c}");
    props.put("bools", "{true,true,true}");
    props.put("string", "foo");
    props.put("strings", "{foo, bar, baz}");
    try {
        instance = fact.createComponentInstance(props);
    } catch (Exception e) {
        fail("Cannot create the under-test instance : " + e.getMessage());
    }
    try {
        instance2 = fact.createComponentInstance(null);
    } catch (Exception e) {
        fail("Cannot create the under-test instance2 : " + e.getMessage());
    }
}
Also used : Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) Before(org.junit.Before)

Example 100 with Factory

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

the class TestFieldProperties method setUp.

@Before
public void setUp() {
    osgiHelper = new OSGiHelper(bc);
    ipojoHelper = new IPOJOHelper(bc);
    Factory fact = ipojoHelper.getFactory("CONFIG-FieldConfigurableCheckService");
    Properties props = new Properties();
    props.put("instance.name", "under-test");
    props.put("b", "1");
    props.put("s", "1");
    props.put("i", "1");
    props.put("l", "1");
    props.put("d", "1");
    props.put("f", "1");
    props.put("c", "a");
    props.put("bool", "true");
    props.put("bs", "{1,2,3}");
    props.put("ss", "{1,2,3}");
    props.put("is", "{1,2,3}");
    props.put("ls", "{1,2,3}");
    props.put("ds", "{1,2,3}");
    props.put("fs", "{1,2,3}");
    props.put("cs", "{a,b,c}");
    props.put("bools", "{true,true,true}");
    props.put("string", "foo");
    props.put("strings", "{foo, bar, baz}");
    try {
        instance = fact.createComponentInstance(props);
    } catch (Exception e) {
        fail("Cannot create the under-test instance : " + e.getMessage());
    }
    try {
        instance2 = fact.createComponentInstance(null);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Cannot create the instance : " + e.getMessage());
    }
}
Also used : IPOJOHelper(org.ow2.chameleon.testing.helpers.IPOJOHelper) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) OSGiHelper(org.ow2.chameleon.testing.helpers.OSGiHelper) Before(org.junit.Before)

Aggregations

Factory (org.apache.felix.ipojo.Factory)111 Properties (java.util.Properties)88 Test (org.junit.Test)71 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)53 Before (org.junit.Before)33 ServiceReference (org.osgi.framework.ServiceReference)29 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)17 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)17 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)14 ServiceContext (org.apache.felix.ipojo.ServiceContext)13 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)11 IOException (java.io.IOException)6 HandlerFactory (org.apache.felix.ipojo.HandlerFactory)6 Architecture (org.apache.felix.ipojo.architecture.Architecture)4 HandlerDescription (org.apache.felix.ipojo.architecture.HandlerDescription)4 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)4 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)4 CompositeManager (org.apache.felix.ipojo.composite.CompositeManager)4 ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)4 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)4