Starting a Bundle on Knopflerfish embedded into Android doesn't work

人走茶凉 提交于 2019-12-25 10:58:50

问题


I am embedding Knopflerfish framework into an android application to start and stop bundles dynamically.

I followed this tutorial, downloaded framework.jar frome this link, and added it to my class path in my eclipse project.

Additionally, below is Activity class, which launches the framework and starts one bundle:

package com.example.knopflerfish_android;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

import org.knopflerfish.framework.FrameworkFactoryImpl;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
import org.osgi.service.startlevel.StartLevel;




public class MainActivity extends Activity {

private static final String TAG = "Zaid Log";
private Framework mFramework;

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


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

        Map<String, String> fwprops = new Hashtable<String, String>(); //CHANGED

        // add any framework properties to fwprops
        fwprops.put("org.osgi.framework.storage", "sdcard/fwdir");

        FrameworkFactory ff = new FrameworkFactoryImpl();
        mFramework = ff.newFramework(fwprops);



        try {

            mFramework.init(); //STUCK HERE!!


        } catch (BundleException be) {
            // framework initialization failed

            Log.d(TAG, be.getStackTrace().toString());
            Log.d(TAG,"failed to initialize Framework");
        }



        setInitlevel(1);


        installBundle("Bundle_AndroidAPI_1.0.0.201308160327.jar");
        startBundle("Bundle_AndroidAPI_1.0.0.201308160327.jar");
        // install/start other bundles...


        setStartLevel(10);

        try {
            mFramework.start();
        } catch (BundleException be) {
            Log.e(TAG, be.toString());
            // framework start failed
        }

        Log.d(TAG, "OSGi framework running, state: " + mFramework.getState());


