OSGi 中怎样自动启动所有 bundle

最近开发了一个 OSGi 的应用,部署之后发现,当应用启动的时候,几乎所有 bundle 都处于 Resolved 状态,而不是 Started 状态。
怎样启动这些 bundle 呢?有如下几种方法 :
1. 手工启动,即在 console 中使用命令 start N 来逐个启动所有bundle,其中 N 表示每个 bundle 的 id
    这种方法过于麻烦,要耗费大量时间,因此不可取。
2.在配置文件中声明为自动启动。在 WEB-INF\eclipse\configuration 中的 config.ini 中,如下配置:
     osgi.bundles=bundle1@start, bundle2@start,......bundleN@start
  这种方法可以自动启动所有bundle,但是写起来仍然比较麻烦,需要把所有bundle 一个一个都配置为@start。
3. 在应用的所有bundle 中选择一个bundle,将其在 config.ini 中配置为自动启动,然后在这个bundle 中,再把
  应用的所有其他bundle 启动起来。假定该bundle 的Activator 类为 OSGiStartingBundleActivator, 代码如下:

public class OSGiStartingBundleActivator implements BundleActivator
{
    public static BundleContext bundleContext = null;
    
    public void start(BundleContext context) throws Exception
    {
        bundleContext = context;
       
        // start bundles if it has been installed and not started
        Bundle[] allBundles = context.getBundles();
        for (int i=0; i<allBundles.length; i++)
        {
             Bundle currBundle = allBundles[i];
             if(currBundle instanceof BundleFragment)
             {
                  continue;
             }

            int currState = currBundle.getState();
            if ( Bundle.ACTIVE != currState && Bundle.RESOLVED==currState  )
            {
                    System.out.println("starting bundle : " + currBundle.getSymbolicName());

                    try
                    {
                            currBundle.start();
                    }
                    catch (BundleException e)
                    {
                            e.printStackTrace();
                    }
              }
          }
    }

    public void stop(BundleContext context) throws Exception
    {
    }
 }


posted @ 2009-09-29 00:31  coding_rabbit  阅读(1828)  评论(4编辑  收藏  举报