You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by stephane frenot <st...@insa-lyon.fr> on 2008/04/10 00:53:05 UTC
Re: ClassLoading problems with Felix embed in a bundle ...
Hello miguel,
you need to patch felix to do this,
you need to transmit classloaders references from the host to the virtuals.
I can make the patch available to the community if needed.
/stephane
On Wed, Apr 9, 2008 at 9:19 PM, Miguel Matos <ra...@gmail.com> wrote:
> Hello!
> I am trying to embed Felix as a bundle. This is related to some issues
> that I want to solve but the problem is not here.
>
> I followed the wiki about how to embed Felix and everything gone fine,
> i.e, my app is running the way I want.
>
> However as I say I need to embed Felix as an OSGi bundle but I'm having
> the (classical) classloading problems.
>
>
> Basicaly I built a simple bundle in whose Activator I do all the necessary
> steps to embed Felix as in the tutorial.
> To this point everything goes fine.
> But I also want to export some services through the system bundle. To do
> this I register a simple service within the embed Felix Activator, and
> adjust the manifest
> accordingly.
>
> However when I try to start a bundle in the embed Felix (not in the "main
> instance") that uses the exported service it fails with a
> classnotfoundexception as show below:
> (Lines with #### are comments made by me right now)
>
> -> vgw start file:/tmp/hw.jar
> Hello World...
> ####Checking for all services
> BundleSystem Bundle
> Property objectClass
> org.osgi.service.startlevel.StartLevel
> Property service.id -> 1
> BundleSystem Bundle
> Property objectClass
> org.osgi.service.packageadmin.PackageAdmin
> Property service.id -> 2
> BundleSystem Bundle
> Property myey -> myalue
> ####My service is here!!!!
> Property objectClass voc.vh.iface.Lookup
> Property service.id -> 3
> #### This is the class of the service object retrieved with
> BundleContext#getService(...)
> O is voc.vh.LookupImpl@a761fe
> #### This are the interfaces implemented by the above object
> EC isClazz: voc.vh.iface.Lookup
> #### This is the result of invoking a method into the obtained object
> through reflection
> Hello BLA BLA
> #### And those are the exceptions obtained when I try to cast the obtained
> object to its interface
> org.osgi.framework.BundleException: Activator start error.
> at org.apache.felix.framework.Felix._startBundle(Felix.java:1652)
> at org.apache.felix.framework.Felix.startBundle(Felix.java:1542)
> at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:369)
> at
> voc.vh.HostApplication.startVirtualizedApplication(HostApplication.java:120)
> at voc.vh.HostApplication.execute(HostApplication.java:171)
> at
> org.apache.felix.shell.impl.Activator$ShellServiceImpl.executeCommand(Activator.java:276)
> at
> org.apache.felix.shell.tui.Activator$ShellTuiRunnable.run(Activator.java:180)
> at java.lang.Thread.run(Thread.java:619)
> Caused by: java.lang.NoClassDefFoundError: voc/vh/iface/Lookup
> at voc.tt.HelloWorld.start(HelloWorld.java:81)
> at
> org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:591)
> at org.apache.felix.framework.Felix._startBundle(Felix.java:1608)
> ... 7 more
> Caused by: java.lang.ClassNotFoundException: voc.vh.iface.Lookup
> at
> org.apache.felix.framework.searchpolicy.R4SearchPolicyCore.findClass(R4SearchPolicyCore.java:200)
> at
> org.apache.felix.framework.searchpolicy.R4SearchPolicy.findClass(R4SearchPolicy.java:45)
> at
> org.apache.felix.framework.searchpolicy.ContentClassLoader.loadClass(ContentClassLoader.java:109)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
> at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
> ... 10 more
> Caused by: java.lang.ClassNotFoundException: voc.vh.iface.Lookup
> at
> org.apache.felix.framework.searchpolicy.R4Wire.getClass(R4Wire.java:109)
> at
> org.apache.felix.framework.searchpolicy.R4SearchPolicyCore.searchImports(R4SearchPolicyCore.java:507)
> at
> org.apache.felix.framework.searchpolicy.R4SearchPolicyCore.findClassOrResource(R4SearchPolicyCore.java:468)
> at
> org.apache.felix.framework.searchpolicy.R4SearchPolicyCore.findClass(R4SearchPolicyCore.java:187)
> ... 14 more
> Bundle Started
>
>
>
> Attached are the relevant code:
>
> Lookup: is the service interface
> LookupImpl: is the implementation of that interface
> HostActivator: is the activator for the hosting bundle
> HostApplication: is the main class of the bundle that embeds felix
> manifest_hostBundle: is the manifest for the above bundle
> manifest_helloWorld: is the manifest for the client bundle of the service
> HelloWorld: is the only class of the bundle that is launched inside the
> embed Felix
>
> Please note that if the HostApplication is run as a normal java
> application everything goes fine.
> Sorry if the information is overwhelming, any help will be appreciated.
>
> mm
>
> package voc.vh;
>
> import java.util.Dictionary;
>
> import org.osgi.framework.BundleActivator;
> import org.osgi.framework.BundleContext;
>
> import voc.vh.iface.Lookup;
>
>
> public class HostActivator implements BundleActivator
> {
> private BundleContext m_context = null;
>
> public void start(BundleContext context)
> {
> m_context = context;
>
> Dictionary d = new java.util.Hashtable();
> d.put("myey", "myalue");
> context.registerService(Lookup.class.getName(),
> new LookupImpl(), d);
> }
>
> public void stop(BundleContext context)
> {
> m_context = null;
> }
> }
> package voc.vh;
>
> import java.util.ArrayList;
> import java.util.List;
> import java.util.Map;
>
> import org.apache.felix.framework.Felix;
> import org.apache.felix.framework.cache.BundleCache;
> import org.apache.felix.framework.util.FelixConstants;
> import org.apache.felix.framework.util.StringMap;
> import org.osgi.framework.Bundle;
> import org.osgi.framework.BundleActivator;
> import org.osgi.framework.BundleContext;
> import org.osgi.framework.BundleException;
> import org.osgi.framework.Constants;
> import org.osgi.framework.ServiceRegistration;
>
> public class HostApplication implements BundleActivator {
>
> private Felix m_felix = null;
>
> private ServiceRegistration serviceRegistration = null;
>
> public static void main(String[] args) {
> HostApplication ha = new HostApplication();
>
> ha.startVirtualHost();
> ha.startVirtualizedApplication("file:/tmp/hw.jar");
>
> // ha.shutdownApplication();
> }
>
> public void startVirtualHost() {
> // Create a case-insensitive configuration property map.j
> Map configMap = new StringMap(false);
> // Configure the Felix instance to be embedded.
> configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP,
> "true");
> // Add core OSGi packages to be exported from the class
> path
> // via the system bundle.
> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES,
> "org.osgi.framework; version=1.3.0,"
> +
> "org.osgi.service.packageadmin; version=1.2.0,"
> +
> "org.osgi.service.startlevel; version=1.0.0,"
> + "org.osgi.service.url;
> version=1.0.0,"
> + "voc.vh.iface,"
> +
> "org.apache.felix.shell.impl");
> // Explicitly specify the directory to use for caching
> bundles.
> configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP,
> "/tmp/felixCache");
>
> try {
> // Create host activator;
> HostActivator ha = new HostActivator();
>
> List list = new ArrayList();
> list.add(ha);
>
> // Now create an instance of the framework with
> // our configuration properties and activator.
> m_felix = new Felix(configMap, list);
>
> // Now start Felix instance.
> m_felix.start();
>
> } catch (Exception ex) {
> System.err.println("Could not create framework: " +
> ex);
> ex.printStackTrace();
> }
>
> System.out.println("Felix Started");
> }
>
> public void startVirtualizedApplication(String path) {
>
> try {
> BundleContext bc = m_felix.getBundleContext();
> Bundle b = bc.installBundle(path);
>
> b.start();
>
> } catch (BundleException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> System.out.println("Bundle Started");
> }
>
> public void shutdownApplication() {
> // Shut down the felix framework when stopping the
> // host application.
> try {
> m_felix.stop();
> } catch (BundleException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
>
> // /// BundleActivator Implementation
> public void start(BundleContext bc) throws Exception {
>
> startVirtualHost();
> startVirtualizedApplication("file:/tmp/hw.jar");
> }
>
> public void stop(BundleContext bc) throws Exception {
> bc = null;
> serviceRegistration.unregister();
> System.out.println("Host stopped");
> }
> }
> package voc.vh;
>
> import voc.vh.iface.Lookup;
>
> public class LookupImpl implements Lookup {
>
> public void hello(String name) {
> System.out.println("Hello " + name + "!");
> }
> }
>
> package voc.vh.iface;
>
> public interface Lookup {
>
> public void hello(String name);
> }
>
> Bundle-Activator: voc.tt.HelloWorld
> Import-Package: org.osgi.framework,voc.vh.iface
> Bundle-Name: Hello World
> Bundle-SymbolicName: helloworld
>
> Bundle-Activator: voc.vh.HostApplication
> Import-Package:
> org.osgi.framework,org.apache.felix.shell,org.apache.felix.framework,org.apache.felix.framework.util,org.apache.felix.shell.impl.Util,org.osgi.util.tracker
> Bundle-Name: Virtual OSGi Containers
> Bundle-SymbolicName: VOC
>
> package voc.tt;
>
> import org.osgi.framework.BundleActivator;
> import org.osgi.framework.BundleContext;
> import org.osgi.framework.ServiceReference;
>
> import voc.vh.iface.Lookup;
>
> public class HelloWorld implements BundleActivator{
>
> public void start(BundleContext bc) throws Exception {
> System.out.println("Hello World...");
>
>
>
> //
> for (ServiceReference sr :
> bc.getAllServiceReferences(null, null)) {
>
>
> System.out.println("Bundle" +
> sr.getBundle().getSymbolicName());
> for (String p : sr.getPropertyKeys()) {
> if (p.equalsIgnoreCase("objectClass")) {
> System.out.print("\tProperty " + p
> );
>
> for (String x : (String[])
> sr.getProperty(p)) {
>
> System.out.println("\t\t"+x);
> }
> }
> else
> System.out.println("\tProperty " +
> p + " -> " + sr.getProperty(p));
> }
>
> }
> ServiceReference bla =
> bc.getServiceReference("voc.vh.iface.Lookup");
> Object o = bc.getService(bla);
>
> System.out.println("O is " + o);
>
> System.out.print("EC is" );
> for ( Class clazz : o.getClass().getInterfaces()){
> System.out.println("Clazz: " + clazz.getName());
> }
>
> o.getClass().getDeclaredMethod("hello", new
> Class[]{String.class}).invoke(o, new String[]{"BLA BLA"});
> // (o.getClass().getDeclaredMethods()[0).invoke("hello", "BLA
> BLA"));
>
> Lookup l = (Lookup) o;
> // (Lookup) o;
> // Lookup look = (Lookup)
> bc.getService(bc.getServiceReference("voc.vh.iface.Lookup"));
> //
> // l.hello("BLA BLA");
> }
>
> public void stop(BundleContext arg0) throws Exception {
> System.out.println("Bye World...");
> }
>
> public void hello(String name) {
> System.out.println("Hello " + name + "!");
> }
> }
>
--
http://perso.citi.insa-lyon.fr/sfrenot
work here : http://tinyurl.com/2ts4xf
sleep here : http://tinyurl.com/33p2gt
play here : http://tinyurl.com/3yarjz