      /*  helloServiceReference= context.getServiceReference(HelloService.class.getName());
        HelloService helloService =(HelloService)context.getService(helloServiceReference);
        System.out.println(helloService.sayHello());*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    private void startBundle(String bundle) {
        Log.d(TAG, "starting bundle " + bundle);
        InputStream bs;
        try {
            bs = getAssets().open("bundles/" + bundle);
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            return;
        }

        long bid = -1;
        org.osgi.framework.Bundle[] bl = mFramework.getBundleContext().getBundles();
        for (int i = 0; bl != null && i < bl.length; i++) {
            if (bundle.equals(bl[i].getLocation())) {
                bid = bl[i].getBundleId();
            }
        }

        org.osgi.framework.Bundle b = mFramework.getBundleContext().getBundle(bid);
        if (b == null) {
            Log.e(TAG, "can't start bundle " + bundle);

            return;
        }

        try {
            b.start(org.osgi.framework.Bundle.START_ACTIVATION_POLICY);
            Log.d(TAG, "bundle " + b.getSymbolicName() + "/" + b.getBundleId() + "/"
                    + b + " started");
            Toast.makeText(getApplicationContext(),"bundle " + b.getSymbolicName() + "/" + b.getBundleId() + "/"
                    + b + " started" , Toast.LENGTH_SHORT).show();


        } catch (BundleException be) {
            Log.e(TAG, be.toString());
        }

        try {
            bs.close();
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        }
    }

    private void installBundle(String bundle) {
        Log.d(TAG, "installing bundle " + bundle);
        Toast.makeText(getApplicationContext(), "installing bundle", Toast.LENGTH_SHORT).show();

        InputStream bs;
        try {
            bs = getAssets().open("bundles/" + bundle);
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            return;
        }

        try {
            mFramework.getBundleContext().installBundle(bundle, bs);
            Log.d(TAG, "bundle " + bundle + " installed");
            Toast.makeText(getApplicationContext(), "bundle " + bundle + " installed", Toast.LENGTH_SHORT).show();
        } catch (BundleException be) {
            Log.e(TAG, be.toString());
        }

        try {
            bs.close();
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        }
    }

    private void setStartLevel(int startLevel) {
        ServiceReference sr = mFramework.getBundleContext()
            .getServiceReference(StartLevel.class.getName());
        if (sr != null) {
            StartLevel ss =
                (StartLevel)mFramework.getBundleContext().getService(sr);
            ss.setStartLevel(startLevel);
            mFramework.getBundleContext().ungetService(sr);
        } else {
            Log.e(TAG, "No start level service " + startLevel);
        }
    }

    private void setInitlevel(int level) {
        ServiceReference sr = mFramework.getBundleContext()
            .getServiceReference(StartLevel.class.getName());
        if (sr != null) {
            StartLevel ss =
                (StartLevel)mFramework.getBundleContext().getService(sr);
            ss.setInitialBundleStartLevel(level);
            mFramework.getBundleContext().ungetService(sr);
            Log.d(TAG, "initlevel " + level + " set");
        } else {
            Log.e(TAG, "No start level service " + level);
        }
    }


}

As I said in my previous question, according to @ldx in this post, "The patches I submitted for better Android/Dalvik support in Knopflerfish have been merged so patching and recompiling KF should not be necessary anymore:"

and therefore I didn't add anything assuming my code is enough as it is now.

When I run the android application, I don't get any error, instead, I get positive messages on my LogCat saying that my bundle was installed and started. See my log below (I ignore the first 2 lines):

08-15 23:32:08.381: E/Trace(22044): error opening trace file: No such file or directory (2)
08-15 23:32:08.381: E/Trace(22044): error opening trace file: No such file or directory (2)
08-15 23:32:09.441: I/System.out(22044): here!
08-15 23:32:10.320: D/dalvikvm(22044): GC_CONCURRENT freed 172K, 10% free 2695K/2984K, paused 73ms+85ms, total 236ms
08-15 23:32:10.720: D/dalvikvm(22044): GC_CONCURRENT freed 324K, 14% free 2816K/3260K, paused 73ms+85ms, total 214ms
08-15 23:32:10.762: D/Zaid Log(22044): initlevel 1 set
08-15 23:32:10.762: D/Zaid Log(22044): installing bundle Bundle_AndroidAPI_1.0.0.201308160327.jar
08-15 23:32:11.060: D/Zaid Log(22044): bundle Bundle_AndroidAPI_1.0.0.201308160327.jar installed
08-15 23:32:11.080: D/Zaid Log(22044): starting bundle Bundle_AndroidAPI_1.0.0.201308160327.jar
08-15 23:32:11.090: D/Zaid Log(22044): bundle Bundle_AndroidAPI/4/BundleImpl[id=4] started
08-15 23:32:11.140: D/Zaid Log(22044): OSGi framework running, state: 32
08-15 23:32:12.012: D/gralloc_goldfish(22044): Emulator without GPU emulation detected.
08-15 23:32:52.941: I/Choreographer(22044): Skipped 47 frames!  The application may be doing too much work on its main thread.

However, my bundle didn't really start. A message in the start method of the bundle would have shown up on the LogCat if my bundle really started. Below is my bundle start method:

public void start(BundleContext bundleContext) throws Exception {
    Activator.context = bundleContext;


    System.out.println("Hello World. I am the OSGI_Android_Bundle!");


}

Note that my bundle was dexified, but yet it doesn't start in my app. Where did I go wrong?

Update 1: I thought I should swap the starting of the framework with the starting of the bundle. The author of the tutorial starts the bundles first, but I thought to change that because it makes sense to start the framework first, so my updated code has the following:

setInitlevel(1);


// install/start other bundles...


setStartLevel(10);

try {
    mFramework.start();
} catch (BundleException be) {
    Log.e(TAG, be.toString());
    // framework start failed
}

Log.d(TAG, "OSGi framework running, state: " + mFramework.getState());


installBundle("Bundle-Test_1.0.0.201308160536.jar");
startBundle("Bundle-Test_1.0.0.201308160536.jar");

Now, the log doesn't show me that my bundle started as before, but rather it tells me that the execution-environment is not supported. See the updated LogCat below:

08-16 01:43:06.821: I/System.out(22737): here!
08-16 01:43:08.080: D/dalvikvm(22737): GC_CONCURRENT freed 191K, 11% free 2656K/2968K, paused 35ms+18ms, total 155ms
08-16 01:43:09.010: D/dalvikvm(22737): GC_CONCURRENT freed 276K, 13% free 2806K/3200K, paused 25ms+15ms, total 110ms
08-16 01:43:09.238: D/Zaid Log(22737): initlevel 1 set
08-16 01:43:09.300: D/Zaid Log(22737): OSGi framework running, state: 32
08-16 01:43:09.300: D/Zaid Log(22737): installing bundle Bundle-Test_1.0.0.201308160536.jar
08-16 01:43:09.530: D/Zaid Log(22737): bundle Bundle-Test_1.0.0.201308160536.jar installed
08-16 01:43:09.540: D/Zaid Log(22737): starting bundle Bundle-Test_1.0.0.201308160536.jar
08-16 01:43:09.550: E/Zaid Log(22737): org.osgi.framework.BundleException: Bundle#5, unable to resolve: Execution environment 'J2SE-1.5' is not supported
08-16 01:43:10.740: D/gralloc_goldfish(22737): Emulator without GPU emulation detected.

Update 2: I removed this line from my bundle MANIFEST following @Neil's answer below,

Bundle-RequiredExecutionEnvironment: J2SE-1.5

but this resulted in the following error:

 org.osgi.framework.BundleException: Bundle#6 start failed

Perhaps, I shouldn't have swapped the order of starting the framework and starting the bundle. Below is my updated LogCat.

08-16 17:02:33.855: E/Trace(28805): error opening trace file: No such file or directory (2)
08-16 17:02:35.111: I/System.out(28805): here!
08-16 17:02:35.591: D/dalvikvm(28805): GC_CONCURRENT freed 199K, 11% free 2679K/2996K, paused 76ms+93ms, total 246ms
08-16 17:02:36.153: D/dalvikvm(28805): GC_CONCURRENT freed 268K, 13% free 2814K/3200K, paused 77ms+141ms, total 297ms
08-16 17:02:36.451: D/Zaid Log(28805): initlevel 1 set
08-16 17:02:36.531: D/Zaid Log(28805): OSGi framework running, state: 32
08-16 17:02:36.531: D/Zaid Log(28805): installing bundle Bundle-Test_1.0.0.201308161323.jar
08-16 17:02:36.611: D/Zaid Log(28805): bundle Bundle-Test_1.0.0.201308161323.jar installed
08-16 17:02:36.631: D/Zaid Log(28805): starting bundle Bundle-Test_1.0.0.201308161323.jar
08-16 17:02:36.646: E/Zaid Log(28805): org.osgi.framework.BundleException: Bundle#6 start failed
08-16 17:02:36.646: E/Zaid Log(28805): More:Bundle#6 start failed
08-16 17:02:36.681: W/System.err(28805): org.osgi.framework.BundleException: Bundle#6 start failed
08-16 17:02:36.681: W/System.err(28805):    at org.knopflerfish.framework.BundleImpl.start0(BundleImpl.java:421)
08-16 17:02:36.681: W/System.err(28805):    at org.knopflerfish.framework.BundleThread.run(BundleThread.java:145)
08-16 17:02:36.681: W/System.err(28805): Caused by: java.lang.ClassNotFoundException: bundle_test.Activator
08-16 17:02:36.681: W/System.err(28805):    at org.knopflerfish.framework.BundleClassLoader.findClass(BundleClassLoader.java:218)
08-16 17:02:36.681: W/System.err(28805):    at org.knopflerfish.framework.BundleClassLoader.loadClass(BundleClassLoader.java:347)
08-16 17:02:36.681: W/System.err(28805):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-16 17:02:36.694: W/System.err(28805):    at org.knopflerfish.framework.BundleImpl.start0(BundleImpl.java:382)
08-16 17:02:36.694: W/System.err(28805):    ... 1 more
08-16 17:02:37.282: I/Choreographer(28805): Skipped 34 frames!  The application may be doing too much work on its main thread.
08-16 17:02:37.551: D/gralloc_goldfish(28805): Emulator without GPU emulation detected.

回答1:


I'll only address the error in your updated question, since it seems the first problem was fixed (please confirm).

The error means that a bundle includes the following requirement:

Bundle-RequiredExecutionEnvironment: J2SE-1.5

In other words, the bundle asserts that it requires standard Java 5 or higher. This is known as the Execution Environment or EE. Normally the OSGi Framework detects what kind of Java runtime you have launched it on, and it publishes a set of EEs to match that. For example if you run on standard Java 5, the framework publishes the EEs named J2SE-1.5, J2SE-1.4 etc all the way back to JRE-1.0, because Java 5 is backwards-compatible with all those versions. However if you have a bundle that asserts Bundle-RequiredExecutionEnvironment: JavaSE-1.6 and you try to run it on Java 5 then it will fail to resolve.

Now, you are trying to run on Android, which is not standard Java. While similar to standard Java, many parts of the standard JRE APIs have been removed. Therefore a bundle that requires standard Java 5 should not resolve on Android, because it may attempt to call APIs that do not exist on that platform.

Probably the easiest thing for you to do is remove the Bundle-RequiredExecutionEnvironment header from the bundle that is not resolving. That will allow it to install and resolve, but of course it may later fail with runtime errors if it really does need to call methods from the standard JRE that are not present on Android.



来源:https://stackoverflow.com/questions/18263747/starting-a-bundle-on-knopflerfish-embedded-into-android-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!