KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > FileUtils


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: FileUtils.java 2083 2005-08-16 05:48:33Z dblevins $
44  */

45 package org.openejb.util;
46
47 import java.io.File JavaDoc;
48 import java.io.FileInputStream JavaDoc;
49 import java.io.FileNotFoundException JavaDoc;
50 import java.io.FileOutputStream JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.io.InputStream JavaDoc;
53 import java.net.URL JavaDoc;
54 import java.util.Hashtable JavaDoc;
55 import java.util.Properties JavaDoc;
56
57 import org.openejb.OpenEJBException;
58 import org.openejb.loader.SystemInstance;
59
60 public class FileUtils {
61
62     private static final java.util.Random JavaDoc _random = new java.util.Random JavaDoc();
63
64     private File JavaDoc home;
65
66     private FileUtils(String JavaDoc homeDir, String JavaDoc defaultDir) {
67         this(homeDir, defaultDir, System.getProperties());
68     }
69
70     public FileUtils(String JavaDoc homeDir, String JavaDoc defaultDir, Hashtable JavaDoc env) {
71         String JavaDoc homePath = null;
72         try {
73             homePath = (String JavaDoc) env.get(homeDir);
74             if (homePath == null) {
75                 homePath = (String JavaDoc) env.get(defaultDir);
76             }
77             
78             if (homePath == null) {
79                 homePath = System.getProperty("user.dir");
80             }
81                 
82             home = new File JavaDoc(homePath);
83             if (!home.exists() || (home.exists() && !home.isDirectory())) {
84                 homePath = System.getProperty("user.dir");
85                 home = new File JavaDoc(homePath);
86             }
87
88             home = home.getAbsoluteFile();
89         } catch (SecurityException JavaDoc e) {
90             //throw new IOException("Cannot resolve the directory: "+homeDir+" : "+e.getMessage());
91
}
92     }
93
94     /**
95      * @see #getDirectory(String, boolean)
96      */

97     public File JavaDoc getDirectory(String JavaDoc path) throws IOException JavaDoc {
98         return getDirectory(path, false);
99     }
100
101     public boolean equals(Object JavaDoc obj) {
102         if (!(obj instanceof FileUtils)) return false;
103         FileUtils that = (FileUtils)obj;
104         return this.getDirectory().equals(that.getDirectory());
105     }
106
107     /**
108      * Resolves the specified path relative to the home directory; create it if requested
109      *
110      * @param path
111      * relative path to the home directory
112      * @param create
113      * shall the directory be created if it doesn't exist?
114      * @return directory
115      * @throws IOException
116      */

117     public File JavaDoc getDirectory(String JavaDoc path, boolean create) throws IOException JavaDoc {
118         File JavaDoc dir = null;
119
120         dir = new File JavaDoc(home, path);
121         dir = dir.getCanonicalFile();
122
123         if (!dir.exists() && create) {
124             try {
125                 if (!dir.mkdirs())
126                     throw new IOException JavaDoc("Cannot create the directory " + dir.getPath());
127             } catch (SecurityException JavaDoc e) {
128                 throw new IOException JavaDoc(
129                     "Permission denied: Cannot create the directory " + dir.getPath() + " : " + e.getMessage());
130             }
131         } else if (dir.exists() && !dir.isDirectory()) {
132             throw new IOException JavaDoc("The path specified is not a valid directory: " + dir.getPath());
133         }
134
135         return dir;
136     }
137
138     public File JavaDoc getDirectory() {
139         return home;
140     }
141
142     public void setDirectory(File JavaDoc dir) {
143         this.home = dir;
144     }
145     
146     public File JavaDoc getFile(String JavaDoc path) throws java.io.FileNotFoundException JavaDoc, java.io.IOException JavaDoc {
147         return getFile(path, true);
148     }
149
150     public File JavaDoc getFile(String JavaDoc path, boolean validate) throws java.io.FileNotFoundException JavaDoc, java.io.IOException JavaDoc {
151         File JavaDoc file = null;
152
153         file = new File JavaDoc(path);
154
155         if (!file.isAbsolute()) {
156             file = new File JavaDoc(home, path);
157         }
158
159         if (validate && !file.exists()) {
160             throw new FileNotFoundException JavaDoc("The path specified is not a valid file: " + file.getPath());
161         } else if (validate && file.isDirectory()) {
162             throw new FileNotFoundException JavaDoc("The path specified is a directory, not a file: " + file.getPath());
163         }
164
165         return file;
166     }
167
168     /**
169      * Creates a string for a temporary directory
170      *
171      * @param pathPrefix
172      * the path prefix to for the directory, e.g. /tmp/openejb @returns the file object associated with the
173      * unique name
174      * @throws java.io.IOException
175      * if it can't find a unique directory name after many iterations
176      */

177     public static File JavaDoc createTempDirectory(String JavaDoc pathPrefix) throws java.io.IOException JavaDoc {
178         for (int maxAttempts = 100; maxAttempts > 0; --maxAttempts) {
179             String JavaDoc path = pathPrefix + _random.nextLong();
180             java.io.File JavaDoc tmpDir = new java.io.File JavaDoc(path);
181             if (tmpDir.exists()) {
182                 continue;
183             } else {
184                 tmpDir.mkdir();
185                 return tmpDir;
186             }
187         }
188         throw new java.io.IOException JavaDoc("Can't create temporary directory.");
189     }
190
191     /**
192      * Creates a string for a temporary directory The path prefix is chosen from the system property "java.io.tmpdir"
193      * plus a file separator plus the string "openejb" @returns the file object associated with the unique name
194      *
195      * @throws java.io.IOException
196      * if it can't find a unique directory name after many iterations
197      */

198     public static File JavaDoc createTempDirectory() throws java.io.IOException JavaDoc {
199         String JavaDoc prefix = System.getProperty("java.io.tmpdir", File.separator + "tmp") + File.separator + "openejb";
200         return createTempDirectory(prefix);
201     }
202
203     /**
204      * Copies the contents of one file to another.
205      *
206      * @param destination
207      * Destination file
208      * @param source
209      * Source file
210      *
211      * @exception java.io.IOException
212      * Thrown if there is an error copying the file.
213      */

214     public static void copyFile(File JavaDoc destination, File JavaDoc source) throws java.io.IOException JavaDoc {
215         copyFile(destination, source, false);
216     }
217
218     /**
219      * Copies the contents of one file to another.
220      *
221      * @param destination
222      * Destination file
223      * @param source
224      * Source file
225      * @param deleteSourceFile
226      * whether or not to delete the source file
227      *
228      * @exception java.io.IOException
229      * Thrown if there is an error copying the file.
230      */

231     public static void copyFile(File JavaDoc destination, File JavaDoc source, boolean deleteSourceFile) throws java.io.IOException JavaDoc {
232         FileInputStream JavaDoc in = null;
233         FileOutputStream JavaDoc out = null;
234         try {
235             in = new FileInputStream JavaDoc(source);
236             out = new FileOutputStream JavaDoc(destination);
237
238             int len;
239             byte[] buffer = new byte[4096];
240             while ((len = in.read(buffer)) != -1) {
241                 out.write(buffer, 0, len);
242             }
243         } catch (java.io.IOException JavaDoc e) {
244             throw e;
245         } finally {
246             in.close();
247             out.close();
248         }
249
250         if (deleteSourceFile) {
251             source.delete();
252         }
253     }
254     
255 }
256
Popular Tags