1 23 24 29 30 package com.sun.enterprise.deployment.backend; 31 32 import com.sun.enterprise.deployment.ApplicationClientDescriptor; 33 import com.sun.enterprise.deployment.util.ModuleDescriptor; 34 import java.io.*; 35 import java.util.List ; 36 import java.util.ArrayList ; 37 import java.util.Iterator ; 38 import java.util.Set ; 39 import java.util.HashSet ; 40 import java.util.Properties ; 41 import java.util.logging.*; 42 import java.util.jar.JarFile ; 43 import java.util.jar.Manifest ; 44 import java.util.jar.Attributes ; 45 import org.xml.sax.SAXParseException ; 46 import com.sun.ejb.codegen.IASEJBCTimes; 47 import com.sun.enterprise.config.ConfigContext; 48 import com.sun.enterprise.config.ConfigException; 49 import com.sun.enterprise.config.serverbeans.DasConfig; 50 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 51 import com.sun.enterprise.deployment.Application; 52 import javax.enterprise.deploy.shared.ModuleType ; 53 import com.sun.enterprise.deployment.autodeploy.AutoDeployConstants; 54 import com.sun.enterprise.deployment.archivist.ApplicationArchivist; 55 import com.sun.enterprise.deployment.archivist.Archivist; 56 import com.sun.enterprise.deployment.archivist.ArchivistFactory; 57 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 58 import com.sun.enterprise.deployment.deploy.shared.DeploymentPlanArchive; 59 import com.sun.enterprise.deployment.deploy.shared.FileArchive; 60 import com.sun.enterprise.deployment.interfaces.ClientJarMaker; 61 import com.sun.enterprise.deployment.interfaces.DeploymentImplConstants; 62 import com.sun.enterprise.deployment.RootDeploymentDescriptor; 63 import com.sun.enterprise.deployment.util.DeploymentProperties; 64 import com.sun.enterprise.deployment.util.ModuleContentLinker; 65 import com.sun.enterprise.instance.BaseManager; 66 import com.sun.enterprise.instance.InstanceEnvironment; 67 import com.sun.enterprise.instance.ModuleEnvironment; 68 import com.sun.enterprise.loader.EJBClassLoader; 69 import com.sun.enterprise.server.ApplicationServer; 70 import com.sun.enterprise.server.Constants; 71 import com.sun.enterprise.util.ConnectorClassLoader; 72 import com.sun.enterprise.util.diagnostics.Reminder; 73 import com.sun.enterprise.util.FileUtil; 74 import com.sun.enterprise.util.i18n.StringManager; 75 import com.sun.enterprise.util.io.FileUtils; 76 import com.sun.enterprise.util.zip.ZipItem; 77 78 83 public abstract class Deployer 84 { 85 86 public static final String WEB_INF_LIB_PREFIX = "WEB-INF/lib/"; 87 public static final String TEMP_DIRECTORY_SUFFIX = "__temp"; 88 89 private static String CLIENT_JAR_MAKER_CHOICE = System.getProperty( 90 DeploymentImplConstants.CLIENT_JAR_MAKER_CHOICE); 91 92 96 98 public abstract void doRequest() throws IASDeploymentException; 99 100 public abstract void doRequestPrepare() throws IASDeploymentException; 101 public abstract void doRequestFinish() throws IASDeploymentException; 102 103 public abstract void cleanup_internal(); 104 protected abstract List getModuleClasspath(Archivist archivist, 105 AbstractArchive archive) throws IASDeploymentException; 106 107 public final void cleanup() 111 { 112 cleanup_internal(); 113 } 114 115 117 public String toString() 118 { 119 return summary.toString(); 120 } 121 122 125 public static final boolean getKeepFailedStubsValue() 126 { 127 return keepFailedStubsValue; 128 } 129 130 134 Deployer(DeploymentRequest r) throws IASDeploymentException 135 { 136 if(r == null) { 137 String msg = localStrings.getString( 138 "enterprise.deployment.backend.null_deployment_request_object"); 139 throw new IASDeploymentException( msg ); 140 } 141 142 request = r; 143 instanceEnv = request.getInstanceEnv(); 144 145 if(instanceEnv == null) { 146 String msg = localStrings.getString( 147 "enterprise.deployment.backend.null_instanceenvironment_in_deployment_request"); 148 throw new IASDeploymentException( msg ); 149 } 150 151 155 } 156 157 158 160 protected final InstanceEnvironment getInstanceEnv() 161 { 162 return instanceEnv; 163 } 164 165 167 protected final DeploymentRequest getRequest() 168 { 169 return request; 170 } 171 172 174 protected final boolean isDirectory() throws IASDeploymentException 175 { 176 return request.isDirectory(); 177 } 178 179 181 protected final boolean isArchive() throws IASDeploymentException 182 { 183 return request.isArchive(); 184 } 185 186 188 protected void addToSummary(String s) 189 { 190 summary.append(s); 191 summary.append("\n"); 192 } 193 194 196 protected void begin() throws IASDeploymentException 197 { 198 timeDeployStarted = System.currentTimeMillis(); 200 201 try 202 { 203 instanceEnv.verify(); 204 } 205 catch(Exception e) 206 { 207 throw new IASDeploymentException(e); 208 } 209 } 210 211 214 public static String getValidationLevel() throws IASDeploymentException { 215 216 init(); 217 return validationLevel; 218 } 219 220 224 protected Application loadDescriptors() throws IASDeploymentException { 225 226 227 BaseManager manager = getManager(); 229 if (manager==null) { 230 throw new IASDeploymentException( 231 localStrings.getString( 232 "enterprise.deployment.backend.no_manager_registered", 233 request.getType() )); 234 } 235 236 init(); 238 239 240 boolean validateXml = true; 241 242 try { 243 Archivist archivist = ArchivistFactory.getArchivistForType(request.getType().getModuleType()); 244 archivist.setAnnotationProcessingRequested(true); 245 246 String appDir = request.getDeployedDirectory().getAbsolutePath(); 247 FileArchive in = new FileArchive(); 248 in.open(appDir); 249 250 if (request.isVerifying()) { 251 archivist.setRuntimeXMLValidation(true); 252 archivist.setRuntimeXMLValidationLevel("full"); 253 } 254 if (validationLevel.equals("none")) { 255 archivist.setXMLValidation(false); 256 } else { 257 archivist.setXMLValidation(true); 258 archivist.setXMLValidationLevel(validationLevel); 259 } 260 261 if (request.getDeploymentPlan() != null) { 270 DeploymentPlanArchive dpa = new DeploymentPlanArchive(); 271 dpa.open(request.getDeploymentPlan().getAbsolutePath()); 272 273 if (request.isApplication()) { 274 ApplicationArchivist aa = (ApplicationArchivist)archivist; 275 aa.copyInto(request.getDescriptor(), dpa, in, false); 276 } else { 277 archivist.copyInto(dpa, in, false); 278 } 279 } 280 281 283 ClassLoader parent = 285 (Boolean.getBoolean(com.sun.enterprise.server.PELaunch.USE_NEW_CLASSLOADER_PROPERTY)) 286 ? com.sun.enterprise.server.PELaunch.getAppServerChain() 287 : ClassLoader.getSystemClassLoader(); 288 289 DeploymentUtils.setParentClassLoader(parent, getManager(), request); 291 List allClassPaths = request.getParentClasspath(); 293 294 ClassLoader sharedClassLoader = request.getParentClassLoader(); 296 297 List moduleClasspath = getModuleClasspath(archivist, in); 298 request.setModuleClasspath(moduleClasspath); 299 300 allClassPaths.addAll(moduleClasspath); 301 302 final ClassLoader ejbClassLoader = DeploymentUtils.getClassLoader( 303 moduleClasspath, sharedClassLoader, null); 304 305 request.setEjbClassLoader(ejbClassLoader); 307 request.setCompleteClasspath(allClassPaths); 308 309 archivist.setClassLoader(ejbClassLoader); 311 Application application = request.getDescriptor(); 312 if (application!=null && ModuleType.EAR.equals(archivist.getModuleType())) { 313 archivist.readPersistenceDeploymentDescriptors(in, application); 314 archivist.setHandleRuntimeInfo(false); 316 boolean modulesReadSuccess = ((ApplicationArchivist) archivist).readModulesDescriptors(application, in); 317 if (modulesReadSuccess) { 318 archivist.setHandleRuntimeInfo(true); 320 archivist.readRuntimeDeploymentDescriptor(in, application); 321 } else { 322 application=null; 325 } 326 } else { 327 application = ApplicationArchivist.openArchive(archivist, in, true); 328 } 329 if (application==null) { 330 throw new IASDeploymentException(localStrings.getString( 331 "enterprise.deployment.backend.error_loading_dds", 332 new Object [] { request.getName(), " " })); 333 } 334 application.setRegistrationName(request.getName()); 335 336 application.setClassLoader(ejbClassLoader); 337 archivist.setDescriptor(application); 338 339 archivist.performOptionalPkgDependenciesCheck(in); 341 342 archivist.validate(ejbClassLoader); 343 344 if (!application.getWebServiceDescriptors().isEmpty()) { 345 ModuleContentLinker visitor = new ModuleContentLinker(in); 346 application.visit((com.sun.enterprise.deployment.util.ApplicationVisitor) visitor); 347 } 348 request.setDescriptor(application); 349 350 return application; 351 } catch(SAXParseException spe) { 352 IASDeploymentException e = new IASDeploymentException( 353 localStrings.getString( 354 "enterprise.deployment.backend.saxerror_loading_dds", 355 request.getName(),String.valueOf(spe.getLineNumber()), 356 String.valueOf(spe.getColumnNumber()), spe.getLocalizedMessage() )); 357 e.initCause(spe); 358 throw e; 359 } catch(IASDeploymentException e) { 360 throw e; 361 } catch (Throwable t) { 362 IASDeploymentException e = new IASDeploymentException( 363 localStrings.getString( 364 "enterprise.deployment.backend.error_loading_dds", 365 request.getName(), t.getMessage() )); 366 e.initCause(t); 367 throw e; 368 } 369 } 370 371 protected void copyAutodeployedClassFile(File srcFile, File destDir) 372 throws IOException { 373 374 File autodeployDir = srcFile.getParentFile(); 375 String packageNameDirs=""; 376 while (!autodeployDir.getName().equals(AutoDeployConstants.DEFAULT_AUTODEPLOY_DIR)) { 377 packageNameDirs=autodeployDir.getName()+ File.separator + packageNameDirs; 378 autodeployDir = autodeployDir.getParentFile(); 379 } 380 destDir = new File (destDir, packageNameDirs); 381 File destFile = new File (destDir, srcFile.getName()); 382 FileUtils.copy(request.getFileSource().getFile(), destFile); 383 } 384 385 protected final void createClientJar(ZipItem[] clientStubs) throws IASDeploymentException { 386 387 Properties props = request.getOptionalArguments(); 388 String clientJarRequested = props.getProperty(DeploymentProperties.CLIENTJARREQUESTED); 389 390 File appDirectory = request.getGeneratedXMLDirectory(); 392 393 if (appDirectory == null || 395 !FileUtils.safeIsDirectory(appDirectory)) { 396 appDirectory = request.getDeployedDirectory(); 397 } 398 File clientJar = new File (appDirectory, request.getName() 399 + DeploymentImplConstants.ClientJarSuffix); 400 401 if (clientJarRequested!=null && 404 Boolean.valueOf(clientJarRequested).booleanValue()) { 405 406 ClientJarMakerThread.createClientJar( 409 request, clientJar, clientStubs, CLIENT_JAR_MAKER_CHOICE); 410 411 request.setClientJar(clientJar); 413 } else { 414 415 Thread clientJarThread = new ClientJarMakerThread( 417 request, clientJar, clientStubs, CLIENT_JAR_MAKER_CHOICE); 418 clientJarThread.start(); 419 420 } 422 } 423 424 425 431 private static void init() throws IASDeploymentException { 432 433 if (validationLevel!=null) { 435 return; 436 } 437 438 try { 439 ConfigContext ctx = ApplicationServer.getServerContext().getConfigContext(); 440 DasConfig dc = ServerBeansFactory.getDasConfigBean(ctx); 441 validationLevel = dc.getDeployXmlValidation(); 442 } catch(ConfigException ce) { 443 IASDeploymentException e = new IASDeploymentException( 444 localStrings.getString( 445 "enterprise.deployment.backend.cannot_get_validationlevel", 446 ce.getMessage() )); 447 e.initCause(ce); 448 throw e; 449 } 450 451 } 452 453 465 466 void checkRegisteredAnywhereElse(String id) throws IASDeploymentException 467 { 468 String error = BaseManager.isRegisteredAnywhere(instanceEnv, id); 469 470 if(error != null) 471 { 472 String msg = localStrings.getString("enterprise.deployment.backend.nameAlreadyExists", new Object [] { id, error}); 473 throw new IASDeploymentException(msg); 474 } 475 } 476 477 478 480 protected void addEJBCTime(IASEJBCTimes ejbcT) 481 { 482 ejbcTime += ejbcT.getTotalTime(); 483 ejbcTiming = ejbcT; 484 } 485 487 protected void addJSPCTime(long time) 488 { 489 jspcTime += time; 490 } 491 492 protected final void finish() throws IASDeploymentException 494 { 495 long total = System.currentTimeMillis() - timeDeployStarted; 496 497 if(total <= 0) 498 total = 1; 499 500 int percentEJBC = (int)(((double)ejbcTime) / ((double)total) * 100.0); 501 int percentJSPC = (int)(((double)jspcTime) / ((double)total) * 100.0); 502 503 deleteLockFile(); 504 StringBuffer timing = new StringBuffer ("Total Deployment Time: "); 505 timing.append(total); 506 timing.append(" msec, "); 507 if(jspcTime > 0) 508 { 509 timing.append("Total JSP Compile Time: "); 510 timing.append(jspcTime); 511 timing.append(" msec (").append(percentJSPC).append("%), "); 512 } 513 timing.append("Total EJB Compiler Module Time: "); 514 timing.append(ejbcTime); 515 timing.append(" msec, Portion spent EJB Compiling: "); 516 timing.append(percentEJBC); 517 timing.append("%"); 518 519 520 521 if(ejbcTiming != null) 522 timing.append("\nBreakdown of EJBC Module Time: " + ejbcTiming); 523 524 logger.fine(timing.toString()); 525 logger.finer(toString()); 526 527 releaseClassLoader(); 529 } 530 531 533 public final void releaseClassLoader() 534 { 535 try 536 { 537 ClassLoader parentCL = request.getParentClassLoader(); 539 if ( (parentCL != null) && (parentCL instanceof EJBClassLoader) ) 540 { 541 ((EJBClassLoader) parentCL).done(); 542 } 543 544 ClassLoader ejbCL = request.getEjbClassLoader(); 546 if ( (ejbCL != null) && (ejbCL instanceof EJBClassLoader) ) 547 { 548 ((EJBClassLoader) ejbCL).done(); 549 } 550 551 } 552 catch (Exception e) 553 { 554 } 556 557 } 558 559 561 576 protected void DeleteOrKeepFailedStubs(File stubsDir) 577 { 578 if(!FileUtils.safeIsDirectory(stubsDir)) 580 return; 582 if(keepFailedStubsValue) 583 { 584 File failedStubsDir = new File (stubsDir.getPath() + FAILED_SUFFIX); 585 586 FileUtils.whack(failedStubsDir); 588 589 stubsDir.renameTo(failedStubsDir); 590 logger.info(Constants.KEEP_FAILED_STUBS + " is set. Backdoor is open. Saving failed generated ejb files in: " + failedStubsDir.getPath()); 591 } 592 else 593 { 594 FileUtils.whack(stubsDir); 595 } 596 } 597 599 protected String getResourceType(File appDir) 600 { 601 FileInputStream fis = null; 602 try{ 603 File manifestFile = new File (appDir, JarFile.MANIFEST_NAME); 604 fis = new FileInputStream(manifestFile); 605 Manifest manifest = new Manifest (fis); 606 Attributes attrs = manifest.getMainAttributes(); 607 return attrs.getValue(Constants.APPLICATION_TYPE); 608 }catch(Throwable t){ 609 return null; 611 } finally { 612 try { 613 if (fis != null) { 614 fis.close(); 615 } 616 } catch (IOException ioe) { 617 } 618 } 619 } 620 621 623 private final void deleteLockFile() 624 { 625 if(lockFile != null) 626 lockFile.delete(); 627 } 628 629 631 634 protected abstract BaseManager getManager(); 635 636 640 protected boolean checkAppclientsMainClasses() { 641 645 boolean result = true; 646 StringBuilder sb = new StringBuilder (); 647 for (Iterator it = request.getDescriptor().getModules(); it != null && it.hasNext(); ) { 648 ModuleDescriptor md = (ModuleDescriptor) it.next(); 649 if (md.getModuleType() == ModuleType.CAR) { 650 String archiveURI = md.getArchiveUri(); 651 ApplicationClientDescriptor acd = (ApplicationClientDescriptor) md.getDescriptor(); 652 String mainClassName = acd.getMainClassName(); 653 if (mainClassName == null || mainClassName.length() == 0) { 654 result = false; 655 logger.log(Level.WARNING, localStrings.getString("enterprise.deployment.backend.no_main_class"), archiveURI); 656 if (sb.length() > 0) { 657 sb.append(", "); 658 } 659 sb.append(archiveURI); 660 } 661 } 662 } 663 if ( ! result) { 664 668 DeploymentStatus mainClassesNotOKStatus = new DeploymentStatus(request.getCurrentDeploymentStatus()); 669 mainClassesNotOKStatus.setStageDescription(localStrings.getString("enterprise.deployment.backend.appclient_mainclass_checking")); 670 mainClassesNotOKStatus.setStageStatus(DeploymentStatus.WARNING); 671 mainClassesNotOKStatus.setStageStatusMessage(localStrings.getString("enterprise.deployment.backend.appclient_mainclass_checking_failed", sb.toString())); 672 } 673 return result; 674 } 675 676 protected DeploymentRequest request; 677 protected Logger logger = DeploymentLogger.get(); 678 protected StringBuffer summary = new StringBuffer (); 679 private InstanceEnvironment instanceEnv; 680 private ModuleEnvironment moduleEnv; 681 private long timeDeployStarted; 682 private long ejbcTime = 0; 683 private long jspcTime = 0; 684 private File lockFile = null; 685 private static StringManager localStrings = StringManager.getManager( Deployer.class ); 686 private static final boolean keepFailedStubsValue; 687 private final static String FAILED_SUFFIX = "_failed"; 688 private static String validationLevel=null; 689 private IASEJBCTimes ejbcTiming = null; 690 static 691 { 692 String s = DeploymentUtils.getSystemPropertyIgnoreCase(Constants.KEEP_FAILED_STUBS); 693 694 if(s != null && s.compareToIgnoreCase("true") == 0) 695 keepFailedStubsValue = true; 696 else 697 keepFailedStubsValue = false; 698 699 700 } 702 706 protected void generatePolicy() throws IASDeploymentException 707 708 { 709 } 711 712 public abstract void removePolicy() throws IASDeploymentException; 713 } 714 | Popular Tags |