KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > server > LPS


1 /******************************************************************************
2  * LPS.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.server;
11
12 import java.lang.Package JavaDoc;
13 import java.lang.Class JavaDoc;
14 import java.util.Calendar JavaDoc;
15 import java.util.Properties JavaDoc;
16 import java.io.*;
17 import java.net.*;
18 import javax.servlet.*;
19 import javax.servlet.ServletConfig JavaDoc.*;
20 import javax.servlet.http.*;
21 import org.openlaszlo.utils.ChainedException;
22 import org.openlaszlo.utils.MathUtils;
23 import org.openlaszlo.utils.LZUtils;
24
25 import org.apache.log4j.*;
26
27 import org.jdom.*;
28 import org.jdom.input.SAXBuilder;
29 import org.jdom.filter.ElementFilter;
30 import org.jdom.input.*;
31
32 /**
33  * LPS is a singleton for global server state.
34  *
35  * @author Eric Bloch
36  * @version 1.0
37  */

38 public class LPS {
39
40     private static long mBootTime = Calendar.getInstance().getTime().getTime();
41
42     private static Properties JavaDoc mProperties = null;
43
44     private static String JavaDoc mHome = null;
45
46     public static Configuration configuration = null;
47     
48     public static String JavaDoc VERSION_FILE = "/org/openlaszlo/server/lps.xml";
49
50     private static String JavaDoc mBuildID;
51     private static String JavaDoc mBuildDate;
52     private static String JavaDoc mVersionID;
53     private static String JavaDoc mRelease;
54
55     public static String JavaDoc mSWFVersionDefault = null;
56     public static int mSWFVersionNumDefault = -1;
57
58
59     /**
60      * Initialize version info
61      */

62     static {
63         // Read in version details from XML file
64
SAXBuilder builder = new SAXBuilder();
65         Document doc;
66         try {
67             InputStream in = LPS.class.getResourceAsStream(VERSION_FILE);
68             doc = builder.build(in);
69         } catch (Throwable JavaDoc t) {
70             throw new RuntimeException JavaDoc(t);
71         }
72         Element root = doc.getRootElement();
73         mBuildID = root.getChildTextNormalize("build-id");
74         mBuildDate = root.getChildTextNormalize("build-date");
75         mVersionID = root.getChildTextNormalize("version-id");
76         mRelease = root.getChildTextNormalize("release");
77     }
78
79
80     /*
81      * Set the home directory
82      */

83     public static void setHome(String JavaDoc home) {
84         mHome = home;
85     }
86
87     /*
88      * Read the xml configuration file.
89      */

90     public static void initialize() {
91         configuration = new Configuration();
92     }
93
94     /**
95      * @return the LPS_HOME property
96      */

97     public static String JavaDoc HOME() {
98         if (mHome == null || mHome.equals("")) {
99             mHome = getSystemProperty("LPS_HOME");
100             if (mHome == null || mHome.equals("")) {
101                 // This is catastrophic
102
throw new
103                     RuntimeException JavaDoc("Server configuration error: can't find LPS_HOME.");
104             }
105         }
106         return mHome;
107     }
108
109     /**
110      * @return the "root directory" of the LPS bits. Typically, HOME/WEB-INF/lps.
111      */

112     public static String JavaDoc ROOT() {
113         return HOME() + File.separator + "WEB-INF" +
114                         File.separator + "lps";
115     }
116
117     /**
118      * @return the "public root directory" of the LPS bits. Typically, HOME/lps.
119      */

120     public static String JavaDoc PUBLIC_ROOT() {
121         return HOME() + File.separator + "lps";
122     }
123
124     /**
125      * @return the location of the lps.jar when
126      * running inside a servlet container
127      */

128     public static File getLPSJarFile() {
129         return new File(HOME() + File.separator + "WEB-INF" +
130                                  File.separator + "lib" +
131                                  File.separator + "lps.jar");
132     }
133
134     /**
135      * @return the location of the config directory.
136      */

137     public static String JavaDoc getConfigDirectory() {
138         return ConfigDir.get(HOME());
139     }
140
141     /**
142      * @return the properties file
143      */

144     // TODO:[2002-12-2 bloch] add parameter/property for
145
// location of properties file.
146
public static File getPropertiesFile() {
147         return new File( getConfigDirectory() + File.separator +
148                          "lps.properties" );
149     }
150
151     /**
152      * @return the location of the default cache directory.
153      */

154     public static String JavaDoc getWorkDirectory() {
155         return ROOT() + File.separator + "work";
156     }
157
158     /**
159      * @return the location of the misc directory.
160      */

161     public static String JavaDoc getMiscDirectory() {
162         return ROOT() + File.separator + "misc";
163     }
164
165     /**
166      * @return the location of the components directory.
167      */

168     public static String JavaDoc getComponentsDirectory() {
169         return PUBLIC_ROOT() + File.separator + "components";
170     }
171
172     /**
173      * @return the location of the fonts directory.
174      */

175     public static String JavaDoc getFontDirectory() {
176         return PUBLIC_ROOT() + File.separator + "fonts";
177     }
178
179     /*
180      * @return the location of the lfc directory
181      */

182     public static String JavaDoc getLFCDirectory() {
183         return ROOT() + File.separator + "lfc";
184     }
185
186     /*
187      * @return the location of the server template directory
188      */

189     public static String JavaDoc getTemplateDirectory() {
190         return ROOT() + File.separator + "templates";
191     }
192
193     /**
194      * @return a string representation of the version
195      * of this build.
196      */

197     public static String JavaDoc getBuild() {
198         // The version number is actually baked into the manifest for the
199
// of the package.
200

201         /* This doesn't work under Tomcat3.3 (see bug 4948, erroneous closed)
202         try {
203             Package p = Package.getPackage("org.openlaszlo.server");
204             return p.getImplementationVersion();
205         } catch (Exception e) {
206             // THIS IS FATAL!
207             throw new RuntimeException(e);
208         }
209         */

210
211         return mBuildID;
212     }
213
214     /**
215      * @return a string representation of the version
216      * of this build.
217      */

218     public static String JavaDoc getVersion() {
219         // The version number is actually baked into the manifest for the
220
// of the package.
221

222         /* This doesn't work under Tomcat3.3 (see bug 4948, erroneous closed)
223         try {
224             Package p = Package.getPackage("org.openlaszlo.server");
225             return p.getSpecificationVersion();
226         } catch (Exception e) {
227             // THIS IS FATAL!
228             throw new RuntimeException(e);
229         }
230         */

231
232         return mVersionID;
233     }
234
235     /**
236      * @return a string representation of the release
237      */

238     public static String JavaDoc getRelease() {
239         // The version number is actually baked into the manifest for the
240
// of the package.
241

242         /* This doesn't work under Tomcat3.3 (see bug 4948, erroneous closed)
243         try {
244             Package p = Package.getPackage("org.openlaszlo.server");
245             return p.getSpecificationVersion();
246         } catch (Exception e) {
247             // THIS IS FATAL!
248             throw new RuntimeException(e);
249         }
250         */

251
252         return mRelease;
253     }
254
255     /*
256      * @return the string version of the LPS in lowercase. E.g. lps-dr, lps-v1.
257      */

258     public static String JavaDoc getShortVersion() {
259         return "lps-" + mVersionID;
260     }
261
262     /**
263      * @return the boot time
264      */

265     public static long getBootTime() {
266         return mBootTime;
267     }
268
269     /**
270      *
271      */

272     public static String JavaDoc getBuildDate() {
273         return mBuildDate;
274     }
275
276
277     /**
278      * Set SWF version default.
279      */

280     public static void setSWFVersionDefault(String JavaDoc swfversion) {
281         if (swfversion.equals("swf7")) {
282             mSWFVersionNumDefault = 7;
283             mSWFVersionDefault = "swf7";
284         } else if (swfversion.equals("swf6")) {
285             mSWFVersionNumDefault = 6;
286             mSWFVersionDefault = "swf6";
287         } else if (swfversion.equals("swf5")) {
288             mSWFVersionNumDefault = 5;
289             mSWFVersionDefault = "swf5";
290         } else {
291             throw new RuntimeException JavaDoc("Unknown SWF version: " + swfversion);
292         }
293     }
294
295     /**
296      * @return swf version number
297      */

298     public static int getSWFVersionNum(String JavaDoc swfversion) {
299         if (swfversion == null) return mSWFVersionNumDefault;
300         if (swfversion.equals("swf7")) return 7;
301         if (swfversion.equals("swf6")) return 6;
302         if (swfversion.equals("swf5")) return 5;
303         return mSWFVersionNumDefault;
304     }
305
306     public static String JavaDoc getSWFVersion(int num) {
307         if (num == 7) return "swf7";
308         if (num == 6) return "swf6";
309         if (num == 5) return "swf5";
310         return mSWFVersionDefault;
311     }
312
313     /**
314      * @return swf version number
315      */

316     public static int getSWFVersionNum(HttpServletRequest req) {
317         return getSWFVersionNum(req.getParameter("lzr"));
318     }
319
320     /**
321      * @return an XML string of info
322      */

323     public static String JavaDoc getInfo(HttpServletRequest req,
324         ServletContext ctxt, String JavaDoc tagName) {
325         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
326
327         InetAddress localHost;
328         InetAddress [] myIPs;
329
330         final double MEG = 1024*1024;
331
332         // Store my ips
333
try {
334             localHost = InetAddress.getLocalHost();
335         } catch (UnknownHostException e) {
336             throw new ChainedException("LPS can't determine localhost ip address");
337         }
338         try {
339             myIPs = InetAddress.getAllByName("localhost");
340         } catch (UnknownHostException e) {
341             throw new ChainedException("Can not determine server IP address!");
342         }
343
344
345         buf.append("<").append(tagName).append(" \n" );
346         buf.append("\t server-port=\"" + req.getServerPort() + "\"\n");
347         buf.append("\t servlet-container=\"" + ctxt.getServerInfo() + "\"\n");
348         buf.append("\t servlet-container-version=\"" + ctxt.getMajorVersion() +
349             "." + ctxt.getMinorVersion() + "\"\n");
350         // buf.append("\t context=\"" + ctxt + "\"\n");
351
buf.append("\t jre-version=\"" + getSystemPropertyOrUnknowable("java.version") + "\"\n");
352         buf.append("\t os-name=\"" + getSystemPropertyOrUnknowable("os.name") + "\"\n");
353         buf.append("\t os-version=\"" + getSystemPropertyOrUnknowable("os.version") + "\"\n");
354         String JavaDoc level = "org.openlaszlo logger not configured!";
355         Logger l = Logger.getLogger("org.openlaszlo");
356         try {
357             if (l != null) {
358                 level = Logger.getLogger("org.openlaszlo").getLevel().toString();
359             }
360         } catch (Throwable JavaDoc t) {
361             level = "unknown";
362         }
363         buf.append("\t log4j-level=\"" + level + "\"\n");
364         buf.append("\t user=\"" + getSystemPropertyOrUnknowable("user.name") + "\"\n");
365         buf.append("\t version=\"" + getVersion() + "\"\n");
366         buf.append("\t release=\"" + getRelease() + "\"\n");
367         buf.append("\t build=\"" + getBuild() + "\"\n");
368         buf.append("\t built-on=\"" + mBuildDate + "\"\n");
369         buf.append("\t max-mem=\"" +
370             MathUtils.formatDouble(Runtime.getRuntime().maxMemory()/(MEG), 2) + "MB\"\n");
371         buf.append("\t total-mem=\"" +
372             MathUtils.formatDouble(Runtime.getRuntime().totalMemory()/(MEG), 2) + "MB\"\n");
373         buf.append("\t free-mem=\"" +
374             MathUtils.formatDouble(Runtime.getRuntime().freeMemory()/(MEG), 2) + "MB\"\n");
375         buf.append("\t lps-home=\"" + mHome + "\"\n" );
376         buf.append("\t localhost=\"" + localHost.getHostAddress() + "\"\n" );
377         for(int i = 0; i < myIPs.length; i++) {
378             buf.append("\t ipaddress-" + (i+1) + "=\"" + myIPs[i].getHostAddress() + "\"\n");
379         }
380         buf.append("\t client=\"" + req.getRemoteHost() + "\"\n");
381         buf.append("/>");
382
383         // TODO: [2003-02-28 bloch] add lps.properties fia properties->xml thingee
384

385         return buf.toString();
386     }
387
388     /**
389      * Safe version of System.getProperty() that won't
390      * throw a SecurityException if the property can't be read
391      */

392     public static String JavaDoc getSystemProperty(String JavaDoc name) {
393         try {
394             return System.getProperty(name);
395         } catch (SecurityException JavaDoc e) {
396             // TODO [2004-07-06 bloch]: log the failure somehow
397
return "";
398         }
399     }
400
401
402     /**
403      * Safe version of System.getProperty() that won't
404      * throw a SecurityException if the property can't be read.
405      * Return 'unknowable' if security won't let us know.
406      */

407     public static String JavaDoc getSystemPropertyOrUnknowable(String JavaDoc name) {
408         return getSystemProperty(name, "unknowable");
409     }
410
411     /**
412      * Safe version of System.getProperty() that won't
413      * throw a SecurityException if the property can't be read
414      */

415     public static String JavaDoc getSystemProperty(String JavaDoc name, String JavaDoc d) {
416         try {
417             return System.getProperty(name, d);
418         } catch (SecurityException JavaDoc e) {
419             // TODO [2004-07-06 bloch]: log the failure somehow
420
return d;
421         }
422     }
423
424     private static void loadProperties() {
425         if (mProperties == null) {
426             File propFile = getPropertiesFile();
427             Properties JavaDoc properties = new Properties JavaDoc();
428             Properties JavaDoc sysProperties = (Properties JavaDoc)System.getProperties().clone();
429             try {
430                 properties.load(new FileInputStream(propFile));
431                 properties = LZUtils.expandProperties(properties);
432                 sysProperties.putAll(properties);
433            
434             } catch (Exception JavaDoc e) {
435                 throw new ChainedException (e);
436             }
437             mProperties = sysProperties;
438         }
439     }
440
441     /** @return the LPS properties */
442     public static Properties JavaDoc getProperties() {
443         loadProperties();
444         return mProperties;
445     }
446
447     private final static String JavaDoc KRANK_PORT_PROPERTY = "krankPortNum";
448     private final static int DEFAULT_KRANK_PORT = 4444;
449
450     /*** @return Krank listener port */
451     public static int getKrankPort () {
452         String JavaDoc portStr = LPS.getProperties().getProperty(KRANK_PORT_PROPERTY);
453         int portnum = DEFAULT_KRANK_PORT;
454         if (portStr == null) {
455             return portnum;
456         }
457         try {
458             portnum = Integer.parseInt(portStr);
459         } catch (NumberFormatException JavaDoc e) {
460             throw new
461                 RuntimeException JavaDoc("Server configuration error: can't parse lps.properties entry '"+KRANK_PORT_PROPERTY+"'");
462         }
463         return portnum;
464     }
465
466     /** @return a property from the LPS property file, defaulting
467      * to value. */

468     public static String JavaDoc getProperty(String JavaDoc name, String JavaDoc value) {
469         loadProperties();
470         return mProperties.getProperty(name, value);
471     }
472
473     /** @return a property from the LPS property file. */
474     public static String JavaDoc getProperty(String JavaDoc name) {
475         loadProperties();
476         return getProperty(name, null);
477     }
478     
479     /** Override a property in the LPS property file. */
480     public static void setProperty(String JavaDoc name, String JavaDoc value) {
481         loadProperties();
482         mProperties.setProperty(name, value);
483     }
484
485     public static boolean isInternalBuild() {
486         return LPS.getBuild().equals("INTERNAL");
487     }
488 }
489
Popular Tags