Okay, here are the relevant java files of my two projects. Maybe I explain I
little bit how the classes work together:

Within my 'host-project' I'm using the functionality of the felix.jar to
create a OSGi instance (OSGiFramework.java). Within the initialize() method
I'm creating a new instance of HostAcitvator (HostActivator.java), which
implements the service interface Lookup (Lookup.java) like in the example on
the wiki. Within my bundle I try to access this Lookup service
(Activator.java). To be able to access the Lookup service interface I added
the whole 'host-project' to the build path of the equinox bundle project. If
I try to export that bundle using the build-in bundle export function I'm
getting the error.

BR,

Markus


2009/6/10 Ted Stockwell <[email protected]>

>
> How about posting the projects so I can try them?
>
>
>
> --- On Tue, 6/9/09, Markus Michel <[email protected]> wrote:
>
> > From: Markus Michel <[email protected]>
> > Subject: Embedded Felix: Using a service provided by the host from a
> bundle
> > To: [email protected]
> > Date: Tuesday, June 9, 2009, 11:52 AM
> > Hi there,
> >
> > after reading the wiki entry (
> >
> http://felix.apache.org/site/apache-felix-framework-launching-and-embedding.html#ApacheFelixFrameworkLaunchingandEmbedding-hostservices
> )
> > I tried to integrate the presented example
> > into my existing code. Within my Eclipse workspace I have
> > the following
> > projects:
> >
> > Host Project, which uses the functionality of the felix.jar
> > to create an
> > OSGi instance and implements the Lookup service
> >
> > Equinox Hello World bundle which tries to use the Lookup
> > service (To get
> > access to the service implementation I added the Host
> > Project as an required
> > project to the build path)
> >
> > If I try to export the bundle to a jar file I'm getting the
> > following errors
> > within a zipped log file:
> >
> > # 09.06.09 18:34:17 MESZ
> > # Eclipse Java Compiler 0.894_R34x, 3.4.2 release,
> > Copyright IBM Corp 2000,
> > 2008. All rights reserved.
> > ----------
> > 1. ERROR in
> >
> /home/markus/master/michel/masterthesis/eclipseWorkspace/TestBundle/src/bla/Activator.java
> > (at line 13)
> >     ServiceReference serviceReference =
> > context.getServiceReference(CanEmulator.Lookup.class.getName());
> >
> > ^^^^^^^^^^^
> > CanEmulator cannot be resolved to a type
> > ----------
> > 2. ERROR in
> >
> /home/markus/master/michel/masterthesis/eclipseWorkspace/TestBundle/src/bla/Activator.java
> > (at line 17)
> >     CanEmulator.Lookup lookup =
> > (CanEmulator.Lookup)
> > context.getService(serviceReference);
> >     ^^^^^^^^^^^
> > CanEmulator cannot be resolved to a type
> > ----------
> > 3. ERROR in
> >
> /home/markus/master/michel/masterthesis/eclipseWorkspace/TestBundle/src/bla/Activator.java
> > (at line 17)
> >     CanEmulator.Lookup lookup =
> > (CanEmulator.Lookup)
> > context.getService(serviceReference);
> >
> >
> >    ^^^^^^^^^^^
> > CanEmulator cannot be resolved
> > ----------
> > 3 problems (3 errors)
> >
> > It seems like the bundle doesn't find the referenced
> > project. Does anybody
> > has an idea how to solve my problem? Do I have to add
> > something to the
> > mainfest file? (I tried to add my project to the bundle
> > import path, but I
> > couldn't find it in the list, because the host project
> > isn't a plugin
> > project)
> >
> > BR,
> >
> > Markus
> >
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
package bla;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import CanEmulator.Lookup;

public class Activator implements BundleActivator
{
	public void start(BundleContext context) throws Exception
	{
		System.out.println("Hello World!!");
		
		ServiceReference serviceReference = context.getServiceReference(Lookup.class.getName());
		
		if (serviceReference != null)
		{
			Lookup lookup = (Lookup) context.getService(serviceReference);
			if (lookup != null)
			{
				System.out.println(lookup.lookup("name1"));
			}
		}
		else
		{
			System.out.println("Service Reference is null!");
		}
		
		// serviceReference = context.getServiceReference(HelloService.class.getName());
		//		
		// if (serviceReference != null)
		// {
		// HelloService helloService = (HelloService) context.getService(serviceReference);
		//			
		// if (helloService != null)
		// {
		// helloService.speak();
		// }
		// }
		
	}
	
	public void stop(BundleContext context) throws Exception
	{
		System.out.println("Goodbye World!!");
	}
}
package CanEmulator;


import java.util.Map;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

public class HostActivator implements BundleActivator
{
	private Map					m_lookupMap		= null;
	private BundleContext		m_context		= null;
	private ServiceRegistration	m_registration	= null;
	
