/*-
 *******************************************************************************
 * Copyright (c) 2011, 2014 Diamond Light Source Ltd.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Matthew Gerring - initial API and implementation and/or initial documentation
 *******************************************************************************/
package org.eclipse.dawnsci.slicing.api;

import java.util.HashMap;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "org.eclipse.dawnsci.slicing.api"; //$NON-NLS-1$

	// The shared instance
	private static Activator plugin;
	
	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

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

	private static HashMap<Class<?>, Object> testServices;

	public static Object getService(Class<?> clazz) {
		if (testServices!=null && testServices.containsKey(clazz)) return testServices.get(clazz);
		if (plugin.getBundle().getBundleContext()==null) return null;
		ServiceReference<?> ref = plugin.getBundle().getBundleContext().getServiceReference(clazz);
		if (ref==null) return null;
		return plugin.getBundle().getBundleContext().getService(ref);
	}

	public static void setTestService(Class<?> clazz, Object impl) {
		if (testServices == null) testServices = new HashMap<Class<?>, Object>(3);
		testServices.put(clazz, impl);
	}


}

@@ -1820,22 +1820,21 @@
  static 
-Object
+%3CT%3E T
  getServ
@@ -1839,25 +1839,25 @@
 rvice(Class%3C
-?
+T
 %3E clazz) %7B%0D%0A
@@ -1924,16 +1924,27 @@
  return 
+clazz.cast(
 testServ
@@ -1954,24 +1954,25 @@
 s.get(clazz)
+)
 ;%0D%0A%09%09if (plu
@@ -2046,17 +2046,17 @@
 ference%3C
-?
+T
 %3E ref = 
@@ -2238,16 +2238,20 @@
 c static
+ %3CT%3E
  void se
@@ -2269,17 +2269,17 @@
 e(Class%3C
-?
+T
 %3E clazz,
@@ -2279,22 +2279,17 @@
  clazz, 
-Object
+T
  impl) %7B

tidy up the code a little and avoid unnecessary casts (Colin P commit)