Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Virgo » Using bundles with Wars(Virgo Web Apps)
Using bundles with Wars [message #1063886] Fri, 14 June 2013 18:21 Go to next message
David Cleary is currently offline David ClearyFriend
Messages: 1
Registered: June 2013
Junior Member
I'm new to Virgo and OSGi. I have an existing Servlet application that runs on Tomcat. I want to move this application to VTS and let it have access to OSGi based services that I've created.

What is the recommended approach for a Web app to gain references to services? I have little to no experience with Spring, and we are looking to stay as close to OSGi standards as possible to allow us to run in different servers.

Thanks
Dave
Re: Using bundles with Wars [message #1063985 is a reply to message #1063886] Mon, 17 June 2013 06:57 Go to previous messageGo to next message
Mihael Schmidt is currently offline Mihael SchmidtFriend
Messages: 81
Registered: August 2010
Member
You could implement a BundleActivator to get access to a bundle context and get a reference to an OSGi service with it (or better use a ServiceTracker. Don't forget to declare the activator in the manifest file.

public class Activator implements BundleActivator {

	private ServiceTracker<IStateServiceClient, IStateServiceClient> stateServiceTracker;

	private static Activator instance;

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static Activator getInstance() {
		return instance;
	}

	@Override
	public void start(BundleContext context) throws Exception {
		instance = this;

		stateServiceTracker = new ServiceTracker<IStateServiceClient, IStateServiceClient>(context,
						IStateServiceClient.class.getName(), null);
		stateServiceTracker.open();
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		stateServiceTracker.close();

		instance = null;
	}

	public IStateServiceClient getStateService() {
		return stateServiceTracker.getService();
	}

}


Manifest addition:
Bundle-Activator: my.package.Activator


In your web classes you can get access to the service like this:
Activator.getInstance().getStateService();


Remember to check for a null return value. If there is no active service registered in the OSGi environment atm then null will be returned.

Hope this helps

Mihael
Re: Using bundles with Wars [message #1064169 is a reply to message #1063985] Tue, 18 June 2013 04:34 Go to previous message
Violeta Georgieva is currently offline Violeta GeorgievaFriend
Messages: 278
Registered: October 2010
Senior Member
Hi,

The BundleContext is available from the ServletContext attributes:

BundleContext ctxt = (BundleContext)
servletContext.getAttribute("osgi-bundlecontext");


OSGI Enterprise Spec chapter "128.6 Interacting with the OSGi Environment"

Regards
Violeta
Previous Topic:Virgo as a daemon
Next Topic:Missing part in understanding OSGI
Goto Forum:
  


Current Time: Tue Apr 16 14:02:52 GMT 2024

Powered by FUDForum. Page generated in 0.45559 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top