1 29 30 package com.caucho.server.util; 31 32 import com.caucho.java.WorkDir; 33 import com.caucho.loader.EnvironmentLocal; 34 import com.caucho.util.Alarm; 35 import com.caucho.util.CharBuffer; 36 import com.caucho.util.CpuUsage; 37 import com.caucho.util.Crc64; 38 import com.caucho.vfs.CaseInsensitive; 39 import com.caucho.vfs.Path; 40 import com.caucho.vfs.Vfs; 41 42 import java.io.File ; 43 import java.io.IOException ; 44 import java.net.InetAddress ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 import java.util.regex.Pattern ; 48 49 53 public class CauchoSystem { 54 private static Logger log 55 = Logger.getLogger("com.caucho.util.CauchoSystem"); 56 57 static EnvironmentLocal<String > _serverIdLocal 58 = new EnvironmentLocal<String >("caucho.server-id"); 59 60 static char _separatorChar = File.separatorChar; 61 static char _pathSeparatorChar = File.pathSeparatorChar; 62 static String _localHost; 63 static String _userDir; 64 static String _userName; 65 static Path _resinHome; 66 static Path _rootDirectory; 67 static boolean _isTesting; 68 static boolean _isTestWindows; 69 70 static boolean _hasJni; 71 72 private static int _isUnix = -1; 73 private static String _newline; 74 private static long _version; 75 76 private static JniCauchoSystem _jniCauchoSystem; 77 private static boolean _isDetailedStatistics; 78 private static String _user; 79 private static String _group; 80 private static String _classPath; 81 82 static CpuUsage _cpuUsage; 83 84 private CauchoSystem() 85 { 86 } 87 88 91 public static boolean isTesting() 92 { 93 return _isTesting; 94 } 95 96 public static void setIsTesting(boolean testing) 97 { 98 _isTesting = testing; 99 } 100 101 104 public static void setResinHome(Path path) 105 { 106 _resinHome = path; 107 } 108 109 112 public static Path getResinHome() 113 { 114 if (_resinHome != null) 115 return _resinHome; 116 117 String path = System.getProperty("resin.home"); 118 if (path != null) { 119 _resinHome = Vfs.lookupNative(path); 120 return _resinHome; 121 } 122 123 String classpath = System.getProperty("java.class.path"); 124 int head = 0; 125 char sep = getFileSeparatorChar(); 126 char pathSep = sep == '/' ? ':' : ';'; 127 while (path == null) { 128 int p = classpath.indexOf(pathSep, head); 129 String subpath; 130 if (p < 0) 131 subpath = classpath.substring(head); 132 else 133 subpath = classpath.substring(head, p); 134 135 if (subpath.endsWith(sep + "lib" + sep + "resin.jar") || 136 subpath.equals("lib" + sep + "resin.jar")) { 137 path = subpath.substring(0, subpath.length() - 138 ("lib" + sep + "resin.jar").length()); 139 } 140 141 else if (subpath.endsWith(sep + "classes") || 142 subpath.equals("classes")) { 143 Path resinPath = Vfs.lookupNative(subpath); 144 resinPath = resinPath.lookup("com/caucho/util/CauchoSystem.class"); 145 if (resinPath.exists()) { 146 path = subpath.substring(0, subpath.length() - "classes".length()); 147 } 148 } 149 150 if (p < 0) 151 break; 152 else 153 head = p + 1; 154 } 155 156 if (path != null) 157 _resinHome = Vfs.lookupNative(path); 158 if (_resinHome != null && _resinHome.isDirectory()) 159 return _resinHome; 160 161 169 170 return Vfs.lookup(); 171 } 172 173 176 202 203 public static long getVersionId() 204 { 205 if (_version == 0) { 206 _version = Crc64.generate(com.caucho.Version.FULL_VERSION); 207 } 208 209 return _version; 210 } 211 212 public static String getResinConfig() 213 { 214 return getResinHome() + "/conf/resin.conf"; 215 } 216 217 224 public static Path getWorkPath() 225 { 226 Path workPath = WorkDir.getLocalWorkDir(); 227 228 if (workPath != null) 229 return workPath; 230 231 String workDir; 232 233 if (CauchoSystem.isWindows()) 235 workDir = "file:/c:/tmp/caucho"; 236 else 237 workDir = "file:/tmp/caucho"; 238 239 Path path; 240 if (workDir.charAt(0) == '/') 241 path = Vfs.lookupNative(workDir); 242 else 243 path = CauchoSystem.getResinHome().lookupNative(workDir); 244 try { 245 path.mkdirs(); 246 } catch (IOException e) { 247 } 248 249 return path; 250 } 251 252 public static String getServerId() 253 { 254 return _serverIdLocal.get(); 255 } 256 257 public static String getUserDir() 258 { 259 if (_userDir == null) 260 _userDir = System.getProperty("user.dir"); 261 262 return _userDir; 263 } 264 265 public static char getFileSeparatorChar() 266 { 267 return _separatorChar; 268 } 269 270 public static char getPathSeparatorChar() 271 { 272 return _pathSeparatorChar; 273 } 274 275 public static String getNewlineString() 276 { 277 if (_newline == null) { 278 Thread thread = Thread.currentThread(); 279 ClassLoader oldLoader = thread.getContextClassLoader(); 280 281 try { 282 thread.setContextClassLoader(ClassLoader.getSystemClassLoader()); 283 284 _newline = System.getProperty("line.separator"); 285 if (_newline != null) { 286 } 287 else if (isWindows()) 288 _newline = "\r\n"; 289 else 290 _newline = "\n"; 291 } finally { 292 thread.setContextClassLoader(oldLoader); 293 } 294 } 295 296 return _newline; 297 } 298 299 public static boolean isWindows() 300 { 301 return _separatorChar == '\\' || _isTestWindows; 302 } 303 304 public static boolean isTest() 305 { 306 return Alarm.isTest(); 307 } 308 309 public static boolean isCaseInsensitive() 310 { 311 return CaseInsensitive.isCaseInsensitive(); 312 } 313 314 public static boolean isUnix() 315 { 316 if (_isUnix >= 0) 317 return _isUnix == 1; 318 319 _isUnix = 0; 320 321 if (_separatorChar == '/' && Vfs.lookup("/bin/sh").canRead()) 322 _isUnix = 1; 323 324 return _isUnix == 1; 325 } 326 327 public static void setWindowsTest(boolean windows) 328 { 329 _isTesting = true; 330 _isTestWindows = windows; 331 } 332 333 public static String getLocalHost() 334 { 335 if (_localHost != null) 336 return _localHost; 337 338 try { 339 InetAddress addr = InetAddress.getLocalHost(); 340 _localHost = addr.getHostName(); 341 } catch (Exception e) { 342 _localHost = "127.0.0.1"; 343 } 344 345 return _localHost; 346 } 347 348 public static boolean isJdk15() 349 { 350 try { 351 return Class.forName("java.lang.annotation.Annotation") != null; 352 } catch (Throwable e) { 353 return false; 354 } 355 } 356 357 public static String getUserName() 358 { 359 if (_userName == null) 360 _userName = System.getProperty("user.name"); 361 362 return _userName; 363 } 364 365 370 public static void setDetailedStatistics(boolean isVerboseStatistics) 371 { 372 _isDetailedStatistics = isVerboseStatistics; 373 } 374 375 379 public static boolean isDetailedStatistics() 380 { 381 return _isDetailedStatistics; 382 } 383 384 public static CpuUsage getCpuUsage() 385 { 386 return CpuUsage.create(); 387 } 388 389 396 public static Class loadClass(String name) 397 throws ClassNotFoundException 398 { 399 return loadClass(name, false, null); 400 } 401 402 412 public static Class loadClass(String name, boolean init, ClassLoader loader) 413 throws ClassNotFoundException 414 { 415 if (loader == null) 416 loader = Thread.currentThread().getContextClassLoader(); 417 418 if (loader == null || loader.equals(CauchoSystem.class.getClassLoader())) 419 return Class.forName(name); 420 else 421 return Class.forName(name, init, loader); 422 } 423 424 427 public static String getClassPath() 428 { 429 if (_classPath != null) 430 return _classPath; 431 432 String cp = System.getProperty("java.class.path"); 433 434 String boot = System.getProperty("sun.boot.class.path"); 435 if (boot != null && ! boot.equals("")) 436 cp = cp + File.pathSeparatorChar + boot; 437 438 Pattern pattern = Pattern.compile("" + File.pathSeparatorChar); 439 440 String []path = pattern.split(cp); 441 442 CharBuffer cb = new CharBuffer(); 443 444 for (int i = 0; i < path.length; i++) { 445 Path subpath = Vfs.lookup(path[i]); 446 447 if (subpath.canRead() || subpath.isDirectory()) { 448 if (cb.length() > 0) 449 cb.append(File.pathSeparatorChar); 450 451 cb.append(path[i]); 452 } 453 } 454 455 _classPath = cb.toString(); 456 457 return _classPath; 458 } 459 460 public static double getLoadAvg() 461 { 462 if (_jniCauchoSystem == null) 463 _jniCauchoSystem = JniCauchoSystem.create(); 464 465 return _jniCauchoSystem.getLoadAvg(); 466 } 467 468 471 public static int setUser(String user, String group) 472 throws Exception 473 { 474 _user = user; 475 _group = group; 476 477 if (_hasJni && user != null) 478 return setUserNative(_user, _group); 479 else 480 return -1; 481 } 482 483 private static native int setUserNative(String user, String group) 484 throws IOException ; 485 486 static { 487 try { 488 System.loadLibrary("resin"); 489 _hasJni = true; 490 } catch (Throwable e) { 491 log.log(Level.FINE, e.toString(), e); 492 } 493 } 494 } 495 | Popular Tags |