1 40 41 package com.sun.jmx.examples.scandir.config; 42 43 import java.io.File ; 44 import java.io.FileFilter ; 45 import java.util.ArrayList ; 46 import java.util.Arrays ; 47 import java.util.List ; 48 import java.util.logging.Logger ; 49 import javax.xml.bind.annotation.XmlAttribute; 50 import javax.xml.bind.annotation.XmlElement; 51 import javax.xml.bind.annotation.XmlElementRef; 52 import javax.xml.bind.annotation.XmlElementWrapper; 53 import javax.xml.bind.annotation.XmlList; 54 import javax.xml.bind.annotation.XmlRootElement; 55 56 65 @XmlRootElement(name="DirectoryScanner", 66 namespace=XmlConfigUtils.NAMESPACE) 67 public class DirectoryScannerConfig { 68 69 75 81 public enum Action { 82 86 NOTIFY, 87 91 DELETE, 92 96 LOGRESULT }; 97 98 private String name; 102 103 private String rootDirectory; 105 106 private final List <FileMatch> includeFiles = 110 new ArrayList <FileMatch>(); 111 112 private final List <FileMatch> excludeFiles = 116 new ArrayList <FileMatch>(); 117 118 119 private Action[] actions = { Action.NOTIFY, Action.LOGRESULT }; 123 124 135 public DirectoryScannerConfig() { 136 this(null); 137 } 138 139 145 public DirectoryScannerConfig(String name) { 146 this.name = name; 147 rootDirectory = null; 148 } 149 150 155 @XmlElement(name="RootDirectory",namespace=XmlConfigUtils.NAMESPACE) 156 public String getRootDirectory() { 157 return rootDirectory; 158 } 159 160 165 public void setRootDirectory(String root) { 166 rootDirectory=root; 167 } 168 169 170 180 @XmlAttribute(name="name",required=true) 181 public String getName() { 182 return this.name; 183 } 184 185 192 public void setName(String name) { 193 if (this.name == null) 194 this.name = name; 195 else if (name == null) 196 throw new IllegalArgumentException ("name=null"); 197 else if (!name.equals(this.name)) 198 throw new IllegalArgumentException ("name="+name); 199 } 200 201 207 @XmlElementWrapper(name="IncludeFiles", 208 namespace=XmlConfigUtils.NAMESPACE) 209 @XmlElementRef 210 public FileMatch[] getIncludeFiles() { 211 synchronized(includeFiles) { 212 return includeFiles.toArray(new FileMatch[0]); 213 } 214 } 215 216 221 public void addIncludeFiles(FileMatch include) { 222 if (include == null) 223 throw new IllegalArgumentException ("null"); 224 synchronized (includeFiles) { 225 includeFiles.add(include); 226 } 227 } 228 229 236 public void setIncludeFiles(FileMatch[] includeFiles) { 237 synchronized (this.includeFiles) { 238 this.includeFiles.clear(); 239 if (includeFiles == null) return; 240 this.includeFiles.addAll(Arrays.asList(includeFiles)); 241 } 242 } 243 244 250 @XmlElementWrapper(name="ExcludeFiles", 251 namespace=XmlConfigUtils.NAMESPACE) 252 @XmlElementRef 253 public FileMatch[] getExcludeFiles() { 254 synchronized(excludeFiles) { 255 return excludeFiles.toArray(new FileMatch[0]); 256 } 257 } 258 259 266 public void setExcludeFiles(FileMatch[] excludeFiles) { 267 synchronized (this.excludeFiles) { 268 this.excludeFiles.clear(); 269 if (excludeFiles == null) return; 270 this.excludeFiles.addAll(Arrays.asList(excludeFiles)); 271 } 272 } 273 274 279 public void addExcludeFiles(FileMatch exclude) { 280 if (exclude == null) 281 throw new IllegalArgumentException ("null"); 282 synchronized (excludeFiles) { 283 this.excludeFiles.add(exclude); 284 } 285 } 286 287 294 @XmlElement(name="Actions",namespace=XmlConfigUtils.NAMESPACE) 295 @XmlList 296 public Action[] getActions() { 297 return (actions == null)?null:actions.clone(); 298 } 299 300 307 public void setActions(Action[] actions) { 308 this.actions = (actions == null)?null:actions.clone(); 309 } 310 311 328 public FileFilter buildFileFilter() { 329 final FileFilter [] ins = getIncludeFiles(); 330 final FileFilter [] outs = getExcludeFiles(); 331 final FileFilter filter = new FileFilter () { 332 public boolean accept(File f) { 333 boolean result = false; 334 if (ins != null) { 336 for (FileFilter in: ins) { 337 if (!in.accept(f)) continue; 339 340 result=true; 342 break; 343 } 344 } else result= true; 345 if (result == false) return false; 346 347 if (outs != null) { 351 for (FileFilter out: outs) { 352 if (!out.accept(f)) continue; 354 355 result=false; 357 break; 358 } 359 } 360 return result; 361 } 362 }; 363 return filter; 364 } 365 366 private Object [] toArray() { 368 final Object [] thisconfig = { 369 name,rootDirectory,actions,excludeFiles,includeFiles 370 }; 371 return thisconfig; 372 } 373 374 @Override 375 public boolean equals(Object o) { 376 if (o == this) return true; 377 if (!(o instanceof DirectoryScannerConfig)) return false; 378 final DirectoryScannerConfig other = (DirectoryScannerConfig)o; 379 final Object [] thisconfig = toArray(); 380 final Object [] otherconfig = other.toArray(); 381 return Arrays.deepEquals(thisconfig,otherconfig); 382 } 383 384 @Override 385 public int hashCode() { 386 final String key = name; 387 if (key == null) return 0; 388 else return key.hashCode(); 389 } 390 391 392 } 393 | Popular Tags |