import org.eclipse.core.runtime.adaptor.EclipseStarter;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;


/**
 * @author Michael Furtak (mfurtak@cra.com)
 */
public class JavaOsgiBootstrapper
{
  /**
   * 
   * @param args
   */
  public static void main(String[] args) throws Exception
  {
    final BundleContext context = EclipseStarter.startup(args, null);

    ServiceTrackerCustomizer stc = new ServiceTrackerCustomizer()
    {
      public Object addingService(ServiceReference ref)
      {
        System.out.println("Service added.");

	// This line throws a ClassCastException
	MyService myService = (MyService)context.getService(ref);

        System.err.println("Service successfully used.");

        return myService;
      }

      public void modifiedService(ServiceReference ref, Object service)
      {
      }

      public void removedService(ServiceReference ref, Object service)
      {
      }
    };

    ServiceTracker tracker = new ServiceTracker(context, MyService.class.getName(), stc);
    tracker.open();
  }
}
