1 22 package org.jboss.deployment.scanner; 23 24 import java.io.FileFilter ; 25 import java.io.File ; 26 import java.util.Arrays ; 27 import java.util.Comparator ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.net.URL ; 31 32 import org.jboss.net.protocol.URLLister; 33 34 47 public class DeploymentFilter implements FileFilter , URLLister.URLFilter 48 { 49 52 private static final Comparator reverseComparator = new Comparator () 53 { 54 public int compare(Object o1, Object o2) 55 { 56 int idx1 = ((String ) o1).length(); 57 int idx2 = ((String ) o2).length(); 58 int comp = 0; 59 60 while (comp == 0 && idx1 > 0 && idx2 > 0) 61 comp = ((String ) o1).charAt(--idx1) - ((String ) o2).charAt(--idx2); 62 63 return (comp == 0) ? (idx1 - idx2) : comp; 64 } 65 }; 66 67 68 private static final String [] DEFAULT_PREFIXES = 69 {"#", "%", ",", ".", "_$"}; 70 71 72 private static final String [] DEFAULT_SUFFIXES = 73 {"#", "$", "%", "~", ",v", 74 ".BAK", ".bak", ".old", ".orig", ".tmp", ".rej", ".sh" }; 75 76 77 private static final String [] DEFAULT_MATCHES = 78 {".make.state", ".nse_depinfo", "CVS", "CVS.admin", "RCS", "RCSLOG", 79 "SCCS", "TAGS", "core", "tags"}; 80 81 82 private ArrayList suffixes; 83 84 85 private ArrayList prefixes; 86 87 88 private ArrayList matches; 89 90 91 public DeploymentFilter() 92 { 93 this(DEFAULT_MATCHES, DEFAULT_PREFIXES, DEFAULT_SUFFIXES); 94 } 95 96 101 public DeploymentFilter(String [] matches, String [] prefixes, String [] suffixes) 102 { 103 if( matches == null ) 104 matches = DEFAULT_MATCHES; 105 Arrays.sort(matches); 106 this.matches = new ArrayList (Arrays.asList(matches)); 107 108 if( prefixes == null ) 109 prefixes = DEFAULT_PREFIXES; 110 Arrays.sort(prefixes); 111 this.prefixes = new ArrayList (Arrays.asList(prefixes)); 112 113 if( suffixes == null ) 114 suffixes = DEFAULT_SUFFIXES; 115 Arrays.sort(suffixes, reverseComparator); 116 this.suffixes = new ArrayList (Arrays.asList(suffixes)); 117 } 118 119 public void addPrefix(String prefix) 120 { 121 this.prefixes.add(prefix); 122 Collections.sort(this.prefixes); 123 } 124 public void addPrefixes(String [] prefixes) 125 { 126 this.prefixes.add(Arrays.asList(prefixes)); 127 Collections.sort(this.prefixes); 128 } 129 130 public void delPrefix(String prefix) 131 { 132 this.prefixes.remove(prefix); 133 } 134 public void delPrefixes(String [] prefixes) 135 { 136 this.prefixes.removeAll(Arrays.asList(prefixes)); 137 Collections.sort(this.prefixes); 138 } 139 140 public void addSuffix(String suffix) 141 { 142 this.suffixes.add(suffix); 143 Collections.sort(this.suffixes, reverseComparator); 144 } 145 public void addSuffixes(String [] suffixes) 146 { 147 this.suffixes.add(Arrays.asList(suffixes)); 148 Collections.sort(this.suffixes, reverseComparator); 149 } 150 151 public void delSuffix(String suffix) 152 { 153 this.suffixes.remove(suffix); 154 } 155 public void delSuffixes(String [] suffixes) 156 { 157 this.suffixes.removeAll(Arrays.asList(suffixes)); 158 Collections.sort(this.suffixes, reverseComparator); 159 } 160 161 public String [] getSuffixes() 162 { 163 String [] tmp = new String [suffixes.size()]; 164 suffixes.toArray(tmp); 165 return tmp; 166 } 167 public void setSuffixes(String [] suffixes) 168 { 169 Arrays.sort(suffixes, reverseComparator); 170 this.suffixes.clear(); 171 this.suffixes.addAll(Arrays.asList(suffixes)); 172 } 173 174 public String [] getPrefixes() 175 { 176 String [] tmp = new String [prefixes.size()]; 177 prefixes.toArray(tmp); 178 return tmp; 179 } 180 public void setPrefixes(String [] prefixes) 181 { 182 Arrays.sort(prefixes); 183 this.prefixes.clear(); 184 this.prefixes.addAll(Arrays.asList(prefixes)); 185 } 186 187 public String [] getMatches() 188 { 189 String [] tmp = new String [matches.size()]; 190 matches.toArray(tmp); 191 return tmp; 192 } 193 public void setMatches(String [] matches) 194 { 195 Arrays.sort(matches); 196 this.matches.clear(); 197 this.matches.addAll(Arrays.asList(matches)); 198 } 199 200 209 public boolean accept(File file) 210 { 211 return accept(file.getName()); 212 } 213 214 public boolean accept(URL baseURL, String memberName) 215 { 216 return accept(memberName); 217 } 218 219 private boolean accept(String name) 220 { 221 int index = Collections.binarySearch(matches, name); 223 if (index >= 0) 224 return false; 225 226 index = Collections.binarySearch(prefixes, name); 228 if (index >= 0) 229 return false; 230 if (index < -1) 231 { 232 int firstLessIndex = -2 - index; 234 String prefix = (String ) prefixes.get(firstLessIndex); 235 if( name.startsWith(prefix) ) 237 return false; 238 } 239 240 index = Collections.binarySearch(suffixes, name, reverseComparator); 242 if (index >= 0) 243 return false; 244 if (index < -1) 245 { 246 int firstLessIndex = -2 - index; 248 String suffix = (String ) suffixes.get(firstLessIndex); 249 if( name.endsWith(suffix) ) 251 return false; 252 } 253 254 return true; 256 } 257 } 258 | Popular Tags |