001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io;
018:
019: import java.io.File;
020: import java.io.IOException;
021:
022: /**
023: * Strategy for deleting files.
024: * <p>
025: * There is more than one way to delete a file.
026: * You may want to limit access to certain directories, to only delete
027: * directories if they are empty, or maybe to force deletion.
028: * <p>
029: * This class captures the strategy to use and is designed for user subclassing.
030: *
031: * @author Stephen Colebourne
032: * @version $Id: FileDeleteStrategy.java 453903 2006-10-07 13:47:06Z scolebourne $
033: * @since Commons IO 1.3
034: */
035: public class FileDeleteStrategy {
036:
037: /**
038: * The singleton instance for normal file deletion, which does not permit
039: * the deletion of directories that are not empty.
040: */
041: public static final FileDeleteStrategy NORMAL = new FileDeleteStrategy(
042: "Normal");
043: /**
044: * The singleton instance for forced file deletion, which always deletes,
045: * even if the file represents a non-empty directory.
046: */
047: public static final FileDeleteStrategy FORCE = new ForceFileDeleteStrategy();
048:
049: /** The name of the strategy. */
050: private final String name;
051:
052: //-----------------------------------------------------------------------
053: /**
054: * Restricted constructor.
055: *
056: * @param name the name by which the strategy is known
057: */
058: protected FileDeleteStrategy(String name) {
059: this .name = name;
060: }
061:
062: //-----------------------------------------------------------------------
063: /**
064: * Deletes the file object, which may be a file or a directory.
065: * All <code>IOException</code>s are caught and false returned instead.
066: * If the file does not exist or is null, true is returned.
067: * <p>
068: * Subclass writers should override {@link #doDelete(File)}, not this method.
069: *
070: * @param fileToDelete the file to delete, null returns true
071: * @return true if the file was deleted, or there was no such file
072: */
073: public boolean deleteQuietly(File fileToDelete) {
074: if (fileToDelete == null || fileToDelete.exists() == false) {
075: return true;
076: }
077: try {
078: return doDelete(fileToDelete);
079: } catch (IOException ex) {
080: return false;
081: }
082: }
083:
084: /**
085: * Deletes the file object, which may be a file or a directory.
086: * If the file does not exist, the method just returns.
087: * <p>
088: * Subclass writers should override {@link #doDelete(File)}, not this method.
089: *
090: * @param fileToDelete the file to delete, not null
091: * @throws NullPointerException if the file is null
092: * @throws IOException if an error occurs during file deletion
093: */
094: public void delete(File fileToDelete) throws IOException {
095: if (fileToDelete.exists() && doDelete(fileToDelete) == false) {
096: throw new IOException("Deletion failed: " + fileToDelete);
097: }
098: }
099:
100: /**
101: * Actually deletes the file object, which may be a file or a directory.
102: * <p>
103: * This method is designed for subclasses to override.
104: * The implementation may return either false or an <code>IOException</code>
105: * when deletion fails. The {@link #delete(File)} and {@link #deleteQuietly(File)}
106: * methods will handle either response appropriately.
107: * A check has been made to ensure that the file will exist.
108: * <p>
109: * This implementation uses {@link File#delete()}.
110: *
111: * @param fileToDelete the file to delete, exists, not null
112: * @return true if the file was deleteds
113: * @throws NullPointerException if the file is null
114: * @throws IOException if an error occurs during file deletion
115: */
116: protected boolean doDelete(File fileToDelete) throws IOException {
117: return fileToDelete.delete();
118: }
119:
120: //-----------------------------------------------------------------------
121: /**
122: * Gets a string describing the delete strategy.
123: *
124: * @return a string describing the delete strategy
125: */
126: public String toString() {
127: return "FileDeleteStrategy[" + name + "]";
128: }
129:
130: //-----------------------------------------------------------------------
131: /**
132: * Force file deletion strategy.
133: */
134: static class ForceFileDeleteStrategy extends FileDeleteStrategy {
135: /** Default Constructor */
136: ForceFileDeleteStrategy() {
137: super ("Force");
138: }
139:
140: /**
141: * Deletes the file object.
142: * <p>
143: * This implementation uses <code>FileUtils.forceDelete() <code>
144: * if the file exists.
145: *
146: * @param fileToDelete the file to delete, not null
147: * @return Always returns <code>true</code>
148: * @throws NullPointerException if the file is null
149: * @throws IOException if an error occurs during file deletion
150: */
151: protected boolean doDelete(File fileToDelete)
152: throws IOException {
153: FileUtils.forceDelete(fileToDelete);
154: return true;
155: }
156: }
157:
158: }
|