package todo;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.util.tracker.ServiceTracker;

import com.buglabs.util.ServiceFilterGenerator;

/**
 * The activator is the main 'hook' for our bundle. When
 * the BUG loads our bundle it will call Activator.start().
 * Similarly, when the BUG unloads our bundle it will call
 * Activator.stop().
 * 
 * @author finsprings
 *
 */
public class Activator implements BundleActivator {
	private MyServiceTracker myServiceTracker;
	private ServiceTracker osgiServiceTracker;

	public void start(BundleContext context) throws Exception {
		System.out.println( "todo: activator start" );
		
		// create our service tracker; we couldn't do
		// it earlier since it needs the context
		myServiceTracker = new MyServiceTracker( context );
		
		// create the filter that the OSGI ServiceTracker will use
		// to determine the services we care to be informed about.
		final Filter f = context.createFilter(
				ServiceFilterGenerator.generateServiceFilter(
						myServiceTracker.getServices() ) );
		
		// create the OSGI ServiceTracker; the object that will actually
		// track services for us.
		osgiServiceTracker = new ServiceTracker(context, f, myServiceTracker);
		osgiServiceTracker.open();
		
		// now we will be in limbo until the services we need arrive
		// (unless they are already available of course, in which
		// case our service tracker will be getting doStart() called
		// on it shortly.
		System.out.println( "todo: activator started" );
	}

	public void stop(BundleContext context) throws Exception {
		System.out.println( "todo: activator stop" );
		
		// tell the OSGI ServiceTracker to stop tracking services for us
		osgiServiceTracker.close();
		
		// get our service tracker to shut the app down
		myServiceTracker.stop();
		
		System.out.println( "todo: activator stopped" );
	}
}