Source Code Cross Referenced for ApplicationArguments.java in  » Database-Client » squirrel-sql-2.6.5a » net » sourceforge » squirrel_sql » client » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » squirrel sql 2.6.5a » net.sourceforge.squirrel_sql.client 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:            /** &quot;Raw&quot; 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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.