1 /*
2  * @(#) $Id: SSLTool.java,v 1.3 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.ssl;
11
12import java.util.*;
13import java.util.logging.Logger;
14import java.security.*;
15
16
17import org.jstk.*;
18
19
20public class SSLTool{
21    public static final Logger logger = Logger.getLogger("org.jstk.ssl");
22    static HashMap cmds = new HashMap();    // Keep it accessible by BenchCommand.
23    static {
24        cmds.put("server", new ServerCommand());
25        cmds.put("client", new ClientCommand());
26        cmds.put("proxy", new ProxyCommand());
27        cmds.put("show", new ShowCommand());
28    }
29    public String progName(){
30        String progName = System.getProperty("org.jstk.ssl.progname");
31        if (progName == null)
32            progName = "java org.jstk.ssl.SSLTool";
33
34        return progName;
35    }
36    public String briefDescription(){
37        return "a tool for SSL communication";
38    }
39    public String extendedUsageString(){
40        StringBuffer sb = new StringBuffer();
41        Iterator itr = cmds.entrySet().iterator();
42        while (itr.hasNext()){
43            Map.Entry ent = (Map.Entry)itr.next();
44            String key = (String)ent.getKey();
45            JSTKCommand cmd = (JSTKCommand)ent.getValue();
46
47            sb.append("  " + key);
48            int blanksNeeded = 12 - key.length();
49            for (int i = 0; i < blanksNeeded; i++)
50                sb.append(" ");
51            sb.append(cmd.briefDescription() + "\n");
52        }
53        return sb.toString();
54    }
55
56    public void printUsage(){
57        System.out.println("Description:: \n  " + briefDescription());
58        System.out.println("Usage:: \n  " + progName() + " <cmd> [<options>]");
59        System.out.println("Commands:: ");
60        System.out.println(extendedUsageString());
61        System.out.println("Notes:: ");
62        System.out.println("  -- Type \"" + progName() + " <cmd> help\" to get command specific help.");
63        System.out.println("  -- Specify option \"-showtime\" to get command execution time. Example:");
64        System.out.println("       " + progName() + " genk -showtime");
65    }
66
67    public void printCmdUsage(JSTKCommand cmd, String cmdString){
68        System.out.println("Description:: \n  " + cmd.briefDescription());
69        System.out.println("\nUsage:: \n  " + progName() + " " + cmdString + " [<options>]\n");
70
71        String[] forms = cmd.useForms();
72        if (forms != null){
73            for (int i = 0; i < forms.length; i++){
74                System.out.println("  " + progName() + " " + cmdString + " " + forms[i]);
75            }
76        }
77
78        System.out.println("\nOptions:: ");
79        System.out.println(cmd.optionsDescription());
80        String[] uses = cmd.sampleUses();
81        if (uses != null){
82            System.out.println("Sample Uses:: ");
83            for (int i = 0; i < uses.length; i++){
84                System.out.println("  " + progName() + " " + cmdString + " " + uses[i]);
85            }
86        }
87    }
88
89    public static void main(String[] args) throws Exception {
90        SSLTool st = new SSLTool();
91        JSTKOptions opts = new JSTKOptions();
92        if (args.length < 1){       // No argument. Print help message.
93            st.printUsage();
94            return;
95        }
96        String cmdString = args[0];
97        if (cmdString.equals("-h") || cmdString.equals("help") || cmdString.equals("-?")){
98            st.printUsage();
99            return;
00        }
01
02        JSTKCommand cmd = (JSTKCommand)cmds.get(cmdString);
03        if (cmd == null){   // Unknown command.
04            System.out.println("Unknown Command: " + cmdString);
05            st.printUsage();
06            return;
07        }
08
09        if (args.length > 1 && (args[1].equals("-h") || args[1].equals("help") || args[1].equals("-?"))){
10            st.printCmdUsage(cmd, cmdString);
11            return;
12        }
13
14        opts.parse(args, 1);
15        boolean showtime = Boolean.valueOf(opts.get("showtime")).booleanValue();
16        long ts = 0, tt = 0;
17        if (showtime)
18            ts = System.currentTimeMillis();
19
20        JSTKResult result = (JSTKResult)cmd.execute(opts);
21
22        if (showtime)
23            tt = System.currentTimeMillis() - ts;
24        System.out.println(result.getText());
25        if (showtime)
26            System.out.println("Execution Time: " + (float)tt/1000.0 + " secs.");
27        System.exit(result.isSuccess() ? 0 : 1);
28    }
29}