1 22 package org.jboss.deployment; 23 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.Enumeration ; 32 import java.util.jar.JarEntry ; 33 import java.util.jar.JarFile ; 34 35 import javax.management.Notification ; 36 37 import org.jboss.mx.util.MBeanProxyExt; 38 import org.jboss.system.ServiceMBeanSupport; 39 import org.jboss.system.server.ServerConfig; 40 import org.jboss.system.server.ServerConfigLocator; 41 import org.jboss.system.server.ServerConfigUtil; 42 import org.jboss.util.file.JarUtils; 43 import org.jboss.util.stream.Streams; 44 45 57 public abstract class SubDeployerSupport extends ServiceMBeanSupport 58 implements SubDeployerExt, SubDeployerExtMBean 59 { 60 67 protected static final String nativeSuffix; 68 69 74 protected static final String nativePrefix; 75 76 77 protected MainDeployerMBean mainDeployer; 78 79 80 protected File tempDeployDir; 81 82 83 protected String [] enhancedSuffixes; 84 85 86 protected String [] suffixes; 87 88 89 protected int relativeOrder = -1; 90 91 92 private File tempNativeDir; 93 94 95 private boolean loadNative = false; 96 97 103 protected void createService() throws Exception 104 { 105 ServerConfig config = ServerConfigLocator.locate(); 107 tempNativeDir = config.getServerNativeDir(); 108 tempDeployDir = config.getServerTempDeployDir(); 109 loadNative = ServerConfigUtil.isLoadNative(); 110 111 mainDeployer = (MainDeployerMBean) 113 MBeanProxyExt.create(MainDeployerMBean.class, 114 MainDeployerMBean.OBJECT_NAME, 115 server); 116 } 117 118 121 protected void startService() throws Exception 122 { 123 mainDeployer.addDeployer(this); 125 } 126 127 130 protected void stopService() throws Exception 131 { 132 mainDeployer.removeDeployer(this); 134 } 135 136 139 protected void destroyService() throws Exception 140 { 141 mainDeployer = null; 143 tempNativeDir = null; 144 } 145 146 153 protected void setSuffixes(String [] suffixes) 154 { 155 this.suffixes = suffixes; 156 } 157 158 164 protected void setRelativeOrder(int relativeOrder) 165 { 166 this.relativeOrder = relativeOrder; 167 } 168 169 182 public void setEnhancedSuffixes(String [] enhancedSuffixes) 183 { 184 if (enhancedSuffixes != null) 185 { 186 int len = enhancedSuffixes.length; 187 suffixes = new String [len]; 188 189 for (int i = 0; i < len; i++) 190 { 191 SuffixOrderHelper.EnhancedSuffix e = 193 new SuffixOrderHelper.EnhancedSuffix(enhancedSuffixes[i]); 194 195 suffixes[i] = e.suffix; 196 } 197 } 198 this.enhancedSuffixes = enhancedSuffixes; 199 } 200 201 206 public String [] getEnhancedSuffixes() 207 { 208 return enhancedSuffixes; 209 } 210 211 216 public String [] getSuffixes() 217 { 218 return suffixes; 219 } 220 221 226 public int getRelativeOrder() 227 { 228 return relativeOrder; 229 } 230 231 244 public boolean accepts(DeploymentInfo sdi) 245 { 246 String [] acceptedSuffixes = getSuffixes(); 247 if (acceptedSuffixes == null) 248 { 249 return false; 250 } 251 else 252 { 253 String urlPath = sdi.url.getPath(); 254 String shortName = sdi.shortName; 255 boolean checkDir = sdi.isDirectory && !(sdi.isXML || sdi.isScript); 256 257 for (int i = 0; i < acceptedSuffixes.length; i++) 258 { 259 if (urlPath.endsWith(acceptedSuffixes[i]) || 262 (checkDir && shortName.endsWith(acceptedSuffixes[i]))) 263 { 264 return true; 265 } 266 } 267 return false; 268 } 269 } 270 271 282 public void init(DeploymentInfo di) throws DeploymentException 283 { 284 processNestedDeployments(di); 285 286 emitNotification(SubDeployer.INIT_NOTIFICATION, di); 287 } 288 289 295 public void create(DeploymentInfo di) throws DeploymentException 296 { 297 emitNotification(SubDeployer.CREATE_NOTIFICATION, di); 298 } 299 300 306 public void start(DeploymentInfo di) throws DeploymentException 307 { 308 emitNotification(SubDeployer.START_NOTIFICATION, di); 309 } 310 311 317 public void stop(DeploymentInfo di) throws DeploymentException 318 { 319 emitNotification(SubDeployer.STOP_NOTIFICATION, di); 320 } 321 322 328 public void destroy(DeploymentInfo di) throws DeploymentException 329 { 330 emitNotification(SubDeployer.DESTROY_NOTIFICATION, di); 331 } 332 333 336 protected void emitNotification(String type, DeploymentInfo di) 337 { 338 Notification notification = new Notification (type, this, getNextNotificationSequenceNumber()); 339 notification.setUserData(di); 340 sendNotification(notification); 341 } 342 343 350 protected void processNestedDeployments(DeploymentInfo di) throws DeploymentException 351 { 352 log.debug("looking for nested deployments in : " + di.url); 353 if (di.isXML) 354 { 355 return; 357 } 358 359 if (di.isDirectory) 360 { 361 File f = new File (di.url.getFile()); 362 if (!f.isDirectory()) 363 { 364 throw new DeploymentException 366 ("Deploy file incorrectly reported as a directory: " + di.url); 367 } 368 369 addDeployableFiles(di, f); 370 } 371 else 372 { 373 try 374 { 375 URL nestedURL = JarUtils.extractNestedJar(di.localUrl, this.tempDeployDir); 377 JarFile jarFile = new JarFile (nestedURL.getFile()); 378 addDeployableJar(di, jarFile); 379 } 380 catch (Exception e) 381 { 382 log.warn("Failed to add deployable jar: " + di.localUrl, e); 383 384 391 return; 392 } 393 } 394 } 395 396 410 protected boolean isDeployable(String name, URL url) 411 { 412 if (url.getPath().indexOf("META-INF") != -1) 415 { 416 return false; 417 } 418 String [] acceptedSuffixes = mainDeployer.getSuffixOrder(); 419 for (int i = 0; i < acceptedSuffixes.length; i++) 420 { 421 if (name.endsWith(acceptedSuffixes[i])) 422 { 423 return true; 424 } 425 } 426 return (name.endsWith(nativeSuffix) && name.startsWith(nativePrefix)); 428 } 429 430 438 protected void addDeployableFiles(DeploymentInfo di, File dir) 439 throws DeploymentException 440 { 441 File [] files = dir.listFiles(); 442 for (int i = 0; i < files.length; i++) 443 { 444 File file = files[i]; 445 String name = file.getName(); 446 try 447 { 448 URL url = file.toURL(); 449 if (isDeployable(name, url)) 450 { 451 deployUrl(di, url, name); 452 continue; 454 } 455 } 456 catch (MalformedURLException e) 457 { 458 log.warn("File name invalid; ignoring: " + file, e); 459 } 460 if (file.isDirectory()) 461 { 462 addDeployableFiles(di, file); 463 } 464 } 465 } 466 467 474 protected void addDeployableJar(DeploymentInfo di, JarFile jarFile) 475 throws DeploymentException 476 { 477 String urlPrefix = "jar:"+di.localUrl.toString()+"!/"; 478 for (Enumeration e = jarFile.entries(); e.hasMoreElements();) 479 { 480 JarEntry entry = (JarEntry )e.nextElement(); 481 String name = entry.getName(); 482 try 483 { 484 URL url = new URL (urlPrefix+name); 485 if (isDeployable(name, url)) 486 { 487 URL nestedURL = JarUtils.extractNestedJar(url, this.tempDeployDir); 489 deployUrl(di, nestedURL, name); 490 } 491 } 492 catch (MalformedURLException mue) 493 { 494 log.warn("Jar entry invalid; ignoring: " + name, mue); 498 } 499 catch (IOException ex) 500 { 501 log.warn("Failed to extract nested jar; ignoring: " + name, ex); 502 } 503 } 504 } 505 506 protected void deployUrl(DeploymentInfo di, URL url, String name) 507 throws DeploymentException 508 { 509 log.debug("nested deployment: " + url); 510 try 511 { 512 529 if (name.endsWith(nativeSuffix) && name.startsWith(nativePrefix)) 530 { 531 File destFile = new File (tempNativeDir, name); 532 log.info("Loading native library: " + destFile.toString()); 533 534 File parent = destFile.getParentFile(); 535 if (!parent.exists()) { 536 parent.mkdirs(); 537 } 538 539 InputStream in = url.openStream(); 540 OutputStream out = new FileOutputStream (destFile); 541 Streams.copyb(in, out); 542 543 out.flush(); 544 out.close(); 545 in.close(); 546 547 if (loadNative) 548 System.load(destFile.toString()); 549 } 550 else 551 { 552 new DeploymentInfo(url, di, getServer()); 553 } 554 } 555 catch (Exception ex) 556 { 557 throw new DeploymentException 558 ("Could not deploy sub deployment "+name+" of deployment "+di.url, ex); 559 } 560 } 561 562 566 570 protected static class ClassConfiguration 571 extends org.jboss.util.property.PropertyContainer 572 { 573 private String nativeLibToken = "XxX"; 574 575 public ClassConfiguration() 576 { 577 super(SubDeployerSupport.class); 579 580 bindMethod("nativeLibToken"); 582 } 583 584 public void setNativeLibToken(final String token) 585 { 586 this.nativeLibToken = token; 587 } 588 589 public String getNativeLibToken() 590 { 591 return nativeLibToken; 592 } 593 } 594 595 596 protected static final ClassConfiguration CONFIGURATION = new ClassConfiguration(); 597 598 603 606 static 607 { 608 String token = CONFIGURATION.getNativeLibToken(); 611 612 String nativex = System.mapLibraryName(token); 614 int xPos = nativex.indexOf(token); 615 nativePrefix = nativex.substring(0, xPos); 616 nativeSuffix = nativex.substring(xPos + 3); 617 } 618 } 619 | Popular Tags |