Versions Compared

Key

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

...

Code Block
package example;

import java.io.*;
import org.osgi.framework.launch.*;
import org.apache.felix.main.AutoProcessor;

public class Main
{
    private static Framework m_fwk = null;

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

        try
        {
            m_fwk = getFrameworkFactory().newFramework(null);
            m_fwk.init()
            AutoProcessor.process(null, m_fwk.getBundleContext());
            m_fwk.start();
            m_fwk.waitForStop(0);
            System.exit(0);
        }
        catch (Exception ex)
        {
            System.err.println("Could not create framework: " + ex);
            ex.printStackTrace();
            System.exit(-1);
        }
    }

    private static FrameworkFactory getFrameworkFactory() throws Exception
    {
        URL url = Main.class.getClassLoader().getResource(
            "META-INF/services/org.osgi.framework.launch.FrameworkFactory");
        if (url != null)
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            try
            {
                for (String s = br.readLine(); s != null; s = br.readLine())
                {
                    s = s.trim();
                    // Try to load first non-empty, non-commented line.
                    if ((s.length() > 0) && (s.charAt(0) != '#'))
                    {
                        return (FrameworkFactory) Class.forName(s).newInstance();
                    }
                }
            }
            finally
            {
                if (br != null) br.close();
            }
        }

        throw new Exception("Could not find framework factory.");
    }
}

...

Code Block
            m_fwk.start();
            m_fwk.waitForStop(0);
            System.exit(0);

These final steps start the framework and cause the launching application thread to wait for the framework to stop and when it does the launching thread calls System.exit() to make sure the VM actually exits.

...