001: package org.enhydra.server;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.lang.reflect.Constructor;
006: import java.text.DateFormat;
007: import java.util.Date;
008: import java.util.HashMap;
009: import java.util.StringTokenizer;
010:
011: import javax.servlet.Servlet;
012:
013: import org.enhydra.server.util.PathUtil;
014: import org.enhydra.server.util.WebAppXML;
015: import org.enhydra.util.ConfigFileInterface;
016:
017: import com.lutris.appserver.server.Application;
018: import com.lutris.appserver.server.httpPresentation.servlet.HttpPresentationServlet;
019: import com.lutris.logging.LogChannel;
020: import com.lutris.logging.Logger;
021: import com.lutris.util.Config;
022: import com.lutris.util.ConfigException;
023: import com.lutris.util.KeywordValueException;
024:
025: /**
026: * <p>Description: This class provide application info from EnhydraServer conf file.</p>
027: * <p>Copyright: Copyright (c) 2002</p>
028: * <p>Company: </p>
029: * @author Damir Milovic, damir@uns.ns.ac.yu
030: * @version 1.0
031: */
032:
033: public class AppInfo {
034:
035: public static final int ENHYDRA_APP = 0;//Standard Enhydra application
036: public static final int WEB_APP = 1; //Standard Web application
037: public static final String TYPE_ENHYDRA_APP = "Standard Enhydra Application";
038: public static final String TYPE_WEB_APP = "Standard Web Application";
039: private String name;
040:
041: private boolean running = false;
042: private int[] connections = null;
043: // private EnhydraServerXML serverConfig = null; // EnhydraServer.xml
044: private HashMap appData = null;
045: // private Config appConfig = null; // "Application" section from serverConfig(EnhydraServer.conf)
046: private Config config = null; //Application config (appName.conf)
047: private String description = "N/A";
048: private String[] additionalClassPaths;
049: private java.util.Date started = null;
050: // private String upTime = "N/A";
051: private String contextPath = "N/A";
052: private String urlPath = "N/A";
053: private int appType = WEB_APP;
054: //Only for Enhydra apps
055: private Application application = null;
056: private String configName = "N/A";
057: private WebAppXML webAppXML = null;
058:
059: /**
060: * Initialize properties from EnhydraServer config file.
061: * This constructor is called by EnhydraServer when ApplicationServer != null
062: * @param appName application name
063: * @param serverConfig EnhydraServer config
064: */
065: public AppInfo(String appName, HashMap attributes)
066: throws KeywordValueException, ConfigException, IOException {
067:
068: name = appName;
069: appData = attributes;
070:
071: //Create WebAppXML for web.xml management
072: urlPath = (String) appData.get("URLPath");//todo: EnhydraServer.APP_URLPATH
073: if (urlPath == null)
074: throw new ConfigException(
075: "Missing param URLPath in application " + name
076: + ", EnhydraServer.xml");
077: //todo: relative urlPath make absolute
078: String appsDir = EnhydraServer.getInstance().getAppsDir();
079: urlPath = PathUtil.makeAbsolutePath(appsDir, urlPath);
080: File webXMLFile = new File(urlPath, "WEB-INF/web.xml");
081: if (webXMLFile.exists()) {
082: webAppXML = new WebAppXML(webXMLFile.getAbsolutePath());
083: } else {
084: webAppXML = null; // if web.xml is missing then it use default and we can't explore them
085: }
086: //Set application type (default is WEB_APP)
087: if (webAppXML != null) {
088: boolean isEnhydraApp = webAppXML.isEnhydraApplication();
089: if (isEnhydraApp) {
090: appType = ENHYDRA_APP;
091: } else {
092: appType = WEB_APP;
093: }
094: }
095: //set Context Path 17.03.2003
096: contextPath = (String) appData.get("contextPath"); //todo: ---||------
097: if (contextPath == null)
098: throw new ConfigException(
099: "Missing param contextPath in application " + name
100: + ", EnhydraServer.xml");
101: else if (!contextPath.startsWith("/")) {
102: throw new ConfigException(
103: "contextPath must start with '/' in application "
104: + name + ", EnhydraServer.xml");
105: }
106: //Set running
107: String appRunning = (String) appData.get("running"); //todo: ---||------
108: if (appRunning != null && appRunning.equalsIgnoreCase("true")) {
109: running = true;
110: } else {
111: running = false;
112: }
113: //Set enabled connections
114: String enabledConns = (String) appData.get("connections"); //todo: ---||------
115: if (enabledConns != null) {
116: StringTokenizer tokenizer = new StringTokenizer(
117: enabledConns, ",");
118: connections = new int[tokenizer.countTokens()];
119: for (int i = 0; i < connections.length; i++) {
120: connections[i] = Integer.parseInt(tokenizer.nextToken()
121: .trim());
122: }
123: }
124: //Set desription
125: description = (String) appData.get("description"); //todo: ---||------
126: if (description == null) {
127: description = "N/A";
128: }
129: //ENHYDRA_APP properties
130: if (appType == ENHYDRA_APP) {
131: //application Config
132: configName = webAppXML.getConfFilePath();
133: if (configName == null)
134: configName = EnhydraServer.DEFAULT_CONF_FILE;
135: String webinfDir = PathUtil.makeAbsolutePath(urlPath,
136: "WEB-INF");
137: // if confFile is relative, make it absolute against web-inf dir
138: File configFile = new File(PathUtil.makeAbsolutePath(
139: webinfDir, configName));
140:
141: ConfigFileInterface configF = createConfigFile(configFile); // TJ
142: // TJ 08.11.2003 put under comment
143: // ConfigFileInterface configF = new ConfigFile(configFile);
144:
145: config = configF.getConfig();
146: //Set additional classpaths
147: if (config.containsKey("Server.ClassPath"))
148: additionalClassPaths = config
149: .getStrings("Server.ClassPath");
150: }
151: }
152:
153: /**
154: * Initialize properties from registered HttpPresentationServelet.
155: * This constructor is called by EnhydraServer when ApplicationServer == null
156: * (i.e. Enhydra applications are executed under unknown Servlet Container).
157: * Each Enhydra application register themselves to EnhydraServer when start.
158: * @param servlet
159: */
160: public AppInfo(Servlet servlet) throws ConfigException, IOException {
161:
162: LogChannel logChannel = Logger.getCentralLogger().getChannel(
163: "Enhydra");
164:
165: // Logger logger = Logger.getLogger("sysOut");
166: urlPath = servlet.getServletConfig().getServletContext()
167: .getRealPath("");
168: //@@@@@@@@@ 09.04.2003
169: //Create WebAppXML for web.xml management
170:
171: if (urlPath == null)
172: throw new ConfigException(
173: "Automatic started, Can't get URLPath in application "
174: + name);
175:
176: File webXMLFile = new File(urlPath, "WEB-INF/web.xml");
177: if (webXMLFile.exists()) {
178: webAppXML = new WebAppXML(webXMLFile.getAbsolutePath());
179: } else {
180: webAppXML = null; // if web.xml is missing then it use default and we can't explore them
181: }
182:
183: //set Context Path 09.04.2003 presume ends of urlPath
184: String[] pathParts = urlPath.split("[\\\\/]");
185: contextPath = "/" + pathParts[pathParts.length - 1]; //Get the last string
186:
187: //application Config
188: configName = webAppXML.getConfFilePath();
189: if (configName == null)
190: configName = EnhydraServer.DEFAULT_CONF_FILE;
191: String webinfDir = PathUtil
192: .makeAbsolutePath(urlPath, "WEB-INF");
193:
194: // if confFile is relative, make it absolute against web-inf dir
195: File configFile = new File(PathUtil.makeAbsolutePath(webinfDir,
196: configName));
197: ConfigFileInterface configF = createConfigFile(configFile); // TJ
198:
199: // TJ 08.11.2003 put under comment
200: // ConfigFileInterface configF = new ConfigFile(configFile);
201:
202: config = configF.getConfig();
203: //Set additional classpaths
204: if (config.containsKey("Server.ClassPath"))
205: additionalClassPaths = config
206: .getStrings("Server.ClassPath");
207: if (servlet instanceof HttpPresentationServlet) {
208: application = ((HttpPresentationServlet) servlet)
209: .getApplication();
210: name = application.getName();
211: if (name == null) {
212: if (logChannel != null)
213: logChannel
214: .write(Logger.ERROR,
215: " Can't create AppInfo, application name == null !");
216: throw new ConfigException(
217: " Can't create AppInfo, application name == null !");
218: }
219:
220: setRunning(true);
221: setStarted(new Date());
222: setAppType(ENHYDRA_APP);
223: }
224: }
225:
226: public String getName() {
227: return name;
228: }
229:
230: public void setName(String name) {
231: this .name = name;
232: }
233:
234: public void setConfig(com.lutris.util.Config config) {
235: this .config = config;
236: }
237:
238: public com.lutris.util.Config getConfig() {
239: return config;
240: }
241:
242: public void setRunning(boolean running) {
243: this .running = running;
244: }
245:
246: public boolean isRunning() {
247: return running;
248: }
249:
250: public void setConnections(int[] connections) {
251: this .connections = connections;
252: }
253:
254: public int[] getConnections() {
255: return connections;
256: }
257:
258: //FIXME println to log file
259: private void log(String message) {
260: DateFormat d = DateFormat.getDateTimeInstance(DateFormat.SHORT,
261: DateFormat.SHORT);
262: System.err.println(d.format(new Date())
263: + " EnhydraServer, AppInfo" + message);
264: }
265:
266: public void setDescription(String description) {
267: this .description = description;
268: }
269:
270: public String getDescription() {
271: return description;
272: }
273:
274: public void setAdditionalClassPaths(String[] additionalClassPaths) {
275: this .additionalClassPaths = additionalClassPaths;
276: }
277:
278: public String[] getAdditionalClassPaths() {
279: return additionalClassPaths;
280: }
281:
282: public void setStarted(java.util.Date started) {
283: this .started = started;
284: }
285:
286: public java.util.Date getStarted() {
287: return started;
288: }
289:
290: /**
291: *
292: * @return up time in seconds.
293: */
294: public long getUpTime() {
295: if (started != null) {
296: long upTimeSec = (System.currentTimeMillis() - started
297: .getTime()) / 1000;
298: return upTimeSec;
299: } else
300: return 0;
301: }
302:
303: public void setContextPath(String contextPath) {
304: this .contextPath = contextPath;
305: }
306:
307: public String getContextPath() {
308: return contextPath;
309: }
310:
311: public void setUrlPath(String urlPath) {
312: this .urlPath = urlPath;
313: }
314:
315: public String getUrlPath() {
316: return urlPath;
317: }
318:
319: public String getType() {
320: String type = "N/A";
321: switch (getAppType()) {
322: case ENHYDRA_APP:
323: type = TYPE_ENHYDRA_APP;
324: break;
325: case WEB_APP:
326: type = TYPE_WEB_APP;
327: break;
328: }
329: return type;
330: }
331:
332: public int getAppType() {
333: return appType;
334: }
335:
336: public void setAppType(int t) {
337: appType = t;
338: }
339:
340: public Application getApplication() {
341: return application;
342: }
343:
344: public void setApplication(Application application) {
345: this .application = application;
346: }
347:
348: public void setConfigName(String configName) {
349: this .configName = configName;
350: }
351:
352: public String getConfigName() {
353: return configName;
354: }
355:
356: public WebAppXML getWebAppXML() {
357: return webAppXML;
358: }
359:
360: public void setWebAppXML(WebAppXML w) {
361: webAppXML = w;
362: }
363:
364: private ConfigFileInterface createConfigFile(File configFile)
365: throws ConfigException { // TJ 08.11.2003.
366: String configFileClass = null;
367: if (webAppXML != null)
368: configFileClass = webAppXML.getConfFileClass();
369: if (configFileClass == null)
370: configFileClass = EnhydraServer.DEFAULT_CONF_FILE_CLASS;
371:
372: Class classObj = null;
373: Class[] classParam = null;
374: try {
375: classObj = Class.forName(configFileClass, true, this
376: .getClass().getClassLoader());
377: classParam = new Class[1];
378: classParam[0] = Class.forName("java.io.File");
379: } catch (ClassNotFoundException ex) {
380: throw new ConfigException(
381: "Can't create application configuration file: ClassNotFound error");
382: }
383:
384: Constructor constr;
385: try {
386: constr = classObj.getConstructor(classParam);
387: } catch (NoSuchMethodException ex) {
388: throw new ConfigException(
389: "Error in constructor: can't create application configuration file.");
390: }
391: Object[] arguments = new Object[1];
392:
393: arguments[0] = (Object) (configFile);
394:
395: ConfigFileInterface configF = null;
396: try {
397: configF = (ConfigFileInterface) constr
398: .newInstance(arguments);
399: } catch (Exception ex) {
400: throw new ConfigException(
401: "Can't create application configuration file.");
402: }
403: return configF;
404:
405: }
406: }
|