2013-07-21 33 views
0

我正在尝试学习OSGI。 (主要是捆绑的动态加载和卸载)。OSGI软件包已安装并已启动,但没有可见的输出

继Neil Bartlett的教程How To Embed OSGi后,我将Equinox OSGi框架实现添加到类路径并启动了游戏。

下面的代码:

import java.util.HashMap; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Map; 
import java.util.ServiceLoader; 

import org.osgi.framework.Bundle; 
import org.osgi.framework.BundleContext; 
import org.osgi.framework.BundleException; 
import org.osgi.framework.Constants; 
import org.osgi.framework.launch.Framework; 
import org.osgi.framework.launch.FrameworkFactory; 


public class BundleManager { 

    /** 
    * @param args 
    * @throws Exception 
    */ 
    public static void main(String[] args) throws Exception { 
     // TODO Auto-generated method stub 

     FrameworkFactory frameworkFactory = ServiceLoader.load(
       FrameworkFactory.class).iterator().next(); 
     Map<String, String> config = new HashMap<String, String>(); 
     //TODO: add some config properties 
     Framework framework = frameworkFactory.newFramework(config); 
     framework.start(); 



     BundleContext context = framework.getBundleContext(); 
     List<Bundle> installedBundles = new LinkedList<Bundle>(); 

     installedBundles.add(context.installBundle(
           "file:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar")); 


     for (Bundle bundle : installedBundles) { 

     if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) == null) 
       bundle.start(); 

     } 


     System.out.println("done!!"); 



    } 

} 

是的,它的工作原理。根本没有错误。但是,我在路径中安装的是一个jar文件的包:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar在其启动方法中包含一个“HelloWorld”。我在eclipse控制台中没有看到“HelloWold”。为什么我看不到那个消息,虽然捆绑包已经启动了?我感谢任何简单的帮助。

注:HellowService.jar是我前面创建的插件项目,在其类中的一个实现BundleActivator的在开始添加方法“HelloWorld”的消息,终于导出它作为一个jar文件的目录C:/Users/student/Documents/eclipse/myPlugins/

编辑:下面是我安装的软件包和启动我的Activator类:

package com.javaworld.sample.service.impl; 

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

import com.javaworld.sample.service.HelloService; 

public class HelloServiceActivator implements BundleActivator { 

    ServiceRegistration helloServiceRegistration; 


     public void start(BundleContext context) throws Exception { 

      HelloServiceFactory helloServiceFactory = new HelloServiceFactory(); 
      helloServiceRegistration =context.registerService(HelloService.class.getName(), helloServiceFactory, null); 

      System.out.println("Hello World!"); 
     } 
     public void stop(BundleContext context) throws Exception { 
      helloServiceRegistration.unregister(); 
     } 


} 

而这里的包的MANIFEST.MF文件:

Manifest-Version: 1.0 
Bundle-ManifestVersion: 2 
Bundle-Name: HelloService 
Bundle-SymbolicName: com.javaworld.sample.HelloService 
Bundle-Version: 1.0.0.qualifier 
Bundle-Activator: com.javaworld.sample.service.impl.HelloServiceActivator 
Bundle-Vendor: JAVAWORLD 
Bundle-RequiredExecutionEnvironment: JavaSE-1.7 
Import-Package: org.osgi.framework;version="1.3.0" 
Export-Package: com.javaworld.sample.service 

我导出包的方式是右键单击bundle项目 - >导出 - >运行jar文件 - >然后我选择启动配置为BundleManager(这是安装包的类)。

我还没有看到“Hello World!”当我从我的应用程序启动包时出现消息。

回答

0

原来我错误地导出了这个包。那是因为我试图自己做。这里的包应如何导出为一个jar文件:

Open the plugin export wizard File > Export... > Plug-in Development > 
    Deployable plug-ins and fragments . 
    Then select the bundle you want to export and the destination directory. Done! 

您现在可以使用的jar文件的路径安装包。在我的情况,那就是:

installedBundles.add(context.installBundle(
           "file:C:/Users/student/Documents/eclipse/myPlugins/plugins/com.javaworld.sample.HelloService_1.0.0.201307220322.jar")); 

来源:http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.pde.doc.user%2Fguide%2Ftools%2Fexport_wizards%2Fexport_plugins.htm

而且可以肯定由于@Neil巴特利特

1

您的启动程序不会等待OSGi Framework停止。我希望这个程序能够启动一切,但之后立即关闭,因为我们到达了方法的末尾。参考我的教程,我将展示如何使用Framework.waitForStop方法。

话虽如此,我希望你的HelloWorld包的输出实际上出现在关闭之前。因此,该捆绑包中也可能存在错误。也许你还没有宣布催化剂?我只能猜测,因为你没有给出任何细节。

+0

是,尼尔......我不为一时照顾,当它停止,但我会稍后添加它(现在我添加了它,但我仍然看不到'你好')。声明激活器是什么意思?我似乎没有这样做。哪里,我该怎么做?谢谢。 –

+0

以下是关于Bundle-Activator的更多信息:http://wiki.osgi。org/wiki/Bundle-Activator –

+0

如果你的意思是实现BundleActivator启动和停止方法,那么我已经提到过我在我的问题中这样做了。我没有在您发送的链接中看到任何新内容。这基本上是我开始的。 –

相关问题