我想嵌入一個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.
我想有我的軟件包有些問題,但它非常簡單,就像我上面描述的一樣。有任何想法嗎?
只是一個尼特,你的聲明有一個修改的方法,您的示例類沒有。 – eckes