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