KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > examples > scandir > config > FileMatch


1 /*
2  * FileMatch.java
3  *
4  * Created on July 13, 2006, 3:47 PM
5  *
6  * @(#)FileMatch.java 1.3 06/08/02
7  *
8  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * -Redistribution of source code must retain the above copyright notice, this
14  * list of conditions and the following disclaimer.
15  *
16  * -Redistribution in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
21  * be used to endorse or promote products derived from this software without
22  * specific prior written permission.
23  *
24  * This software is provided "AS IS," without a warranty of any kind. ALL
25  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
26  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
27  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
28  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
29  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
30  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
31  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
32  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
33  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
34  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
35  *
36  * You acknowledge that this software is not designed, licensed or intended
37  * for use in the design, construction, operation or maintenance of any
38  * nuclear facility.
39  */

40
41 package com.sun.jmx.examples.scandir.config;
42
43 import java.io.File JavaDoc;
44 import java.io.FileFilter JavaDoc;
45 import java.util.Arrays JavaDoc;
46 import java.util.Date JavaDoc;
47 import java.util.logging.Logger JavaDoc;
48 import javax.xml.bind.annotation.XmlElement;
49 import javax.xml.bind.annotation.XmlRootElement;
50
51 /**
52  * The <code>FileMatch</code> Java Bean is used to model
53  * the configuration of a {@link FileFilter} which
54  * matches {@link File files} against a set of criteria.
55  * <p>
56  * The <code>FileMatch</code> class also implements
57  * {@link FileFilter} - applying an {@code AND} on all
58  * its conditions. {@code OR} conditions can be obtained
59  * by supplying several instances of <code>FileMatch</code>
60  * to the encapsulating {@link DirectoryScannerConfig}, which
61  * respectively applies an {@code OR} on all its
62  * {@code <FileFilter>} elements.
63  * </p>
64  *
65  * <p>
66  * This class is annotated for XML binding.
67  * </p>
68  * @author Sun Microsystems, 2006 - All rights reserved.
69  */

