Search in sources :

Example 11 with Dependency

use of org.apache.felix.dm.Dependency in project felix by apache.

the class AbstractBuilder method addUnamedDependencies.

/**
 * Registers all unnamed dependencies into a given service. Named dependencies are
 * handled differently, and are managed by the ServiceLifecycleHandler class.
 * @throws Exception
 */
protected static void addUnamedDependencies(Bundle b, DependencyManager dm, Component s, MetaData srvMeta, List<MetaData> depsMeta) throws Exception {
    for (MetaData dependency : depsMeta) {
        String name = dependency.getString(Params.name, null);
        if (name == null) {
            DependencyBuilder depBuilder = new DependencyBuilder(dependency);
            Log.instance().info("adding dependency %s into service %s", dependency, srvMeta);
            Dependency d = depBuilder.build(b, dm);
            s.add(d);
        }
    }
}
Also used : Dependency(org.apache.felix.dm.Dependency)

Example 12 with Dependency

use of org.apache.felix.dm.Dependency in project felix by apache.

the class DependencyBuilder method build.

public Dependency build(Bundle b, DependencyManager dm) throws Exception {
    Dependency dp = null;
    DependencyType type;
    try {
        type = DependencyType.valueOf(m_metaData.getString(Params.type));
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("no \"type\" parameter found from metaData: " + m_metaData);
    }
    switch(type) {
        case ServiceDependency:
            dp = createServiceDependency(b, dm);
            break;
        case ConfigurationDependency:
            dp = createConfigurationDependency(b, dm);
            break;
        case BundleDependency:
            dp = createBundleDependency(dm);
            break;
        case ResourceDependency:
            dp = createResourceDependency(dm);
            break;
        default:
            throw new IllegalArgumentException("Can't build service dependency: " + type);
    }
    return dp;
}
Also used : ConfigurationDependency(org.apache.felix.dm.ConfigurationDependency) Dependency(org.apache.felix.dm.Dependency) ResourceDependency(org.apache.felix.dm.ResourceDependency) BundleDependency(org.apache.felix.dm.BundleDependency) ServiceDependency(org.apache.felix.dm.ServiceDependency)

Example 13 with Dependency

use of org.apache.felix.dm.Dependency in project felix by apache.

the class DependencyBuilder method createBundleDependency.

/**
 * Creates a BundleDependency that we parsed from a component descriptor entry.
 * @param b
 * @param dm
 * @param parser
 * @return
 */
private Dependency createBundleDependency(DependencyManager dm) {
    String added = m_metaData.getString(Params.added, null);
    String changed = m_metaData.getString(Params.changed, null);
    String removed = m_metaData.getString(Params.removed, null);
    boolean required = "true".equals(m_metaData.getString(Params.required, "true"));
    String filter = m_metaData.getString(Params.filter, null);
    int stateMask = m_metaData.getInt(Params.stateMask, -1);
    boolean propagate = "true".equals(m_metaData.getString(Params.propagate, "false"));
    Dependency dp = createBundleDependency(dm, added, changed, removed, required, propagate, filter, stateMask);
    return dp;
}
Also used : ConfigurationDependency(org.apache.felix.dm.ConfigurationDependency) Dependency(org.apache.felix.dm.Dependency) ResourceDependency(org.apache.felix.dm.ResourceDependency) BundleDependency(org.apache.felix.dm.BundleDependency) ServiceDependency(org.apache.felix.dm.ServiceDependency)

Example 14 with Dependency

use of org.apache.felix.dm.Dependency in project felix by apache.

the class FactorySet method doAdd.

/**
 * Add or create a new Service instance, given its configuration. This method is invoked by the
 * SerialExecutor, hence it's thread safe and we'll invoke Service's callbacks without being
 * synchronized (hence this will avoid potential dead locks).
 */
