Search in sources :

Example 6 with ParseException

use of org.apache.felix.ipojo.parser.ParseException in project felix by apache.

the class InstanceHandler method configure.

/**
 * Configure method.
 * @param metadata : component type metadata.
 * @param configuration : instance configuration.
 * @throws ConfigurationException : occurs an instance cannot be parsed correctly.
 * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
 */
public void configure(Element metadata, Dictionary configuration) throws ConfigurationException {
    m_scope = getCompositeManager().getServiceContext();
    // Prepare the configuration to append.
    Properties toAppend = new Properties();
    Enumeration keys = configuration.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, configuration.get(key));
        }
    }
    Element[] instances = metadata.getElements("instance");
    m_configurations = new ManagedConfiguration[instances.length];
    for (int i = 0; i < instances.length; i++) {
        Properties conf = null;
        try {
            conf = parseInstance(instances[i]);
        } catch (ParseException e) {
            error("An instance cannot be parsed correctly", e);
            throw new ConfigurationException("An instance cannot be parsed correctly", e);
        }
        Properties instanceConfiguration = new Properties();
        instanceConfiguration.putAll(conf);
        instanceConfiguration.putAll(toAppend);
        m_configurations[i] = new ManagedConfiguration(instanceConfiguration);
    }
    m_description = new InstanceHandlerDescription(this, m_configurations);
}
Also used : Enumeration(java.util.Enumeration) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Element(org.apache.felix.ipojo.metadata.Element) ParseException(org.apache.felix.ipojo.parser.ParseException) Properties(java.util.Properties)

Example 7 with ParseException

use of org.apache.felix.ipojo.parser.ParseException 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 8 with ParseException

use of org.apache.felix.ipojo.parser.ParseException 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 9 with ParseException

use of org.apache.felix.ipojo.parser.ParseException in project felix by apache.

the class TestInstantiate method getInstanceMetadata.

/**
 * Returns the instance metadatas of the component with the given name,
 * defined in the given bundle.
 *
 * @param bundle    the bundle from which the component is defined.
 * @param component the name of the defined component.
 * @return the list of instance metadata of the component with the given name,
 *         defined in the given bundle, or {@code null} if not found.
 */
public static Element[] getInstanceMetadata(Bundle bundle, String component) {
    // Retrieves the component description from the bundle's manifest.
    String elem = (String) bundle.getHeaders().get("iPOJO-Components");
    if (elem == null) {
        throw new IllegalArgumentException("Cannot find iPOJO-Components descriptor in the specified bundle (" + bundle.getSymbolicName() + "). Not an iPOJO bundle.");
    }
    // Parses the retrieved description and find the component with the
    // given name.
    List<Element> list = new ArrayList<Element>();
    try {
        Element element = ManifestMetadataParser.parseHeaderMetadata(elem);
        Element[] childs = element.getElements("instance");
        for (int i = 0; i < childs.length; i++) {
            String name = childs[i].getAttribute("component");
            if (name != null && name.equalsIgnoreCase(component)) {
                list.add(childs[i]);
            }
        }
        if (list.isEmpty()) {
            // Component not found...
            return null;
        } else {
            return (Element[]) list.toArray(new Element[list.size()]);
        }
    } catch (ParseException e) {
        throw new IllegalStateException("Cannot parse the components from specified bundle (" + bundle.getSymbolicName() + "): " + e.getMessage());
    }
}
Also used : Element(org.apache.felix.ipojo.metadata.Element) ArrayList(java.util.ArrayList) ParseException(org.apache.felix.ipojo.parser.ParseException)

Example 10 with ParseException

use of org.apache.felix.ipojo.parser.ParseException in project felix by apache.

the class TestBadLFCCallback method getManipulationForComponent.

private Element getManipulationForComponent() {
    // On KF we must cast the result.
    String header = (String) getTestBundle().getHeaders().get("iPOJO-Components");
    Element elem = null;
    try {
        elem = ManifestMetadataParser.parseHeaderMetadata(header);
    } catch (ParseException e) {
        fail("Parse Exception when parsing iPOJO-Component");
    }
    assertNotNull("Check elem not null", elem);
    Element manip = getManipulationForComponent(elem, type);
    assertNotNull("Check manipulation metadata not null for " + type, manip);
    return manip;
}
Also used : Element(org.apache.felix.ipojo.metadata.Element) ParseException(org.apache.felix.ipojo.parser.ParseException)

Aggregations

Element (org.apache.felix.ipojo.metadata.Element)14 ParseException (org.apache.felix.ipojo.parser.ParseException)14 Properties (java.util.Properties)4 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)3 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 Enumeration (java.util.Enumeration)2 Comparator (java.util.Comparator)1 Dictionary (java.util.Dictionary)1 ServiceImporter (org.apache.felix.ipojo.composite.service.instantiator.ServiceImporter)1 SourceManager (org.apache.felix.ipojo.composite.util.SourceManager)1 Dependency (org.apache.felix.ipojo.handlers.dependency.Dependency)1 Attribute (org.apache.felix.ipojo.metadata.Attribute)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Filter (org.osgi.framework.Filter)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 BaseTest (org.ow2.chameleon.testing.helpers.BaseTest)1