1 22 package org.jboss.deployment; 23 24 import java.util.ArrayList ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Set ; 28 import java.util.StringTokenizer ; 29 30 import org.jboss.logging.Logger; 31 32 41 public final class SuffixOrderHelper 42 { 43 45 56 public static final String [] DEFAULT_ENHANCED_SUFFIXES = {}; 57 83 84 85 public static final int DEFAULT_RELATIVE_ORDER = 850; 86 87 88 public static final Logger log = Logger.getLogger(SuffixOrderHelper.class); 89 90 92 93 private final DeploymentSorter sorter; 94 95 96 private String [] enhancedSuffixes; 97 98 99 private List suffixes; 100 101 102 private Set staticSuffixes; 103 104 106 public SuffixOrderHelper(DeploymentSorter sorter) 107 { 108 this.sorter = sorter; 109 this.suffixes = new ArrayList (); 110 this.staticSuffixes = new HashSet (); 111 } 112 113 115 123 public String [] getSuffixOrder() 124 { 125 return sorter.getSuffixOrder(); 126 } 127 128 133 public String [] getEnhancedSuffixes() 134 { 135 return enhancedSuffixes; 136 } 137 138 143 public void setEnhancedSuffixes(String [] enhancedSuffixes) 144 { 145 this.enhancedSuffixes = enhancedSuffixes; 146 } 147 148 158 public void initialize() 159 { 160 if (enhancedSuffixes == null) 162 { 163 enhancedSuffixes = DEFAULT_ENHANCED_SUFFIXES; 164 } 165 166 suffixes.clear(); 168 staticSuffixes.clear(); 169 170 for (int i = 0; i < enhancedSuffixes.length; i++) 172 { 173 EnhancedSuffix es = new EnhancedSuffix(enhancedSuffixes[i]); 174 addSuffix(es); 175 176 staticSuffixes.add(es.suffix); 178 } 179 180 sorter.setSuffixOrder(produceSuffixOrder()); 182 } 183 184 191 public void addEnhancedSuffixes(String [] enhancedSuffixes) 192 { 193 if (enhancedSuffixes != null) 194 { 195 int size = suffixes.size(); 197 198 for (int i = 0; i < enhancedSuffixes.length; i++) 200 { 201 EnhancedSuffix es = new EnhancedSuffix(enhancedSuffixes[i]); 202 addSuffix(es); 203 } 204 if (suffixes.size() > size) 205 { 206 sorter.setSuffixOrder(produceSuffixOrder()); 208 } 209 } 210 } 211 212 219 public void addSuffixes(String [] suffixes, int relativeOrder) 220 { 221 if (suffixes != null) 222 { 223 int size = this.suffixes.size(); 225 226 for (int i = 0; i < suffixes.length; i++) 227 { 228 addSuffix(new EnhancedSuffix(suffixes[i], relativeOrder)); 229 } 230 231 if (this.suffixes.size() > size) 232 { 233 sorter.setSuffixOrder(produceSuffixOrder()); 235 } 236 } 237 } 238 239 243 public void removeEnhancedSuffixes(String [] enhancedSuffixes) 244 { 245 if (enhancedSuffixes != null) 246 { 247 int size = suffixes.size(); 249 250 for (int i = 0; i < enhancedSuffixes.length; i++) 251 { 252 EnhancedSuffix es = new EnhancedSuffix(enhancedSuffixes[i]); 253 254 if (staticSuffixes.contains(es.suffix)) 256 { 257 continue; 258 } 259 else 260 { 261 suffixes.remove(es); 263 } 264 } 265 266 if (this.suffixes.size() < size) 267 { 268 sorter.setSuffixOrder(produceSuffixOrder()); 270 } 271 } 272 } 273 274 278 public void removeSuffixes(String [] suffixes, int relativeOrder) 279 { 280 if (suffixes != null) 281 { 282 int size = this.suffixes.size(); 284 285 for (int i = 0; i < suffixes.length; i++) 286 { 287 if (staticSuffixes.contains(suffixes[i])) 289 { 290 continue; 291 } 292 else 293 { 294 this.suffixes.remove(new EnhancedSuffix(suffixes[i], relativeOrder)); 296 } 297 } 298 299 if (this.suffixes.size() < size) 300 { 301 sorter.setSuffixOrder(produceSuffixOrder()); 303 } 304 } 305 } 306 307 309 312 private String [] produceSuffixOrder() 313 { 314 String [] suffixOrder = new String [suffixes.size()]; 315 316 for (int i = 0; i < suffixes.size(); i++) 317 { 318 suffixOrder[i] = ((EnhancedSuffix)suffixes.get(i)).suffix; 319 } 320 return suffixOrder; 321 } 322 323 335 private void addSuffix(EnhancedSuffix enhancedSuffix) 336 { 337 if (staticSuffixes.contains(enhancedSuffix.suffix)) 339 { 340 log.debug("Static suffix exists; ignoring request for adding enhanced suffix: " + enhancedSuffix); 341 } 342 else 343 { 344 int size = suffixes.size(); 345 346 if (size == 0) 348 { 349 suffixes.add(enhancedSuffix); 350 } 351 else 352 { 353 for (int i = size - 1; i > -1; i--) 355 { 356 EnhancedSuffix entry = (EnhancedSuffix)suffixes.get(i); 357 if (enhancedSuffix.order >= entry.order) 358 { 359 suffixes.add(i + 1, enhancedSuffix); 361 break; 362 } 363 else if (i == 0) 364 { 365 suffixes.add(0, enhancedSuffix); 367 } 368 } 369 } 370 } 371 } 372 373 377 public final static class EnhancedSuffix 378 { 379 380 public String suffix; 381 382 383 public int order; 384 385 388 public EnhancedSuffix(String suffix, int order) 389 { 390 this.suffix = suffix; 391 this.order = order; 392 } 393 394 398 public EnhancedSuffix(String enhancedSuffix) throws IllegalArgumentException 399 { 400 StringTokenizer tokenizer = new StringTokenizer (enhancedSuffix, ":"); 401 int tokens = tokenizer.countTokens(); 402 403 switch (tokens) 404 { 405 case 1: 406 this.order = DEFAULT_RELATIVE_ORDER; 407 this.suffix = enhancedSuffix; 408 break; 409 410 case 2: 411 this.order = Integer.parseInt(tokenizer.nextToken()); 412 this.suffix = tokenizer.nextToken(); 413 break; 414 415 default: 416 throw new IllegalArgumentException ("Cannot parse enhancedSuffix: " + enhancedSuffix); 417 } 418 } 419 420 426 public boolean equals(Object other) 427 { 428 if (other == this) 429 return true; 430 431 if (!(other instanceof EnhancedSuffix)) 432 return false; 433 434 EnhancedSuffix that = (EnhancedSuffix)other; 435 436 return this.suffix.equals(that.suffix) && this.order == that.order; 438 } 439 440 443 public int hashCode() 444 { 445 int result = 17; 446 result = 37 * result + suffix.hashCode(); 447 result = 37 * result + order; 448 return result; 449 } 450 451 454 public String toString() 455 { 456 return order + ":" + suffix; 457 } 458 } 459 } 460 | Popular Tags |