package org.openntf.domino.xsp;

import java.io.InputStream;
import java.net.URL;
import java.util.Dictionary;
import java.util.Hashtable;

import javax.faces.context.FacesContext;

import org.eclipse.core.runtime.Plugin;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

/**
 * Activator for the org.openntf.domino.xsp library
 * 
 * @since org.openntf.domino.xsp 2.5.0
 * 
 */
public class Activator extends Plugin {
	public static final String PLUGIN_ID = Activator.class.getPackage().getName();

	public static Activator instance;

	private static String version;
	private ServiceRegistration consoleCommandService;

	//private static BundleContext context;

	/**
	 * Registers the AmgrCommandProvider to handle commands
	 * 
	 * @param bundle
	 * 
	 * @param bundleContext
	 */
	private void registerCommandProvider(final BundleContext bundleContext) {
		CommandProvider cp = new OsgiCommandProvider();
		Bundle bundle = bundleContext.getBundle();
		Dictionary<String, Object> cpDictionary = new Hashtable<String, Object>(7);
		cpDictionary.put("service.vendor", bundle.getHeaders().get("Bundle-Vendor"));
		cpDictionary.put("service.ranking", new Integer(Integer.MIN_VALUE));
		cpDictionary.put("service.pid", bundle.getBundleId() + "." + cp.getClass().getName());

		consoleCommandService = bundleContext.registerService(CommandProvider.class.getName(), cp, cpDictionary);
	}

	/**
	 * Gets the current Activator instance
	 * 
	 * @return Activator
	 * @since org.openntf.domino.xsp 2.5.0
	 */
	public static Activator getDefault() {
		return instance;
	}

	//	/**
	//	 * Gets the bundle context, i.e. the top level of the plugin. Used to get resources from resources folder.
	//	 * 
	//	 * @see LogReader
	//	 * 
	//	 * @return BundleContext for all resouorces in this plugin
	//	 * @since org.openntf.domino.xsp 2.5.0
	//	 */
	//	static BundleContext getContext() {
	//		return context;
	//	}

	/**
	 * Gets the Bundle-Version property from the MANIFEST-MF
	 * 
	 * @return current version
	 * @since org.openntf.domino.xsp 2.5.0
	 */
	public static String getVersion() {
		if (version == null) {
			version = instance.getBundle().getHeaders().get("Bundle-Version");
		}
		return version;
	}

	/**
	 * Gets a resource, relative to the root of the plugin
	 * 
	 * @param path
	 *            String relative to the root of the plugin, e.g. "/resources/log-transform.xsl"
	 * @return InputStream containing the contents of the resource
	 * @since org.openntf.domino.xsp 4.5.0
	 */
	public InputStream getResourceAsStream(final String path) throws Exception {
		//BundleContext ctx = getContext();
		Bundle bundle = getBundle();
		URL url = bundle.getEntry(path);
		if (url == null) {
			return null;
		} else {
			InputStream result = url.openStream();
			return result;
		}
	}

	/**
	 * Constructor
	 */
	public Activator() {
		System.out.println("Activating org.openntf.domino.xsp!");
		instance = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext )
	 */
	@Override
	public void start(final BundleContext bundleContext) throws Exception {
		super.start(bundleContext);
		registerCommandProvider(bundleContext);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	@Override
	public void stop(final BundleContext bundleContext) throws Exception {
		if (consoleCommandService != null) {
			consoleCommandService.unregister();
			consoleCommandService = null;
		}
		super.stop(bundleContext);
	}

	/**
	 * @deprecated use {@link ODAPlatform#getXspPropertyAsString} instead.
	 * 
	 */
	@Deprecated
	public static String getXspPropertyAsString(final String propertyName) {
		return ODAPlatform.getXspPropertyAsString(propertyName, null);
	}

	/**
	 * @deprecated use {@link ODAPlatform#isAPIEnabled} instead.
	 */
	@Deprecated
	public static boolean isAPIEnabled() {
		return ODAPlatform.isAPIEnabled(null);
	}

	/**
	 * @deprecated use {@link ODAPlatform#isAPIEnabled} instead.
	 */
	@Deprecated
	public static boolean isAPIEnabled(final FacesContext ctx) {
		return ODAPlatform.isAPIEnabled(null);
	}

	/**
	 * @deprecated use {@link ODAPlatform#isDebug} instead.
	 */
	@Deprecated
	public static boolean isDebug() {
		return ODAPlatform.isDebug();
	}

}

@@ -721,16 +721,33 @@
 stration
+%3CCommandProvider%3E
  console
@@ -762,16 +762,16 @@
 ervice;%0A
-
 %0A%09//priv
@@ -1491,26 +1491,16 @@
 er.class
-.getName()
 , cp, cp
@@ -1700,347 +1700,8 @@
 %09%7D%0A%0A
-%09//%09/**%0A%09//%09 * Gets the bundle context, i.e. the top level of the plugin. Used to get resources from resources folder.%0A%09//%09 * %0A%09//%09 * @see LogReader%0A%09//%09 * %0A%09//%09 * @return BundleContext for all resouorces in this plugin%0A%09//%09 * @since org.openntf.domino.xsp 2.5.0%0A%09//%09 */%0A%09//%09static BundleContext getContext() %7B%0A%09//%09%09return context;%0A%09//%09%7D%0A%0A
 %09/**
@@ -2362,46 +2362,8 @@
 n %7B%0A
-%09%09//BundleContext ctx = getContext();%0A
 %09%09Bu
@@ -2562,24 +2562,24 @@
 ructor%0A%09 */%0A
+
 %09public Acti
@@ -2592,68 +2592,8 @@
 ) %7B%0A
-%09%09System.out.println(%22Activating org.openntf.domino.xsp!%22);%0A
 %09%09in

Clean up XSP activator a bit