View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.util;
18  
19  import java.io.File;
20  import java.io.FileFilter;
21  import java.io.FileInputStream;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.nio.channels.FileChannel;
25  
26  /***
27   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
28   * @version $Id: DirectoryHelper.java 516448 2007-03-09 16:25:47Z ate $
29   */
30  public class DirectoryHelper
31      extends
32          AbstractFileSystemHelper
33      implements 
34          FileSystemHelper
35  {
36  
37      protected File directory;
38      /***
39       * 
40       */
41      public DirectoryHelper(File directory)
42      {
43          super();
44          if(!directory.exists())
45          {
46              directory.mkdirs();
47          }
48          
49          if(!directory.isDirectory())
50          {
51              throw new IllegalArgumentException("DirectoryHelper(File) requires directory not a file.");
52          }
53          this.directory = directory;
54          
55  
56          
57      }
58  
59      /***
60       * <p>
61       * copyFrom
62       * </p>
63       *
64       * @see org.apache.jetspeed.util.FileSystemHelper#copyFrom(java.io.File)
65       * @param directory
66       * @throws IOException
67       */
68      public void copyFrom( File srcDirectory ) throws IOException
69      {
70          copyFrom(srcDirectory, new FileFilter() {
71              public boolean accept(File pathname)
72              {
73                 return true;
74              }
75             });
76      }
77      
78      /***
79       * <p>
80       * copyFrom
81       * </p>
82       *
83       * @see org.apache.jetspeed.util.FileSystemHelper#copyFrom(java.io.File, java.io.FileFilter)
84       * @param directory
85       * @param fileFilter
86       * @throws IOException
87       */
88      public void copyFrom( File srcDirectory, FileFilter fileFilter ) throws IOException
89      {
90          if(!srcDirectory.isDirectory())
91          {
92              throw new IllegalArgumentException("DirectoryHelper.copyFrom(File) requires directory not a file.");
93          }
94          copyFiles(srcDirectory, directory, fileFilter);        
95  
96      }
97      /***
98       * 
99       * <p>
100      * copyFiles
101      * </p>
102      *
103      * @param srcDir Source directory to copy from.
104      * @param dstDir Destination directory to copy to.
105      * @throws IOException
106      * @throws FileNotFoundException
107 
108      */
109     protected void copyFiles(File srcDir, File dstDir, FileFilter fileFilter) throws IOException
110     {
111         FileChannel srcChannel = null;
112         FileChannel dstChannel = null;
113 
114         try
115         {
116         File[] children = srcDir.listFiles(fileFilter);
117         for(int i=0; i<children.length; i++)
118         {
119             File child = children[i];
120             if(child.isFile())
121             {
122                 File toFile = new File(dstDir, child.getName());
123                 toFile.createNewFile();
124                 srcChannel = new FileInputStream(child).getChannel();
125                 dstChannel = new FileOutputStream(toFile).getChannel();
126                 dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
127                 srcChannel.close();
128                 dstChannel.close();
129             }
130             else
131             {
132                 File newSubDir = new File(dstDir, child.getName());
133                 newSubDir.mkdir();
134                 copyFiles(child, newSubDir, fileFilter);
135             }
136         }
137         }
138         finally
139         {
140             if ( srcChannel != null && srcChannel.isOpen() )
141             {
142                 try
143                 {
144                     srcChannel.close();
145                 }
146                 catch (Exception e)
147                 {
148                     
149                 }
150             }
151             if ( dstChannel != null && dstChannel.isOpen() )
152             {
153                 try
154                 {
155                     dstChannel.close();
156                 }
157                 catch (Exception e)
158                 {
159                     
160                 }
161             }
162         }
163     }
164 
165     /***
166      * <p>
167      * remove
168      * </p>
169      *
170      * @see org.apache.jetspeed.util.FileSystemHelper#remove()
171      * 
172      */
173     public boolean remove()
174     {
175         return doRemove(directory);
176     }
177     
178     /***
179      * 
180      * <p>
181      * doRemove
182      * </p>
183      *
184      * @param file
185      * @return <code>true</code> if the removal war successful, otherwise returns
186      * <code>false</code>.
187      */
188     protected boolean doRemove(File file)
189     {
190         if (file.isDirectory())
191 		{
192 			String[] children = file.list();
193 			for (int i = 0; i < children.length; i++)
194 			{
195 				boolean success = doRemove(new File(file, children[i]));
196 				if (!success)
197 				{
198 					return false;
199 				}
200 			}
201 		}
202 
203 		// The directory is now empty so delete it OR it is a plain file
204 		return file.delete();        
205     }
206 
207     /***
208      * <p>
209      * getRootDirectory
210      * </p>
211      *
212      * @see org.apache.jetspeed.util.FileSystemHelper#getRootDirectory()
213      * @return
214      */
215     public File getRootDirectory()
216     {       
217         return directory;
218     }
219 
220     /***
221      * <p>
222      * close
223      * </p>
224      *
225      * @see org.apache.jetspeed.util.FileSystemHelper#close()
226      * 
227      */
228     public void close()
229     {
230         // TODO Auto-generated method stub
231 
232     }
233     /***
234      * <p>
235      * getSourcePath
236      * </p>
237      *
238      * @see org.apache.jetspeed.util.FileSystemHelper#getSourcePath()
239      * @return
240      */
241     public String getSourcePath()
242     {       
243         return getRootDirectory().getAbsolutePath();
244     }
245         
246 }