1 22 package org.jboss.system.server.profileservice; 23 24 import java.io.IOException ; 25 import java.net.URI ; 26 import java.net.URISyntaxException ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.StringTokenizer ; 33 import java.util.concurrent.ConcurrentHashMap ; 34 import java.util.concurrent.CopyOnWriteArrayList ; 35 36 import org.jboss.deployers.spi.structure.DeploymentContext; 37 import org.jboss.profileservice.spi.Profile; 38 import org.jboss.profileservice.spi.ProfileKey; 39 import org.jboss.profileservice.spi.ProfileService; 40 import org.jboss.util.JBossObject; 41 import org.jboss.util.StringPropertyReplacer; 42 import org.jboss.virtual.VFS; 43 import org.jboss.virtual.VirtualFile; 44 import org.jboss.virtual.VirtualFileFilter; 45 46 55 public abstract class VFSScanner extends JBossObject 56 { 57 58 private ProfileService profileService; 59 60 61 private ProfileKey profileKey; 62 63 64 private URI serverHomeURI; 65 66 67 private List <URI > uriList = new CopyOnWriteArrayList <URI >(); 68 69 70 private List <VirtualFile> vdfList = new CopyOnWriteArrayList <VirtualFile>();; 71 72 73 private VirtualFileFilter filter; 74 75 76 private boolean doRecursiveSearch = true; 77 78 79 private Map <VirtualFile, String > deployedSet = new ConcurrentHashMap <VirtualFile, String >(); 80 81 86 public ProfileKey getProfileKey() 87 { 88 return profileKey; 89 } 90 91 96 public void setProfileKey(ProfileKey profileKey) 97 { 98 this.profileKey = profileKey; 99 } 100 101 106 public ProfileService getProfileService() 107 { 108 return profileService; 109 } 110 111 116 public void setProfileService(ProfileService profileService) 117 { 118 this.profileService = profileService; 119 } 120 121 128 public void setURIs(final String listspec) throws URISyntaxException , IOException 129 { 130 if (listspec == null) 131 { 132 throw new NullPointerException ("listspec argument cannot be null"); 133 } 134 List <URI > list = new LinkedList <URI >(); 135 136 StringTokenizer stok = new StringTokenizer (listspec, ","); 137 while (stok.hasMoreTokens()) 138 { 139 String urispec = stok.nextToken().trim(); 140 141 log.debug("Adding URI from spec: " + urispec); 142 143 URI uri = makeURI(urispec); 144 145 log.debug("URI: " + uri); 146 147 list.add(uri); 148 } 149 setURIList(list); 150 } 151 152 158 public void setURIList(final List <URI > list) throws IOException 159 { 160 if (list == null) 161 { 162 throw new NullPointerException ("list argument cannot be null"); 163 } 164 165 uriList.clear(); 167 168 for(int n = 0; n < list.size(); n ++) 169 { 170 URI uri = list.get(n); 171 if (uri == null) 172 { 173 throw new IllegalArgumentException ("list element["+n+"] is null"); 174 } 175 addURI(uri); 176 } 177 log.debug("URI list: " + uriList); 178 } 179 180 185 public List <URI > getURIList() 186 { 187 return new ArrayList <URI >(uriList); 188 } 189 190 195 public void setRecursiveSearch(boolean recurse) 196 { 197 doRecursiveSearch = recurse; 198 } 199 200 205 public boolean getRecursiveSearch() 206 { 207 return doRecursiveSearch; 208 } 209 210 218 public void setFilter(String classname) 219 throws ClassNotFoundException , IllegalAccessException , InstantiationException 220 { 221 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 222 Class <VirtualFileFilter> filterClass = (Class <VirtualFileFilter>) loader.loadClass(classname); 223 filter = filterClass.newInstance(); 224 } 225 226 231 public String getFilter() 232 { 233 if (filter == null) 234 { 235 return null; 236 } 237 return filter.getClass().getName(); 238 } 239 240 245 public void setFilterInstance(VirtualFileFilter filter) 246 { 247 this.filter = filter; 248 } 249 250 255 public VirtualFileFilter getFilterInstance() 256 { 257 return filter; 258 } 259 260 266 public void addURI(final URI uri) throws IOException 267 { 268 if (uri == null) 269 { 270 throw new NullPointerException ("uri argument cannot be null"); 271 } 272 if( uriList.add(uri) == true ) 273 { 274 log.debug("Added URI: " + uri); 275 VirtualFile vf = getVFforURI(uri); 276 vdfList.add(vf); 277 } 278 } 279 280 286 public void removeURI(final URI uri) 287 throws IOException 288 { 289 if (uri == null) 290 { 291 throw new NullPointerException ("uri argument cannot be null"); 292 } 293 VirtualFile vf = getVFforURI(uri); 294 vdfList.remove(vf); 295 boolean success = uriList.remove(uri); 296 297 if (success) 298 { 299 log.debug("Removed URI: " + uri); 300 } 301 } 302 303 309 public boolean hasURI(final URI uri) 310 { 311 if (uri == null) 312 { 313 throw new NullPointerException ("uri argument cannot be null"); 314 } 315 return uriList.contains(uri); 316 } 317 318 323 public void start() throws Exception 324 { 325 vdfList.clear(); 328 for (Iterator <URI > i = uriList.iterator(); i.hasNext(); ) 329 { 330 URI uri = i.next(); 331 VirtualFile vf = this.getVFforURI(uri); 332 vdfList.add(vf); 333 } 334 if( profileKey == null ) 335 { 336 profileKey = new ProfileKey("default"); 337 } 338 scan(); 339 } 340 341 346 public synchronized void scan() throws Exception 347 { 348 if (vdfList == null) 349 { 350 throw new IllegalStateException ("not initialized"); 351 } 352 353 boolean trace = log.isTraceEnabled(); 354 355 if (trace) 357 { 358 log.trace("Scanning for new deployments"); 359 } 360 361 List <VirtualFile> toDeployList = new LinkedList <VirtualFile>(); 363 synchronized (vdfList) 364 { 365 for (Iterator i = vdfList.iterator(); i.hasNext();) 366 { 367 VirtualFile component = (VirtualFile)i.next(); 368 if (component.isLeaf()) 369 { 370 toDeployList.add(component); 372 } 373 else 374 { 375 addDeployments(toDeployList, component); 377 } 378 } 379 } 380 381 if (trace) 382 { 383 log.trace("toDeployList"); 384 for (Iterator i = toDeployList.iterator(); i.hasNext();) 385 { 386 log.trace(i.next()); 387 } 388 } 389 LinkedList <VirtualFile> toRemoveList = new LinkedList <VirtualFile>(); 390 LinkedList <VirtualFile> toCheckForUpdateList = new LinkedList <VirtualFile>(); 391 392 synchronized (deployedSet) 393 { 394 for (VirtualFile deployedComponent : deployedSet.keySet()) 396 { 397 if (toDeployList.contains(deployedComponent)) 398 { 399 toCheckForUpdateList.add(deployedComponent); 400 } 401 else 402 { 403 toRemoveList.add(deployedComponent); 404 } 405 } 406 } 407 408 412 for (Iterator i = toRemoveList.iterator(); i.hasNext();) 413 { 414 VirtualFile deployedComponent = (VirtualFile)i.next(); 415 undeploy(deployedComponent); 416 } 417 418 422 ArrayList <VirtualFile> toUpdateList = new ArrayList <VirtualFile>(toCheckForUpdateList.size()); 424 for (Iterator i = toCheckForUpdateList.iterator(); i.hasNext();) 425 { 426 VirtualFile deployedComponent = (VirtualFile)i.next(); 427 if (isModified(deployedComponent)) 428 { 429 if (trace) 430 { 431 log.trace("Re-deploying " + deployedComponent); 432 } 433 toUpdateList.add(deployedComponent); 434 } 435 } 436 437 440 for (int i = toUpdateList.size() - 1; i >= 0; i--) 442 { 443 VirtualFile vf = toUpdateList.get(i); 444 undeploy(vf); 445 } 446 447 for (int i = 0; i < toUpdateList.size(); i++) 449 { 450 VirtualFile vf = toUpdateList.get(i); 451 deploy(vf); 452 } 453 454 458 for (Iterator i = toDeployList.iterator(); i.hasNext();) 460 { 461 VirtualFile component = (VirtualFile)i.next(); 462 463 if (!deployedSet.containsKey(component)) 465 { 466 deploy(component); 467 } 468 469 i.remove(); 471 472 479 } 480 481 497 } 498 499 506 private URI makeURI(String urispec) throws URISyntaxException 507 { 508 urispec = StringPropertyReplacer.replaceProperties(urispec); 510 return serverHomeURI.resolve(urispec); 511 } 512 513 523 private void addDeployments(List <VirtualFile> list, VirtualFile root) 524 throws IOException 525 { 526 List <VirtualFile> components = root.getChildren(); 527 528 for (VirtualFile component : components) 529 { 530 if( filter != null && filter.accepts(component) == false) 532 continue; 533 if (component.isLeaf()) 534 { 535 list.add(component); 536 } 537 else if (component.getName().indexOf('.') == -1 && this.doRecursiveSearch) 539 { 540 addDeployments(list, component); 542 } 543 else 544 { 545 list.add(component); 546 } 547 } 548 } 549 550 555 private void deploy(final VirtualFile component) 556 { 557 if (profileService == null) 559 { 560 return; 561 } 562 if (log.isTraceEnabled()) 563 { 564 log.trace("Deploying: " + component); 565 } 566 567 DeploymentContext deployment = null; 568 try 569 { 570 Profile profile = profileService.getProfile(profileKey); 571 deployment = add(profile, component); 572 } 573 catch (Exception e) 574 { 575 log.debug("Failed to deploy: " + component, e); 576 } 577 578 600 601 if (!deployedSet.containsKey(component)) 602 { 603 deployedSet.put(component, deployment.getName()); 604 } 605 } 606 607 612 private void undeploy(final VirtualFile component) 613 { 614 try 615 { 616 if (log.isTraceEnabled()) 617 { 618 log.trace("Undeploying: " + component); 619 } 620 String name = deployedSet.remove(component); 621 Profile profile = profileService.getProfile(profileKey); 622 remove(profile, name); 623 } 624 catch (Exception e) 625 { 626 log.error("Failed to undeploy: " + component, e); 627 } 628 } 629 630 638 protected abstract DeploymentContext add(Profile profile, VirtualFile file) throws Exception ; 639 640 646 protected abstract void remove(Profile profile, String name); 647 648 655 662 663 671 public boolean isModified(VirtualFile component) throws IOException 672 { 673 ComponentContext cc = null; 676 long deployedLastModified = cc.deployedLastModified; 678 679 long lastModified = (cc.watchComponent != null) ? cc.watchComponent.getLastModified() 682 : component.getLastModified(); 683 684 return deployedLastModified != lastModified; 685 } 686 687 private VirtualFile getVFforURI(URI uri) 688 throws IOException 689 { 690 VFS vfs = VFS.getVFS(uri); 691 return vfs.getRoot(); 692 } 693 694 698 private static class ComponentContext 699 { 700 public VirtualFile watchComponent; 701 public long deployedLastModified; 702 703 public ComponentContext(VirtualFile watchComponent, long deployedLastModified) 704 { 705 this.watchComponent = watchComponent; 706 this.deployedLastModified = deployedLastModified; 707 } 708 } 709 } 710 | Popular Tags |