1 4 5 9 10 package org.openlaszlo.server; 11 12 import java.lang.Package ; 13 import java.lang.Class ; 14 import java.util.Calendar ; 15 import java.util.Properties ; 16 import java.io.*; 17 import java.net.*; 18 import javax.servlet.*; 19 import javax.servlet.ServletConfig .*; 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 38 public class LPS { 39 40 private static long mBootTime = Calendar.getInstance().getTime().getTime(); 41 42 private static Properties mProperties = null; 43 44 private static String mHome = null; 45 46 public static Configuration configuration = null; 47 48 public static String VERSION_FILE = "/org/openlaszlo/server/lps.xml"; 49 50 private static String mBuildID; 51 private static String mBuildDate; 52 private static String mVersionID; 53 private static String mRelease; 54 55 public static String mSWFVersionDefault = null; 56 public static int mSWFVersionNumDefault = -1; 57 58 59 62 static { 63 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 t) { 70 throw new RuntimeException (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 83 public static void setHome(String home) { 84 mHome = home; 85 } 86 87 90 public static void initialize() { 91 configuration = new Configuration(); 92 } 93 94 97 public static String HOME() { 98 if (mHome == null || mHome.equals("")) { 99 mHome = getSystemProperty("LPS_HOME"); 100 if (mHome == null || mHome.equals("")) { 101 throw new 103 RuntimeException ("Server configuration error: can't find LPS_HOME."); 104 } 105 } 106 return mHome; 107 } 108 109 112 public static String ROOT() { 113 return HOME() + File.separator + "WEB-INF" + 114 File.separator + "lps"; 115 } 116 117 120 public static String PUBLIC_ROOT() { 121 return HOME() + File.separator + "lps"; 122 } 123 124 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 137 public static String getConfigDirectory() { 138 return ConfigDir.get(HOME()); 139 } 140 141 144 public static File getPropertiesFile() { 147 return new File( getConfigDirectory() + File.separator + 148 "lps.properties" ); 149 } 150 151 154 public static String getWorkDirectory() { 155 return ROOT() + File.separator + "work"; 156 } 157 158 161 public static String getMiscDirectory() { 162 return ROOT() + File.separator + "misc"; 163 } 164 165 168 public static String getComponentsDirectory() { 169 return PUBLIC_ROOT() + File.separator + "components"; 170 } 171 172 175 public static String getFontDirectory() { 176 return PUBLIC_ROOT() + File.separator + "fonts"; 177 } 178 179 182 public static String getLFCDirectory() { 183 return ROOT() + File.separator + "lfc"; 184 } 185 186 189 public static String getTemplateDirectory() { 190 return ROOT() + File.separator + "templates"; 191 } 192 193 197 public static String getBuild() { 198 201 210 211 return mBuildID; 212 } 213 214 218 public static String getVersion() { 219 222 231 232 return mVersionID; 233 } 234 235 238 public static String getRelease() { 239 242 251 252 return mRelease; 253 } 254 255 258 public static String getShortVersion() { 259 return "lps-" + mVersionID; 260 } 261 262 265 public static long getBootTime() { 266 return mBootTime; 267 } 268 269 272 public static String getBuildDate() { 273 return mBuildDate; 274 } 275 276 277 280 public static void setSWFVersionDefault(String 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 ("Unknown SWF version: " + swfversion); 292 } 293 } 294 295 298 public static int getSWFVersionNum(String 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 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 316 public static int getSWFVersionNum(HttpServletRequest req) { 317 return getSWFVersionNum(req.getParameter("lzr")); 318 } 319 320 323 public static String getInfo(HttpServletRequest req, 324 ServletContext ctxt, String tagName) { 325 StringBuffer buf = new StringBuffer (); 326 327 InetAddress localHost; 328 InetAddress [] myIPs; 329 330 final double MEG = 1024*1024; 331 332 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 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 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 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 385 return buf.toString(); 386 } 387 388 392 public static String getSystemProperty(String name) { 393 try { 394 return System.getProperty(name); 395 } catch (SecurityException e) { 396 return ""; 398 } 399 } 400 401 402 407 public static String getSystemPropertyOrUnknowable(String name) { 408 return getSystemProperty(name, "unknowable"); 409 } 410 411 415 public static String getSystemProperty(String name, String d) { 416 try { 417 return System.getProperty(name, d); 418 } catch (SecurityException e) { 419 return d; 421 } 422 } 423 424 private static void loadProperties() { 425 if (mProperties == null) { 426 File propFile = getPropertiesFile(); 427 Properties properties = new Properties (); 428 Properties sysProperties = (Properties )System.getProperties().clone(); 429 try { 430 properties.load(new FileInputStream(propFile)); 431 properties = LZUtils.expandProperties(properties); 432 sysProperties.putAll(properties); 433 434 } catch (Exception e) { 435 throw new ChainedException (e); 436 } 437 mProperties = sysProperties; 438 } 439 } 440 441 442 public static Properties getProperties() { 443 loadProperties(); 444 return mProperties; 445 } 446 447 private final static String KRANK_PORT_PROPERTY = "krankPortNum"; 448 private final static int DEFAULT_KRANK_PORT = 4444; 449 450 451 public static int getKrankPort () { 452 String 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 e) { 460 throw new 461 RuntimeException ("Server configuration error: can't parse lps.properties entry '"+KRANK_PORT_PROPERTY+"'"); 462 } 463 return portnum; 464 } 465 466 468 public static String getProperty(String name, String value) { 469 loadProperties(); 470 return mProperties.getProperty(name, value); 471 } 472 473 474 public static String getProperty(String name) { 475 loadProperties(); 476 return getProperty(name, null); 477 } 478 479 480 public static void setProperty(String name, String 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 |