View Javadoc

1   package net.sf.bse;
2   
3   /*
4    * Copyright (c) 2002-2003 BSE project contributors 
5    * (http://bse.sourceforge.net/)
6    * 
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.ByteArrayOutputStream;
28  import java.io.FileInputStream;
29  import java.io.IOException;
30  import java.io.PrintStream;
31  import java.security.cert.Certificate;
32  import java.text.DateFormat;
33  import java.text.ParseException;
34  import java.util.Date;
35  import java.util.Locale;
36  import java.util.Map;
37  
38  import org.bouncycastle.jce.provider.JDKX509CertificateFactory;
39  import org.bouncycastle.jce.provider.X509CertificateObject;
40  
41  /***
42   * Abstract class representing a command.
43   *
44   * @author Bill Foote (bill.foote@sun.com)
45   * @version $Revision: 1.1 $ $Date: 2003/01/17 14:40:16 $
46   */
47  public abstract class Command
48  {    
49      private Map arguments;
50      
51      protected Command(Map arguments)
52      {
53          this.arguments = arguments;
54      }
55      
56      /***
57       * @return the given argument, or null if it's not there
58       */
59      protected String getArg(String key)
60      {
61          return (String)arguments.get(key);
62      }
63      
64      protected Date getDateArg(String key)
65      {
66          DateFormat df = 
67              DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRANCE);
68          try
69          {
70              df.setLenient(false);
71              return df.parse(getArg(key));
72          } 
73          catch (ParseException ex)
74          {
75              System.err.println();
76              System.err.println(
77                  "Cannot parse the date given for \"" + key + "\".");
78              System.err.println(ex.toString());
79              System.err.println();
80              System.exit(1);
81              return null;
82          }
83      }
84      
85      protected byte[] readBytesFromFile(String fn) throws IOException
86      {
87          FileInputStream fis = new FileInputStream(fn);
88          ByteArrayOutputStream bos = new ByteArrayOutputStream();
89          int ch;
90          while ((ch = fis.read()) != -1)
91          {
92              bos.write(ch);
93          }
94          return bos.toByteArray();
95      }
96      
97      protected X509CertificateObject readX509(ByteArrayInputStream bis)
98          throws Exception
99      {
100         JDKX509CertificateFactory cf = new JDKX509CertificateFactory();
101         // I'm really supposed to use the generic API, but I ended up
102         // going straight to the implementation as part of debugging.
103         // It works now, so I don't really want to experiment with
104         // switching back.
105         Certificate cert = cf.engineGenerateCertificate(bis);
106         return (X509CertificateObject) cert;
107     }
108     
109     public abstract void usageMessage(PrintStream out);
110     
111     public abstract String[] getRequiredArgs();
112     
113     public abstract String[] getOptionalArgs();
114     
115     /***
116      * Execute this command.
117      */
118     public abstract void run() throws Exception;    
119 }