2014-01-07 43 views
1

我想嵌入一個OSGI包在我的Java應用程序中使用。嵌入OSGI聲明性服務包工作正常,但沒有輸出可見

下面是捆綁我唯一的java文件(我這樣做,現在簡單):

package it.eng.test.ds.consumer; 

public class Consumer { 


    public void activate(){ 
     System.out.println("I'm here, in the activate ds automatic method ;)"); 
    } 

    public void deactivate() 
    { 
     System.out.println("Bye Bye!"); 
    } 

} 

下面是consumer.xml

<?xml version="1.0" encoding="UTF-8"?> 
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="it.eng.test.ds.consumer" 
activate="activate" 
deactivate="deactivate" 
modified="modified" 
enabled="true" 
immediate="true"> 
    <implementation class="it.eng.test.ds.consumer.Consumer"/> 

</scr:component> 

下面是MANIFEST.MF

Manifest-Version: 1.0 
Bundle-ManifestVersion: 2 
Bundle-Name: Ds-bundle 
Bundle-SymbolicName: it.eng.test.ds.consumer 
Bundle-Version: 1.0.0.qualifier 
Bundle-RequiredExecutionEnvironment: JavaSE-1.7 
-Component: OSGI-INF/consumer.xml 

接下來,我在我的應用程序中嵌入了Equinox,啓動了所需的捆綁包(org.eclipse.equinox.ds及其依賴項),最後啓動了我的軟件包,名爲it.eng.test.ds.consumer_1.0.0.201401071018.jar

下面是我的應用程序代碼:

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.launch.Framework; 
import org.osgi.framework.launch.FrameworkFactory; 


public class MainEmbedder { 


    public static void main(String[] args) 
    { 


     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); 
     try { 
      framework.start(); 
     } catch (BundleException e) { 

      e.printStackTrace(); 
     } 


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

     try { 



      installedBundles.add(context.installBundle("file:C:\\Users\\student\\Desktop\\DS-Plugins\\org.eclipse.osgi.services_3.3.100.v20120522-1822.jar")); 


      installedBundles.add(context.installBundle("file:C:\\Users\\student\\Desktop\\DS-Plugins\\org.eclipse.equinox.util_1.0.200.v20100503.jar")); 

      installedBundles.add(context.installBundle(
           "file:C:\\Users\\student\\Desktop\\DS-Plugins\\org.eclipse.equinox.ds_1.4.1.v20120926-201320.jar")); 


      installedBundles.add(context.installBundle(
        "file:C:\\Users\\student\\Desktop\\DS-Plugins\\plugins\\it.eng.test.ds.consumer_1.0.0.201401071018.jar")); 




     } catch (BundleException e) { 
      e.printStackTrace(); 
     } 


     for (Bundle bundle : installedBundles) { 

      try { 
       bundle.start(); 


      } catch (BundleException e) { 

       e.printStackTrace(); 
      } 

     }System.out.println("after starting bundles"); 





    } 
} 

當我運行我的應用程序,我可以看到消息"after starting bundles",但我沒有看到消息"I'm here, in the activate ds automatic method ;)"。這意味着我的包沒有進入激活方法。這是爲什麼?我怎麼解決這個問題?謝謝。

更新:
我試圖啓動與Felix框架包,使用下面的代碼:

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import org.apache.felix.framework.Felix; 
import org.apache.felix.framework.util.FelixConstants; 
import org.osgi.framework.Bundle; 
import org.osgi.framework.BundleContext; 
import org.osgi.framework.BundleException; 
import org.osgi.framework.Constants; 
import org.osgi.framework.ServiceRegistration; 

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


    public HostApplication() 
    { 
     // Create a configuration property map. 
     Map config = new HashMap(); 
     config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT); 
     // Create host activator; 
     m_activator = new HostActivator(); 
     List list = new ArrayList(); 
     list.add(m_activator); 

     config.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list); 

     try 
     { 
      // Now create an instance of the framework with 
      // our configuration properties. 
      m_felix = new Felix(config); 
      // Now start Felix instance. 
      m_felix.start(); 
     } 
     catch (Exception ex) 
     { 
      System.err.println("Could not create framework: " + ex); 
      ex.printStackTrace(); 
     } 




     // Register the application's context as an OSGi service! 
     BundleContext bundleContext1 = m_felix.getBundleContext(); 



       System.out.println("6"); 

       try { 
        Bundle dsBundle = bundleContext1.installBundle("file:C:\\Users\\student\\Desktop\\DS-Plugins\\org.apache.felix.scr-1.8.0.jar"); 
        dsBundle.start(); 
        if(dsBundle.getState()== dsBundle.ACTIVE) 
         System.out.println("DS Bundle is Active!"); 

        Bundle consumerBundle = bundleContext1.installBundle("file:C:\\Users\\student\\Desktop\\DS-Plugins\\plugins\\it.eng.test.ds.consumer_1.0.0.201401071018.jar"); 
        consumerBundle.start(); 
        if(consumerBundle.getState()== consumerBundle.ACTIVE) 
         System.out.println("Consumer Bundle is Active!"); 


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


       } catch (BundleException e) { 

        e.printStackTrace(); 

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





       shutdownApplication(); 


    } 

    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. 
     try { 
      m_felix.stop(); 
     } catch (BundleException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     try { 
      m_felix.waitForStop(0); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

現在我得到以下錯誤:

ERROR: it.eng.test.ds.consumer (2): Component descriptor entry 'OSGI-INF/consumer.xml' not found. 

我想有我的軟件包有些問題,但它非常簡單,就像我上面描述的一樣。有任何想法嗎?

+0

只是一個尼特,你的聲明有一個修改的方法,您的示例類沒有。 – eckes

回答

1

我已經重播了你的設置,我已經開始工作了。

(A GitHub庫爲here,如果一切都失敗)

檢查OSGI-INF /實際上是在你的包,你將它添加到build.properties(如果您使用PDE)

問候,弗蘭克

+0

謝謝。我會考慮你的提示。注意:我沒有將equinox本身作爲一個包(org.eclipse.osgi)來安裝。在那行代碼中,我安裝了org.eclipse.osgi.services,但這只是我在輸入問題時犯的一個錯誤。你可以現在檢查我的編輯代碼。 –

+0

我想這可能是捆綁版本。任何指向您使用的軟件包的鏈接? –

+1

他們都在我已經發布的回購,但給你的更新,我真的認爲你的問題是你的build.properties –

0

我不確定你的確切意圖是什麼,但將osgi包嵌入標準的java應用程序可能不是最好的事情。我會建議將你的應用程序轉換爲osgi(如果可能,它不是太大)。如果您對那些考慮使用ds組件模擬包的方法無能爲力,例如只需手動創建組件實例並步行設置它的依賴關係即可。但不要混淆兩個世界。

+0

「手動創建組件實例」是什麼意思?你能給個例子嗎? –

+0

只是用新的例如新的MyComponent(); – MikeR