1 7 8 package org.jboss.deployment.scanner; 9 10 import java.io.UnsupportedEncodingException ; 11 import java.net.MalformedURLException ; 12 import java.net.URL ; 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.LinkedList ; 18 import java.util.List ; 19 import java.util.Set ; 20 import java.util.StringTokenizer ; 21 22 import org.jboss.deployment.IncompleteDeploymentException; 23 import org.jboss.deployment.NetBootFile; 24 import org.jboss.deployment.NetBootHelper; 25 import org.jboss.util.NullArgumentException; 26 27 62 63 68 public class HttpURLDeploymentScanner 69 extends URLDeploymentScanner 70 implements HttpURLDeploymentScannerMBean 71 { 72 73 75 77 protected String defaultHttpDirectoryListerUrl = null; 78 protected String httpDirectoryDownload = null; 79 80 protected HttpLister defaultHttpLister = null; 81 82 protected HashMap scannedHttpUrls = new HashMap (); 84 86 88 public HttpURLDeploymentScanner () { super (); } 89 90 92 100 public String getDefaultHttpDirectoryListerUrl () 101 { 102 if (defaultHttpDirectoryListerUrl == null) 103 defaultHttpDirectoryListerUrl = NetBootHelper.getDefaultListUrl (); 104 105 return this.defaultHttpDirectoryListerUrl; 106 } 107 108 111 public void setDefaultHttpDirectoryListerUrl (String url) 112 { 113 this.defaultHttpDirectoryListerUrl = url; 114 } 115 116 122 public String getDefaultHttpDirectoryDownloadUrl () 123 { 124 if (httpDirectoryDownload == null) 125 httpDirectoryDownload = NetBootHelper.getDefaultDownloadUrl (); 126 127 return this.httpDirectoryDownload; 128 } 129 130 133 public void setDefaultHttpDirectoryDownloadUrl (String url) 134 { 135 this.httpDirectoryDownload = url; 136 } 137 138 141 public void setURLList(final List list) 142 { 143 } 145 146 149 public void setURLs(final String listspec) throws MalformedURLException 150 { 151 if (listspec == null) 152 throw new NullArgumentException("listspec"); 153 154 boolean debug = log.isDebugEnabled(); 155 156 List fileList = new LinkedList (); 157 158 StringTokenizer stok = new StringTokenizer (listspec, ","); 159 while (stok.hasMoreTokens()) 160 { 161 String urlspec = stok.nextToken().trim(); 162 163 if (debug) 164 { 165 log.debug("Adding URL from spec: " + urlspec); 166 } 167 168 if (urlspec.startsWith ("file:") || urlspec.startsWith ("http:")) 173 { 174 URL url = makeURL(urlspec); 175 if (debug) log.debug("File URL: " + url); 176 fileList.add(url); 177 } 178 else 179 { 180 URL url = makeURL(urlspec); 181 if (debug) log.debug("HTTP URL: " + url); 182 183 addHttpDeployment (urlspec, this.getDefaultHttpDirectoryLister ()); 184 } 185 } 186 187 super.setURLList(fileList); 191 } 192 193 public synchronized void scan() throws Exception 194 { 195 196 super.scan (); 199 200 boolean trace = log.isTraceEnabled(); 201 202 if (trace) log.trace("Scanning for new http deployments"); 204 205 synchronized (scannedHttpUrls) 207 { 208 Iterator listers = this.getAllDeploymentListers().iterator (); 211 while (listers.hasNext ()) 212 { 213 HttpLister lister = (HttpLister)listers.next (); 214 215 Iterator deployments = this.getHttpDeploymentsForLister (lister).iterator (); 218 while (deployments.hasNext ()) 219 { 220 HttpDeploymentFolder deploymentFolder = (HttpDeploymentFolder)deployments.next (); 223 scanRemoteDirectory (deploymentFolder); 224 } 225 } 226 } 227 228 if (trace) log.trace("Scanning existing deployments for removal or modification"); 234 235 List removed = new LinkedList (); 236 List modified = new LinkedList (); 237 238 Iterator listers = this.getAllDeploymentListers().iterator (); 239 while (listers.hasNext ()) 240 { 241 HttpLister lister = (HttpLister)listers.next (); 242 243 Iterator deployments = this.getHttpDeploymentsForLister (lister).iterator (); 244 while (deployments.hasNext ()) 245 { 246 HttpDeploymentFolder deploymentFolder = (HttpDeploymentFolder)deployments.next (); 247 248 NetBootFile[] remoteFiles = NetBootHelper.listFilesFromDirectory (deploymentFolder.getCompleteListingUrl ());; 251 252 Iterator deployedFiles = deploymentFolder.getDeployedFiles ().iterator (); 253 while (deployedFiles.hasNext ()) 254 { 255 DeployedRemoteURL deployed = (DeployedRemoteURL)deployedFiles.next (); 256 257 NetBootFile alreadyDeployed = findFileWithName (deployed.getFile ().getName (), remoteFiles); 258 259 if (alreadyDeployed == null) 260 { 261 removed.add (deployed); 262 } 263 else if (alreadyDeployed.LastModified () > deployed.getFile ().LastModified ()) 264 { 265 deployed.updateFile (alreadyDeployed); modified.add (deployed); 267 } 268 269 } 270 271 } 272 } 273 274 for (Iterator iter=removed.iterator(); iter.hasNext();) 275 { 276 DeployedRemoteURL du = (DeployedRemoteURL)iter.next(); 277 undeploy(du); 278 } 279 280 for (Iterator iter=modified.iterator(); iter.hasNext();) 281 { 282 DeployedRemoteURL du = (DeployedRemoteURL)iter.next(); 283 undeploy(du); 284 deploy(du); 285 } 286 287 if( lastIncompleteDeploymentException != null ) 289 { 290 try 291 { 292 Object [] args = {}; 293 String [] sig = {}; 294 Object o = getServer().invoke(getDeployer(), 295 "checkIncompleteDeployments", args, sig); 296 } 297 catch (Exception e) 298 { 299 log.error(e); 300 } 301 } 302 } 303 304 305 306 307 protected void scanRemoteDirectory(HttpDeploymentFolder httpFolder) throws Exception 308 { 309 boolean trace = log.isTraceEnabled(); 310 311 if (trace) log.trace("Scanning directory: " + httpFolder.getRelativeFolder ()); 312 313 NetBootFile[] content = null; 314 try 315 { 316 content = NetBootHelper.listFilesFromDirectory (httpFolder.getCompleteListingUrl ()); 317 } 318 catch (Exception e) 319 { 320 log.trace(e); 321 return; 322 } 323 324 332 333 List list = new LinkedList (); 335 HashMap linkNameToObjects = new HashMap (); 336 337 for (int i = 0; i < content.length; i++) 338 { 339 if (trace) log.trace("Checking deployment file: " + content[i]); 340 341 NetBootFile found = findFileWithName(content[i].getName (), httpFolder.getDeployedFiles()); 344 if (found == null) 345 { 346 URL target = httpFolder.getUrlForFile (content[i]); 347 list.add(target); 348 linkNameToObjects.put (target, content[i]); 349 } 350 } 351 352 if (sorter != null) 356 { 357 updateSorter(); 358 Collections.sort(list, sorter); 359 } 360 361 Iterator iter = list.iterator(); 363 while (iter.hasNext()) 364 { 365 URL url = (URL )iter.next(); 366 NetBootFile dep = (NetBootFile)linkNameToObjects.get (url); 367 368 deploy( new DeployedRemoteURL(httpFolder, dep) ); 369 iter.remove(); 370 if (sorter != null && iter.hasNext() && updateSorter()) 371 { 372 Collections.sort(list, sorter); 373 iter = list.iterator(); 374 } 375 } 376 } 377 378 379 381 383 385 387 protected void undeploy(DeployedRemoteURL deployedUrl) 388 { 389 URL url = null; 390 try 391 { 392 url = deployedUrl.getFolder ().getUrlForFile (deployedUrl.getFile ()); 393 394 if (log.isTraceEnabled()) log.trace("Undeploying: " + url); 395 396 deployer.undeploy(url); 397 398 deployedUrl.getFolder ().removeDeployedFile (deployedUrl); 399 } 400 catch (Exception e) 401 { 402 log.error("Failed to undeploy: " + url, e); 403 } 404 } 405 406 protected void deploy(DeployedRemoteURL deployedUrl) 407 throws MalformedURLException 408 { 409 410 URL url = deployedUrl.getFolder ().getUrlForFile (deployedUrl.getFile ()); 411 412 if( url == null ) return; 413 414 if (log.isTraceEnabled()) log.trace("Deploying: " + url); 415 416 try 417 { 418 deployer.deploy(url); 419 } 420 catch (IncompleteDeploymentException e) 421 { 422 lastIncompleteDeploymentException = e; 423 } 424 catch (Exception e) 425 { 426 log.error("Failed to deploy: " + url, e); 427 } 428 429 deployedUrl.getFolder ().addDeployedFile (deployedUrl); 430 } 431 432 435 protected NetBootFile findFileWithName (String name, NetBootFile[] files) 436 { 437 for (int i=0; i<files.length; i++) 438 { 439 if (files[i].getName ().equals (name)) 440 return files[i]; 441 } 442 return null; 443 } 444 445 protected NetBootFile findFileWithName (String name, List deployedRemoteURL) 446 { 447 NetBootFile[] tmp = new NetBootFile[deployedRemoteURL.size ()]; 448 Iterator iter = deployedRemoteURL.iterator (); 449 int i=0; 450 while (iter.hasNext()) 451 { 452 DeployedRemoteURL url = (DeployedRemoteURL)iter.next (); 453 tmp[i] = url.getFile (); 454 i++; 455 } 456 return findFileWithName(name, tmp); 457 } 458 459 462 protected synchronized void addHttpDeployment (String relativeName, HttpLister lister) 463 { 464 ArrayList deps = (ArrayList )scannedHttpUrls.get (lister); 465 if (deps == null) 466 { 467 deps = new ArrayList (); 468 scannedHttpUrls.put (lister, deps); 469 } 470 deps.add (new HttpDeploymentFolder (relativeName, lister)); 471 } 472 473 protected List getHttpDeploymentsForLister (HttpLister lister) 474 { 475 ArrayList deps = (ArrayList )scannedHttpUrls.get (lister); 476 if (deps == null) 477 { 478 deps = new ArrayList (); 479 } 480 return deps; 481 } 482 483 protected Set getAllDeploymentListers () 484 { 485 return this.scannedHttpUrls.keySet (); 486 } 487 488 491 protected HttpLister getDefaultHttpDirectoryLister () 492 { 493 if (defaultHttpLister == null) 494 defaultHttpLister = new HttpLister (getDefaultHttpDirectoryDownloadUrl(), 495 getDefaultHttpDirectoryListerUrl()); 496 497 return this.defaultHttpLister; 498 } 499 500 502 504 protected class HttpLister 505 { 506 public String downloadUrl = null; 507 public String httpListerUrl = null; 508 509 public HttpLister (String download, String list) 510 { 511 downloadUrl = download; 512 httpListerUrl = list; 513 } 514 515 public String getDownloadUrl () { return this.downloadUrl; } 516 public String getHttpListerUrl () { return this.httpListerUrl; } 517 518 public int hashCode () { return this.httpListerUrl.hashCode (); } 519 520 public boolean equals (Object obj) 521 { 522 if (obj instanceof HttpLister) 523 return ((HttpLister)obj).httpListerUrl.equals (this.httpListerUrl); 524 else 525 return false; 526 } 527 528 } 529 530 protected class HttpDeploymentFolder 531 { 532 public String folder = null; 533 public HttpLister myLister = null; 534 public ArrayList deployedFiles = new ArrayList (); 535 536 public HttpDeploymentFolder (String folder, HttpLister accessor) 537 { 538 this.folder = folder; 539 this.myLister = accessor; 540 } 541 542 public String getRelativeFolder () { return this.folder; } 543 public HttpLister getAssociatedLister () { return this.myLister; } 544 545 public void addDeployedFile (DeployedRemoteURL file) { deployedFiles.add (file); } 546 public void removeDeployedFile (DeployedRemoteURL file) { deployedFiles.remove (file); } 547 548 public List getDeployedFiles () { return this.deployedFiles; } 549 550 public String getCompleteListingUrl () 551 throws UnsupportedEncodingException 552 { 553 return NetBootHelper.buildListUrlForFolder (this.myLister.getHttpListerUrl (), this.folder); 554 } 555 556 public URL getUrlForFile (NetBootFile file) 557 throws MalformedURLException 558 { 559 return new URL (NetBootHelper.buildDownloadUrlForFile (this.myLister.getDownloadUrl (), 560 this.folder, 561 file.getName ())); 562 } 563 564 } 565 566 protected class DeployedRemoteURL 567 { 568 HttpDeploymentFolder folder = null; 569 NetBootFile file = null; 570 571 public DeployedRemoteURL (HttpDeploymentFolder folder, NetBootFile file) 572 { 573 this.folder = folder; 574 this.file = file; 575 } 576 577 public HttpDeploymentFolder getFolder () { return this.folder; } 578 public NetBootFile getFile () { return this.file; } 579 580 public void updateFile (NetBootFile newer) { this.file = newer; } 581 } 582 583 } 584 | Popular Tags |