导航

OSGi系列 - 用Eclipse开发Bundle

Posted on 2012-05-15 15:01  eastson  阅读(3441)  评论(0编辑  收藏  举报

Bundle开发过程中如何调试一直是个困扰的问题,今天我们看看Eclipse在这方面是如何解决的。

 

我用的Eclipse JavaEE Indigo 3.7.2版本。

 

第一步:开启Eclipse,新增一个Plug-in项目。

 

第二步:输入项目名称HelloWorldBundle,注意这里的插件运行方式要选择an OSGi Framework

 

第三步:输入Bundle有关的元数据信息,这里我们使用默认值。我们选择自动产生Activator类,以便简化我们后面的处理。

 

第四步:我们选择从Hello OSGi Bundle模板生成Bundle项目。

 

第五步:输入Hello OSGi Bundle模板需要的启动和停止信息。

 

第六步:到此为止,Eclipse依据我们的选择自动生成了HelloWorldBundle插件。虽然这是一个非常简单的Bundle,但是毫无疑问,这个是一个完整的Bundle,它是可以直接运行的。

 

HelloWorldBundle的目录结构:

 

Activator.java文件的内容:

package helloworldbundle;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        System.out.println("Hello World!!");
    }
    
    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        System.out.println("Goodbye World!!");
    }

}

 

MANIFEST.MF的内容:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloWorldBundle
Bundle-SymbolicName: HelloWorldBundle
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: helloworldbundle.Activator
Import-Package: org.osgi.framework;version="1.3.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6

 

第七步:在Activator.java的start()stop()方法处增加两个断点:

 

第八步:在HelloWorldBundle项目上点击鼠标右键,在弹出的Debug As功能里面选择OSGi Framework

 

第九步:一切OK,Eclipse切换到了Debug视图,Console窗口出现了OSGi>提示符,代码也在我们定义的start断点处停了下来。

 

第十步:在OSGi>提示符后面输入close命令,以便停止OSGi框架(shutdown and exit)。这个时候,代码就会在stop断点处停下来。

 

使用Eclipse提供的调试功能,将可以让我们在Bundle的开发过程中如虎添翼了。

 

更多的参考链接:

Coding bundles on the Slug demo movie

Remote debugging on the Slug demo movie