View Javadoc

1   /*
2   Toolforge core - Core of the Toolforge application suite
3   Copyright (C) 2004  Toolforge <www.toolforge.nl>
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19  package nl.toolforge.core.util.file;
20  
21  import org.apache.tools.ant.DirectoryScanner;
22  
23  import java.io.BufferedReader;
24  import java.io.BufferedWriter;
25  import java.io.File;
26  import java.io.FileWriter;
27  import java.io.IOException;
28  import java.io.InputStreamReader;
29  import java.util.Random;
30  
31  
32  /***
33   * File utilities. Stuff that is not implemented by <code>org.apache.commons.io.FileUtils</code>.
34   *
35   * @author D.A. Smedes
36   * @author A. Mooy
37   *
38   * @version $Id:
39   */
40  public final class MyFileUtils {
41  
42    /*** Randomizer for use by createTempDirectory() */
43    private static Random randomizer = new Random();
44    
45    /***  */
46    private static File systemTempDir = new File(System.getProperty("java.io.tmpdir"));
47  
48    
49    /***
50     * @return A File instance pointing to the system temp dir
51     *          (specified in the "java.io.tmpdir" system property). 
52     */
53    public static File getSystemTempDirectory() {
54      return systemTempDir;
55    }
56    
57    /***
58     * Creates a temporary directory with some random positive <code>long</code> as its name.
59     *
60     * @return A <code>File</code> reference to the temporary directory.
61     *
62     * @throws IOException When some IO error occurred.
63     */
64    public static File createTempDirectory() throws IOException {
65      File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + Math.abs(randomizer.nextLong()));
66  
67      tmp.mkdirs();
68  
69      return tmp;
70    }
71  
72   /***
73     * Makes the specified file or directory writeable. If the <code>recurse</code>
74     * parameter is true and the specified target is a directory, this method will
75     * recurse into subdirectories.
76     *
77     * @param target File or directory to make writeable.
78     * @param recurse
79     * @throws IOException
80     * @throws InterruptedException
81     */
82    public static void makeWriteable(File target, boolean recurse) throws IOException, InterruptedException {
83      String osName  = System.getProperty("os.name");
84      String command = null;
85      
86      if (target == null) {
87        throw new IllegalArgumentException("The specified file or directory is null.");
88      }
89      
90      if (osName.toUpperCase().startsWith("WINDOWS")) {
91        if (target.isDirectory()) {
92          command = "cmd.exe /c attrib -r " + target + "//*.*" + (recurse ? " /s" : "");
93        } else {
94          command = "cmd.exe /c attrib -r " + target;
95        }
96      }
97      else if (osName.toUpperCase().startsWith("LINUX")) {
98        if (target.isDirectory()) {
99          command = "chmod " + (recurse ? "-R" : "") + " -f u+w " + target;
100       } else {
101         command = "chmod -f u+w " + target;
102       }
103     }
104     else {
105       //all os-es other then Windows and Linux.
106       if (target.isDirectory()) {
107         command = "chmod " + (recurse ? "-R" : "") + " -f u+w " + target;
108       } else {
109         command = "chmod -f u+w " + target;
110       }
111     }
112     
113     Process proc = Runtime.getRuntime().exec(command);
114     
115     proc.waitFor();
116   }
117 
118   /***
119    * Makes all files in <code>dir</code> and all its subdirectories read-only. Uses the Ant
120    * <code>DirectoryScanner</code>.
121    *
122    * @param dir Starting directory.
123    */
124   public static void makeReadOnly(File dir) {
125 
126     DirectoryScanner scanner = new DirectoryScanner();
127     scanner.setBasedir(dir);
128     scanner.setIncludes(new String[]{"**/*"});
129     scanner.scan();
130 
131     String[] files = scanner.getIncludedFiles();
132     String[] dirs = scanner.getIncludedDirectories();
133 
134     for (int i = 0; i < files.length; i++) {
135       new File(dir, files[i]).setReadOnly();
136     }
137     for (int i = 0; i < dirs.length; i++) {
138       new File(dir, dirs[i]).setReadOnly();
139     }
140   }
141 
142   /***
143    * Writes <code>fileRef</code> to <code>dir</code>. <code>fileRef</code> should be available in the classpath of
144    * <code>classLoader</code>.
145    *
146    * @param dir         The directory to write to
147    * @param fileRef     The original filename (path)
148    * @param classLoader The class loader that contains <code>fileRef</code>
149    *
150    * @throws IOException
151    */
152 //  public static void writeFile(File dir, File fileRef, ClassLoader classLoader) throws IOException {
153 //    writeFile(dir, fileRef, fileRef, classLoader);
154 //  }
155 
156   /***
157    * Writes <code>fileRef</code> to <code>dir</code> as <code>newFileRef</code>. <code>fileRef</code> should be available in the classpath of
158    * <code>classLoader</code>.
159    *
160    * @param dir         The directory to write to
161    * @param fileRef     The original filename (path)
162    * @param newFileName The new filename
163    * @param classLoader The class loader that contains <code>fileRef</code>
164    *
165    * @throws IOException
166    */
167   public static void writeFile(File dir, File fileRef, File newFileName, ClassLoader classLoader) throws IOException {
168 
169     if (dir == null || fileRef == null || newFileName == null || "".equals(newFileName) || classLoader == null) {
170       throw new NullPointerException("");
171     }
172 
173     BufferedReader in =
174         new BufferedReader(new InputStreamReader(classLoader.getResourceAsStream(fileRef.getPath())));
175 
176     dir.mkdirs();
177 
178     if (newFileName.getPath().lastIndexOf("/") > 0) {
179       String subDir = newFileName.getPath().substring(0, newFileName.getPath().lastIndexOf("/"));
180       new File(dir, subDir).mkdirs();
181     }
182 
183     BufferedWriter out =
184         new BufferedWriter(new FileWriter(new File(dir, newFileName.getPath())));
185 
186     String str;
187     while ((str = in.readLine()) != null) {
188       out.write(str);
189     }
190     out.close();
191     in.close();
192   }
193 
194 }