/*******************************************************************************
 * Copyright (c) 2014 Ericsson and others.
 * 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:
 *     Marc Khouzam (Ericsson) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.examples.dsf.gdb.service;

import java.util.Hashtable;

import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.ImmediateDataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.ImmediateRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.debug.service.command.CommandCache;
import org.eclipse.cdt.dsf.debug.service.command.ICommandControlService.ICommandControlDMContext;
import org.eclipse.cdt.dsf.gdb.IGdbDebugConstants;
import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
import org.eclipse.cdt.dsf.mi.service.IMICommandControl;
import org.eclipse.cdt.dsf.mi.service.command.CommandFactory;
import org.eclipse.cdt.dsf.mi.service.command.output.MIGDBVersionInfo;
import org.eclipse.cdt.dsf.service.AbstractDsfService;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.examples.dsf.gdb.GDBExamplePlugin;
import org.eclipse.cdt.examples.dsf.gdb.service.command.GdbExtendedCommandFactory_6_8;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IStatusHandler;
import org.osgi.framework.BundleContext;

public class GDBExtendedService extends AbstractDsfService implements IGDBExtendedFunctions {
	
	private IMICommandControl fCommandControl;
	private CommandFactory fCommandFactory;
	private CommandCache fVersionCache;
	
    public GDBExtendedService(DsfSession session) {
    	super(session);
    }

    @Override
    public void initialize(final RequestMonitor rm) {
    	super.initialize(new ImmediateRequestMonitor(rm) {
    		@Override
    		protected void handleSuccess() {
    			doInitialize(rm);
			}
		});
	}
	
	private void doInitialize(RequestMonitor rm) {
		fCommandControl = getServicesTracker().getService(IMICommandControl.class);
		fCommandFactory = fCommandControl.getCommandFactory();
		
		fVersionCache = new CommandCache(getSession(), fCommandControl);
		fVersionCache.setContextAvailable(fCommandControl.getContext(), true);

		register(new String[] { IGDBExtendedFunctions.class.getName() },
				 new Hashtable<String, String>());
		
		rm.done();
	}


	@Override
	public void shutdown(RequestMonitor rm) {
		unregister();
		super.shutdown(rm);
	}
	
	@Override
	protected BundleContext getBundleContext() {
		return GDBExamplePlugin.getBundleContext();
	}

	@Override
	public void notify(ICommandControlDMContext ctx, String str, RequestMonitor rm) {
		IStatus status = new Status( 
				IStatus.INFO, 
				GdbPlugin.getUniqueIdentifier(), 
				IGdbDebugConstants.STATUS_HANDLER_CODE, 
				str, 
				null);
		IStatusHandler statusHandler = DebugPlugin.getDefault().getStatusHandler(status);
		if (statusHandler != null) {
			try {
				statusHandler.handleStatus(status, null);
			}
			catch(CoreException e) {
				GDBExamplePlugin.getDefault().getLog().log(e.getStatus());
			}
		}
		rm.done();
	}

	@Override
	public void getVersion(ICommandControlDMContext ctx, final DataRequestMonitor<String> rm) {
		if (fCommandFactory instanceof GdbExtendedCommandFactory_6_8) {
			GdbExtendedCommandFactory_6_8 factory = (GdbExtendedCommandFactory_6_8)fCommandFactory;

			// Use the cache to avoid having to go to GDB more than once for a value
			// that does not change.  No need to even clear the cache since the GDB version will never change.
			fVersionCache.execute(factory.createCLIGDBVersion(ctx), 
					new ImmediateDataRequestMonitor<MIGDBVersionInfo>(rm) {
				@Override
				protected void handleSuccess() {
					rm.done(getData().getVersion());
				}
			});
		} else {
			rm.done(new Status(IStatus.ERROR, GDBExamplePlugin.PLUGIN_ID,
					NOT_SUPPORTED, "Not supported", null)); //$NON-NLS-1$
		}
	}
}

@@ -365,18 +365,17 @@
 .html%0A *
- 
 %0A
+
  * Contr
@@ -1067,59 +1067,8 @@
 ts;%0A
-import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;%0A
 impo
@@ -1853,18 +1853,17 @@
 tions %7B%0A
-%09
 %0A
+
 %09private
@@ -1976,18 +1976,17 @@
 nCache;%0A
-%09
 %0A
+
     publ
@@ -2275,18 +2275,17 @@
 %09%7D);%0A%09%7D%0A
-%09
 %0A
+
 %09private
@@ -2459,19 +2459,17 @@
 tory();%0A
-%09%09
 %0A
+
 %09%09fVersi
@@ -2711,11 +2711,9 @@
 ));%0A
-%09%09
 %0A
+
 %09%09rm
@@ -2825,10 +2825,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09@Ov
@@ -3053,17 +3053,16 @@
  Status(
- 
 %0A%09%09%09%09ISt
@@ -3075,47 +3075,40 @@
 NFO,
- 
 %0A%09%09%09%09G
-dbPlugin.getUniqueIdentifier(), 
+DBExamplePlugin.PLUGIN_ID,
 %0A%09%09%09
@@ -3147,17 +3147,16 @@
 ER_CODE,
- 
 %0A%09%09%09%09str
@@ -3156,17 +3156,16 @@
 %09%09%09%09str,
- 
 %0A%09%09%09%09nul
@@ -3900,16 +3900,16 @@
 change.%0A
+
 %09%09%09fVers
@@ -3958,17 +3958,16 @@
 on(ctx),
- 
 %0A%09%09%09%09%09ne

Remove warning