Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011, 2012 SAP AG 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: Lazar Kirchev, SAP AG - initial API and implementation /
 
 
 package org.eclipse.equinox.console.commands;
 
 import java.util.Set;
 
This class provides help for the legacy equinox commands, which are adapted to Gogo commands.
 
 public class HelpCommand {
 	private BundleContext context;
 	private static final String COMMANDS = ".commands";
 	
     	private BundleContext context;
     	public CommandProviderCustomizer(BundleContext context) {
     		this. = context;
     	}
     	
 			if (reference.getProperty("osgi.command.function") != null) {
 				// must be a gogo function already; don' track
 				return null;
 			}
 			CommandProvider command = .getService(reference);
 			.ungetService(reference);
 		}
 
 		public void modifiedService(
 				Set<CommandProviderservice) {
 			// nothing to do
 		}
 
 		public void removedService(ServiceReference<CommandProviderreference,
 				Set<CommandProviderproviders) {
 			CommandProvider provider = .getService(reference);
 			providers.remove(provider);
 		}
     	
     }
 	
 	public HelpCommand(BundleContext context) {
 		this. = context;
 	}
 	
 	public void startService() {
 		Dictionary<StringObjectprops = new Hashtable<StringObject>();
 		props.put(."equinox");
 		props.put(.new String[] {"help"});
 		.registerService(HelpCommand.class.getName(), thisprops);
 	}

Provides help for the available commands. Prints the names, descriptions and parameters of all registered commands. If -scope <command_scope> is passed to the command, help is printed only for the commands with the specified scope. If a command name is passed as argument to the help command, then the help message only for the particular command is displayed (if such is defined).

Parameters:
session
args
Throws:
java.lang.Exception
	public void help(final CommandSession sessionString... argsthrows Exception {
		String command = null;
		String scope = null;
		if (args.length > 0) {
			if (args[0].equals("-scope")) {
				if (args.length < 2) {
					..println("Specify scope");
					return;
else {
					scope = args[1];
				}
else {
				command = args[0];
			}
		}
		if (command != null) {
			printGogoCommandHelp(sessioncommand);
			return;
		}
		if ((scope == null) || "equinox".equals(scope)) {
		}
		printAllGogoCommandsHelp(sessionscope);
	}
	private void printGogoCommandHelp(final CommandSession sessionString commandthrows Exception {
		try {
			session.execute("felix:help " + command);
catch (IllegalArgumentException e) {
		}
	}
	private void printAllLegacyCommandsHelp() {
		for (CommandProvider commandProvider : ) {
			..println(commandProvider.getHelp());
		}
	}
	private void printAllGogoCommandsHelp(final CommandSession sessionString scopethrows Exception {
		@SuppressWarnings("unchecked")
		Set<StringcommandNames = (Set<String>) session.get();
		try {
			for (String commandName : commandNames) {
				if (scope != null && !commandName.startsWith(scope + ":")) {
					continue;
				}
				session.execute("felix:help " + commandName);
			}
catch (IllegalArgumentException e) {
		}
	}
	private void printLegacyCommandHelp(String command) {
			Method[] methods = provider.getClass().getMethods();
			for (Method method : methods) {
				Object retval = null;
				if (method.getName().equals("_" + command)) {
					try {
						Method helpMethod = provider.getClass().getMethod("_help"CommandInterpreter.class);
						ArrayList<ObjectargsList = new ArrayList<Object>();
						argsList.add(command);
						retval = helpMethod.invoke(providernew CustomCommandInterpreter(argsList));
catch (Exception e) {
						break;
					}
					if (retval != null && retval instanceof String) {
					}
					break;
				}
			}
		}
	}
	private boolean checkStarted(String symbolicName) {
		Bundle[] bundles = .getBundles();
		for (Bundle bundle : bundles) {
			if (bundle.getSymbolicName().equals(symbolicName) && bundle.getState() == .) {
				return true;
			}
		}
		return false;
	}
	private void handleCommandNotFound() {
		if (checkStarted("org.apache.felix.gogo.command")) {
			..println("Cannot find felix:help command");
else {
			..println("Cannot find felix:help command; bundle org.apache.felix.gogo.command is not started");
		}
	}
New to GrepCode? Check out our FAQ X