001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.loader;
017:
018: import java.io.File;
019: import java.io.FileInputStream;
020: import java.io.FileNotFoundException;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.util.Hashtable;
024:
025: public class FileUtils {
026:
027: private static final java.util.Random _random = new java.util.Random();
028:
029: private File home;
030:
031: private FileUtils(String homeDir, String defaultDir) {
032: this (homeDir, defaultDir, SystemInstance.get().getProperties());
033: }
034:
035: public FileUtils(String homeDir, String defaultDir, Hashtable env) {
036: String homePath = null;
037: try {
038: homePath = (String) env.get(homeDir);
039: if (homePath == null) {
040: homePath = (String) env.get(defaultDir);
041: }
042:
043: if (homePath == null) {
044: homePath = System.getProperty("user.dir");
045: }
046:
047: home = new File(homePath);
048: if (!home.exists()
049: || (home.exists() && !home.isDirectory())) {
050: homePath = System.getProperty("user.dir");
051: home = new File(homePath);
052: }
053:
054: try {
055: home = home.getCanonicalFile();
056: } catch (IOException e) {
057: // this shouldn't happen, but let's get absolute file
058: home = home.getAbsoluteFile();
059: }
060: } catch (SecurityException e) {
061:
062: }
063: }
064:
065: public File getDirectory(String path) throws IOException {
066: return getDirectory(path, false);
067: }
068:
069: public boolean equals(Object obj) {
070: if (!(obj instanceof FileUtils))
071: return false;
072: FileUtils that = (FileUtils) obj;
073: return this .getDirectory().equals(that.getDirectory());
074: }
075:
076: public File getDirectory(String path, boolean create)
077: throws IOException {
078: File dir = null;
079:
080: dir = new File(home, path);
081: dir = dir.getCanonicalFile();
082:
083: if (!dir.exists() && create) {
084: try {
085: if (!dir.mkdirs())
086: throw new IOException(
087: "Cannot create the directory "
088: + dir.getPath());
089: } catch (SecurityException e) {
090: throw new IOException(
091: "Permission denied: Cannot create the directory "
092: + dir.getPath() + " : "
093: + e.getMessage());
094: }
095: } else if (dir.exists() && !dir.isDirectory()) {
096: throw new IOException(
097: "The path specified is not a valid directory: "
098: + dir.getPath());
099: }
100:
101: return dir;
102: }
103:
104: public File getDirectory() {
105: return home;
106: }
107:
108: public void setDirectory(File dir) {
109: this .home = dir;
110: }
111:
112: public File getFile(String path)
113: throws java.io.FileNotFoundException, java.io.IOException {
114: return getFile(path, true);
115: }
116:
117: public File getFile(String path, boolean validate)
118: throws java.io.FileNotFoundException, java.io.IOException {
119: File file = null;
120:
121: file = new File(path);
122:
123: if (!file.isAbsolute()) {
124: file = new File(home, path);
125: }
126:
127: if (validate && !file.exists()) {
128: throw new FileNotFoundException(
129: "The path specified is not a valid file: "
130: + file.getPath());
131: } else if (validate && file.isDirectory()) {
132: throw new FileNotFoundException(
133: "The path specified is a directory, not a file: "
134: + file.getPath());
135: }
136:
137: return file;
138: }
139:
140: public static File createTempDirectory(String pathPrefix)
141: throws java.io.IOException {
142: for (int maxAttempts = 100; maxAttempts > 0; --maxAttempts) {
143: String path = pathPrefix + _random.nextLong();
144: java.io.File tmpDir = new java.io.File(path);
145: if (tmpDir.exists()) {
146: continue;
147: } else {
148: tmpDir.mkdir();
149: return tmpDir;
150: }
151: }
152: throw new java.io.IOException(
153: "Can't create temporary directory.");
154: }
155:
156: public static File createTempDirectory() throws java.io.IOException {
157: String prefix = System.getProperty("java.io.tmpdir",
158: File.separator + "tmp")
159: + File.separator + "openejb";
160: return createTempDirectory(prefix);
161: }
162:
163: public static void copyFile(File destination, File source)
164: throws java.io.IOException {
165: copyFile(destination, source, false);
166: }
167:
168: public static void copyFile(File destination, File source,
169: boolean deleteSourceFile) throws java.io.IOException {
170: FileInputStream in = null;
171: FileOutputStream out = null;
172: try {
173: in = new FileInputStream(source);
174: out = new FileOutputStream(destination);
175:
176: int len;
177: byte[] buffer = new byte[4096];
178: while ((len = in.read(buffer)) != -1) {
179: out.write(buffer, 0, len);
180: }
181: } catch (java.io.IOException e) {
182: throw e;
183: } finally {
184: in.close();
185: out.close();
186: }
187:
188: if (deleteSourceFile) {
189: source.delete();
190: }
191: }
192:
193: }
|