1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
85 if (options.process(args) != options.OK) {
86 return USAGE_ERROR;
87 }
88
89
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 }