KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 1998-2004 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.util;
31
32 import java.util.*;
33
34 import java.util.logging.Logger;
35 import java.util.logging.Level;
36
37 import java.util.regex.Pattern;
38 import java.io.*;
39 import java.net.*;
40
41 import com.caucho.log.Log;
42 import com.caucho.vfs.*;
43 import com.caucho.java.WorkDir;
44 import com.caucho.loader.EnvironmentLocal;
45
46 /**
47  * A wrapper for Caucho system variables, allowing tests to override
48  * the default variables.
49  */

50 public class CauchoSystem {
51   private static final Logger log = Log.open(CauchoSystem.class);
52   
53   static char separatorChar = File.separatorChar;
54   static char pathSeparatorChar = File.pathSeparatorChar;
55   static String _localHost;
56   static String _userDir;
57   static String _userName;
58   static Path _resinHome;
59   static Path _serverRoot;
60   static boolean _isTesting;
61
62   static boolean _hasJni;
63   
64   private static int isUnix = -1;
65   private static String newline;
66   private static long _version;
67
68   private static String _user;
69   private static String _group;
70   private static String _classPath;
71   
72   static CpuUsage cpuUsage;
73
74   private CauchoSystem()
75   {
76   }
77
78   /**
79    * Returns true if we're currently running a test.
80    */

81   public static boolean isTesting()
82   {
83     return _isTesting;
84   }
85
86   public static void setIsTesting(boolean testing)
87   {
88     _isTesting = testing;
89   }
90
91   /**
92    * Sets the Path to be used as ResinHome.
93    */

94   public static void setResinHome(Path path)
95   {
96     _resinHome = path;
97   }
98
99   /**
100    * Gets the Path used as ResinHome.
101    */

102   public static Path getResinHome()
103   {
104     if (_resinHome != null)
105       return _resinHome;
106
107     String path = System.getProperty("resin.home");
108     if (path != null) {
109       _resinHome = Vfs.lookupNative(path);
110       return _resinHome;
111     }
112
113     String classpath = System.getProperty("java.class.path");
114     int head = 0;
115     char sep = getFileSeparatorChar();
116     char pathSep = sep == '/' ? ':' : ';';
117     while (path == null) {
118       int p = classpath.indexOf(pathSep, head);
119       String subpath;
120       if (p < 0)
121         subpath = classpath.substring(head);
122       else
123         subpath = classpath.substring(head, p);
124
125       if (subpath.endsWith(sep + "lib" + sep + "resin.jar") ||
126       subpath.equals("lib" + sep + "resin.jar")) {
127     path = subpath.substring(0, subpath.length() -
128                  ("lib" + sep + "resin.jar").length());
129       }
130
131       else if (subpath.endsWith(sep + "classes") ||
132            subpath.equals("classes")) {
133     Path resinPath = Vfs.lookupNative(subpath);
134     resinPath = resinPath.lookup("com/caucho/util/CauchoSystem.class");
135     if (resinPath.exists()) {
136       path = subpath.substring(0, subpath.length() - "classes".length());
137     }
138       }
139
140       if (p < 0)
141     break;
142       else
143     head = p + 1;
144     }
145
146     if (path != null)
147       _resinHome = Vfs.lookupNative(path);
148     if (_resinHome != null && _resinHome.isDirectory())
149       return _resinHome;
150
151     /*
152     String userHome = System.getProperty("user.home");
153     if (userHome != null)
154       resinHome = Pwd.lookupNative(userHome).lookup(".resin-home");
155
156     if (resinHome != null && resinHome.isDirectory())
157       return resinHome;
158     */

159
160     return Vfs.lookup();
161   }
162
163   /**
164    * Gets the Path used as ServerRoot.
165    */

166   public static Path getServerRoot()
167   {
168     if (_serverRoot != null)
169       return _serverRoot;
170
171     String path = System.getProperty("server.root");
172     if (path != null) {
173       _serverRoot = Vfs.lookupNative(path);
174       return _serverRoot;
175     }
176
177     path = System.getProperty("resin.home");
178     if (path != null) {
179       _serverRoot = Vfs.lookupNative(path);
180
181       return _serverRoot;
182     }
183
184     _serverRoot = getResinHome();
185
186     return _serverRoot;
187   }
188
189
190   public static long getVersionId()
191   {
192     if (_version == 0) {
193       _version = Crc64.generate(com.caucho.Version.FULL_VERSION);
194     }
195     
196     return _version;
197   }
198
199   public static String getResinConfig()
200   {
201     return getResinHome() + "/conf/resin.conf";
202   }
203
204   /**
205    * Returns a path to the work directory. The work directory is
206    * specified in the resin.conf by /caucho.com/java/work-path. If
207    * unspecified, it defaults to /tmp/caucho.
208    *
209    * @return directory path to work in.
210    */

211   public static Path getWorkPath()
212   {
213     Path workPath = WorkDir.getLocalWorkDir();
214
215     if (workPath != null)
216       return workPath;
217
218     String workDir;
219     
220     // Windows uses /temp as a work dir
221
if (CauchoSystem.isWindows())
222       workDir = "file:/c:/tmp/caucho";
223     else
224       workDir = "file:/tmp/caucho";
225
226     Path path;
227     if (workDir.charAt(0) == '/')
228       path = Vfs.lookupNative(workDir);
229     else
230       path = CauchoSystem.getResinHome().lookupNative(workDir);
231     try {
232       path.mkdirs();
233     } catch (IOException e) {
234     }
235
236     return path;
237   }
238
239   public static String getUserDir()
240   {
241     if (_userDir == null)
242       _userDir = System.getProperty("user.dir");
243
244     return _userDir;
245   }
246
247   public static char getFileSeparatorChar()
248   {
249     return separatorChar;
250   }
251
252   public static char getPathSeparatorChar()
253   {
254     return pathSeparatorChar;
255   }
256
257   public static String getNewlineString()
258   {
259     if (newline == null) {
260       newline = System.getProperty("line.separator");
261       if (newline == null)
262         newline = "\n";
263     }
264     
265     return newline;
266   }
267
268   public static boolean isWindows()
269   {
270     return separatorChar == '\\';
271   }
272
273   public static boolean isTest()
274   {
275     return Alarm.isTest();
276   }
277
278   public static boolean isCaseInsensitive()
279   {
280     return CaseInsensitive.isCaseInsensitive();
281   }
282
283   public static boolean isUnix()
284   {
285     if (isUnix >= 0)
286       return isUnix == 1;
287
288     isUnix = 0;
289
290     if (separatorChar == '/' && Vfs.lookup("/bin/sh").canRead())
291       isUnix = 1;
292
293     return isUnix == 1;
294   }
295
296   public static void setWindowsTest(boolean windows)
297   {
298     _isTesting = true;
299     if (windows)
300       separatorChar = '\\';
301     else
302       separatorChar = File.separatorChar;
303   }
304
305   public static String getLocalHost()
306   {
307     if (_localHost != null)
308       return _localHost;
309
310     try {
311       InetAddress addr = InetAddress.getLocalHost();
312       _localHost = addr.getHostName();
313     } catch (Exception e) {
314       _localHost = "127.0.0.1";
315     }
316
317     return _localHost;
318   }
319
320   public static String getUserName()
321   {
322     if (_userName == null)
323       _userName = System.getProperty("user.name");
324
325     return _userName;
326   }
327
328   public static CpuUsage getCpuUsage()
329   {
330     return CpuUsage.create();
331   }
332
333   /**
334    * Loads a class from the context class loader.
335    *
336    * @param name the classname, separated by '.'
337    *
338    * @return the loaded class.
339    */

340   public static Class loadClass(String name)
341     throws ClassNotFoundException
342   {
343     return loadClass(name, false, null);
344   }
345   
346   /**
347    * Loads a class from a classloader. If the loader is null, uses the
348    * context class loader.
349    *
350    * @param name the classname, separated by '.'
351    * @param init if true, resolves the class instances
352    * @param loader the class loader
353    *
354    * @return the loaded class.
355    */

356   public static Class loadClass(String name, boolean init, ClassLoader loader)
357     throws ClassNotFoundException
358   {
359     if (loader == null)
360       loader = Thread.currentThread().getContextClassLoader();
361
362     if (loader == null || loader.equals(CauchoSystem.class))
363       return Class.forName(name);
364     else
365       return Class.forName(name, init, loader);
366   }
367
368   /**
369    * Returns the system classpath, including the bootpath
370    */

371   public static String getClassPath()
372   {
373     if (_classPath != null)
374       return _classPath;
375     
376     String cp = System.getProperty("java.class.path");
377     
378     String boot = System.getProperty("sun.boot.class.path");
379     if (boot != null && ! boot.equals(""))
380       cp = cp + File.pathSeparatorChar + boot;
381
382     Pattern pattern = Pattern.compile("" + File.pathSeparatorChar);
383     
384     String []path = pattern.split(cp);
385
386     CharBuffer cb = CharBuffer.allocate();
387
388     for (int i = 0; i < path.length; i++) {
389       Path subpath = Vfs.lookup(path[i]);
390
391       if (subpath.canRead() || subpath.isDirectory()) {
392     if (cb.length() > 0)
393       cb.append(File.pathSeparatorChar);
394
395     cb.append(path[i]);
396       }
397     }
398
399     _classPath = cb.toString();
400
401     return _classPath;
402   }
403
404   /**
405    * Sets the runtime user so we don't need to run as root.
406    */

407   public static int setUser(String user, String group)
408     throws Exception
409   {
410     _user = user;
411     _group = group;
412
413     if (_hasJni && user != null)
414       return setUserNative(_user, _group);
415     else
416       return -1;
417   }
418   
419   private static native int setUserNative(String user, String group)
420     throws IOException;
421
422   static {
423     try {
424       System.loadLibrary("resin");
425       _hasJni = true;
426     } catch (Throwable e) {
427       log.log(Level.FINE, e.toString(), e);
428     }
429   }
430 }
431
Popular Tags