View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package org.apache.jdo.impl.enhancer;
18  
19  import java.io.PrintWriter;
20  
21  
22  /***
23   * Base class for JDO command line enhancer and tests.
24   *
25   * @author Martin Zaun
26   */
27  public class GenericMain
28      extends LogSupport
29  {
30      // return values for process() method
31      static public final int OK = 0;
32      static public final int USAGE_ERROR = -1;
33      static public final int USER_EXCEPTION = -2;
34      static public final int INTERNAL_ERROR = -3;
35  
36      /***
37       *  The options and arguments.
38       */
39      protected GenericOptions options;
40  
41      /***
42       * Creates an instance.
43       */
44      public GenericMain(PrintWriter out,
45                         PrintWriter err)
46      {
47          this(out, err, new GenericOptions(out, err));
48      }
49  
50      /***
51       * Creates an instance.
52       */
53      public GenericMain(PrintWriter out,
54                         PrintWriter err,
55                         GenericOptions options)
56      {
57          super(out, err);
58          this.options = options;
59      }
60  
61      // ----------------------------------------------------------------------
62  
63      /***
64       * Initializes all components.
65       */
66      protected void init()
67          throws EnhancerFatalError, EnhancerUserException
68      {}
69      
70      /***
71       * Do processing (to be overloaded by subclasses).
72       */
73      protected int process()
74      {
75          return OK;
76      }
77      
78      /***
79       * Process command line arguments, run initialization and do processing.
80       */
81      public int run(String[] args)
82      {
83          try {
84              // process passed command-line arguments
85              if (options.process(args) != options.OK) {
86                  return USAGE_ERROR;
87              }
88  
89              // run initialization and do processing
90              init();
91              return process();
92          } catch (RuntimeException ex) {
93              printlnErr("exception caught", ex);
94              return INTERNAL_ERROR;
95          } catch (Exception ex) {
96              printlnErr("exception caught", ex);
97              return USER_EXCEPTION;
98          }
99      }
100 
101     // ----------------------------------------------------------------------
102 
103     /***
104      * Runs this class
105      */
106     static public void main(String[] args)
107     {
108         final PrintWriter out = new PrintWriter(System.out, true);
109         out.println("--> GenericMain.main()");
110         final GenericMain main = new GenericMain(out, out);
111         int res = main.run(args);
112         out.println("<-- GenericMain.main(): exit = " + res);
113         System.exit(res);
114     }
115 }