001: package net.sourceforge.squirrel_sql.client;
002:
003: /*
004: * TODO: i18n
005: */
006:
007: /*
008: * Copyright (C) 2001-2006 Colin Bell
009: * colbell@users.sourceforge.net
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public
022: * License along with this library; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: import org.apache.commons.cli.CommandLine;
026: import org.apache.commons.cli.CommandLineParser;
027: import org.apache.commons.cli.GnuParser;
028: import org.apache.commons.cli.HelpFormatter;
029: import org.apache.commons.cli.Option;
030: import org.apache.commons.cli.OptionBuilder;
031: import org.apache.commons.cli.Options;
032: import org.apache.commons.cli.ParseException;
033:
034: /**
035: * Application arguments.
036: *
037: * <B>Note:</B> <EM>This class <B>cannot</B> use the logging package as this
038: * class is used to initialize the logging package. Nor can it use any classes
039: * that themselves use the logging package.</EM>
040: *
041: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
042: */
043: public class ApplicationArguments {
044: /**
045: * Option descriptions.
046: *
047: * <UL>
048: * <LI>element 0 - short option
049: * <LI>element 1 - long option (null if none)
050: * <LI>element 2 - option description
051: * </UL>
052: */
053: private interface IOptions {
054: String[] HELP = { "h", "help", "Display Help and exit" };
055: String[] SQUIRREL_HOME = { "home", "squirrel-home",
056: "SQuirreL home directory" };
057: String[] LOG_FILE = { "l", "log-config-file",
058: "Logging configuration file" };
059: String[] USE_DEFAULT_METAL_THEME = { "m",
060: "use-default-metal-theme", "Use default metal theme" };
061: String[] USE_NATIVE_LAF = { "n", "native-laf",
062: "Use native look and feel" };
063: String[] NO_PLUGINS = { "nop", "no-plugins",
064: "Don't load plugins" };
065: String[] NO_SPLASH = { "nos", "no-splash",
066: "Don't display splash screen" };
067: String[] USER_SETTINGS_DIR = { "userdir", "user-settings-dir",
068: "User settings directory" };
069: }
070:
071: /** Only instance of this class. */
072: private static ApplicationArguments s_instance;
073:
074: /** Collection of possible options that acn be passed. */
075: private final Options _options = new Options();
076:
077: /** Parsed command line that was passed to application. */
078: private CommandLine _cmdLine;
079:
080: /** "Raw" arguments straight from the command line. */
081: private String[] _rawArgs;
082:
083: /** Squirrels home directory. */
084: private String _squirrelHome = null;
085:
086: /**
087: * If not <TT>null</TT> then is an override for the users .squirrel-sql
088: * settings directory.
089: */
090: private String _userSettingsDir = null;
091:
092: /** Path for logging configuration file */
093: private String _loggingConfigFile = null;
094:
095: /**
096: * Ctor specifying arguments from command line.
097: *
098: * @param args Arguments passed on command line.
099: *
100: * @throws ParseException
101: * Thrown if unable to parse arguments.
102: */
103: private ApplicationArguments(String[] args) throws ParseException {
104: super ();
105: createOptions();
106:
107: // set up array to return for public access to cmd line args
108: _rawArgs = args;
109:
110: final CommandLineParser parser = new GnuParser();
111: try {
112: _cmdLine = parser.parse(_options, args);
113: } catch (ParseException ex) {
114: System.err.println("Parsing failed. Reason: "
115: + ex.getMessage());
116: printHelp();
117: throw ex;
118: }
119:
120: if (_cmdLine.hasOption(IOptions.SQUIRREL_HOME[0])) {
121: _squirrelHome = _cmdLine
122: .getOptionValue(IOptions.SQUIRREL_HOME[0]);
123: }
124: if (_cmdLine.hasOption(IOptions.USER_SETTINGS_DIR[0])) {
125: _userSettingsDir = _cmdLine
126: .getOptionValue(IOptions.USER_SETTINGS_DIR[0]);
127: }
128: if (_cmdLine.hasOption(IOptions.LOG_FILE[0])) {
129: _loggingConfigFile = _cmdLine
130: .getOptionValue(IOptions.LOG_FILE[0]);
131: }
132: }
133:
134: /**
135: * Initialize application arguments.
136: *
137: * @param args Arguments passed on command line.
138: *
139: * @return <TT>true</TT> if arguments parsed successfully else
140: * <TT>false<.TT>. If parsing was unsuccessful an error was written
141: * to standard error.
142: */
143: public synchronized static boolean initialize(String[] args) {
144: if (s_instance == null) {
145: try {
146: s_instance = new ApplicationArguments(args);
147: } catch (ParseException ex) {
148: return false;
149: }
150: } else {
151: System.out
152: .println("ApplicationArguments.initialize() called twice");
153: }
154: return true;
155: }
156:
157: /**
158: * Return the single instance of this class.
159: *
160: * @return the single instance of this class.
161: *
162: * @throws IllegalStateException
163: * Thrown if ApplicationArguments.getInstance() called
164: * before ApplicationArguments.initialize()
165: */
166: public static ApplicationArguments getInstance() {
167: if (s_instance == null) {
168: throw new IllegalStateException(
169: "ApplicationArguments.getInstance() called before ApplicationArguments.initialize()");
170: }
171: return s_instance;
172: }
173:
174: /**
175: * @return override for the user settings directory. Will be
176: * <TT>null</TT> if not overridden.
177: */
178: public String getSquirrelHomeDirectory() {
179: return _squirrelHome;
180: }
181:
182: /**
183: * @return The name of the directory that Squirrel is installed into.
184: */
185: public String getUserSettingsDirectoryOverride() {
186: return _userSettingsDir;
187: }
188:
189: /**
190: * @return <TT>true</TT> if splashscreen should be shown.
191: */
192: public boolean getShowSplashScreen() {
193: return !_cmdLine.hasOption(IOptions.NO_SPLASH[0]);
194: }
195:
196: /**
197: * @return <TT>true</TT> if help information should be written to
198: * standard output.
199: */
200: public boolean getShowHelp() {
201: return _cmdLine.hasOption(IOptions.HELP[0]);
202: }
203:
204: /**
205: * @return the logging configuration file name. Will be
206: * <TT>null</TT> if not passed.
207: */
208: public String getLoggingConfigFileName() {
209: return _loggingConfigFile;
210: }
211:
212: /**
213: * @return <TT>true</TT> if the plugins should be loaded.
214: */
215: public boolean getLoadPlugins() {
216: return !_cmdLine.hasOption(IOptions.NO_PLUGINS[0]);
217: }
218:
219: /**
220: * @return <TT>true</TT> if the default metal theme should be used
221: * rather than the SQuirreL metal theme.
222: */
223: public boolean useDefaultMetalTheme() {
224: return _cmdLine.hasOption(IOptions.USE_DEFAULT_METAL_THEME[0]);
225: }
226:
227: /**
228: * Retrieve whether to use the native Look and Feel.
229: *
230: * @return <TT>true</TT> to use the native LAF.
231: */
232: public boolean useNativeLAF() {
233: return _cmdLine.hasOption(IOptions.USE_NATIVE_LAF[0]);
234: }
235:
236: /**
237: * @return The raw arguments passed on the command line.
238: */
239: public String[] getRawArguments() {
240: return _rawArgs;
241: }
242:
243: void printHelp() {
244: HelpFormatter formatter = new HelpFormatter();
245: formatter.printHelp("squirrel-sql", _options);
246: }
247:
248: /**
249: * Create the <TT>Options</TT> object used to parse the command line.
250: */
251: private void createOptions() {
252: Option opt;
253:
254: opt = createAnOption(IOptions.NO_SPLASH);
255: _options.addOption(opt);
256:
257: opt = createAnOption(IOptions.HELP);
258: _options.addOption(opt);
259:
260: opt = createAnOption(IOptions.NO_PLUGINS);
261: _options.addOption(opt);
262:
263: opt = createAnOption(IOptions.USE_DEFAULT_METAL_THEME);
264: _options.addOption(opt);
265:
266: opt = createAnOption(IOptions.USE_NATIVE_LAF);
267: _options.addOption(opt);
268:
269: opt = createAnOptionWithArgument(IOptions.SQUIRREL_HOME);
270: _options.addOption(opt);
271:
272: opt = createAnOptionWithArgument(IOptions.USER_SETTINGS_DIR);
273: _options.addOption(opt);
274:
275: opt = createAnOptionWithArgument(IOptions.LOG_FILE);
276: _options.addOption(opt);
277: }
278:
279: private Option createAnOption(String[] argInfo) {
280: Option opt = new Option(argInfo[0], argInfo[2]);
281: if (!isStringEmpty(argInfo[1])) {
282: opt.setLongOpt(argInfo[1]);
283: }
284:
285: return opt;
286: }
287:
288: private Option createAnOptionWithArgument(String[] argInfo) {
289: OptionBuilder.withArgName(argInfo[0]);
290: OptionBuilder.hasArg();
291: OptionBuilder.withDescription(argInfo[2]);
292: Option opt = OptionBuilder.create(argInfo[0]);
293: if (!isStringEmpty(argInfo[1])) {
294: opt.setLongOpt(argInfo[1]);
295: }
296: return opt;
297: }
298:
299: private static boolean isStringEmpty(String str) {
300: return str == null || str.length() == 0;
301: }
302:
303: /**
304: * Resets the internally stored instance so that the next call to initialize
305: * will function as the first call. Useful for unit tests, so it uses package
306: * level access.
307: */
308: static final void reset() {
309: s_instance = null;
310: }
311: }
|