Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fix compilation errors in example (reported by Roger Searjeant)

...

No Format
launcher/
   lib/
      org.apache.felix.main-1.0.34.jar
   bundle/
      org.apache.felix.shell-1.0.0.jar
      org.apache.felix.shell.tui-1.0.0.jar
   src/
      example/
         Main.java

...

Code Block
package example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.osgi.framework.Constants;
import org.apache.felix.framework.Felix;
import org.apache.felix.framework.cache.BundleCache;
import org.apache.felix.framework.util.StringMap;
import org.apache.felix.framework.main.AutoActivator;

public class Main
{
    private static Felix m_felix = null;

    public static void main(String[] argv) throws Exception
    {
        // Print welcome banner.
        System.out.println("\nWelcome to Felix.");
        System.out.println("=================\n");

        Map configMap = new StringMap(false);
        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");
        configMap.put(AutoActivator.AUTO_START_PROP + ".1",
            "file:bundle/org.apache.felix.shell-1.0.0.jar " +
            "file:bundle/org.apache.felix.shell.tui-1.0.0.jar");
        configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache");

        try
        {
            List list = new ArrayList();
            list.add(new AutoActivator(configProps));
            Map configMap = new StringMap(configProps, falseconfigMap));
            m_felix = new Felix(configMap, list);
            m_felix.start();
        }
        catch (Exception ex)
        {
            System.err.println("Could not create framework: " + ex);
            ex.printStackTrace();
            System.exit(-1);
        }
    }
}

...

This simply creates a map to hold configuration properties where the keys are strings and lookups are case insensitive, since the OSGi specification says that properties are not case sensitive.

Code Block
        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");

...

Code Block
            List list = new ArrayList();
            list.add(new AutoActivator(configPropsconfigMap));

This above creates a list to hold custom framework activators and adds an instance of org.apache.felix.main.AutoActivator to it, which will process the auto-install and auto-start configuration properties during framework startup.

Code Block
            Map configMap = new StringMap(configProps, false);

This above creates a case-insensitive map from the configuration properties, since the OSGi specification says that properties are not case sensitive.

Code Block

            m_felix = new Felix(configMap, list);
            m_felix.start();

...

No Format
javac -d . -classpath lib/org.apache.felix.main-1.0.24.jar src/example/Main.java

...

No Format
java -cp .:lib/org.apache.felix.main-1.0.24.jar example.Main

After executing this command, a cache/ directory is created that contains the installed bundles, which were installed from the bundle/ directory.

...

Code Block
public class HostApplication
{
    private HostActivator m_activator = null;
    private Felix m_felix = null;

    public HostApplication()
    {
        // Create a case-insensitive configuration property map.
        Map configMap = new StringMap(false);
        // Configure the Felix instance to be embedded.
        configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true");
        // Add 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");
        // Explicitly specify the directory to use for caching bundles.
        configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache");

        try
        {
            // Create host activator;
            m_activator = new HostActivator();
            List list = new ArrayList();
            list.add(m_activator);

            // Now create an instance of the framework with
            // our configuration properties and activator.
            m_felix = new Felix(configMap, list);

            // Now start Felix instance.
            m_felix.start();
        }
        catch (Exception ex)
        {
            System.err.println("Could not create framework: " + ex);
            ex.printStackTrace();
        }
    }

    public Bundle[] getInstalledBundles()
    {
        // Use the system bundle activator to gain external
        // access to the set of installed bundles.
        return m_activator.getBundles();
    }






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

...