View Javadoc

1   package nl.toolforge.karma.cli;
2   
3   import java.io.File;
4   
5   import nl.toolforge.karma.cli.cmd.CLICommandResponseHandler;
6   import nl.toolforge.karma.core.boot.WorkingContext;
7   import nl.toolforge.karma.core.boot.WorkingContextConfiguration;
8   import nl.toolforge.karma.core.boot.WorkingContextException;
9   import nl.toolforge.karma.core.cmd.Command;
10  import nl.toolforge.karma.core.cmd.CommandContext;
11  import nl.toolforge.karma.core.cmd.CommandException;
12  import nl.toolforge.karma.core.cmd.CommandFactory;
13  import nl.toolforge.karma.core.cmd.CommandLoadException;
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  import java.util.prefs.Preferences;
18  
19  /***
20   * The command-line-interface for Karma. This class runs one command, then quits (gracefully hopefully). All arguments
21   * are considered a command-line that will be interpreted by the <code>CommandContext</code>.
22   *
23   * @author D.A. Smedes
24   * @version $Id: CLI.java,v 1.66 2004/11/29 11:14:09 hippe Exp $
25   */
26  public final class CLI {
27  
28    private static Log logger = LogFactory.getLog(CLI.class);
29  
30  
31    /***
32     *
33     * @param args  The working context (0), whether to update (1) and the command
34     *              plus his options (3 ...).
35     */
36    public void runCli(String[] args) {
37      boolean updateStores = new Boolean(args[1]).booleanValue();
38  
39      //compose the command line to be passeg to the getCommand method.
40      String commandLine = args[2];
41      for (int i = 3; i < args.length; i++) {
42        if (args[i].indexOf(" ") != -1) {
43          //put quotes around arguments that contain spaces
44          commandLine += " \""+ args[i] +"\"";
45        } else {
46          commandLine += " " + args[i];
47        }
48      }
49      logger.debug("runCli - commandline has become: "+commandLine);
50  
51      // todo WorkingContext should be initializing the logging system. Some other way.
52      WorkingContext workingContext;
53      if ( args[0] == null || args[0].equals("") ) {
54        workingContext = new WorkingContext(Preferences.userRoot().get(WorkingContext.WORKING_CONTEXT_PREFERENCE, WorkingContext.DEFAULT));
55      } else {
56        workingContext = new WorkingContext(args[0]);
57      }
58      WorkingContextConfiguration configuration = new WorkingContextConfiguration(workingContext);
59  
60      try {
61        configuration.load();
62      } catch (WorkingContextException e) {
63        //
64      }
65      workingContext.configure(configuration);
66  
67      System.out.println("[ karma ] Checking command ...");
68  
69      try {
70        Command command = null;
71        try {
72          CommandFactory factory = CommandFactory.getInstance();
73          command = factory.getCommand(commandLine);
74        } catch (CommandLoadException e) {
75          throw new CommandException(e.getErrorCode(),  e.getMessageArguments());
76        }
77  
78        System.out.println("[ karma ] Command `" + command.getName() + "` ok !");
79        System.out.println("[ karma ] Working context : " + workingContext.getName());
80  
81        CommandContext commandContext = new CommandContext(workingContext);
82        try {
83          commandContext.init(new CLICommandResponseHandler(), updateStores);
84        }  catch (CommandException e) {
85          System.out.println("\n" + "[ karma ] " + e.getMessage());
86        }
87  
88        commandContext.execute(command);
89      } catch (CommandException e) {
90  
91        System.out.println("\n" + "[ karma ] " + e.getMessage());
92  
93        System.exit(1);
94      } catch (Throwable e) {
95        logger.fatal("Exception caught by CLI catch-all. ", e);
96        
97        String logfile = System.getProperty("karma.home", System.getProperty("user.home")) + File.separator + "logs" + File.separator + "karma-default.log";
98        
99        System.out.println("Something went BOOM inside of Karma.");
100       System.out.println("Details: " + (e.getMessage() != null ? e.getMessage() : e.getClass().getName()));
101       System.out.println("See the log file (" + logfile + ") for more information.");
102       System.out.println("Please report recurring problems to the Karma developers (http://sourceforge.net/tracker/?group_id=98766).");
103       System.out.println("We apologize for the inconvenience.");
104       System.out.println();
105       
106       System.exit(1);
107     }
108     
109     System.exit(0);
110   }
111 
112 }