1 /*
2 * db2sa: DB2 Syntax Assistant
3 * Copyright (C) Andres Gomez Casanova
4 *
5 * This file is part of db2sa.
6 *
7 * db2sa is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * db2sa 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
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 *
20 * Contact:
21 * a n g o c a at y a h o o dot c o m
22 * Cra. 45 No 61 - 31, Bogota, Colombia.
23 *
24 * Author: $LastChangedBy: angoca $:
25 * Date: $LastChangedDate: 2009-07-08 19:56:43 +0200 (Wed, 08 Jul 2009) $:
26 * Revision: $LastChangedRevision: 357 $:
27 * URL: $HeadURL: https://db2sa.svn.sourceforge.net/svnroot/db2sa/branches/db2sa_beta/source-code/src/main/java/name/angoca/db2sa/Main.java $:
28 */
29 package name.angoca.db2sa;
30
31 import name.angoca.db2sa.cli.AbstractInterfaceController;
32 import name.angoca.db2sa.cli.exceptions.InputReaderException;
33 import name.angoca.db2sa.cli.jline.JlineInterfaceController;
34 import name.angoca.db2sa.cli.system.SystemInterfaceController;
35 import name.angoca.db2sa.exceptions.AbstractDB2SAException;
36 import name.angoca.db2sa.messages.Messages;
37
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42 * This is the class where the application can be started.<br/>
43 * <b>Control Version</b><br />
44 * <ul>
45 * <li>0.0.1 Class creation.</li>
46 * <li>0.0.2 Calls the interface controller with the System implementation.</li>
47 * <li>0.0.3 Specific interface controller.</li>
48 * <li>0.0.4 Logger.</li>
49 * <li>0.0.5 Prompt from the configurator.</li>
50 * <li>0.0.6 Delete the syntax call.</li>
51 * <li>0.1.0 Changed for tests.</li>
52 * <li>0.1.1 Log time.</li>
53 * <li>0.1.2 final.</li>
54 * <li>1.0.0 Moved to version 1.</li>
55 * </ul>
56 *
57 * @author Andres Gomez Casanova <a
58 * href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
59 * @version 1.0.0 2009-07-19
60 */
61 public final class Main {
62
63 /**
64 * Logger.
65 */
66 private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
67
68 /**
69 * Default constructor private.
70 */
71 private Main() {
72 // Nothing to do. No Tests
73 }
74
75 /**
76 * Main method.
77 *
78 * @param args
79 * Arguments for the main method.
80 */
81 public static void main(final String[] args) {
82 Main. LOGGER.debug("Application Start {}"); //$NON-NLS-1$
83 try {
84 final int controllerType = Main.getControllerType(args);
85 final AbstractInterfaceController controller = Main
86 .prepare(controllerType);
87 controller.start();
88 } catch (final AbstractDB2SAException exception) {
89 Main.LOGGER.error(exception.getMessage());
90 }
91 }
92
93 /**
94 * Parses the parameters and returns the interface controller type.
95 *
96 * @param args
97 * Arguments.
98 * @return Type of controller.
99 */
100 static int getControllerType(final String[]/* [!]! */args) {
101 int type = 0;
102 if (args != null && args.length > 0 && args[0].equals("System")) { //$NON-NLS-1$
103 type = 1;
104 }
105 return type;
106 }
107
108 /**
109 * Starts the selected user interface.
110 *
111 * @param type
112 * Type of user interface.
113 * @return The interface instantiated.
114 * @throws InputReaderException
115 * If there is a problem establishing the console or user
116 * interface.
117 */
118 static AbstractInterfaceController prepare(final int type)
119 throws InputReaderException {
120 final String prompt = Configurator.getInstance().getProperty(
121 Constants.PROMPT);
122 if (prompt == null) {
123 // TODO Lanzar una excepción propia
124 throw new RuntimeException(Messages
125 .getString("Main.PromptNotDefined")); //$NON-NLS-1$
126 }
127 AbstractInterfaceController controller = null;
128 if (type == 1) {
129 controller = new SystemInterfaceController(prompt);
130 } else {
131 controller = new JlineInterfaceController(prompt);
132 }
133 return controller;
134 }
135 }