Search in sources :

Example 1 with PropertyMetadata

use of org.apache.felix.scr.impl.metadata.PropertyMetadata in project felix by apache.

the class XmlHandler method readPropertiesEntry.

/**
 * Reads the name property file from the bundle owning this descriptor. All
 * properties read from the properties file are added to the current
 * component's property meta data list.
 *
 * @param entryName The name of the bundle entry containing the propertes
 *      to be added. This must not be <code>null</code>.
 *
 * @throws ParseException If the entry name is <code>null</code> or no
 *      entry with the given name exists in the bundle or an error occurrs
 *      reading the properties file.
 */
private void readPropertiesEntry(String entryName) throws ParseException {
    if (entryName == null) {
        throw new ParseException("Missing entry attribute of properties element", null);
    }
    URL entryURL = m_bundle.getEntry(entryName);
    if (entryURL == null) {
        throw new ParseException("Missing bundle entry " + entryName, null);
    }
    Properties props = new Properties();
    InputStream entryStream = null;
    try {
        entryStream = entryURL.openStream();
        props.load(entryStream);
    } catch (IOException ioe) {
        throw new ParseException("Failed to read properties entry " + entryName, ioe);
    } finally {
        if (entryStream != null) {
            try {
                entryStream.close();
            } catch (IOException ignore) {
            // don't care
            }
        }
    }
    // create PropertyMetadata for the properties from the file
    for (Map.Entry<Object, Object> pEntry : props.entrySet()) {
        PropertyMetadata prop = new PropertyMetadata();
        prop.setName(String.valueOf(pEntry.getKey()));
        prop.setValue(String.valueOf(pEntry.getValue()));
        m_currentComponent.addProperty(prop);
    }
}
Also used : InputStream(java.io.InputStream) PropertyMetadata(org.apache.felix.scr.impl.metadata.PropertyMetadata) ParseException(org.apache.felix.scr.impl.parser.ParseException) IOException(java.io.IOException) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL)

Example 2 with PropertyMetadata

use of org.apache.felix.scr.impl.metadata.PropertyMetadata in project felix by apache.

the class XmlHandler method startElement.

/**
 * Method called when a tag opens
 *
 * @param   uri
 * @param   localName
 * @param   attributes
 * @exception   ParseException
 */
