summaryrefslogtreecommitdiffstats
path: root/simple/simple-common/src/main/java/org/simpleframework/common/buffer/FileWatcher.java
blob: 6d1f3b27ca8d708a309864d8e7dd2373f0e0ef85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * FileWatcher.java February 2008
 *
 * Copyright (C) 2008, Niall Gallagher <niallg@users.sf.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package org.simpleframework.common.buffer;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

/**
 * The <code>FileWatcher</code> object is used to create files that
 * are to be used for file buffers. All files created by this are
 * created in the <code>java.io.tmpdir</code> path. Temporary files
 * created in this directory last for a configurable length of time
 * before they are deleted.
 * 
 * @author Niall Gallagher
 */
class FileWatcher implements FileFilter {
   
   /**
    * This is the prefix for the temporary files created.
    */
   private final String prefix;
   
   /**
    * This is the duration the files created will exist for.
    */
   private final long duration;
   
   /**
    * Constructor for the <code>FileWatcher</code> object. This will
    * allow temporary files to exist for five minutes. After this 
    * time the will be removed from the underlying directory. Any
    * request for a new file will result in a sweep of the temporary
    * directory for all matching files, if they have expired they
    * will be deleted.
    * 
    * @param prefix this is the file name prefix for the files
    */
   public FileWatcher(String prefix) {
      this(prefix, 300000);  
   }
   
   /**
    * Constructor for the <code>FileWatcher</code> object. This will
    * allow temporary files to exist for a configurable length of time.
    * After this time the will be removed from the underlying directory. 
    * Any request for a new file will result in a sweep of the temporary
    * directory for all matching files, if they have expired they
    * will be deleted.
    * 
    * @param prefix this is the file name prefix for the files
    * @param duration this is the duration the files exist for
    */
   public FileWatcher(String prefix, long duration) {
      this.duration = duration;
      this.prefix = prefix;
   }
   
   /**
    * This will create a temporary file which can be used as a buffer
    * for <code>FileBuffer</code> objects. The file returned by this
    * method will be created before it is returned, which ensures it
    * can be used as a means to buffer bytes. All files are created
    * in the <code>java.io.tmpdir</code> location, which represents
    * the underlying file system temporary file destination.
    * 
    * @return this returns a created temporary file for buffers
    */
   public File create() throws IOException {
      File path = create(prefix);      
      
      if(!path.isDirectory()) {
         File parent = path.getParentFile();
         
         if(parent.isDirectory()) {
            clean(parent);
         }
      }
      return path;  
   }
   
   /**
    * This will create a temporary file which can be used as a buffer
    * for <code>FileBuffer</code> objects. The file returned by this
    * method will be created before it is returned, which ensures it
    * can be used as a means to buffer bytes. All files are created
    * in the <code>java.io.tmpdir</code> location, which represents
    * the underlying file system temporary file destination.
    * 
    * @param prefix this is the prefix of the file to be created
    * 
    * @return this returns a created temporary file for buffers
    */
   private File create(String prefix) throws IOException {
      File file = File.createTempFile(prefix, null);
      
      if(!file.exists()) {
         file.createNewFile();
      }
      return file;
   }

   /**
    * When this method is invoked the files that match the pattern
    * of the temporary files are evaluated for deletion. Only those
    * files that have not been modified in the duration period can
    * be deleted. This ensures the file system is not exhausted.
    * 
    * @param path this is the path of the file to be evaluated
    */
   private void clean(File path) throws IOException {
      File[] list = path.listFiles(this);
      
      for(File next : list) {
         for(int i = 0; i < 3; i++) {
            if(next.delete()) {
               break;
            }
         }
      }
   }
   
   /**
    * This determines if the file provided is an acceptable file for
    * deletion. Acceptable files are those that match the pattern
    * of files created by this file system object. If the file is
    * a matching file then it is a candidate for deletion.
    * 
    * @param file this is the file to evaluate for deletion
    * 
    * @return this returns true if the file matches the pattern
    */
   public boolean accept(File file) {
      String name = file.getName();
  
      if(file.isDirectory()) {
         return false;
      }
      return accept(file, name);
   }
   
   /**
    * This determines if the file provided is an acceptable file for
    * deletion. Acceptable files are those that match the pattern
    * of files created by this file system object. If the file is
    * a matching file then it is a candidate for deletion.
    * 
    * @param file this is the file to evaluate for deletion
    * @param name this is the name of the file to be evaluated
    * 
    * @return this returns true if the file matches the pattern
    */
   private boolean accept(File file, String name) {
      long time = System.currentTimeMillis();
      long modified = file.lastModified();
      
      if(modified + duration > time) { // not yet expired
         return false;
      }
      return name.startsWith(prefix);
   }
}