Package net.sourceforge.javautil.groovy.cli.debug

Source Code of net.sourceforge.javautil.groovy.cli.debug.ExecuteMainCommand

package net.sourceforge.javautil.groovy.cli.debug;

import net.sourceforge.javautil.classloader.boot.EntryPoint;
import net.sourceforge.javautil.classloader.boot.EntryPointType;
import net.sourceforge.javautil.common.ReflectionUtil;
import net.sourceforge.javautil.common.reflection.cache.ClassCache;
import net.sourceforge.javautil.groovy.cli.GroovyCLIContext;
import net.sourceforge.javautil.ui.cli.CommandLineArgumentsStandard;
import net.sourceforge.javautil.ui.cli.CommandLineCommand;
import net.sourceforge.javautil.ui.command.UICommandAbstract;
import net.sourceforge.javautil.ui.command.UICommandArguments;
import net.sourceforge.javautil.ui.command.UICommandArguments.PassedOption;

/**
* This will execute a main command.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class ExecuteMainCommand extends CommandLineCommand<GroovyCLIContext, CommandLineArgumentsStandard> {
 
  public ExecuteMainCommand(String name) {
    super(name, "Execute a main, entry point type or runnable class");
   
    this.addArgument("class", Class.class, "The main, entry point type or runnable class");
    this.addOption("cl", String.class, "Use the class loader from the groovy environment using the name specified", "");
  }

  public Object execute(GroovyCLIContext ctx, CommandLineArgumentsStandard arguments) {
    EntryPoint point = (EntryPoint) ctx.getUI().getDomain().getVariable("entry");
    EntryPointType type = (EntryPointType) ctx.getUI().getDomain().getVariable("epcli");
   
    ClassLoader original = null;
    PassedOption cl = arguments.getOption("cl");
    if (cl != null && ctx.getUI().getDomain().getVariable((String) cl.getValue()) instanceof ClassLoader) {
      original = Thread.currentThread().getContextClassLoader();
      Thread.currentThread().setContextClassLoader((ClassLoader)ctx.getUI().getDomain().getVariable((String) cl.getValue()));
    }
   
    String mainClass = (String) arguments.getArgument(0).getValue();
   
    try {
      point.boot(ClassCache.getFor(ReflectionUtil.getClass(mainClass)), type, mainClass);
      return new ExecutionResults(mainClass, null);
    } catch (Throwable e) {
      return new ExecutionResults(mainClass, e);
    } finally {
      if (original != null) Thread.currentThread().setContextClassLoader(original);
    }
  }

}
TOP

Related Classes of net.sourceforge.javautil.groovy.cli.debug.ExecuteMainCommand

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.