	public HostActivator(Map lookupMap)
	{
		// Save a reference to the service's backing store.
		m_lookupMap = lookupMap;
	}
	
	public void start(BundleContext context)
	{
		// Save a reference to the bundle context.
		m_context = context;
		// Create a property lookup service implementation.
		Lookup lookup = new Lookup()
		{
			@Override
			public Object lookup(String name)
			{
				return m_lookupMap.get(name);
			}
		};
		// Register the property lookup service and save
		// the service registration.
		m_registration = m_context.registerService(Lookup.class.getName(), lookup, null);
	}
	
	public void stop(BundleContext context)
	{
		// Unregister the property lookup service.
		m_registration.unregister();
		m_context = null;
	}
}
package CanEmulator;

public interface Lookup
{
	public Object lookup(String name);
}
package CanEmulator;

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.apache.felix.main.AutoActivator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

public class OSGiFramework implements Runnable
{
	private Thread				thread;
	private boolean				stopRequested;
	private BundleContext		bundleContext	= null;
	private Bundle				bundle			= null;
	private Felix				felixInstance	= null;
	private Map<String, Object>	configMap		= null;
	private String				vehicleId		= "";
	
	private HostActivator		hostActivator	= null;
	private final Map			lookupMap		= new HashMap();
	
	public OSGiFramework(String vehicleId)
	{
		this.vehicleId = vehicleId;
		stopRequested = false;
	}
	
	public void start()
	{
		thread = new Thread(this);
		thread.start();
	}
	
	public void stopRequest()
	{
		stopRequested = true;
	}
	
	private void initialize()
	{
		// Initialize the map for the property lookup service.
		lookupMap.put("name1", "value1");
		
		lookupMap.put("name2", "value2");
		lookupMap.put("name3", "value3");
		lookupMap.put("name4", "value4");
		
		// create new map for storing the felix configuration properties
		configMap = new HashMap<String, Object>();
		
		// Export the host provided service interface package.
		configMap.put(FelixConstants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "CanEmulator.lookup; version=1.0.0");
		
		// add autoactivator that specifies all bundles that shall be loaded together with the system bundle
		configMap.put(AutoActivator.AUTO_START_PROP + ".1", "file:/home/markus/bundles/org.apache.felix.shell-1.2.0.jar " + "file:/home/markus/bundles/org.apache.felix.shell.tui-1.2.0.jar");
		List<Object> list = new ArrayList<Object>();
		list.add(new AutoActivator(configMap));
		
		// Create host activator;
		hostActivator = new HostActivator(lookupMap);
		list.add(hostActivator);
		
		configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
		
		// set felix-cache folder
		configMap.put("org.osgi.framework.storage", "felix-cache/" + vehicleId);
	}
	
	public BundleContext getBundleContext()
	{
		return bundleContext;
	}
	
	public Bundle[] getInstalledBundles()
	{
		return bundleContext.getBundles();
	}
	
	@SuppressWarnings("unused")
	private String getBundleState(Bundle bundle)
	{
		// get current state of bundle
		int state = bundle.getState();
		
		// map state to a String
		switch (state)
		{
			case Bundle.UNINSTALLED:
				return "UNINSTALLED";
			case Bundle.INSTALLED:
				return "INSTALLED";
			case Bundle.RESOLVED:
				return "RESOLVED";
			case Bundle.STARTING:
				return "STARTING";
			case Bundle.STOPPING:
				return "STOPPING";
			case Bundle.ACTIVE:
				return "ACTIVE";
			default:
				throw new IllegalStateException("Bundle state '" + state + "' not known");
		}
	}
	
	public String getVehicleId()
	{
		return vehicleId;
	}
	
	@Override
	public void run()
	{
		try
		{
			// set framework properties
			initialize();
			
			// create & start framework
			try
			{
				felixInstance = new Felix(configMap);
				felixInstance.start();
				
				// save reference to the bundle context.
				bundleContext = felixInstance.getBundleContext();
			}
			catch (Exception ex)
			{
				System.err.println("Could not create felix framework: " + ex);
				ex.printStackTrace();
				System.exit(1);
			}
			
			bundle = bundleContext.installBundle("file:/home/markus/bundles/plugins/TestBundle_1.0.0.jar");
			
			// start bundle
			bundle.start();
			
			// wait until the framework shall be stopped
			while (!stopRequested)
			{
				// wait
				Thread.sleep(500);
			}
			
			// stop bundle
			bundle.stop();
			
			// stop framework
			felixInstance.stop();
			felixInstance.waitForStop(0);
		}
		catch (Exception e)
		{
			System.out.println("error: osgi framework crashed");
			System.out.println(e);
			System.exit(1);
		}
	}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to