Crossfire JXClient, Trunk
FileMatch.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of Oracle nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * This source code is provided to illustrate the usage of a given feature
34  * or technique and has been deliberately simplified. Additional steps
35  * required for a production-quality application, such as security checks,
36  * input validation and proper error handling, might not be present in
37  * this sample code.
38  */
39 
40 
41 package com.sun.jmx.examples.scandir.config;
42 
43 import java.io.File;
44 import java.io.FileFilter;
45 import java.util.Arrays;
46 import java.util.Date;
47 import java.util.logging.Logger;
48 import javax.xml.bind.annotation.XmlElement;
49 import javax.xml.bind.annotation.XmlRootElement;
50 
70 @XmlRootElement(name="FileFilter",
71  namespace=XmlConfigUtils.NAMESPACE)
72 public class FileMatch implements FileFilter {
73 
74  //
75  // A logger for this class.
76  //
77  // private static final Logger LOG =
78  // Logger.getLogger(FileMatch.class.getName());
79 
83  private String directoryPattern;
84 
88  private String filePattern;
89 
93  private long sizeExceedsMaxBytes;
94 
99  private Date lastModifiedAfter;
100 
105  private Date lastModifiedBefore;
106 
110  public FileMatch() {
111  }
112 
129  @XmlElement(name="DirectoryPattern",namespace=XmlConfigUtils.NAMESPACE)
130  public String getDirectoryPattern() {
131  return this.directoryPattern;
132  }
133 
143  public void setDirectoryPattern(String directoryPattern) {
144  this.directoryPattern = directoryPattern;
145  }
146 
160  @XmlElement(name="FilePattern",namespace=XmlConfigUtils.NAMESPACE)
161  public String getFilePattern() {
162  return this.filePattern;
163  }
164 
173  public void setFilePattern(String filePattern) {
174  this.filePattern = filePattern;
175  }
176 
184  @XmlElement(name="SizeExceedsMaxBytes",namespace=XmlConfigUtils.NAMESPACE)
185  public long getSizeExceedsMaxBytes() {
186  return this.sizeExceedsMaxBytes;
187  }
188 
196  public void setSizeExceedsMaxBytes(long sizeLimitInBytes) {
197  this.sizeExceedsMaxBytes = sizeLimitInBytes;
198  }
199 
208  @XmlElement(name="LastModifiedAfter",namespace=XmlConfigUtils.NAMESPACE)
209  public Date getLastModifiedAfter() {
210  return (lastModifiedAfter==null)?null:(Date)lastModifiedAfter.clone();
211  }
212 
220  public void setLastModifiedAfter(Date lastModifiedAfter) {
221  this.lastModifiedAfter =
222  (lastModifiedAfter==null)?null:(Date)lastModifiedAfter.clone();
223  }
224 
233  @XmlElement(name="LastModifiedBefore",namespace=XmlConfigUtils.NAMESPACE)
234  public Date getLastModifiedBefore() {
235  return (lastModifiedBefore==null)?null:(Date)lastModifiedBefore.clone();
236  }
237 
245  public void setLastModifiedBefore(Date lastModifiedBefore) {
246  this.lastModifiedBefore =
247  (lastModifiedBefore==null)?null:(Date)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  //
261  public boolean accept(File 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(f.lastModified())))
288  return false;
289 
290  // Check whether f was last modified before lastModifiedBefore
291  if (lastModifiedBefore != null &&
292  lastModifiedBefore.before(new Date(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[] toArray() {
301  final Object[] thisconfig = {
302  directoryPattern, filePattern, lastModifiedAfter,
303  lastModifiedBefore, sizeExceedsMaxBytes
304  };
305  return thisconfig;
306  }
307 
308  @Override
309  public boolean equals(Object o) {
310  if (o == this) return true;
311  if (!(o instanceof FileMatch)) return false;
312  final FileMatch other = (FileMatch)o;
313  final Object[] thisconfig = toArray();
314  final Object[] otherconfig = other.toArray();
315  return Arrays.deepEquals(thisconfig,otherconfig);
316  }
317 
318  @Override
319  public int hashCode() {
320  return Arrays.deepHashCode(toArray());
321  }
322 
323 }
com.sun.jmx.examples.scandir.config.FileMatch.lastModifiedBefore
Date lastModifiedBefore
Definition: FileMatch.java:105
com.sun.jmx.examples.scandir.config.FileMatch.setFilePattern
void setFilePattern(String filePattern)
Definition: FileMatch.java:173
com.sun.jmx.examples.scandir.config.FileMatch.lastModifiedAfter
Date lastModifiedAfter
Definition: FileMatch.java:99
com.sun.jmx.examples.scandir.config.FileMatch.filePattern
String filePattern
Definition: FileMatch.java:88
com.sun.jmx.examples.scandir.config.FileMatch.FileMatch
FileMatch()
Definition: FileMatch.java:110
com.sun.jmx.examples.scandir.config.XmlConfigUtils
Definition: XmlConfigUtils.java:62
com.sun.jmx.examples.scandir.config.FileMatch.sizeExceedsMaxBytes
long sizeExceedsMaxBytes
Definition: FileMatch.java:93
com.sun.jmx.examples.scandir.config.FileMatch.setLastModifiedBefore
void setLastModifiedBefore(Date lastModifiedBefore)
Definition: FileMatch.java:245
com.sun.jmx.examples.scandir.config.FileMatch.accept
boolean accept(File f)
Definition: FileMatch.java:261
com.sun.jmx.examples.scandir.config.FileMatch.toArray
Object[] toArray()
Definition: FileMatch.java:300
com.sun.jmx.examples.scandir.config.XmlConfigUtils.NAMESPACE
static final String NAMESPACE
Definition: XmlConfigUtils.java:69
com.sun.jmx.examples.scandir.config.FileMatch.setSizeExceedsMaxBytes
void setSizeExceedsMaxBytes(long sizeLimitInBytes)
Definition: FileMatch.java:196
com.sun.jmx.examples.scandir.config.FileMatch.equals
boolean equals(Object o)
Definition: FileMatch.java:309
com.sun.jmx.examples.scandir.config.FileMatch
Definition: FileMatch.java:72
com.sun.jmx.examples.scandir.config.FileMatch.hashCode
int hashCode()
Definition: FileMatch.java:319
com.sun.jmx.examples.scandir.config.FileMatch.setLastModifiedAfter
void setLastModifiedAfter(Date lastModifiedAfter)
Definition: FileMatch.java:220
com.sun.jmx.examples.scandir.config.FileMatch.directoryPattern
String directoryPattern
Definition: FileMatch.java:83
com.sun.jmx.examples.scandir.config.FileMatch.setDirectoryPattern
void setDirectoryPattern(String directoryPattern)
Definition: FileMatch.java:143