/**
 *	Generated by Dragonfly SDK
 *
 */
package phunky.servicetracker;

import org.osgi.framework.BundleContext;
import com.buglabs.application.AbstractServiceTracker;

import phunky.*;

 /**
 *	Service tracker for the BugApp Bundle;
 *
 */
public class PhunkyServiceTracker extends AbstractServiceTracker {
	private PhunkyApplication application;	

	public PhunkyServiceTracker(BundleContext context) {
		super(context);
	}
	
	/**
	 * Determines if the application can start.
	 */
	public boolean canStart() {
		final boolean result = super.canStart();
		System.out.println("result="+result);
		return result;
	}
	
	/**
	 * If canStart returns true
     * this method is called to start the application.
     * Place your fun logic here. 
	 */
	public void doStart() {
		if(!getApplication().isAlive()) {
			if(getApplication().getRan()) {
				application = new PhunkyApplication(this);
			}
			getApplication().start();
		}

	}

	/**
	 * Called when a service that this application depends is unregistered.
	 */
	public void doStop() {
		getApplication().tearDown();

	}

	/**
	 * Allows the user to set the service dependencies by
     * adding them to services list returned by getServices().
     * i.e.nl getServices().add(MyService.class.getName());
	 */
	public void initServices() {
		getServices().addAll(getApplication().getServices());
	}
	
	/**
	 * Returns the application thread.
	 */
	public PhunkyApplication getApplication() {
		if(application == null) {
			application = new PhunkyApplication(this);
		}
		
		return application;
	}
}