Versions Compared

Key

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

...

This package is simply part of the host application, which is potentially packaged into a JAR file and started with the "java -jar" command. Assuming that we have a Now consider the following host application bundle activator as previously presented, the following code snippet shows how the host application could create an embedded version of the Felix framework and provide the property lookup service to installed bundles, which will be used to register/unregister the property lookup service when embedded framework instance starts/stops:

Code Block
package host.core;

import java.util.Map;
import javaorg.util.HashMap;
import host.service.lookuposgi.framework.BundleActivator;
import org.apache.felixosgi.framework.FelixBundleContext;
import org.apacheosgi.felix.framework.util.StringMapServiceRegistration;
import orghost.apache.felix.framework.util.MutablePropertyResolverImpl;
import org.apache.felix.framework.cache.BundleCache;service.lookup;

public class HostApplicationHostActivator implements BundleActivator
{
    private HostActivatorMap m_activatorlookupMap = null;
    private FelixBundleContext m_felixcontext = null;
    private MapServiceRegistration m_lookupMapregistration = new HashMap()null;

    public HostApplicationHostActivator(Map lookupMap)
    {
        // InitializeSave thea mapreference forto the propertyservice's lookupbacking servicestore.
        m_lookupMap.put("name1", "value1") = lookupMap;
    }

    m_lookupMap.put("name2", "value2");
public void start(BundleContext context)
    {
         m_lookupMap.put("name3", "value3");// Save a reference to the bundle context.
        m_lookupMap.put("name4", "value4")context = context;

        // Create a case-insensitiveproperty configurationlookup propertyservice mapimplementation.
        MapLookup configMaplookup = new StringMapLookup(false); {
        // Add the host providedpublic serviceObject interface package and the core OSGi
lookup(String name)
            //{
 packages to be exported from the class path via the system bundle.
       return configMapm_lookupMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES,get(name);
            "org.osgi.framework; version=1.3.0," +}
            "org.osgi.service.packageadmin; version=1.2.0," +
};
        // Register the property lookup  "org.osgi.service.startlevel; version=1.0.0," + and save
        //    "org.osgi.service.url; version=1.0.0," +the service registration.
        m_registration = m_context.registerService(
  "host.service.lookup; version=1.0.0");
        // Explicitly specify the directory to use for caching bundles.
        configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache");
 Lookup.class.getName(), lookup, null);
    }

    try
public void stop(BundleContext context)
     {
        // Unregister the property // Now create an instance of the frameworklookup service.
            m_felix = new Felix();

            // Create host activator;
  registration.unregister();
          m_activatorcontext = new HostActivator()null;
    }
}

Given the above host application bundle activator, the following code snippet shows how the host application could create an embedded version of the Felix framework and provide the property lookup service to installed bundles:

Code Block

package host.core;

import java.util.Map;
import java.util.HashMap;
import host.service.lookup;
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;

public class HostApplication
{
    private HostActivator m_activator = null;
    private Felix m_felix = null;
    private Map m_lookupMap = new HashMap();

    public HostApplication()
    {
        // Initialize the map for the property lookup service.
        m_lookupMap.put("name1", "value1");
        m_lookupMap.put("name2", "value2");
        m_lookupMap.put("name3", "value3");
        m_lookupMap.put("name4", "value4");

        // Create a case-insensitive configuration property map.
        Map configMap = new StringMap(false);
        // Add the host 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.lookup; 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();


            // Now start Felix instance with our config properties
            // andCreate host activator.;
            m_felix.start(
activator                = new MutablePropertyResolverImplHostActivator(configMap),m_lookupMap);

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

    public Bundle[] getInstalledBundles()
catch (Exception ex)
        {
        // Use the system bundle activator to gain external
   System.err.println("Could not create framework: " + ex);
      // access to the set of installed bundlesex.printStackTrace();
        return m_activator.getContext().getBundles();}
    }
}

Rather than having the host application bundle activator register the service, it is also possible for the the host application to simply get the bundle context from the bundle activator and register the service directly, but the presented approach is perhaps a little cleaner since it allows the host application to register/unregister the service when the system bundle starts/stops.

Anchor
host-service-usage
host-service-usage

...