KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > java > WorkDir


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.java;
31
32 import com.caucho.loader.EnvironmentLocal;
33 import com.caucho.vfs.MemoryPath;
34 import com.caucho.vfs.MergePath;
35 import com.caucho.vfs.Path;
36 import com.caucho.vfs.Vfs;
37
38 import javax.annotation.PostConstruct;
39
40 public class WorkDir {
41   private static final EnvironmentLocal<Path> _localWorkDir
42     = new EnvironmentLocal<Path>("caucho.work-dir");
43   
44   private Path _path;
45
46   public WorkDir()
47   {
48   }
49   
50   /**
51    * Returns the local work directory.
52    */

53   public static Path getLocalWorkDir()
54   {
55     return getLocalWorkDir(Thread.currentThread().getContextClassLoader());
56   }
57
58   /**
59    * Returns the local work directory.
60    */

61   public static Path getLocalWorkDir(ClassLoader JavaDoc loader)
62   {
63     Path path = _localWorkDir.get(loader);
64
65     if (path != null)
66       return path;
67
68     // Windows uses /temp as a work dir
69
if (com.caucho.server.util.CauchoSystem.isWindows())
70       path = Vfs.lookup("file:/c:/tmp/caucho");
71     else
72       path = Vfs.lookup("file:/tmp/caucho");
73
74     _localWorkDir.setGlobal(path);
75     
76     try {
77       path.mkdirs();
78     } catch (java.io.IOException JavaDoc e) {
79     }
80
81     return path;
82   }
83
84   /**
85    * Sets the work dir.
86    */

87   public static void setLocalWorkDir(Path path)
88   {
89     setLocalWorkDir(path, Thread.currentThread().getContextClassLoader());
90   }
91
92   /**
93    * Sets the work dir.
94    */

95   public static void setLocalWorkDir(Path path, ClassLoader JavaDoc loader)
96   {
97     try {
98       if (path instanceof MergePath)
99     path = ((MergePath) path).getWritePath();
100
101       if (path instanceof MemoryPath) {
102     String JavaDoc pathName = path.getPath();
103
104     path = Vfs.lookup("file:/tmp/caucho/qa/" + pathName);
105       }
106     
107       // path.mkdirs();
108
} catch (Exception JavaDoc e) {
109       throw new RuntimeException JavaDoc(e);
110     }
111     
112     _localWorkDir.set(path, loader);
113   }
114   
115   /**
116    * Sets the value.
117    */

118   public void setValue(Path path)
119   {
120     _path = path;
121   }
122   
123   /**
124    * @deprecated
125    */

126   public void setId(Path path)
127     throws java.io.IOException JavaDoc
128   {
129     setValue(path);
130   }
131
132   /**
133    * Stores self.
134    */

135   @PostConstruct
136   public void init()
137   {
138     setLocalWorkDir(_path);
139   }
140 }
141
142
Popular Tags