在Android中嵌入OSGi Felix

时间:2015-11-20 18:30:11

标签: java android osgi apache-felix

我的问题与旧的未解决的问题Android embedded Felix missing requirement osgi.ee非常相关。

我只是将 OSGi Felix 5.4.0 的实例嵌入到Android应用中,并尝试在其中安装 pure-Java 非常简单的Bundle,由以下代码:

@Component
public class ExampleBattery
{
    @Activate
    public void activate()
    {
        Thread t = new Thread(new Work());
        t.start();
    }

    private class Work implements Runnable
    {
        boolean continueLoop = true;

        @Override
        public void run() 
        {
            while (continueLoop)
            {
                System.out.println("hello");
                try 
                {
                    Thread.sleep(2500);
                } 
                catch (InterruptedException e) 
                {
                    continueLoop = false;
                }
            }
        }
    } 
}

显然在其自己的清单中需要 osgi.ee = JavaSE

Manifest-Version: 1.0
Bnd-LastModified: 1448019954932
Bundle-ManifestVersion: 2
Bundle-Name: ExampleBattery.ExampleBattery
Bundle-SymbolicName: ExampleBattery.ExampleBattery
Bundle-Version: 1.0.0
Created-By: 1.8.0_66 (Oracle Corporation)
Private-Package: user.producer.battery
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"
Service-Component: OSGI-INF/user.producer.battery.ExampleBattery.xml
Tool: Bnd-3.0.0.201509101326

以下代码显示了我在Android应用中的确切做法:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    launchFramework();
}

private void launchFramework()
{
    // properties Map for the Felix instance
    Map<String, String> configMap = configMap = new HashMap<String, String>();

    // set some properties (like Felix's cache directory, etc.)
    configMap.put("felix.cache.rootdir", felixBaseDir);                 
    configMap.put("org.osgi.framework.storage", FELIX_CACHE_DIR_NAME);  
    try
    {
        // create and initialize the instance of the Framework
        this.felixInstance = new Felix(configMap);
        this.felixInstance.init();
        Log.i("Felix", "Framework successfully created and initialized");

        // simply use the Bundle context of the Felix 
        // instance to install ExampleBattery from the project's assets folder
        installBasicBundlesFromAssets();
        Log.i("Felix", "All basic Bundles installed successfully");

        // start the framework
        this.felixInstance.start();
        Log.i("Felix", "Framework successfully started");       
        //
        Log.i("Felix", "Starting installed Bundles ...");

        // simply call "start()" on all the installed Bundles
        if (startInstalledBundles())
        {
            Log.i("Felix", "ALL OK");
        }
    }
    catch (Exception ex)
    {
        Log.e("Felix", "Could not create framework: " + ex);
        return;
    }
}

看起来非常简单,但是当我尝试启动 ExampleBattery 捆绑包时,我收到以下错误:

  

03-29 05:29:44.942:E / Felix(8156):无法启动Bundle“ExampleBattery.ExampleBattery”:    org.osgi.framework.BundleException:无法解析ExampleBattery.ExampleBattery [1](R 1.0):   缺少要求[ExampleBattery.ExampleBattery [1](R 1.0)] osgi.ee; (及(osgi.ee = JavaSE的)(版本= 1.7))   未解决的要求:[[ExampleBattery.ExampleBattery [1](R 1.0)] osgi.ee; (及(osgi.ee = JavaSE的)(版本= 1.7))]

这种情况非常奇怪,因为Felix默认使用 felix.jar 中的 default.properties 配置文件导出Java:

  

....
  org.osgi.framework.system.capabilities = $ {eecap - $ {java.specification.version}}

     

eecap-1.8 = osgi.ee; osgi.ee = “的OSGi /最小值”; version:List =“1.0,1.1,1.2”,osgi.ee; osgi.ee = “JavaSE的”;版本:列表= “1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8”
   eecap-1.7 = osgi.ee; osgi.ee = “的OSGi /最小值”; version:List =“1.0,1.1,1.2”,osgi.ee; osgi.ee = “JavaSE的”;版本:List =“1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7”
  ....

我真的不知道发生了什么,感谢大家的回答。

1 个答案:

答案 0 :(得分:1)

我可以通过将以下属性设置为felix来解决该问题:

hosSchema[0].status