View Javadoc

1   /*
2    * Copyright 1999,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.log4j.rolling.helper;
18  
19  import java.io.File;
20  
21  
22  /***
23   * File rename action.
24   *
25   * @author Curt Arnold
26   */
27  public final class FileRenameAction extends ActionBase {
28    /***
29     * Source.
30     */
31    private final File source;
32  
33    /***
34     * Destination.
35     */
36    private final File destination;
37  
38    /***
39     * If true, rename empty files, otherwise delete empty files.
40     */
41    private final boolean renameEmptyFiles;
42  
43    /***
44     * Creates an FileRenameAction.
45     *
46     * @param src current file name.
47     * @param dst new file name.
48     * @param renameEmptyFiles if true, rename file even if empty, otherwise delete empty files.
49     */
50    public FileRenameAction(
51      final File src, final File dst, boolean renameEmptyFiles) {
52      source = src;
53      destination = dst;
54      this.renameEmptyFiles = renameEmptyFiles;
55    }
56  
57    /***
58     * Rename file.
59     *
60     * @return true if successfully renamed.
61     */
62    public boolean execute() {
63      return execute(source, destination, renameEmptyFiles);
64    }
65  
66    /***
67     * Rename file.
68     * @param source current file name.
69     * @param destination new file name.
70     * @param renameEmptyFiles if true, rename file even if empty, otherwise delete empty files.
71     * @return true if successfully renamed.
72     */
73    public static boolean execute(
74      final File source, final File destination, boolean renameEmptyFiles) {
75      if (renameEmptyFiles || (source.length() > 0)) {
76        return source.renameTo(destination);
77      }
78  
79      return source.delete();
80    }
81  }