70 @XmlRootElement(name="FileFilter",
71         namespace=XmlConfigUtils.NAMESPACE)
72 public class FileMatch implements FileFilter JavaDoc {
73     
74     //
75
// A logger for this class.
76
//
77
// private static final Logger LOG =
78
// Logger.getLogger(FileMatch.class.getName());
79

80     /**
81      * A regular expression against which directory names should be matched.
82      */

83     private String JavaDoc directoryPattern;
84
85     /**
86      * A regular expression against which file names should be matched.
87      */

88     private String JavaDoc filePattern;
89
90     /**
91      * File whose size in bytes exceeds this limit will be selected.
92      */

93     private long sizeExceedsMaxBytes;
94
95     /**
96      * A file which will be selected only if it was last modified after
97      * this date
98      */

99     private Date JavaDoc lastModifiedAfter;
100
101     /**
102      * A file which will be selected only if it was last modified before
103      * this date
104      */

105     private Date JavaDoc lastModifiedBefore;
106
107     /**
108      * Creates a new instance of FileMatch
109      */

110     public FileMatch() {
111     }
112  
113     /**
114      * Getter for property directoryPattern. This is a regular expression
115      * against which directory names should be matched.
116      * Applies only to directory, and tells whether a directory should be
117      * included or excluded from the search.
118      * <p>If File.isDirectory() && directoryPattern!=null &&
119      * File.getName().matches(directoryPattern),
120      * then File matches this filter.<br>
121      * If File.isDirectory() && directoryPattern!=null &&
122      * File.getName().matches(directoryPattern)==false,
123      * then File doesn't match this filter.<br>
124      * </p>
125      * @see java.util.regex.Pattern
126      * @see java.lang.String#matches(java.lang.String)
127      * @return Value of property directoryPattern.
128      */

129     @XmlElement(name="DirectoryPattern",namespace=XmlConfigUtils.NAMESPACE)
130     public String JavaDoc getDirectoryPattern() {
131         return this.directoryPattern;
132     }
133
134     /**
135      * Setter for property directoryPattern.
136      * @param directoryPattern New value of property directoryPattern.
137      * This is a regular expression
138      * against which directory names should be {@link #getDirectoryPattern
139      * matched}.
140      * @see java.util.regex.Pattern
141      * @see java.lang.String#matches(java.lang.String)
142      */

143     public void setDirectoryPattern(String JavaDoc directoryPattern) {
144         this.directoryPattern = directoryPattern;
145     }
146
147     /**
148      * Getter for property filePattern. This is a regular expression
149      * against which file names should be matched.
150      * Applies only to files.
151      * <p>
152      * If File.isDirectory()==false && filePattern!=null &&
153      * File.getName().matches(filePattern)==false,
154      * then File doesn't match this filter.
155      * </p>
156      * @see java.util.regex.Pattern
157      * @see java.lang.String#matches(java.lang.String)
158      * @return Value of property filePatern.
159      */

160     @XmlElement(name="FilePattern",namespace=XmlConfigUtils.NAMESPACE)
161     public String JavaDoc getFilePattern() {
162         return this.filePattern;
163     }
164
165     /**
166      * Setter for property filePattern.
167      * @param filePattern New value of property filePattern.
168      * This is a regular expression
169      * against which file names should be {@link #getFilePattern matched}.
170      * @see java.util.regex.Pattern
171      * @see java.lang.String#matches(java.lang.String)
172      */

173     public void setFilePattern(String JavaDoc filePattern) {
174         this.filePattern = filePattern;
175     }
176
177     /**
178      * Getter for property sizeExceedsMaxBytes.
179      * Ignored if 0 or negative. Otherwise, files whose size in bytes does
180      * not exceed this limit will be excluded by this filter.
181      *
182      * @return Value of property sizeExceedsMaxBytes.
183      */

184     @XmlElement(name="SizeExceedsMaxBytes",namespace=XmlConfigUtils.NAMESPACE)
185     public long getSizeExceedsMaxBytes() {
186         return this.sizeExceedsMaxBytes;
187     }
188
189     /**
190      * Setter for property sizeExceedsMaxBytes.
191      * @param sizeLimitInBytes New value of property sizeExceedsMaxBytes.
192      * Ignored if 0 or negative. Otherwise, files whose size in bytes does
193      * not exceed this limit will be excluded by this filter.
194      *
195      */

196     public void setSizeExceedsMaxBytes(long sizeLimitInBytes) {
197         this.sizeExceedsMaxBytes = sizeLimitInBytes;
198     }
199
200     /**
201      * Getter for property {@code lastModifiedAfter}.
202      * A file will be selected only if it was last modified after
203      * {@code lastModifiedAfter}.
204      * <br>This condition is ignored if {@code lastModifiedAfter} is
205      * {@code null}.
206      * @return Value of property {@code lastModifiedAfter}.
207      */

208     @XmlElement(name="LastModifiedAfter",namespace=XmlConfigUtils.NAMESPACE)
209     public Date JavaDoc getLastModifiedAfter() {
210         return (lastModifiedAfter==null)?null:(Date JavaDoc)lastModifiedAfter.clone();
211     }
212
213     /**
214      * Setter for property {@code lastModifiedAfter}.
215      * @param lastModifiedAfter A file will be selected only if it was
216      * last modified after {@code lastModifiedAfter}.
217      * <br>This condition is ignored if {@code lastModifiedAfter} is
218      * {@code null}.
219      */

220     public void setLastModifiedAfter(Date JavaDoc lastModifiedAfter) {
221         this.lastModifiedAfter =
222                 (lastModifiedAfter==null)?null:(Date JavaDoc)lastModifiedAfter.clone();
223     }
224
225     /**
226      * Getter for property {@code lastModifiedBefore}.
227      * A file will be selected only if it was last modified before
228      * {@code lastModifiedBefore}.
229      * <br>This condition is ignored if {@code lastModifiedBefore} is
230      * {@code null}.
231      * @return Value of property {@code lastModifiedBefore}.
232      */

233     @XmlElement(name="LastModifiedBefore",namespace=XmlConfigUtils.NAMESPACE)
234     public Date JavaDoc getLastModifiedBefore() {
235         return (lastModifiedBefore==null)?null:(Date JavaDoc)lastModifiedBefore.clone();
236     }
237
238     /**
239      * Setter for property {@code lastModifiedBefore}.
240      * @param lastModifiedBefore A file will be selected only if it was
241      * last modified before {@code lastModifiedBefore}.
242      * <br>This condition is ignored if {@code lastModifiedBefore} is
243      * {@code null}.
244      */

245     public void setLastModifiedBefore(Date JavaDoc lastModifiedBefore) {
246         this.lastModifiedBefore =
247              (lastModifiedBefore==null)?null:(Date JavaDoc)lastModifiedBefore.clone();
248     }
249
250     // Accepts or rejects a file with regards to the values of the fields
251
// configured in this bean. The accept() method is the implementation
252
// of FileFilter.accept(File);
253
//
254
/**
255      * A file is accepted when all the criteria that have been set
256      * are matched.
257      * @param f The file to match against the configured criteria.
258      * @return {@code true} if the file matches all criteria,
259      * {@code false} otherwise.
260      */

261     public boolean accept(File JavaDoc f) {
262         
263         // Directories are accepted if they match against the directory pattern.
264
//
265
if (f.isDirectory()) {
266             if (directoryPattern != null
267                 && !f.getName().matches(directoryPattern))
268                 return false;
269             else return true;
270         }
271         
272         // If we reach here, the f is not a directory.
273
//
274
// Files are accepted if they match all other conditions.
275

276         // Check whether f matches filePattern
277
if (filePattern != null
278                 && !f.getName().matches(filePattern))
279             return false;
280         
281         // Check whether f exceeeds size limit
282
if (sizeExceedsMaxBytes > 0 && f.length() <= sizeExceedsMaxBytes)
283             return false;
284         
285         // Check whether f was last modified after lastModifiedAfter
286
if (lastModifiedAfter != null &&
287                 lastModifiedAfter.after(new Date JavaDoc(f.lastModified())))
288             return false;
289         
290         // Check whether f was last modified before lastModifiedBefore
291
if (lastModifiedBefore != null &&
292                 lastModifiedBefore.before(new Date JavaDoc(f.lastModified())))
293             return false;
294         
295         // All conditions were met: accept file.
296
return true;
297     }
298
299     // used by equals()
300
private Object JavaDoc[] toArray() {
301         final Object JavaDoc[] thisconfig = {
302             directoryPattern, filePattern, lastModifiedAfter,
303             lastModifiedBefore, sizeExceedsMaxBytes
304         };
305         return thisconfig;
306     }
307     
308     @Override JavaDoc
309     public boolean equals(Object JavaDoc o) {
310         if (o == this) return true;
311         if (!(o instanceof FileMatch)) return false;
312         final FileMatch other = (FileMatch)o;
313         final Object JavaDoc[] thisconfig = toArray();
314         final Object JavaDoc[] otherconfig = other.toArray();
315         return Arrays.deepEquals(thisconfig,otherconfig);
316     }
317     
318     @Override JavaDoc
319     public int hashCode() {
320         return Arrays.deepHashCode(toArray());
321     }
322
323 }
324
Popular Tags