Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Anchor
introduction
introduction

...

Code Block
package host.core;

import java.util.Map;
import host.service.command.Command;
import org.apache.felix.framework.Felix;
import org.apache.felix.framework.util.StringMap;
import org.apache.felix.framework.util.MutablePropertyResolverImpl;
import org.apache.felix.framework.cache.BundleCache;
import org.osgi.util.tracker.ServiceTracker;

public class HostApplication
{
    private HostActivator m_activator = null;
    private Felix m_felix = null;
    private ServiceTracker m_tracker = null;

    public HostApplication()
    {
        // Create a case-insensitive configuration property map.
        Map configMap = new StringMap(false);
        // Add the bundle provided service interface package and the core OSGi
        // packages to be exported from the class path via the system bundle.
        configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES,
            "org.osgi.framework; version=1.3.0," +
            "org.osgi.service.packageadmin; version=1.2.0," +
            "org.osgi.service.startlevel; version=1.0.0," +
            "org.osgi.service.url; version=1.0.0," +
            "host.service.command; version=1.0.0");
        // Explicitly specify the directory to use for caching bundles.
        configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache");

        try
        {
            // Now create an instance of the framework.
            m_felix = new Felix();

            // Create host activator;
            m_activator = new HostActivator(m_lookupMap);

            // Now start Felix instance with our config properties
            // and activator.
            m_felix.start(
                new MutablePropertyResolverImpl(configMap),
                m_activator);
        }
        catch (Exception ex)
        {
            System.err.println("Could not create framework: " + ex);
            ex.printStackTrace();
        }

        m_tracker = new ServiceTracker(
            m_activator.getContext(), Command.class.getName(), null);
        m_tracker.open();
    }

    public boolean execute(String name, String commandline)
    {
        // See if any of the currently tracked command services
        // match the specified command name, if so then execute it.
        Object[] services = m_tracker.getServices();
        for (int i = 0; (services != null) && (i < services.length); i++)
        {
            try
            {
                if (((Command) services[i]).getName().equals(name))
                {
                    return ((Command) services[i]).execute(commandline);
                }
            }
            catch (Exception ex)
            {
                // Since the services returned by the tracker could become
                // invalid at any moment, we will catch all exceptions, log
                // a message, and then ignore faulty services.
                System.err.println(ex);
            }
        }
        return false;
    }

    public void shutdownApplication()
    {
        // Shut down the felix framework when stopping the
        // host application.
        m_felix.shutdown();
    }
}

The above example is overly simplistic with respect to concurrency issues and error conditions, but it demonstrates the overall approach for using bundle-provided services from the host application.

Anchor
service-reflection
service-reflection

Using Bundle Services via Reflection

It possible for the host application to use services provided by bundles without having access to the service interface classes and thus not need to put the service interface classes on the class path. To do this, the host application uses the same general approach to acquire the system bundle context object, which it can use to look up service objects. Using either an LDAP filter or the service interface class name, the host application can retrieve the service object and then use standard Java reflection to invoke methods on the service object.

Anchor
caveat
caveat

Caveat

The code in this document has not been thoroughly tested or even compiled and may be out of date with respect to the current Felix source code. If you find errors please report them so the that they can be corrected.