Search in sources :

Example 1 with XmlHandler

use of org.apache.felix.scr.impl.xml.XmlHandler in project felix by apache.

the class BundleComponentActivator method loadDescriptor.

private void loadDescriptor(final URL descriptorURL) {
    // simple path for log messages
    final String descriptorLocation = descriptorURL.getPath();
    InputStream stream = null;
    try {
        stream = descriptorURL.openStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        XmlHandler handler = new XmlHandler(m_bundle, this, getConfiguration().isFactoryEnabled(), getConfiguration().keepInstances());
        KXml2SAXParser parser;
        parser = new KXml2SAXParser(in);
        parser.parseXML(handler);
        // or one or more component elements embedded in a larger document
        for (Object o : handler.getComponentMetadataList()) {
            ComponentMetadata metadata = (ComponentMetadata) o;
            ComponentRegistryKey key = null;
            try {
                // check and reserve the component name (if not null)
                if (metadata.getName() != null) {
                    key = m_componentRegistry.checkComponentName(m_bundle, metadata.getName());
                }
                // validate the component metadata
                metadata.validate(this);
                // Request creation of the component manager
                ComponentHolder<?> holder = m_componentRegistry.createComponentHolder(this, metadata);
                // register the component after validation
                m_componentRegistry.registerComponentHolder(key, holder);
                m_holders.add(holder);
                log(LogService.LOG_DEBUG, "BundleComponentActivator : Bundle [{0}] ComponentHolder created for {1}", new Object[] { m_bundle.getBundleId(), metadata.getName() }, null, null, null);
            } catch (Throwable t) {
                // There is a problem with this particular component, we'll log the error
                // and proceed to the next one
                log(LogService.LOG_ERROR, "Cannot register Component", metadata, null, t);
                // make sure the name is not reserved any more
                if (key != null) {
                    m_componentRegistry.unregisterComponentHolder(key);
                }
            }
        }
    } catch (IOException ex) {
        // 112.4.1 If an XML document specified by the header cannot be located in the bundle and its attached
        // fragments, SCR must log an error message with the Log Service, if present, and continue.
        log(LogService.LOG_ERROR, "Problem reading descriptor entry ''{0}''", new Object[] { descriptorLocation }, null, null, ex);
    } catch (Exception ex) {
        log(LogService.LOG_ERROR, "General problem with descriptor entry ''{0}''", new Object[] { descriptorLocation }, null, null, ex);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata) ComponentException(org.osgi.service.component.ComponentException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) KXml2SAXParser(org.apache.felix.scr.impl.parser.KXml2SAXParser) XmlHandler(org.apache.felix.scr.impl.xml.XmlHandler) BufferedReader(java.io.BufferedReader)

Example 2 with XmlHandler

use of org.apache.felix.scr.impl.xml.XmlHandler in project felix by apache.

the class XmlHandlerTest method readMetadata.

// ---------- helper
private List readMetadata(final Reader reader) throws IOException, ComponentException, XmlPullParserException, Exception {
    try {
        final KXml2SAXParser parser = new KXml2SAXParser(reader);
        XmlHandler handler = new XmlHandler(new MockBundle(), logger, false, false);
        parser.parseXML(handler);
        return handler.getComponentMetadataList();
    } finally {
        try {
            reader.close();
        } catch (IOException ignore) {
        }
    }
}
Also used : XmlHandler(org.apache.felix.scr.impl.xml.XmlHandler) MockBundle(org.apache.felix.scr.impl.MockBundle) IOException(java.io.IOException) KXml2SAXParser(org.apache.felix.scr.impl.parser.KXml2SAXParser)

Aggregations

IOException (java.io.IOException)2 KXml2SAXParser (org.apache.felix.scr.impl.parser.KXml2SAXParser)2 XmlHandler (org.apache.felix.scr.impl.xml.XmlHandler)2 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 MockBundle (org.apache.felix.scr.impl.MockBundle)1 ComponentMetadata (org.apache.felix.scr.impl.metadata.ComponentMetadata)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ComponentException (org.osgi.service.component.ComponentException)1