public void startElement(String uri, String localName, Attributes attributes) throws ParseException {
    // So we check this for the first element, we receive.
    if (firstElement) {
        firstElement = false;
        if (localName.equals("component") && "".equals(uri)) {
            overrideNamespace = NAMESPACE_URI;
        }
    }
    if (overrideNamespace != null && "".equals(uri)) {
        uri = overrideNamespace;
    }
    // the namespace - we allow both: with or without namespace!
    if (this.isComponent && "".equals(uri)) {
        uri = NAMESPACE_URI;
    }
    // get the namespace code for the namespace uri
    DSVersion namespaceCode = NAMESPACE_CODE_MAP.get(uri);
    // from now on uri points to the namespace
    if (namespaceCode != null) {
        try {
            // 112.4.3 Component Element
            if (localName.equals("component")) {
                this.isComponent = true;
                // Create a new ComponentMetadata
                m_currentComponent = new ComponentMetadata(namespaceCode);
                // name attribute is optional (since DS 1.1)
                if (attributes.getAttribute("name") != null) {
                    m_currentComponent.setName(attributes.getAttribute("name"));
                }
                // enabled attribute is optional
                if (attributes.getAttribute("enabled") != null) {
                    m_currentComponent.setEnabled(attributes.getAttribute("enabled").equals("true"));
                }
                // immediate attribute is optional
                if (attributes.getAttribute("immediate") != null) {
                    m_currentComponent.setImmediate(attributes.getAttribute("immediate").equals("true"));
                }
                // factory attribute is optional
                if (attributes.getAttribute("factory") != null) {
                    m_currentComponent.setFactoryIdentifier(attributes.getAttribute("factory"));
                }
                // configuration-policy is optional (since DS 1.1)
                if (attributes.getAttribute("configuration-policy") != null) {
                    m_currentComponent.setConfigurationPolicy(attributes.getAttribute("configuration-policy"));
                }
                // activate attribute is optional (since DS 1.1)
                if (attributes.getAttribute("activate") != null) {
                    m_currentComponent.setActivate(attributes.getAttribute("activate"));
                }
                // deactivate attribute is optional (since DS 1.1)
                if (attributes.getAttribute("deactivate") != null) {
                    m_currentComponent.setDeactivate(attributes.getAttribute("deactivate"));
                }
                // modified attribute is optional (since DS 1.1)
                if (attributes.getAttribute("modified") != null) {
                    m_currentComponent.setModified(attributes.getAttribute("modified"));
                }
                // configuration-pid attribute is optional (since DS 1.2)
                String configurationPidString = attributes.getAttribute("configuration-pid");
                if (configurationPidString != null) {
                    String[] configurationPid = configurationPidString.split(" ");
                    m_currentComponent.setConfigurationPid(configurationPid);
                }
                m_currentComponent.setConfigurableServiceProperties("true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, CONFIGURABLE_SERVICE_PROPERTIES)));
                m_currentComponent.setPersistentFactoryComponent("true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, PERSISTENT_FACTORY_COMPONENT)));
                m_currentComponent.setDeleteCallsModify("true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, DELETE_CALLS_MODIFY)));
                if (attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, OBSOLETE_FACTORY_COMPONENT_FACTORY) != null) {
                    m_currentComponent.setObsoleteFactoryComponentFactory("true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, OBSOLETE_FACTORY_COMPONENT_FACTORY)));
                } else if (!namespaceCode.isDS13()) {
                    m_currentComponent.setObsoleteFactoryComponentFactory(m_globalObsoleteFactoryComponentFactory);
                }
                m_currentComponent.setConfigureWithInterfaces("true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, CONFIGURE_WITH_INTERFACES)));
                m_currentComponent.setDelayedKeepInstances(m_globalDelayedKeepInstances || "true".equals(attributes.getAttribute(NAMESPACE_URI_1_0_FELIX_EXTENSIONS, DELAYED_KEEP_INSTANCES)));
                // Add this component to the list
                m_components.add(m_currentComponent);
            } else // not inside a component element, ignore current element
            if (!this.isComponent) {
                m_logger.log(LogService.LOG_DEBUG, "Not currently parsing a component; ignoring element {0} (bundle {1})", new Object[] { localName, m_bundle.getLocation() }, null, null, null);
            } else // 112.4.4 Implementation
            if (localName.equals("implementation")) {
                // Set the implementation class name (mandatory)
                m_currentComponent.setImplementationClassName(attributes.getAttribute("class"));
            } else // 112.4.5 [...] Property Elements
            if (localName.equals("property")) {
                PropertyMetadata prop = new PropertyMetadata();
                // name attribute is mandatory
                prop.setName(attributes.getAttribute("name"));
                // type attribute is optional
                if (attributes.getAttribute("type") != null) {
                    prop.setType(attributes.getAttribute("type"));
                }
                // 112.4.5: If the value attribute is specified, the body of the element is ignored.
                if (attributes.getAttribute("value") != null) {
                    prop.setValue(attributes.getAttribute("value"));
                    m_currentComponent.addProperty(prop);
                } else {
                    // hold the metadata pending
                    m_pendingProperty = prop;
                }
            } else // 112.4.5 Properties [...] Elements
            if (localName.equals("properties")) {
                readPropertiesEntry(attributes.getAttribute("entry"));
            } else // 112.4.6 Service Element
            if (localName.equals("service")) {
                m_currentService = new ServiceMetadata();
                // servicefactory attribute is optional
                if (attributes.getAttribute("servicefactory") != null) {
                    m_currentService.setServiceFactory(attributes.getAttribute("servicefactory").equals("true"));
                }
                if (attributes.getAttribute("scope") != null) {
                    m_currentService.setScope(attributes.getAttribute("scope"));
                }
                m_currentComponent.setService(m_currentService);
            } else if (localName.equals("provide")) {
                m_currentService.addProvide(attributes.getAttribute("interface"));
            } else // 112.4.7 Reference element
            if (localName.equals("reference")) {
                ReferenceMetadata ref = new ReferenceMetadata();
                // name attribute is optional (since DS 1.1)
                if (attributes.getAttribute("name") != null) {
                    ref.setName(attributes.getAttribute("name"));
                }
                ref.setInterface(attributes.getAttribute("interface"));
                // Cardinality
                if (attributes.getAttribute("cardinality") != null) {
                    ref.setCardinality(attributes.getAttribute("cardinality"));
                }
                if (attributes.getAttribute("policy") != null) {
                    ref.setPolicy(attributes.getAttribute("policy"));
                }
                if (attributes.getAttribute("policy-option") != null) {
                    ref.setPolicyOption(attributes.getAttribute("policy-option"));
                }
                if (attributes.getAttribute("scope") != null) {
                    ref.setScope(attributes.getAttribute("scope"));
                }
                if (attributes.getAttribute("target") != null) {
                    ref.setTarget(attributes.getAttribute("target"));
                    PropertyMetadata prop = new PropertyMetadata();
                    prop.setName((ref.getName() == null ? ref.getInterface() : ref.getName()) + ".target");
                    prop.setValue(attributes.getAttribute("target"));
                    m_currentComponent.addProperty(prop);
                }
                // method reference
                ref.setBind(attributes.getAttribute("bind"));
                ref.setUpdated(attributes.getAttribute("updated"));
                ref.setUnbind(attributes.getAttribute("unbind"));
                // field reference
                ref.setField(attributes.getAttribute("field"));
                ref.setFieldOption(attributes.getAttribute("field-option"));
                ref.setFieldCollectionType(attributes.getAttribute("field-collection-type"));
                m_currentComponent.addDependency(ref);
            } else // used by the Maven SCR Plugin, which is just silently ignored)
            if (!localName.equals("components")) {
                m_logger.log(LogService.LOG_DEBUG, "Ignoring unsupported element {0} (bundle {1})", new Object[] { localName, m_bundle.getLocation() }, null, null, null);
            }
        } catch (Exception ex) {
            throw new ParseException("Exception during parsing", ex);
        }
    } else // used by the Maven SCR Plugin, which is just silently ignored)
    if (!localName.equals("components")) {
        m_logger.log(LogService.LOG_DEBUG, "Ignoring unsupported element '{'{0}'}'{1} (bundle {2})", new Object[] { uri, localName, m_bundle.getLocation() }, null, null, null);
    }
}
Also used : DSVersion(org.apache.felix.scr.impl.metadata.DSVersion) PropertyMetadata(org.apache.felix.scr.impl.metadata.PropertyMetadata) ParseException(org.apache.felix.scr.impl.parser.ParseException) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata) ServiceMetadata(org.apache.felix.scr.impl.metadata.ServiceMetadata) ReferenceMetadata(org.apache.felix.scr.impl.metadata.ReferenceMetadata) IOException(java.io.IOException) ParseException(org.apache.felix.scr.impl.parser.ParseException)

Aggregations

IOException (java.io.IOException)2 PropertyMetadata (org.apache.felix.scr.impl.metadata.PropertyMetadata)2 ParseException (org.apache.felix.scr.impl.parser.ParseException)2 InputStream (java.io.InputStream)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ComponentMetadata (org.apache.felix.scr.impl.metadata.ComponentMetadata)1 DSVersion (org.apache.felix.scr.impl.metadata.DSVersion)1 ReferenceMetadata (org.apache.felix.scr.impl.metadata.ReferenceMetadata)1 ServiceMetadata (org.apache.felix.scr.impl.metadata.ServiceMetadata)1