1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.isenshi.util;
17
18 import java.io.BufferedReader;
19 import java.io.BufferedWriter;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.OutputStream;
28 import java.io.OutputStreamWriter;
29 import java.io.UnsupportedEncodingException;
30 import java.io.Writer;
31 import java.net.MalformedURLException;
32 import java.net.URL;
33 import java.net.URLDecoder;
34 import java.util.jar.JarEntry;
35 import java.util.jar.JarInputStream;
36 import java.util.jar.JarOutputStream;
37 import java.util.zip.ZipEntry;
38
39 import org.apache.commons.io.FileUtils;
40 import org.apache.commons.io.IOUtils;
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43 import org.seasar.tuigwaa.system.Constants;
44
45 public class ResourceUtils {
46
47 private static Log log = LogFactory.getLog(ResourceUtils.class);
48
49 public static final void writeJar(JarOutputStream jOutput, String directory, String prefix, String ignorePath){
50 File file = getFile(directory);
51 addFile(jOutput, file, prefix, ignorePath);
52 }
53
54 public static final void readJar(JarInputStream jinput, String directory) {
55 try {
56 JarEntry jar;
57 while ((jar = jinput.getNextJarEntry()) != null) {
58 String path = PathUtils.join(directory, jar.getName());
59 if (jar.isDirectory()) {
60 File savePath = getFile(path);
61 savePath.mkdir();
62 } else {
63 int length = 0;
64 int bufsize = 1024;
65 byte[] buf = new byte[bufsize];
66 OutputStream fos = getOutputStream(path);
67 while (jinput.available() > 0
68 && (length = jinput.read(buf, 0, bufsize)) != -1) {
69 fos.write(buf, 0, length);
70 }
71 fos.close();
72 }
73 }
74 } catch (FileNotFoundException e) {
75 e.printStackTrace();
76 } catch (IOException e) {
77 e.printStackTrace();
78 }
79 }
80
81 public static final void deleteDirectory(String path) {
82 File file = getFile(path);
83 deleteDirectory(file);
84 }
85
86 public static final void deleteDirectory(File file) {
87
88 try {
89 if (file.exists()) {
90 FileUtils.deleteDirectory(file);
91 }
92 } catch (IOException ioe) {
93
94 log.error(ioe.getMessage());
95 }
96 }
97
98 public static final String[] getChildrenNames(String path) {
99 File file = getFile(path);
100 return file.list();
101 }
102
103 public static Writer getWriter(String path) {
104 try {
105 return new OutputStreamWriter(getOutputStream(path), "Windows-31J");
106 } catch (UnsupportedEncodingException e) {
107
108 return null;
109 }
110 }
111
112 public static InputStream getInputStream(String path) {
113 File file = getFile(path);
114 InputStream stream = null;
115 try {
116 stream = new FileInputStream(file);
117 } catch (FileNotFoundException e) {
118
119 }
120 return stream;
121 }
122
123 public static boolean isExist(String path) {
124 return getFile(path).exists();
125 }
126
127 public static OutputStream getOutputStream(String path) {
128 File file = getFile(path);
129 return getOutputStream(file);
130 }
131
132 public static OutputStream getOutputStream(File file) {
133 checkNewParent(file);
134 OutputStream os = null;
135 try {
136 os = new FileOutputStream(file);
137 } catch (FileNotFoundException e) {
138 e.printStackTrace();
139 throw new RuntimeException(e);
140 }
141 return os;
142 }
143
144 public static void checkNewParent(File file) {
145 File parent = file.getParentFile();
146
147 if (parent.exists()) {
148 return;
149 }
150 checkNewParent(parent);
151 parent.mkdir();
152 }
153
154 public static String getPath(String path) {
155 if (path == null) {
156 return null;
157 }
158 URL url = null;
159 try {
160 url = getFile(path).toURL();
161 } catch (MalformedURLException e) {
162 e.printStackTrace();
163 }
164 return getFileName(url);
165 }
166
167 public static File getFile(final String path) {
168 URL url = getClassLoader().getResource("");
169 return new File(getFileName(url) + File.separator + path);
170 }
171
172 public static InputStream getResourceAsStream(final String path) {
173 return getClassLoader().getResourceAsStream("Domain.xml");
174 }
175
176 public static ClassLoader getClassLoader() {
177 return Thread.currentThread().getContextClassLoader();
178 }
179
180 public static String getFileName(URL url) {
181 String s = url.getFile();
182 try {
183 return URLDecoder.decode(s, "UTF8");
184 } catch (UnsupportedEncodingException ex) {
185 ex.printStackTrace();
186 throw new RuntimeException(ex);
187 }
188 }
189
190 public static void writeContent(File file, String content) {
191
192 }
193
194 public static void writeContent(String path, String content) {
195 BufferedWriter writer = null;
196 try {
197 writer = new BufferedWriter(getWriter(path));
198 writer.write(content);
199 } catch (UnsupportedEncodingException e) {
200 e.printStackTrace();
201 } catch (FileNotFoundException e) {
202 e.printStackTrace();
203 } catch (IOException e) {
204 e.printStackTrace();
205 } finally {
206 IOUtils.closeQuietly(writer);
207 }
208 }
209
210 public static String readContent(String path) {
211 File file = getFile(path);
212 return readContent(file);
213 }
214
215 public static InputStream readContentAsStream(String path){
216 File file = getFile(path);
217 InputStream is = null;
218 try{
219 is = new FileInputStream(file);
220 }catch(IOException ioe){
221 throw new RuntimeException(ioe);
222 }
223 return is;
224 }
225
226 public static String readContent(File file) {
227 StringBuffer buf = new StringBuffer();
228
229
230 if (!file.exists()) {
231 return null;
232 }
233
234 BufferedReader reader = null;
235 try {
236 reader = new BufferedReader(new InputStreamReader(
237 new FileInputStream(file), "Windows-31J"));
238 String line = null;
239 while ((line = reader.readLine()) != null) {
240 buf.append(line);
241
242 buf.append(Constants.LINEBREAK_CODE);
243 }
244 } catch (UnsupportedEncodingException e) {
245 e.printStackTrace();
246 throw new RuntimeException(e);
247 } catch (FileNotFoundException e) {
248 e.printStackTrace();
249 throw new RuntimeException(e);
250 } catch (IOException e) {
251 e.printStackTrace();
252 throw new RuntimeException(e);
253 } finally {
254 IOUtils.closeQuietly(reader);
255 }
256 return buf.toString();
257 }
258
259 public static void removeContent(String path) {
260 boolean success = true;
261 File file = getFile(path);
262 if (file.isDirectory()) {
263 String[] names = file.list();
264 for (int i = 0; i < names.length; i++) {
265 String childpath = PathUtils.join(path, names[i]);
266 removeContent(childpath);
267 }
268 }
269 success = file.delete();
270
271 if (!success)
272 throw new RuntimeException("deleting " + path + " failed.");
273 }
274
275 /***
276 * Provides File object with the given name which exists on upward path to
277 * the directory the current thread resides. This returns the first object
278 * with the given name, for example, the name is "foo", and current thread
279 * directory "/home/foo/xxx/foo/classes", returns "/home/foo/xxx/foo".
280 * FIXME: want to use FileUtils wild card, needs commons.io update.
281 *
282 * @param name
283 * specifies the file or directory name
284 * @return file object if the given name exists, if not return null
285 */
286 public static final File getUpwardFileByName(String name) {
287 File file = getFile("");
288
289 while (true) {
290 if (file.getName().equals(name)) {
291 break;
292 }
293 file = file.getParentFile();
294 if (file == null) {
295 break;
296 }
297 }
298 return file;
299 }
300
301 public static boolean createDirectory(String filepath) {
302 return createDirectory(new File(filepath));
303 }
304
305 public static boolean createDirectory(File file) {
306 boolean flag = true;
307 if (!file.exists()) {
308 flag = file.mkdirs();
309 }
310 return flag;
311 }
312
313
314 public static void copyDirectory(String srcDirectoryPath, String destDirectoryPath) throws IOException{
315
316 File src = new File(srcDirectoryPath);
317 if(!src.isDirectory()){
318 throw new IOException(srcDirectoryPath + " should be directory");
319 }
320
321 File[] childrens = src.listFiles();
322 for(int i=0;i<childrens.length;i++){
323 copyFile(childrens[i].getPath(),destDirectoryPath);
324 }
325 }
326
327 public static void copyFile(String srcpath, String destpath)
328 throws IOException {
329
330 File src = new File(srcpath);
331 File dest = new File(destpath);
332
333 if (src.exists()) {
334
335 if (!dest.exists()) {
336 createDirectory(dest);
337 }
338 if (src.isDirectory()) {
339 File[] childrens = src.listFiles();
340 String newdest = destpath + "/" + src.getName();
341 for (int i = 0; i < childrens.length; i++) {
342 copyFile(childrens[i].getPath(), newdest);
343 }
344 } else {
345 if (dest.isDirectory()) {
346 FileUtils.copyFileToDirectory(src, dest);
347 } else {
348 FileUtils.copyFile(src, dest);
349 }
350 }
351 }
352 }
353
354 public static void writeFile(String outputPath, InputStream is)
355 throws IOException {
356
357 try {
358 FileOutputStream fos = new FileOutputStream(outputPath, false);
359 byte[] buf = new byte[8192];
360 while (true) {
361 int length = is.read(buf);
362 if (length == -1)
363 break;
364 fos.write(buf, 0, length);
365 }
366 fos.flush();
367 fos.close();
368 } catch (FileNotFoundException fnfe) {
369 log.error("couldn't open file " + outputPath + " : "
370 + fnfe.getMessage());
371 throw fnfe;
372 } catch (IOException ioe) {
373 log.error(ioe.getMessage());
374 throw ioe;
375 }
376 }
377
378 private static final void addFile(JarOutputStream jOutput, File file, String prefix, String ignorePath){
379
380 prefix = CharUtil.chartrim(prefix, '/');
381 String path = ("".equals(prefix)) ? file.getName() : prefix + "/"
382 + file.getName();
383 if (file.isDirectory()) {
384 path = path + "/";
385 }
386
387 if(ignorePath != null && path.indexOf(ignorePath) != -1){
388 return;
389 }
390
391 try {
392 jOutput.putNextEntry(new ZipEntry(path));
393 if (file.isDirectory()) {
394 File[] files = file.listFiles();
395 for (int i = 0; i < files.length; i++) {
396 addFile(jOutput, files[i], path, ignorePath);
397 }
398 } else {
399 InputStream is = new FileInputStream(file);
400 int length = 0;
401 int bufsize = 1024;
402 byte[] buf = new byte[bufsize];
403 while ((length = is.read(buf, 0, bufsize)) != -1) {
404 jOutput.write(buf, 0, length);
405 }
406 }
407 } catch (IOException e) {
408 e.printStackTrace();
409 }
410
411 }
412
413 }