private void doAdd(Dictionary configuration) {
    // Check if the service exists.
    ServiceKey serviceKey = new ServiceKey(configuration);
    Object service = m_services.get(serviceKey);
    if (service == null || service == SERVICE_CREATING) {
        try {
            // Create the Service / impl, unless it is explicitly provided from the
            // configuration (using the specific key "dm.factory.instance").
            Component s = m_dm.createComponent();
            Class<?> implClass = m_bundle.loadClass(m_srvMeta.getString(Params.impl));
            Object impl = configuration.get(DM_FACTORY_INSTANCE);
            if (impl == null) {
                String factoryMethod = m_srvMeta.getString(Params.factoryMethod, null);
                if (factoryMethod == null) {
                    m_impl = implClass.newInstance();
                } else {
                    Method m = implClass.getDeclaredMethod(factoryMethod);
                    m.setAccessible(true);
                    m_impl = m.invoke(null);
                }
            } else {
                m_impl = impl;
            }
            // Invoke "configure" callback
            if (m_configure != null) {
                invokeConfigure(m_impl, m_configure, configuration);
            }
            // Create Service
            s.setImplementation(m_impl);
            if (m_provide != null) {
                // Merge service properties with the configuration provided by the factory.
                Dictionary serviceProperties = mergeSettings(m_serviceProperties, configuration);
                s.setInterface(m_provide, serviceProperties);
            }
            s.setComposition(m_srvMeta.getString(Params.composition, null));
            ServiceLifecycleHandler lfcleHandler = new ServiceLifecycleHandler(s, m_bundle, m_dm, m_srvMeta, m_depsMeta);
            // The dependencies will be plugged by our lifecycle handler.
            s.setCallbacks(lfcleHandler, "init", "start", "stop", "destroy");
            // Adds dependencies (except named dependencies, which are managed by the lifecycle handler).
            for (MetaData dependency : m_depsMeta) {
                String name = dependency.getString(Params.name, null);
                if (name == null) {
                    DependencyBuilder depBuilder = new DependencyBuilder(dependency);
                    Log.instance().info("ServiceLifecycleHandler.init: adding dependency %s into service %s", dependency, m_srvMeta);
                    Dependency d = depBuilder.build(m_bundle, m_dm);
                    s.add(d);
                }
            }
            // Register the Service instance, and keep track of it.
            Log.instance().info("ServiceFactory: created service %s", m_srvMeta);
            m_dm.add(s);
            m_services.put(serviceKey, s);
        } catch (Throwable t) {
            // Make sure the SERVICE_CREATING flag is also removed
            m_services.remove(serviceKey);
            Log.instance().error("ServiceFactory: could not instantiate service %s", t, m_srvMeta);
        }
    } else {
        // Reconfigure an already existing Service.
        if (m_configure != null) {
            Log.instance().info("ServiceFactory: updating service %s", m_impl);
            invokeConfigure(m_impl, m_configure, configuration);
        }
        // Update service properties
        if (m_provide != null) {
            Dictionary settings = mergeSettings(m_serviceProperties, configuration);
            ((Component) service).setServiceProperties(settings);
        }
    }
}
Also used : Dictionary(java.util.Dictionary) Method(java.lang.reflect.Method) Dependency(org.apache.felix.dm.Dependency) Component(org.apache.felix.dm.Component)

Aggregations

Dependency (org.apache.felix.dm.Dependency)14 Component (org.apache.felix.dm.Component)7 DependencyManager (org.apache.felix.dm.DependencyManager)7 BundleDependency (org.apache.felix.dm.BundleDependency)4 ConfigurationDependency (org.apache.felix.dm.ConfigurationDependency)4 ResourceDependency (org.apache.felix.dm.ResourceDependency)4 ServiceDependency (org.apache.felix.dm.ServiceDependency)4 Ensure (org.apache.felix.dm.itest.util.Ensure)4 URL (java.net.URL)3 Hashtable (java.util.Hashtable)3 ArrayList (java.util.ArrayList)2 Dictionary (java.util.Dictionary)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2 ResourceHandler (org.apache.felix.dm.ResourceHandler)2 DependencyContext (org.apache.felix.dm.context.DependencyContext)2 Method (java.lang.reflect.Method)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1