001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.java;
031:
032: import com.caucho.loader.EnvironmentLocal;
033: import com.caucho.vfs.MemoryPath;
034: import com.caucho.vfs.MergePath;
035: import com.caucho.vfs.Path;
036: import com.caucho.vfs.Vfs;
037:
038: import javax.annotation.PostConstruct;
039:
040: public class WorkDir {
041: private static final EnvironmentLocal<Path> _localWorkDir = new EnvironmentLocal<Path>(
042: "caucho.work-dir");
043:
044: private Path _path;
045:
046: public WorkDir() {
047: }
048:
049: /**
050: * Returns the local work directory.
051: */
052: public static Path getLocalWorkDir() {
053: return getLocalWorkDir(Thread.currentThread()
054: .getContextClassLoader());
055: }
056:
057: /**
058: * Returns the local work directory.
059: */
060: public static Path getLocalWorkDir(ClassLoader loader) {
061: Path path = _localWorkDir.get(loader);
062:
063: if (path != null)
064: return path;
065:
066: // Windows uses /temp as a work dir
067: if (com.caucho.server.util.CauchoSystem.isWindows())
068: path = Vfs.lookup("file:/c:/tmp/caucho");
069: else
070: path = Vfs.lookup("file:/tmp/caucho");
071:
072: _localWorkDir.setGlobal(path);
073:
074: try {
075: path.mkdirs();
076: } catch (java.io.IOException e) {
077: }
078:
079: return path;
080: }
081:
082: /**
083: * Sets the work dir.
084: */
085: public static void setLocalWorkDir(Path path) {
086: setLocalWorkDir(path, Thread.currentThread()
087: .getContextClassLoader());
088: }
089:
090: /**
091: * Sets the work dir.
092: */
093: public static void setLocalWorkDir(Path path, ClassLoader loader) {
094: try {
095: if (path instanceof MergePath)
096: path = ((MergePath) path).getWritePath();
097:
098: if (path instanceof MemoryPath) {
099: String pathName = path.getPath();
100:
101: path = Vfs.lookup("file:/tmp/caucho/qa/" + pathName);
102: }
103:
104: // path.mkdirs();
105: } catch (Exception e) {
106: throw new RuntimeException(e);
107: }
108:
109: _localWorkDir.set(path, loader);
110: }
111:
112: /**
113: * Sets the value.
114: */
115: public void setValue(Path path) {
116: _path = path;
117: }
118:
119: /**
120: * @deprecated
121: */
122: public void setId(Path path) throws java.io.IOException {
123: setValue(path);
124: }
125:
126: /**
127: * Stores self.
128: */
129: @PostConstruct
130: public void init() {
131: setLocalWorkDir(_path);
132: }
133: }
|