001: package com.jat.core;
002:
003: import java.util.Enumeration;
004: import java.util.Vector;
005:
006: import com.jat.core.config.Config;
007: import com.jat.core.init.Initable;
008: import com.jat.core.log.LogManager;
009:
010: /**
011: * <p>Title: JAT</p>
012: * <p>Description: This class is responsible for JAT project activation.
013: * </p>
014: * <p>
015: * You can use this class in your standalone application (e.g. batch program, server application, ect.)
016: * or Web application.<br>
017: * In case of Web Application you can use the servlet {@link com.jat.core.ProjectServlet} to instantiate this class.
018: * </p>
019: * <p>
020: * A configuration file is needed to instantiate this class.
021: * You can pass the configuration file name in the main method (in case of standalone application) or in the servlet initialization configuration of your application server.
022: * Otherwise the default configuration file name will be used.
023: * </p>
024: * <p><b>Configuration:</b><br/>
025: * The following sections must be configured:
026: * <ul>
027: * <li><b>general</b>:
028: * <ul>
029: * <li><b>date_format</b>: date format (ex. <i>dd/MM/yyyy</i>)</li>
030: * <li><b>long_date_format</b>: long date format (ex. <i>dd/MM/yyyy HH:mm</i>)</li>
031: * <li><b>version</b>: project version
032: * </ul>
033: * </li>
034: * <li><b>config</b>: further configuration file name list (absolute or relative path included)</li>
035: * <li><b>log</b>: logging information (see {@link com.jat.core.log.LogManager})
036: * <li><b>init</b>: the list of object to be pre-loaded; these objects must implement the {@link com.jat.core.init.Initable} interface</li>
037: * <li><b>xml_object</b>: the configuration for the business object XML representation (see {@link com.com.jat.business.BusinessObject} and {@link com.jat.business.BusinessObjectList})
038: * <ul>
039: * <li><b>schema</b> (<u>optional</u>) the XML schema filename (with absolute or relative path)</li>
040: * <li><b>list</b> the name of the tag for a list of objects (see {@link com.jat.business.BusinessObjectList}. It must be conformed to the XML schema, if used</li>
041: * <li><b>object</b> the name of the tag for an object (see {@link com.com.jat.business.BusinessObject}.It must be conformed to the XML schema, if used</li>
042: * <li><b>property</b> the name of the tag for a property of an object (see {@link com.com.jat.business.BusinessObject}.It must be conformed to the XML schema, if used</li>
043: * </ul>
044: * </li>
045: * <li><b>authentication</b> the class name responsible for authentication, if needed. This class must extends the {@link com.jat.business.authentication.Authenticator} class</li>
046: * </ul>
047: * </p>
048: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
049: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
050: * @author stf
051: * @version 1.1
052: * @see com.jat.core.log.LogManager
053: * @see com.jat.core.config.Config
054: */
055:
056: public final class Project {
057:
058: public static Project getInstance() {
059: return project_;
060: }
061:
062: public void init() throws Exception {
063: errorOccours_ = 0;
064: try {
065: this .initConfigFiles();
066: LogManager.init();
067: initBusinessObjects();
068: } catch (Exception ex) {
069: try {
070: LogManager
071: .sendError("Project::init:: exception: " + ex);
072: } catch (Exception ignored) {
073: }
074: throw new Exception("init exception: " + ex);
075: }
076: }
077:
078: public void initBusinessObjects() throws Exception {
079: try {
080: Vector vect = Config.getCurrent().getSubValues("init",
081: "object");
082: Initable initable = null;
083: for (Enumeration e = vect.elements(); e.hasMoreElements();) {
084: initable = (Initable) (Class.forName((String) e
085: .nextElement())).newInstance();
086: initable.init();
087: LogManager.sendDebug("Project::initBusinessObjects: "
088: + initable.getClass().getName()
089: + ": init successfully");
090: }
091: } catch (Exception ex) {
092: LogManager
093: .sendError("Project::initBusinessObjects: exception: "
094: + ex);
095: }
096: }
097:
098: public void initConfigFiles() throws Exception {
099: try {
100: Config config = Config.init(configFile);
101: Vector configFiles = config
102: .getSubValues("config", "config");
103: config.addFiles(configFiles);
104: } catch (Exception ex) {
105: throw new Exception("exception initialiting config files: "
106: + ex);
107: }
108: }
109:
110: private Project(String _configFile) throws Exception {
111: try {
112: if (_configFile == null)
113: this .configFile = DEFAULT_CONFIG_FILE;
114: else
115: this .configFile = _configFile;
116: project_ = this ;
117: System.out.println("JAT Project starting...");
118: init();
119: LogManager.sendLog("JAT Project '" + this .getProjectName()
120: + "' successfully inited.");
121: if (errorOccours_ == 0)
122: System.out.println("JAT Project '"
123: + this .getProjectName()
124: + "' successfully started");
125: else
126: System.out
127: .println("JAT Project '"
128: + this .getProjectName()
129: + "' started but some error(s) occours. Please, see error log for more details.");
130: inited_ = true;
131: } catch (Exception ex) {
132: initError_ = true;
133: initException_ = ex;
134: System.out.println("Project init error: " + ex);
135: try {
136: LogManager.sendError("Project:: " + ex);
137: } catch (Exception ignored) {
138: }
139: throw ex;
140: }
141: }
142:
143: public static synchronized void create(String configFile)
144: throws Exception {
145: if (Project.initError_)
146: throw initException_;
147: if (!inited_)
148: new Project(configFile);
149: }
150:
151: public static void notifyError() {
152: errorOccours_++;
153: }
154:
155: public static int getErrors() {
156: return errorOccours_;
157: }
158:
159: public static String getHostApplication() {
160: if (hostAddress == null) {
161: try {
162: hostAddress = java.net.InetAddress.getLocalHost()
163: .getHostAddress();
164: } catch (Exception e) {
165: System.out
166: .println("Project::getHostApplication:: exception: "
167: + e);
168: hostAddress = "N.D.";
169: }
170: }
171: return hostAddress;
172: }
173:
174: public String getProjectName() {
175: try {
176: return Config.getCurrent().getValue("general",
177: "project_name");
178: } catch (Exception ex) {
179: return "Unknown";
180: }
181: }
182:
183: public String getVersion() throws Exception {
184: return Config.getCurrent().getValue("general", "version");
185: }
186:
187: public static void main(String[] args) {
188: String configFile = DEFAULT_CONFIG_FILE;
189: try {
190: configFile = args[0];
191: System.out.println("JAT application: config file: "
192: + configFile);
193: } catch (Exception ex) {
194: System.out
195: .println("JAT application: config file not found. Using default: "
196: + configFile);
197: }
198: try {
199: Project.create(configFile);
200: } catch (Exception ex) {
201: ex.printStackTrace();
202: }
203: }
204:
205: protected static final String DEFAULT_CONFIG_FILE = "./config/general.config";
206: protected String configFile;
207:
208: /**
209: * The unique CGConfig instance
210: */
211: private static Project project_ = null;
212:
213: private static boolean inited_ = false;
214: private static boolean initError_ = false;
215: private static int errorOccours_ = 0;
216: private static Exception initException_ = null;
217: private static String hostAddress;
218: private static String format;
219: private static final String DEFAULT_FORMAT = "dd/MM/yyyy";
220: }
|