Search in sources :

Example 1 with ComponentMetadata

use of org.apache.felix.scr.impl.metadata.ComponentMetadata 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 ComponentMetadata

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

the class SCRCheck method check.

@Override
public void check(final CheckContext ctx) throws IOException, MojoExecutionException {
    ctx.getLog().info("Checking for SCR descriptor...");
    final String val = ctx.getManifest().getMainAttributes().getValue("Service-Component");
    if (val != null) {
        final String[] components = val.split(",");
        for (final String cmp : components) {
            final File xmlFile = new File(ctx.getRootDir(), cmp.trim().replace('/', File.separatorChar));
            if (!xmlFile.exists()) {
                throw new MojoExecutionException("SCR descriptor file '" + cmp + "' not found in bundle");
            }
            final List<ComponentMetadata> mList = loadDescriptor(ctx, xmlFile);
            for (final ComponentMetadata md : mList) {
                check(ctx, md);
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata) File(java.io.File)

Example 3 with ComponentMetadata

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

the class BindMethodTest method newMetadata.

private ComponentMetadata newMetadata() {
    ComponentMetadata metadata = new ComponentMetadata(DSVersion.DS11);
    metadata.setName("foo");
    metadata.setImplementationClassName(Object.class.getName());
    metadata.validate(null);
    return metadata;
}
Also used : ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata)

Example 4 with ComponentMetadata

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

the class ConfiguredComponentHolderTest method test_singleton.

public void test_singleton() {
    // setup a holder
    final String name = "test.singleton";
    final ComponentMetadata cm = createComponentMetadata(name);
    final TestingConfiguredComponentHolder holder = new TestingConfiguredComponentHolder(cm);
    holder.enableComponents(false);
    // assert single component and no map
    final SingleComponentManager cmgr = getSingleManager(holder);
    assertNotNull("Expect single component manager", cmgr);
    assertEquals("Expect no other component manager list", 1, getComponentManagers(holder).size());
    // configure with the singleton configuration
    final Dictionary config = new Hashtable();
    config.put("value", name);
    TargetedPID targetedPid = new TargetedPID(name);
    holder.configurationUpdated(targetedPid, null, config, 0);
    // assert single component and no map
    final SingleComponentManager cmgrAfterConfig = getSingleManager(holder);
    assertNotNull("Expect single component manager", cmgrAfterConfig);
    assertEquals("Expect no other component manager list", 1, getComponentManagers(holder).size());
    // // assert configuration of single component
    final Map componentConfig = ((MockImmediateComponentManager) cmgrAfterConfig).getConfiguration();
    assertEquals("Expect exact configuration set", config, componentConfig);
    // unconfigure singleton
    holder.configurationDeleted(targetedPid, null);
    // assert single component and no map
    final SingleComponentManager cmgrAfterUnconfig = getSingleManager(holder);
    assertNotNull("Expect single component manager", cmgrAfterUnconfig);
    assertEquals("Expect no other component manager list", 1, getComponentManagers(holder).size());
// assert no configuration of single component
// TODO multipids fix, correct assertion        assertFalse( "Expect no configuration", cmgrAfterUnconfig.hasConfiguration() );
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata) TargetedPID(org.apache.felix.scr.impl.metadata.TargetedPID) Map(java.util.Map)

Example 5 with ComponentMetadata

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

the class ConfiguredComponentHolderTest method test_none.

public void test_none() {
    // setup a holder
    final String name = "test.none";
    final ComponentMetadata cm = createComponentMetadata(name);
    final TestingConfiguredComponentHolder holder = new TestingConfiguredComponentHolder(cm);
    holder.enableComponents(false);
    // assert single component and no map
    final SingleComponentManager cmgr = getSingleManager(holder);
    assertNotNull("Expect single component manager", cmgr);
    assertEquals("Expect no other component manager list", 1, getComponentManagers(holder).size());
}
Also used : ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata)

Aggregations

ComponentMetadata (org.apache.felix.scr.impl.metadata.ComponentMetadata)17 ComponentContainer (org.apache.felix.scr.impl.manager.ComponentContainer)3 SingleComponentManager (org.apache.felix.scr.impl.manager.SingleComponentManager)3 Bundle (org.osgi.framework.Bundle)3 BundleContext (org.osgi.framework.BundleContext)3 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 Dictionary (java.util.Dictionary)2 Hashtable (java.util.Hashtable)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ComponentMethodsImpl (org.apache.felix.scr.impl.inject.ComponentMethodsImpl)2 TargetedPID (org.apache.felix.scr.impl.metadata.TargetedPID)2 BaseObject (org.apache.felix.scr.impl.metadata.instances.BaseObject)2 Level1Object (org.apache.felix.scr.impl.metadata.instances.Level1Object)2 Level3Object (org.apache.felix.scr.impl.metadata.instances.Level3Object)2 Level2Object (org.apache.felix.scr.impl.metadata.instances2.Level2Object)2 Test (org.junit.Test)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ComponentException (org.osgi.service.component.ComponentException)2 BufferedReader (java.io.BufferedReader)1