1 /***
2 * Copyright (c) 2003, 2004, Chess iT
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Chess iT, nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific
17 * prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32 package nl.chess.it.util.config.examples.extended;
33
34 import nl.chess.it.util.config.ConfigValidationResult;
35 import nl.chess.it.util.config.examples.simple.SimpleConfig;
36
37 import org.apache.log4j.Logger;
38
39 import java.util.Iterator;
40
41
42 /***
43 * This class makes sure the correct order is used in initializing the various part of the application. It will also complain if
44 * the surroundings of the application are not correctly configured (i.e., no valid configuration file given).
45 *
46 * <p>
47 * It serves as the main Singleton class of this application.
48 * </p>
49 *
50 * @author Guus Bosman (Chess iT)
51 * @version $Revision: 1.1.1.1 $
52 */
53 public class Application {
54 private static Application instance = new Application();
55 private Logger logger = Logger.getLogger(Application.class);
56 private SimpleConfig config;
57
58 private Application() {
59 logger.info("Starting application.");
60
61 displayVersionInformation();
62
63 readConfiguration();
64
65 logger.info("Succesfully started application.");
66 }
67
68 /***
69 * Displays information about who built this application on the logger. If anything goes wrong a warning is displayed but the
70 * application continues.
71 */
72 private void displayVersionInformation() {
73 VersionInformation versionInformation = null;
74
75 try {
76 versionInformation = new VersionInformation();
77 }
78 catch (Exception e) {
79 logger.warn("Couldn't read version information (not fatal).", e);
80
81 return;
82 }
83
84 ConfigValidationResult result = versionInformation.validateConfiguration();
85
86 if (result.thereAreErrors()) {
87 logger.warn("There are errors in the version information file (not fatal).");
88
89 for (Iterator iter = result.getErrors().iterator(); iter.hasNext();) {
90 logger.debug(" > " + iter.next());
91 }
92
93 return;
94 }
95
96 if (result.thereAreUnusedProperties()) {
97 logger.debug("Unused properties in version information: " + result.getUnusedProperties());
98 }
99
100
101 logger.info("Application version " + versionInformation.getVersion() + ", built by " + versionInformation.getBuiltBy() +
102 " on " + versionInformation.getBuiltOn() + ".");
103 }
104
105 /***
106 * Report a (fatal) starting up error and shut down the application.
107 *
108 * @param string Human readable description of the problem.
109 * @param e (Optional) exception that explains the situation a bit better.
110 */
111 private void fatalError(String string, Throwable e) {
112 String niceDescription = "Ending application because " + string + ".";
113
114 if (e == null) {
115 logger.fatal(niceDescription);
116 }
117 else {
118 logger.fatal(niceDescription, e);
119 }
120
121 System.err.println(niceDescription);
122 endApplication();
123 }
124
125 /***
126 * Reads and validates the configuration file.
127 */
128 private void readConfiguration() {
129 logger.debug("Reading configuration from " + SimpleConfig.RESOURCE_NAME);
130
131 try {
132 config = new SimpleConfig(SimpleConfig.RESOURCE_NAME);
133 }
134 catch (Exception e) {
135 fatalError("Couldn't read configuration.", e);
136 }
137
138 ConfigValidationResult result = config.validateConfiguration();
139
140 if (result.thereAreErrors()) {
141 logger.fatal("There are errors in the configuration.");
142
143 for (Iterator iter = result.getErrors().iterator(); iter.hasNext();) {
144 logger.error(" > " + iter.next());
145 }
146
147 fatalError("there are errors in the configuration", null);
148 }
149
150 if (result.thereAreUnusedProperties()) {
151 logger.info("Unused properties: " + result.getUnusedProperties());
152 }
153 }
154
155 public static Application getInstance() {
156 return instance;
157 }
158
159 /***
160 * Ends the application.
161 */
162 private void endApplication() {
163 logger.debug("Ending application.");
164 System.exit(1);
165 }
166
167 public SimpleConfig getConfig() {
168 return config;
169 }
170 }