View Javadoc

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