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 */
017package org.apache.wicket.util.file;
018
019import java.io.File;
020import java.io.IOException;
021
022import org.apache.commons.io.FileDeleteStrategy;
023
024/**
025 * A {@link FileDeleteStrategy} that can delete folders.
026 */
027public class FolderDeleteStrategy extends FileDeleteStrategy
028{
029        /**
030         * Construct.
031         */
032        protected FolderDeleteStrategy()
033        {
034                super("folder");
035        }
036
037        @Override
038        public boolean deleteQuietly(final File folder)
039        {
040                if (folder == null || folder.isFile())
041                {
042                        return false;
043                }
044
045                File[] files = folder.listFiles();
046                if (files != null)
047                {
048                        for (File file : files)
049                        {
050                                if (file.isDirectory())
051                                {
052                                        deleteQuietly(file);
053                                }
054                                else
055                                {
056                                        super.deleteQuietly(file);
057                                }
058                        }
059                }
060
061                return super.deleteQuietly(folder);
062        }
063
064        @Override
065        public void delete(final File folder) throws IOException
066        {
067                if (folder == null || folder.isFile())
068                {
069                        return;
070                }
071
072                File[] files = folder.listFiles();
073                if (files != null)
074                {
075                        for (File file : files)
076                        {
077                                super.delete(file);
078                        }
079                }
080        }
081
082
083}