KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > cmdline > lib > ApplicationBase


1 /*====================================================================
2
3 ObjectWeb Util CommandLine Package.
4 Copyright (C) 2003-2005 INRIA - USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle.
23 Contributor(s): Christophe Contreras.
24
25 ====================================================================*/

26
27 package org.objectweb.util.cmdline.lib;
28
29 // Package dependencies.
30
import org.objectweb.util.misc.api.ExceptionWrapper;
31 import org.objectweb.util.cmdline.api.Application;
32 import org.objectweb.util.cmdline.api.CommandLine;
33 import org.objectweb.util.cmdline.api.Console;
34
35 /**
36  * Abstract base class for Application instances.
37  *
38  * This provides the following properties:
39  *
40  * - MailingList: The associated mailing list of this development.
41  *
42  * - VersionInformation: The associated version information.
43  *
44  * This provides the following methods:
45  *
46  * - main and start: Starts the application.
47  *
48  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
49  *
50  * @version 0.1
51  */

52
53 abstract public class ApplicationBase
54               extends DefaultCommandLineHolder
55            implements Application
56 {
57     // ==================================================================
58
//
59
// Internal state.
60
//
61
// ==================================================================
62

63     /** The associated console. */
64     private Console console_;
65
66     // ==================================================================
67
//
68
// Constructors.
69
//
70
// ==================================================================
71

72     /** The default constructor. */
73     public
74     ApplicationBase()
75     {
76         this(null, false, true);
77     }
78
79     /**
80      * The constructor with the command line manager.
81      *
82      * @param commandLine The command line manager.
83      */

84     public
85     ApplicationBase(CommandLine commandLine)
86     {
87         this(commandLine, false, true);
88     }
89
90     /**
91      * The constructor with the command line manager.
92      *
93      * @param commandLine The command line manager.
94      * @param withDefaultOptions True to add default options in the command line.
95      */

96     public
97     ApplicationBase(CommandLine commandLine,
98                     boolean withDefaultOptions)
99     {
100         this(commandLine, false, withDefaultOptions);
101     }
102
103     /**
104      * The constructor with the command line manager.
105      *
106      * @param commandLine The command line manager.
107      * @param silentMessage Boolean for indicating if messages should be displayed.
108      * @param withDefaultOptions True to add default options on the command line.
109      */

110     public
111     ApplicationBase(CommandLine commandLine,
112                     boolean silentMessage,
113                     boolean withDefaultOptions)
114     {
115         // Calls the DefaultCommandLineHolder constructor.
116
super(commandLine);
117
118         // Inits internal state.
119
setConsole(new DefaultConsole(getIdentity(), silentMessage));
120
121         if(withDefaultOptions)
122         {
123             // Adds the --version command line option.
124
if(withDefaultOptions && commandLine != null)
125                 commandLine.addOption(new DefaultOptionVersion(this));
126
127             // Adds the --silent command line option.
128
if(commandLine != null)
129                 commandLine.addOption(new DefaultOptionSilentMessage(console_));
130         }
131     }
132
133     // ==================================================================
134
//
135
// Internal methods.
136
//
137
// ==================================================================
138

139     /**
140      * Reports a Java system property.
141      *
142      * @param propertyName The name of the property to report.
143      */

144     protected void
145     report_property(String JavaDoc propertyName)
146     {
147        getConsole().getErrorStream().
148        println("System property " + propertyName + " = " +
149                System.getProperty(propertyName));
150     }
151
152     // ==================================================================
153
//
154
// Public methods for org.objectweb.util.api.Identifiable
155
//
156
// ==================================================================
157

158     /**
159      * Obtains its identity.
160      *
161      * Should be defined in subclasses.
162      *
163      * @return Its identity.
164      */

165     public String JavaDoc
166     getIdentity()
167     {
168         return this.getClass().getName();
169     }
170
171     // ==================================================================
172
//
173
// Public methods for org.objectweb.util.cmdline.api.CommandLineHolder
174
//
175
// ==================================================================
176

177     // ==================================================================
178
//
179
// Public methods for org.objectweb.util.cmdline.api.ConsoleHolder
180
//
181
// ==================================================================
182

183     /**
184      * Obtains the associated console.
185      *
186      * @return The associated console.
187      */

188     public Console
189     getConsole()
190     {
191         return console_;
192     }
193
194     /**
195      * Sets the associated console.
196      *
197      * @param console The associated console.
198      */

199     public void
200     setConsole(Console console)
201     {
202         console_ = console;
203         getCommandLine().setConsole(console);
204     }
205
206     // ==================================================================
207
//
208
// Public methods for org.objectweb.util.cmdline.api.Application
209
//
210
// ==================================================================
211

212     /**
213      * Obtains the mailing list associated to this development.
214      *
215      * @return The mailing list associated to this development.
216      */

217     public String JavaDoc
218     getMailingList()
219     {
220         return "openccm@objectweb.org";
221     }
222
223     /**
224      * Obtains the associated version information.
225      *
226      * @return The associated version information.
227      */

228     public String JavaDoc[]
229     getVersionInformation()
230     {
231         return new String JavaDoc [] {
232             getIdentity(),
233             "",
234             "Copyright (C) 2000-2005 INRIA - USTL - LIFL - GOAL",
235             "This is free software; you can redistribute it and/or",
236             "modify it under the terms of the GNU Lesser General Public",
237             "License as published by the Free Software Foundation; either",
238             "version 2.1 of the License, or any later version.",
239             "",
240             "This is distributed in the hope that it will be useful,",
241             "but WITHOUT ANY WARRANTY; without even the implied warranty of",
242             "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU",
243             "Lesser General Public License for more details.",
244             "",
245             "You should have received a copy of the GNU Lesser General Public",
246             "License along with this library; if not, write to the Free Software",
247             "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"
248         };
249     }
250
251     /**
252      * Reports an exception stack.
253      *
254      * @param exception The exception to report.
255      */

256     public void
257     report_exception(Exception JavaDoc exception)
258     {
259        java.io.PrintStream JavaDoc err = getConsole().getErrorStream();
260        err.print("Please report the following exception trace to ");
261        err.println(getMailingList());
262 // err.println("including your used ORB name and version.");
263
report_property("os.name");
264        report_property("os.version");
265        report_property("java.vendor");
266        report_property("java.version");
267        exception.printStackTrace();
268     }
269
270     /**
271      * Executes the main function, i.e.
272      * 1 - arguments = getCommandLine().parse(args);
273      * 2 - status = starts(arguments);
274      * 3 - System.exit(status);
275      *
276      * @param args The command line arguments.
277      */

278     public void
279     runMain(String JavaDoc[] args)
280     {
281         try {
282             // Parses the command line arguments.
283
String JavaDoc[] arguments = getCommandLine().parse(args);
284             
285             // Starts the application.
286
int status = start(arguments);
287             
288             // Exits according to the status of the application.
289
System.exit(status);
290         // If any exception thrown then reports it.
291
//
292
} catch(ExceptionWrapper exc) {
293             report_exception(exc.getException());
294         } catch(Exception JavaDoc exc) {
295             report_exception(exc);
296         }
297     }
298     
299     /**
300      * Starts the main function.
301      *
302      * Must be defined into subclasses.
303      *
304      * @param args The command line arguments.
305      *
306      * @return The status of the application.
307      */

308     abstract public int
309     start(String JavaDoc[] args);
310 }
311
Popular Tags