KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > server > AppInfo


1 package org.enhydra.server;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.lang.reflect.Constructor JavaDoc;
6 import java.text.DateFormat JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.StringTokenizer JavaDoc;
10
11 import javax.servlet.Servlet JavaDoc;
12
13 import org.enhydra.server.util.PathUtil;
14 import org.enhydra.server.util.WebAppXML;
15 import org.enhydra.util.ConfigFileInterface;
16
17 import com.lutris.appserver.server.Application;
18 import com.lutris.appserver.server.httpPresentation.servlet.HttpPresentationServlet;
19 import com.lutris.logging.LogChannel;
20 import com.lutris.logging.Logger;
21 import com.lutris.util.Config;
22 import com.lutris.util.ConfigException;
23 import com.lutris.util.KeywordValueException;
24 /**
25  * <p>Description: This class provide application info from EnhydraServer conf file.</p>
26  * <p>Copyright: Copyright (c) 2002</p>
27  * <p>Company: </p>
28  * @author Damir Milovic, damir@uns.ns.ac.yu
29  * @version 1.0
30  */

31
32 public class AppInfo {
33
34     public static final int ENHYDRA_APP = 0;//Standard Enhydra application
35
public static final int WEB_APP = 1; //Standard Web application
36
public static final String JavaDoc TYPE_ENHYDRA_APP = "Standard Enhydra Application";
37     public static final String JavaDoc TYPE_WEB_APP = "Standard Web Application";
38     private String JavaDoc name;
39
40     private boolean running = false;
41     private int[] connections = null;
42     // private EnhydraServerXML serverConfig = null; // EnhydraServer.xml
43
private HashMap JavaDoc appData = null;
44     // private Config appConfig = null; // "Application" section from serverConfig(EnhydraServer.conf)
45
private Config config = null; //Application config (appName.conf)
46
private String JavaDoc description = "N/A";
47     private String JavaDoc[] additionalClassPaths;
48     private java.util.Date JavaDoc started = null;
49     private String JavaDoc upTime = "N/A";
50     private String JavaDoc contextPath = "N/A";
51     private String JavaDoc urlPath = "N/A";
52     private int appType = WEB_APP;
53     //Only for Enhydra apps
54
private Application application = null;
55     private String JavaDoc configName = "N/A";
56     private WebAppXML webAppXML = null;
57
58     /**
59      * Initialize properties from EnhydraServer config file.
60      * This constructor is called by EnhydraServer when ApplicationServer != null
61      * @param appName application name
62      * @param serverConfig EnhydraServer config
63      */

64     public AppInfo(String JavaDoc appName,HashMap JavaDoc attributes)
65             throws KeywordValueException,ConfigException,IOException JavaDoc {
66
67         name = appName;
68         appData = attributes;
69
70         //Create WebAppXML for web.xml management
71
urlPath = (String JavaDoc)appData.get("URLPath");//todo: EnhydraServer.APP_URLPATH
72
if (urlPath == null)
73             throw new ConfigException("Missing param URLPath in application "+name+", EnhydraServer.xml");
74         //todo: relative urlPath make absolute
75
String JavaDoc appsDir = EnhydraServer.getInstance().getAppsDir();
76         urlPath = PathUtil.makeAbsolutePath(appsDir, urlPath);
77         File JavaDoc webXMLFile = new File JavaDoc(urlPath,"WEB-INF/web.xml");
78         if(webXMLFile.exists()){
79             webAppXML = new WebAppXML(webXMLFile.getAbsolutePath());
80         }else{
81             webAppXML = null; // if web.xml is missing then it use default and we can't explore them
82
}
83         //Set application type (default is WEB_APP)
84
if(webAppXML != null){
85             boolean isEnhydraApp = webAppXML.isEnhydraApplication();
86             if(isEnhydraApp){
87                 appType = ENHYDRA_APP;
88             }else{
89                 appType = WEB_APP;
90             }
91         }
92         //set Context Path 17.03.2003
93
contextPath = (String JavaDoc)appData.get("contextPath"); //todo: ---||------
94
if (contextPath == null)
95             throw new ConfigException("Missing param contextPath in application "+name+", EnhydraServer.xml");
96         else if(!contextPath.startsWith("/")){
97             throw new ConfigException("contextPath must start with '/' in application "+name+", EnhydraServer.xml");
98         }
99         //Set running
100
String JavaDoc appRunning = (String JavaDoc)appData.get("running"); //todo: ---||------
101
if(appRunning != null && appRunning.equalsIgnoreCase("true")){
102             running = true;
103         }else{
104             running = false;
105         }
106         //Set enabled connections
107
String JavaDoc enabledConns = (String JavaDoc)appData.get("connections"); //todo: ---||------
108
if(enabledConns != null){
109             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(enabledConns,",");
110             connections = new int[tokenizer.countTokens()];
111             for(int i=0;i<connections.length;i++){
112                 connections[i] = Integer.parseInt(tokenizer.nextToken().trim());
113             }
114         }
115         //Set desription
116
description = (String JavaDoc)appData.get("description"); //todo: ---||------
117
if(description == null){
118             description = "N/A";
119         }
120         //ENHYDRA_APP properties
121
if(appType == ENHYDRA_APP){
122             //application Config
123
configName = webAppXML.getConfFilePath();
124             String JavaDoc webinfDir = PathUtil.makeAbsolutePath(urlPath,"WEB-INF");
125             // if confFile is relative, make it absolute against web-inf dir
126
File JavaDoc configFile = new File JavaDoc(PathUtil.makeAbsolutePath(webinfDir,configName));
127
128             ConfigFileInterface configF = createConfigFile(configFile); // TJ
129
// TJ 08.11.2003 put under comment
130
// ConfigFileInterface configF = new ConfigFile(configFile);
131

132             config = configF.getConfig();
133             //Set additional classpaths
134
if (config.containsKey("Server.ClassPath"))
135                 additionalClassPaths = config.getStrings("Server.ClassPath");
136         }
137     }
138     /**
139      * Initialize properties from registered HttpPresentationServelet.
140      * This constructor is called by EnhydraServer when ApplicationServer == null
141      * (i.e. Enhydra applications are executed under unknown Servlet Container).
142      * Each Enhydra application register themselves to EnhydraServer when start.
143      * @param servlet
144      */

145     public AppInfo(Servlet JavaDoc servlet) throws ConfigException, IOException JavaDoc{
146
147     LogChannel logChannel = Logger.getCentralLogger().getChannel("Enhydra");
148
149
150 // Logger logger = Logger.getLogger("sysOut");
151
urlPath = servlet.getServletConfig().getServletContext().getRealPath("");
152         //@@@@@@@@@ 09.04.2003
153
//Create WebAppXML for web.xml management
154

155         if (urlPath == null)
156             throw new ConfigException("Automatic started, Can't get URLPath in application "+name);
157         
158         File JavaDoc webXMLFile = new File JavaDoc(urlPath,"WEB-INF/web.xml");
159         if(webXMLFile.exists()){
160             webAppXML = new WebAppXML(webXMLFile.getAbsolutePath());
161         }else{
162             webAppXML = null; // if web.xml is missing then it use default and we can't explore them
163
}
164         
165         //set Context Path 09.04.2003 presume ends of urlPath
166
String JavaDoc[] pathParts = urlPath.split("[\\\\/]");
167         contextPath = "/"+pathParts[pathParts.length-1]; //Get the last string
168

169         //application Config
170
configName = webAppXML.getConfFilePath();
171         String JavaDoc webinfDir = PathUtil.makeAbsolutePath(urlPath,"WEB-INF");
172         
173         // if confFile is relative, make it absolute against web-inf dir
174
File JavaDoc configFile = new File JavaDoc(PathUtil.makeAbsolutePath(webinfDir,configName));
175         ConfigFileInterface configF = createConfigFile(configFile); // TJ
176

177 // TJ 08.11.2003 put under comment
178
// ConfigFileInterface configF = new ConfigFile(configFile);
179

180         config = configF.getConfig();
181         //Set additional classpaths
182
if (config.containsKey("Server.ClassPath"))
183             additionalClassPaths = config.getStrings("Server.ClassPath");
184         if(servlet instanceof HttpPresentationServlet){
185             application = ((HttpPresentationServlet) servlet).getApplication();
186             name = application.getName();
187             if(name == null){
188                if(logChannel!=null)
189                 logChannel.write(Logger.ERROR," Can't create AppInfo, application name == null !");
190                 throw new ConfigException(" Can't create AppInfo, application name == null !");
191             }
192
193             setRunning(true);
194             setStarted(new Date JavaDoc());
195             setAppType(ENHYDRA_APP);
196         }
197     }
198     public String JavaDoc getName() {
199         return name;
200     }
201     public void setName(String JavaDoc name) {
202         this.name = name;
203     }
204     public void setConfig(com.lutris.util.Config config) {
205         this.config = config;
206     }
207     public com.lutris.util.Config getConfig() {
208         return config;
209     }
210     public void setRunning(boolean running) {
211         this.running = running;
212     }
213     public boolean isRunning() {
214         return running;
215     }
216     public void setConnections(int[] connections) {
217         this.connections = connections;
218     }
219     public int[] getConnections() {
220         return connections;
221     }
222     //FIXME println to log file
223
private void log(String JavaDoc message){
224         DateFormat JavaDoc d = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
225         System.err.println(d.format(new Date JavaDoc())+" EnhydraServer, AppInfo"+message);
226     }
227
228     public void setDescription(String JavaDoc description) {
229         this.description = description;
230     }
231     public String JavaDoc getDescription() {
232         return description;
233     }
234     public void setAdditionalClassPaths(String JavaDoc[] additionalClassPaths) {
235         this.additionalClassPaths = additionalClassPaths;
236     }
237     public String JavaDoc[] getAdditionalClassPaths() {
238         return additionalClassPaths;
239     }
240     public void setStarted(java.util.Date JavaDoc started) {
241         this.started = started;
242     }
243     public java.util.Date JavaDoc getStarted() {
244         return started;
245     }
246     /**
247      *
248      * @return up time in seconds.
249      */

250     public long getUpTime() {
251         if(started != null) {
252             long upTimeSec = (System.currentTimeMillis()-started.getTime())/1000;
253             return upTimeSec;
254         }
255         else
256             return 0;
257     }
258     public void setContextPath(String JavaDoc contextPath) {
259         this.contextPath = contextPath;
260     }
261     public String JavaDoc getContextPath() {
262         return contextPath;
263     }
264     public void setUrlPath(String JavaDoc urlPath) {
265         this.urlPath = urlPath;
266     }
267     public String JavaDoc getUrlPath() {
268         return urlPath;
269     }
270     public String JavaDoc getType() {
271         String JavaDoc type = "N/A";
272         switch(getAppType()){
273             case ENHYDRA_APP:
274                 type = TYPE_ENHYDRA_APP;
275                 break;
276             case WEB_APP:
277                 type = TYPE_WEB_APP;
278                 break;
279         }
280         return type;
281     }
282     public int getAppType(){
283         return appType;
284     }
285     public void setAppType(int t){
286         appType = t;
287     }
288     public Application getApplication() {
289         return application;
290     }
291     public void setApplication(Application application) {
292         this.application = application;
293     }
294     public void setConfigName(String JavaDoc configName) {
295         this.configName = configName;
296     }
297     public String JavaDoc getConfigName() {
298         return configName;
299     }
300     public WebAppXML getWebAppXML(){
301         return webAppXML;
302     }
303     public void setWebAppXML(WebAppXML w){
304         webAppXML = w;
305     }
306
307     private ConfigFileInterface createConfigFile (File JavaDoc configFile)
308       throws ConfigException { // TJ 08.11.2003.
309
String JavaDoc configFileClass = null;
310       if (webAppXML != null)
311         configFileClass = webAppXML.getConfFileClass();
312       if (configFileClass == null)
313           configFileClass = EnhydraServer.DEFAULT_CONF_FILE_CLASS;
314
315       Class JavaDoc classObj = null;
316       Class JavaDoc[] classParam = null;
317       try {
318         classObj = Class.forName(configFileClass, true, this.getClass().getClassLoader());
319         classParam = new Class JavaDoc[1];
320         classParam[0] = Class.forName("java.io.File");
321       }
322       catch (ClassNotFoundException JavaDoc ex){
323          throw new ConfigException("Can't create application configuration file: ClassNotFound error");
324       }
325
326       Constructor JavaDoc constr;
327       try {
328         constr = classObj.getConstructor(classParam);
329       }
330       catch (NoSuchMethodException JavaDoc ex){
331         throw new ConfigException("Error in constructor: can't create application configuration file.");
332       }
333       Object JavaDoc[] arguments = new Object JavaDoc[1];
334
335       arguments[0] = (Object JavaDoc)(configFile);
336
337       ConfigFileInterface configF = null;
338       try {
339         configF = (ConfigFileInterface)constr.newInstance(arguments);
340       }
341       catch (Exception JavaDoc ex){
342         throw new ConfigException("Can't create application configuration file.");
343       }
344       return configF;
345
346     }
347 }
Popular Tags