001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)DirectoryManager.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.engine.xslt.util;
030:
031: import com.sun.jbi.engine.xslt.TEResources;
032:
033: import java.io.File;
034:
035: /**
036: *
037: *
038: * @author Sun Microsystems, Inc.
039: *
040: */
041: public class DirectoryManager implements TEResources {
042: /**
043: *
044: */
045: private static StringTranslator sTranslator = new StringTranslator(
046: "com.sun.jbi.engine.xslt", null);
047:
048: /**
049: * DOCUMENT ME!
050: *
051: * @param dirName DOCUMENT ME!
052: *
053: * @throws Exception exception
054: */
055: public static void deleteDir(String dirName) throws Exception {
056:
057: File dir = new File(dirName);
058:
059: if (dir.isDirectory()) {
060: String[] children = dir.list();
061:
062: for (int i = 0; i < children.length; i++) {
063:
064: boolean success = (new File(dirName + File.separator
065: + children[i])).delete();
066:
067: if (!success) {
068: throw new Exception(sTranslator
069: .getString(TEResources.DELETE_FAILED));
070: }
071: }
072: }
073:
074: // The directory is now empty so delete it
075: boolean success = dir.delete();
076:
077: if (!success) {
078: throw new Exception(sTranslator
079: .getString(TEResources.DELETE_FAILED));
080: }
081: }
082:
083: /**
084: * DOCUMENT ME!
085: *
086: * @param directoryOrFile NOT YET DOCUMENTED
087: *
088: * @throws Exception exception
089: */
090: public static void deleteRecursively(String directoryOrFile)
091: throws Exception {
092: if (directoryOrFile == null) {
093: return;
094: }
095:
096: File directory = new File(directoryOrFile);
097:
098: try {
099: if (directory.isFile()) {
100: directory.delete();
101:
102: return;
103: }
104:
105: File[] fileList = directory.listFiles();
106:
107: if (fileList == null) {
108: deleteRecursively(directory.getAbsolutePath());
109:
110: return;
111: }
112:
113: if (fileList.length == 0) {
114: directory.delete();
115:
116: return;
117: } else {
118: for (int i = 0; i < fileList.length; i++) {
119: File entry = fileList[i];
120:
121: if (entry.isFile()) {
122: entry.delete();
123: }
124:
125: if (entry.isDirectory()) {
126: deleteRecursively(entry.getAbsolutePath());
127: }
128: }
129:
130: boolean success = directory.delete();
131:
132: if (!success) {
133: throw new Exception(
134: directory.getAbsolutePath()
135: + sTranslator
136: .getString(TEResources.DELETE_FAILED));
137: }
138: }
139: } catch (Exception e) {
140: throw new Exception(directory.getAbsolutePath()
141: + sTranslator.getString(TEResources.DELETE_FAILED));
142: }
143:
144: return;
145: }
146: }
|