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.jetspeed.util;
018:
019: import java.io.File;
020: import java.io.FileFilter;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.nio.channels.FileChannel;
025:
026: /**
027: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
028: * @version $Id: DirectoryHelper.java 516448 2007-03-09 16:25:47Z ate $
029: */
030: public class DirectoryHelper extends AbstractFileSystemHelper implements
031: FileSystemHelper {
032:
033: protected File directory;
034:
035: /**
036: *
037: */
038: public DirectoryHelper(File directory) {
039: super ();
040: if (!directory.exists()) {
041: directory.mkdirs();
042: }
043:
044: if (!directory.isDirectory()) {
045: throw new IllegalArgumentException(
046: "DirectoryHelper(File) requires directory not a file.");
047: }
048: this .directory = directory;
049:
050: }
051:
052: /**
053: * <p>
054: * copyFrom
055: * </p>
056: *
057: * @see org.apache.jetspeed.util.FileSystemHelper#copyFrom(java.io.File)
058: * @param directory
059: * @throws IOException
060: */
061: public void copyFrom(File srcDirectory) throws IOException {
062: copyFrom(srcDirectory, new FileFilter() {
063: public boolean accept(File pathname) {
064: return true;
065: }
066: });
067: }
068:
069: /**
070: * <p>
071: * copyFrom
072: * </p>
073: *
074: * @see org.apache.jetspeed.util.FileSystemHelper#copyFrom(java.io.File, java.io.FileFilter)
075: * @param directory
076: * @param fileFilter
077: * @throws IOException
078: */
079: public void copyFrom(File srcDirectory, FileFilter fileFilter)
080: throws IOException {
081: if (!srcDirectory.isDirectory()) {
082: throw new IllegalArgumentException(
083: "DirectoryHelper.copyFrom(File) requires directory not a file.");
084: }
085: copyFiles(srcDirectory, directory, fileFilter);
086:
087: }
088:
089: /**
090: *
091: * <p>
092: * copyFiles
093: * </p>
094: *
095: * @param srcDir Source directory to copy from.
096: * @param dstDir Destination directory to copy to.
097: * @throws IOException
098: * @throws FileNotFoundException
099:
100: */
101: protected void copyFiles(File srcDir, File dstDir,
102: FileFilter fileFilter) throws IOException {
103: FileChannel srcChannel = null;
104: FileChannel dstChannel = null;
105:
106: try {
107: File[] children = srcDir.listFiles(fileFilter);
108: for (int i = 0; i < children.length; i++) {
109: File child = children[i];
110: if (child.isFile()) {
111: File toFile = new File(dstDir, child.getName());
112: toFile.createNewFile();
113: srcChannel = new FileInputStream(child)
114: .getChannel();
115: dstChannel = new FileOutputStream(toFile)
116: .getChannel();
117: dstChannel.transferFrom(srcChannel, 0, srcChannel
118: .size());
119: srcChannel.close();
120: dstChannel.close();
121: } else {
122: File newSubDir = new File(dstDir, child.getName());
123: newSubDir.mkdir();
124: copyFiles(child, newSubDir, fileFilter);
125: }
126: }
127: } finally {
128: if (srcChannel != null && srcChannel.isOpen()) {
129: try {
130: srcChannel.close();
131: } catch (Exception e) {
132:
133: }
134: }
135: if (dstChannel != null && dstChannel.isOpen()) {
136: try {
137: dstChannel.close();
138: } catch (Exception e) {
139:
140: }
141: }
142: }
143: }
144:
145: /**
146: * <p>
147: * remove
148: * </p>
149: *
150: * @see org.apache.jetspeed.util.FileSystemHelper#remove()
151: *
152: */
153: public boolean remove() {
154: return doRemove(directory);
155: }
156:
157: /**
158: *
159: * <p>
160: * doRemove
161: * </p>
162: *
163: * @param file
164: * @return <code>true</code> if the removal war successful, otherwise returns
165: * <code>false</code>.
166: */
167: protected boolean doRemove(File file) {
168: if (file.isDirectory()) {
169: String[] children = file.list();
170: for (int i = 0; i < children.length; i++) {
171: boolean success = doRemove(new File(file, children[i]));
172: if (!success) {
173: return false;
174: }
175: }
176: }
177:
178: // The directory is now empty so delete it OR it is a plain file
179: return file.delete();
180: }
181:
182: /**
183: * <p>
184: * getRootDirectory
185: * </p>
186: *
187: * @see org.apache.jetspeed.util.FileSystemHelper#getRootDirectory()
188: * @return
189: */
190: public File getRootDirectory() {
191: return directory;
192: }
193:
194: /**
195: * <p>
196: * close
197: * </p>
198: *
199: * @see org.apache.jetspeed.util.FileSystemHelper#close()
200: *
201: */
202: public void close() {
203: // TODO Auto-generated method stub
204:
205: }
206:
207: /**
208: * <p>
209: * getSourcePath
210: * </p>
211: *
212: * @see org.apache.jetspeed.util.FileSystemHelper#getSourcePath()
213: * @return
214: */
215: public String getSourcePath() {
216: return getRootDirectory().getAbsolutePath();
217: }
218:
219: }
|