KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > avk > FileUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * FileUtil.java
21  *
22  * Created on October 14, 2005, 4:40 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.ide.avk;
26
27 import java.io.File JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31
32 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
33 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
34
35 import org.netbeans.modules.j2ee.sun.ide.Installer;
36 import org.netbeans.modules.j2ee.sun.ide.j2ee.DeploymentManagerProperties;
37
38 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager;
39
40 /**
41  *
42  * @author Nitya Doraisamy
43  */

44 public class FileUtil {
45     
46     public static void clearResults(SunDeploymentManagerInterface sunDm, DeploymentManagerProperties dmProps){
47         String JavaDoc domainDir = dmProps.getLocation() + dmProps.getDomainName();
48         String JavaDoc resultDir = domainDir + File.separator + "logs" + File.separator + "reporttool"; //N0I18N
49
deleteAllFilesUnder(new File JavaDoc(resultDir), sunDm);
50     }
51          
52     private static void deleteAllFilesUnder(File JavaDoc directory, SunDeploymentManagerInterface sunDm) {
53         try {
54             if (directory != null && !directory.exists())
55                 return;
56                 
57             Set JavaDoc files = getFiles(directory, sunDm);
58             Set JavaDoc dirs = new java.util.HashSet JavaDoc();
59             Set JavaDoc filesList = new java.util.HashSet JavaDoc();
60             for (Iterator JavaDoc i = files.iterator(); i.hasNext();) {
61                 File JavaDoc actualFile = new File JavaDoc(directory, i.next().toString());
62                 if(actualFile.isDirectory()){
63                     dirs.add(actualFile);
64                 } else
65                     filesList.add(actualFile);
66             }
67                         
68             deleteFiles(directory, filesList);
69             deleteFiles(directory, dirs);
70             directory.delete();
71         } catch (Exception JavaDoc ex) {
72             return;
73         }
74     }
75
76     private static Set JavaDoc getFiles(File JavaDoc resultDir, SunDeploymentManagerInterface sunDm) throws Exception JavaDoc {
77         Set JavaDoc result = null;
78         ClassLoader JavaDoc origClassLoader = Thread.currentThread().getContextClassLoader();
79         try{
80             Class JavaDoc[] argClass = new Class JavaDoc[1];
81             argClass[0] = File JavaDoc.class;
82             Object JavaDoc[] argObject = new Object JavaDoc[1];
83             argObject[0] = resultDir;
84             
85             Class JavaDoc controllerUtilClass = ServerLocationManager.getNetBeansAndServerClassLoader(sunDm.getPlatformRoot()).
86                     loadClass("com.sun.enterprise.util.FileUtil"); //NOI18N
87

88             
89             Method JavaDoc method = controllerUtilClass.getMethod("getAllFilesAndDirectoriesUnder", argClass);
90             
91             Thread.currentThread().setContextClassLoader(
92                     ServerLocationManager.getNetBeansAndServerClassLoader(sunDm.getPlatformRoot()));
93             
94             result = (Set JavaDoc)method.invoke(controllerUtilClass.newInstance(), argObject);
95             
96         } catch (Exception JavaDoc e){
97             throw e;
98         } finally {
99             Thread.currentThread().setContextClassLoader(origClassLoader);
100         }
101         return result;
102     }
103     
104     private static void deleteFiles(File JavaDoc directory, Set JavaDoc files){
105         for (Iterator JavaDoc i = files.iterator(); i.hasNext();) {
106             File JavaDoc next = (File JavaDoc) i.next();
107             next.delete();
108         }
109     }
110     
111 }
112
Popular Tags