KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > FTFileUtil


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.util.FTFileUtil
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.util;
23
24 import java.io.FileWriter JavaDoc;
25 import java.io.File JavaDoc;
26 import java.security.AccessController JavaDoc;
27 import java.security.PrivilegedAction JavaDoc;
28 import java.security.PrivilegedActionException JavaDoc;
29 import java.security.PrivilegedExceptionAction JavaDoc;
30
31 /**
32   Convience functions for performing file manipulations
33   in ij scripts.
34   */

35 public class FTFileUtil
36 {
37     /**
38       Create a file.
39
40       @param name the file's name.
41       @length the number of bytes of test data in the file.
42       @exception Exception oops.
43       */

44     public static void mkFile(String JavaDoc fileName, int length) throws Exception JavaDoc
45     {
46         FileWriter JavaDoc fw = new FileWriter JavaDoc(fileName);
47         int offset = 0;
48         String JavaDoc data = "Amber!";
49         for (int ix=0;ix<length;ix++)
50         {
51             fw.write(data,offset,1);
52             offset++;
53             if (offset >= data.length()) offset = 0;
54         }
55         fw.close();
56     }
57
58     /**
59      * rename a file.
60      * This method is called by some tests through a SQL procedure:
61      * RENAME_FILE(LOCATION VARCHAR(32000), NAME VARCHAR(32000),
62      * NEW_NAME VARCHAR(32000))
63      * @param location location of the file
64      * @param name the file's name
65      * @param newName the file's new name
66     */

67     public static void renameFile(String JavaDoc location, String JavaDoc name ,
68                                   String JavaDoc newName) throws Exception JavaDoc
69     {
70         final File JavaDoc src = new File JavaDoc(location, name);
71         final File JavaDoc dst = new File JavaDoc(location, newName);
72         
73         // needs to run in a privileged block as it will be
74
// called through a SQL statement and thus a generated
75
// class. The generated class on the stack has no permissions
76
// granted to it.
77
AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
78                 public Object JavaDoc run() throws Exception JavaDoc {
79                     if(!src.renameTo(dst))
80                     {
81                         throw new Exception JavaDoc("unable to rename File: " +
82                                             src.getAbsolutePath() +
83                                             " To: " + dst.getAbsolutePath());
84                     }
85                     
86                     return null; // nothing to return
87
}
88             });
89     }
90
91
92     /**
93      * Check if a file exists ?
94      *
95      * This method is called by some tests through a SQL function:
96      * fileExists(fileName varchar(128))returns VARCHAR(100)
97      *
98      * @param name the file's name.
99      * @return <tt>"true"</tt> if the given file exists
100      * <tt>"false"</tt> otherwise.
101      * @exception Exception if any exception occurs
102      */

103     public static String JavaDoc fileExists(String JavaDoc fileName)
104         throws PrivilegedActionException JavaDoc
105     {
106         final File JavaDoc fl = new File JavaDoc(fileName);
107                 
108         // needs to run in a privileged block as it will be
109
// called through a SQL statement and thus a generated
110
// class. The generated class on the stack has no permissions
111
// granted to it.
112

113         return (String JavaDoc)
114             AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
115                 public Object JavaDoc run()
116                 {
117                     if(fl.exists()) {
118                         return "true";
119                     }else {
120                         return "false";
121                     }
122                 }
123             });
124     }
125
126
127     /**
128      * Remove a directory and all of its contents.
129      *
130      * @param name the file's name.
131      * @return <tt>true</tt> if the omplete directory was removed
132      * <tt>false</tt> otherwise.f false is returned then some of
133      * the files in the directory may have been removed.
134      */

135
136     private static boolean removeDirectory(File JavaDoc directory) {
137
138         if (directory == null)
139             return false;
140         if (!directory.exists())
141             return true;
142         if (!directory.isDirectory())
143             return false;
144
145         String JavaDoc[] list = directory.list();
146
147         if (list != null) {
148             for (int i = 0; i < list.length; i++) {
149                 File JavaDoc entry = new File JavaDoc(directory, list[i]);
150
151                 if (entry.isDirectory())
152                 {
153                     if (!removeDirectory(entry))
154                         return false;
155                 }
156                 else
157                 {
158                     if (!entry.delete())
159                         return false;
160                 }
161             }
162         }
163
164         return directory.delete();
165     }
166
167     /**
168      * Remove a directory and all of its contents.
169      * This method is called by some tests through a SQL function:
170      * removeDirectory(fileName varchar(128)) returns VARCHAR(100)
171      *
172      * @param name the file's name.
173      * @return <tt>"true"</tt> if the omplete directory was removed
174      * <tt>"false"</tt> otherwise.f false is returned then some of
175      * the files in the directory may have been removed.
176      */

177
178     public static String JavaDoc removeDirectory(final String JavaDoc directory)
179         throws PrivilegedActionException JavaDoc
180     {
181         // needs to run in a privileged block as it will be
182
// called through a SQL statement and thus a generated
183
// class. The generated class on the stack has no permissions
184
// granted to it.
185

186         return (String JavaDoc)
187             AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
188                     public Object JavaDoc run()
189                     {
190                         return (removeDirectory(
191                                new File JavaDoc(directory)) ? "true" : "false");
192                     }
193                 });
194     }
195     
196 }
197
198
199
200
Popular Tags