KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > util > CauchoSystem


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.net.InetAddress JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47 import java.util.regex.Pattern JavaDoc;
48
49 /**
50  * A wrapper for Caucho system variables, allowing tests to override
51  * the default variables.
52  */

53 public class CauchoSystem {
54   private static Logger JavaDoc log
55     = Logger.getLogger("com.caucho.util.CauchoSystem");
56
57   static EnvironmentLocal<String JavaDoc> _serverIdLocal
58     = new EnvironmentLocal<String JavaDoc>("caucho.server-id");
59
60   static char _separatorChar = File.separatorChar;
61   static char _pathSeparatorChar = File.pathSeparatorChar;
62   static String JavaDoc _localHost;
63   static String JavaDoc _userDir;
64   static String JavaDoc _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 JavaDoc _newline;
74   private static long _version;
75
76   private static JniCauchoSystem _jniCauchoSystem;
77   private static boolean _isDetailedStatistics;
78   private static String JavaDoc _user;
79   private static String JavaDoc _group;
80   private static String JavaDoc _classPath;
81
82   static CpuUsage _cpuUsage;
83   
84   private CauchoSystem()
85   {
86   }
87
88   /**
89    * Returns true if we're currently running a test.
90    */

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   /**
102    * Sets the Path to be used as ResinHome.
103    */

104   public static void setResinHome(Path path)
105   {
106     _resinHome = path;
107   }
108
109   /**
110    * Gets the Path used as ResinHome.
111    */

112   public static Path getResinHome()
113   {
114     if (_resinHome != null)
115       return _resinHome;
116
117     String JavaDoc path = System.getProperty("resin.home");
118     if (path != null) {
119       _resinHome = Vfs.lookupNative(path);
120       return _resinHome;
121     }
122
123     String JavaDoc 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 JavaDoc 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     /*
162     String userHome = System.getProperty("user.home");
163     if (userHome != null)
164       resinHome = Pwd.lookupNative(userHome).lookup(".resin-home");
165
166     if (resinHome != null && resinHome.isDirectory())
167       return resinHome;
168     */

169
170     return Vfs.lookup();
171   }
172
173   /**
174    * Gets the Path used as root directory
175    */

176   /**
177   public static Path getRootDirectory()
178   {
179     if (_rootDirectory != null)
180       return _rootDirectory;
181
182     String path = System.getProperty("server.root");
183
184     if (path != null) {
185       _rootDirectory = Vfs.lookupNative(path);
186       return _rootDirectory;
187     }
188
189     path = System.getProperty("resin.home");
190
191     if (path != null) {
192       _rootDirectory = Vfs.lookupNative(path);
193
194       return _rootDirectory;
195     }
196
197     _rootDirectory = getResinHome();
198
199     return _rootDirectory;
200   }
201    */

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 JavaDoc getResinConfig()
213   {
214     return getResinHome() + "/conf/resin.conf";
215   }
216
217   /**
218    * Returns a path to the work directory. The work directory is
219    * specified in the resin.conf by /caucho.com/java/work-path. If
220    * unspecified, it defaults to /tmp/caucho.
221    *
222    * @return directory path to work in.
223    */

224   public static Path getWorkPath()
225   {
226     Path workPath = WorkDir.getLocalWorkDir();
227
228     if (workPath != null)
229       return workPath;
230
231     String JavaDoc workDir;
232
233     // Windows uses /temp as a work dir
234
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 JavaDoc e) {
247     }
248
249     return path;
250   }
251
252   public static String JavaDoc getServerId()
253   {
254     return _serverIdLocal.get();
255   }
256
257   public static String JavaDoc 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 JavaDoc getNewlineString()
276   {
277     if (_newline == null) {
278       Thread JavaDoc thread = Thread.currentThread();
279       ClassLoader JavaDoc 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 JavaDoc getLocalHost()
334   {
335     if (_localHost != null)
336       return _localHost;
337
338     try {
339       InetAddress JavaDoc addr = InetAddress.getLocalHost();
340       _localHost = addr.getHostName();
341     } catch (Exception JavaDoc 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 JavaDoc e) {
353       return false;
354     }
355   }
356
357   public static String JavaDoc getUserName()
358   {
359     if (_userName == null)
360       _userName = System.getProperty("user.name");
361
362     return _userName;
363   }
364
365   /**
366    * Set true to cause the tracking of detailed statistcs, default false.
367    * Detailed statistics cause various parts of Resin to keep more detailed
368    * statistics at the possible expense of performance.
369    */

370   public static void setDetailedStatistics(boolean isVerboseStatistics)
371   {
372     _isDetailedStatistics = isVerboseStatistics;
373   }
374
375   /**
376    * Detailed statistics cause various parts of Resin to keep more detailed
377    * statistics at the possible expense of some performance.
378    */

379   public static boolean isDetailedStatistics()
380   {
381     return _isDetailedStatistics;
382   }
383
384   public static CpuUsage getCpuUsage()
385   {
386     return CpuUsage.create();
387   }
388
389   /**
390    * Loads a class from the context class loader.
391    *
392    * @param name the classname, separated by '.'
393    *
394    * @return the loaded class.
395    */

396   public static Class JavaDoc loadClass(String JavaDoc name)
397     throws ClassNotFoundException JavaDoc
398   {
399     return loadClass(name, false, null);
400   }
401
402   /**
403    * Loads a class from a classloader. If the loader is null, uses the
404    * context class loader.
405    *
406    * @param name the classname, separated by '.'
407    * @param init if true, resolves the class instances
408    * @param loader the class loader
409    *
410    * @return the loaded class.
411    */

412   public static Class JavaDoc loadClass(String JavaDoc name, boolean init, ClassLoader JavaDoc loader)
413     throws ClassNotFoundException JavaDoc
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   /**
425    * Returns the system classpath, including the bootpath
426    */

427   public static String JavaDoc getClassPath()
428   {
429     if (_classPath != null)
430       return _classPath;
431
432     String JavaDoc cp = System.getProperty("java.class.path");
433
434     String JavaDoc boot = System.getProperty("sun.boot.class.path");
435     if (boot != null && ! boot.equals(""))
436       cp = cp + File.pathSeparatorChar + boot;
437
438     Pattern JavaDoc pattern = Pattern.compile("" + File.pathSeparatorChar);
439
440     String JavaDoc []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   /**
469    * Sets the runtime user so we don't need to run as root.
470    */

471   public static int setUser(String JavaDoc user, String JavaDoc group)
472     throws Exception JavaDoc
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 JavaDoc user, String JavaDoc group)
484     throws IOException JavaDoc;
485
486   static {
487     try {
488       System.loadLibrary("resin");
489       _hasJni = true;
490     } catch (Throwable JavaDoc e) {
491       log.log(Level.FINE, e.toString(), e);
492     }
493   }
494 }
495
Popular Tags