1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2004, 2009, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.audit;
  28 
  29 import java.util.ListIterator;
  30 
  31 import com.sun.javatest.Parameters;
  32 import com.sun.javatest.tool.Command;
  33 import com.sun.javatest.tool.CommandContext;
  34 import com.sun.javatest.tool.CommandManager;
  35 import com.sun.javatest.tool.Desktop;
  36 import com.sun.javatest.util.HelpTree;
  37 import com.sun.javatest.util.I18NResourceBundle;
  38 
  39 
  40 /**
  41  * The CommandManager for audit support.
  42  */
  43 public class AuditCommandManager extends CommandManager
  44 {
  45 
  46     public HelpTree.Node getHelp() {
  47         String[] cmds = {
  48             "audit",
  49             "showAudit"
  50         };
  51         return new HelpTree.Node(i18n, "cmgr.help", cmds);
  52     }
  53 
  54     //----------------------------------------------------------------------------
  55 
  56     public boolean parseCommand(String cmd, ListIterator<String> argIter, CommandContext ctx)
  57         throws Command.Fault
  58     {
  59         if (cmd.equalsIgnoreCase(AuditCommand.getName())) {
  60             ctx.addCommand(new AuditCommand(argIter));
  61             return true;
  62         }
  63 
  64         if (cmd.equalsIgnoreCase(ShowAuditCommand.getName())) {
  65             ctx.addCommand(new ShowAuditCommand(argIter));
  66             return true;
  67         }
  68 
  69         return false;
  70     }
  71 
  72     private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(AuditCommandManager.class);
  73 
  74     //----------------------------------------------------------------------------
  75 
  76     private static class AuditCommand extends Command
  77     {
  78         static String getName() {
  79             return "audit";
  80         }
  81 
  82         AuditCommand(ListIterator<String> argIter) throws Fault {
  83             super(getName());
  84 
  85             while (argIter.hasNext()) {
  86                 String arg = nextArg(argIter);
  87                 if (arg.equalsIgnoreCase("-showEnvValues"))
  88                     showAllEnvValues = true;
  89                 else if (arg.equalsIgnoreCase("-showMultipleEnvValues"))
  90                     showMultipleEnvValues = true;
  91                 else {
  92                     putbackArg(argIter);
  93                     break;
  94                 }
  95             }
  96         }
  97 
  98         public boolean isActionCommand() {
  99             return true;
 100         }
 101 
 102         public void run(CommandContext ctx) throws Fault {
 103             Parameters p = getConfig(ctx);
 104 
 105             Audit a = new Audit(p);
 106             a.report(System.out, showAllEnvValues, showMultipleEnvValues);
 107 
 108             if (a.isOK())
 109                 ctx.printMessage(i18n, "audit.ok");
 110             else
 111                 ctx.printErrorMessage(i18n, "audit.failed");
 112         }
 113 
 114         private boolean showAllEnvValues;
 115         private boolean showMultipleEnvValues;
 116     }
 117 
 118     //----------------------------------------------------------------------------
 119 
 120     private static class ShowAuditCommand extends Command {
 121         static String getName() {
 122             return "showAudit";
 123         }
 124 
 125         ShowAuditCommand(ListIterator argIter) {
 126             super(getName());
 127         }
 128 
 129         public int getDesktopMode() {
 130             return DESKTOP_REQUIRED_DTMODE;
 131         }
 132 
 133         public void run(CommandContext ctx) {
 134             Desktop d = ctx.getDesktop();
 135             AuditToolManager tm = (AuditToolManager) (d.getToolManager(AuditToolManager.class));
 136             tm.startTool();
 137         }
 138     }
 139 }
 140