1 23 24 package com.sun.enterprise.deployment.archivist; 25 26 import static com.sun.enterprise.deployment.io.DescriptorConstants.PERSISTENCE_DD_ENTRY; 27 28 import java.io.*; 29 import java.util.jar.*; 30 import java.util.jar.Attributes.Name; 31 import java.util.zip.*; 32 import java.util.*; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 36 import org.xml.sax.SAXParseException ; 37 import org.xml.sax.InputSource ; 38 39 import javax.enterprise.deploy.shared.ModuleType ; 40 41 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler; 42 import com.sun.enterprise.deployment.annotation.AnnotationProcessor; 43 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException; 44 import com.sun.enterprise.deployment.annotation.ErrorHandler; 45 import com.sun.enterprise.deployment.annotation.ResultType; 46 import com.sun.enterprise.deployment.annotation.factory.AnnotatedElementHandlerFactory; 47 import com.sun.enterprise.deployment.annotation.factory.ScannerFactory; 48 import com.sun.enterprise.deployment.annotation.factory.SJSASFactory; 49 import com.sun.enterprise.deployment.annotation.impl.AnnotationUtils; 50 import com.sun.enterprise.deployment.annotation.impl.DefaultErrorHandler; 51 import com.sun.enterprise.deployment.annotation.ProcessingContext; 52 import com.sun.enterprise.deployment.annotation.ProcessingResult; 53 import com.sun.enterprise.deployment.annotation.Scanner; 54 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 55 import com.sun.enterprise.deployment.deploy.shared.AbstractArchiveFactory; 56 import com.sun.enterprise.deployment.deploy.shared.JarArchiveFactory; 57 import com.sun.enterprise.deployment.deploy.shared.Archive; 58 import com.sun.enterprise.deployment.io.*; 59 import com.sun.enterprise.deployment.*; 60 import com.sun.enterprise.deployment.util.DescriptorVisitor; 61 import com.sun.enterprise.deployment.util.DOLUtils; 62 import com.sun.enterprise.deployment.backend.OptionalPkgDependency; 63 import com.sun.enterprise.deployment.backend.IASDeploymentException; 64 import com.sun.enterprise.deployment.util.ModuleDescriptor; 65 import com.sun.enterprise.deployment.util.TracerVisitor; 66 import com.sun.enterprise.util.LocalStringManagerImpl; 67 import com.sun.enterprise.util.shared.ArchivistUtils; 68 import com.sun.enterprise.util.io.FileUtils; 69 import com.sun.logging.LogDomains; 70 71 78 public abstract class Archivist { 79 protected static final Logger logger = 80 LogDomains.getLogger(LogDomains.DPL_LOGGER); 81 82 static { 83 AnnotationUtils.setLogger(logger); 84 AnnotationUtils.setLoggerTarget("*"); 85 } 86 87 public static String MANIFEST_VERSION_VALUE = "1.0"; 88 89 protected String path; 91 92 protected boolean handleRuntimeInfo = true; 94 95 protected boolean annotationProcessingRequested = false; 97 98 protected Manifest manifest; 100 101 protected AbstractArchiveFactory abstractArchiveFactory= null; 103 104 107 protected PluggableArchivists pa = null; 108 109 private static LocalStringManagerImpl localStrings = 111 new LocalStringManagerImpl(Archivist.class); 112 113 protected ClassLoader classLoader=null; 115 116 private boolean isValidatingXML = true; 118 119 private boolean isValidatingRuntimeXML = false; 121 122 private String validationLevel = "parsing"; 124 125 private String runtimeValidationLevel = "parsing"; 127 128 private ErrorHandler annotationErrorHandler = null; 130 131 private static String WSDL = ".wsdl"; 132 private static String XML = ".xml"; 133 private static String XSD = ".xsd"; 134 135 136 protected static String APPLICATION_EXTENSION = ".ear"; 137 protected static String APPCLIENT_EXTENSION = ".jar"; 138 protected static String WEB_EXTENSION = ".war"; 139 protected static String EJB_EXTENSION = ".jar"; 140 protected static String CONNECTOR_EXTENSION = ".rar"; 141 protected static String UPLOAD_EXTENSION = ".tmp"; 143 144 private static String PROCESS_ANNOTATION_FOR_OLD_DD = 145 "process.annotation.for.old.dd"; 146 147 private static boolean processAnnotationForOldDD = 148 Boolean.getBoolean(PROCESS_ANNOTATION_FOR_OLD_DD); 149 150 151 public Archivist() { 152 abstractArchiveFactory = new JarArchiveFactory(); 155 annotationErrorHandler = new DefaultErrorHandler(); 156 } 157 158 163 protected void initializeContext(Archivist other) { 164 handleRuntimeInfo = other.isHandlingRuntimeInfo(); 165 annotationProcessingRequested = other.isAnnotationProcessingRequested(); 166 isValidatingXML = other.isValidatingXML; 167 validationLevel = other.validationLevel; 168 abstractArchiveFactory = other.getAbstractArchiveFactory(); 169 classLoader = other.classLoader; 170 annotationErrorHandler = other.annotationErrorHandler; 171 } 172 173 177 public void setAbstractArchiveFactory(AbstractArchiveFactory aa) { 178 abstractArchiveFactory = aa; 179 } 180 181 184 public AbstractArchiveFactory getAbstractArchiveFactory() { 185 return abstractArchiveFactory; 186 } 187 188 195 public RootDeploymentDescriptor open(AbstractArchive archive) 196 throws IOException, SAXParseException { 197 198 setManifest(archive.getManifest()); 199 200 RootDeploymentDescriptor descriptor = readDeploymentDescriptors(archive); 201 if (descriptor!=null){ 202 postOpen(descriptor, archive); 203 } 204 return descriptor; 205 } 206 207 214 public RootDeploymentDescriptor open(String path) 215 throws IOException, SAXParseException { 216 217 this.path = path; 218 File file = new File(path); 219 if (!file.exists()) { 220 throw new FileNotFoundException(path); 221 } 222 AbstractArchive abstractArchive = abstractArchiveFactory.openArchive(path); 223 RootDeploymentDescriptor descriptor = open(abstractArchive); 224 225 abstractArchive.close(); 226 227 validate(null); 229 230 return descriptor; 231 } 232 233 237 public RootDeploymentDescriptor open(File file) throws IOException, SAXParseException { 238 return open(file.getAbsolutePath()); 239 } 240 241 246 protected void postStandardDDsRead(RootDeploymentDescriptor descriptor, 247 AbstractArchive archive) throws IOException { 248 } 249 250 255 protected void postAnnotationProcess(RootDeploymentDescriptor descriptor, 256 AbstractArchive archive) throws IOException { 257 } 258 259 264 protected void postRuntimeDDsRead(RootDeploymentDescriptor descriptor, 265 AbstractArchive archive) throws IOException { 266 } 267 268 274 protected void postOpen(RootDeploymentDescriptor descriptor, AbstractArchive archive) 275 throws IOException 276 { 277 } 278 279 288 public RootDeploymentDescriptor readDeploymentDescriptors(AbstractArchive abstractArchive) 289 throws IOException, SAXParseException { 290 291 BundleDescriptor descriptor = (BundleDescriptor)readStandardDeploymentDescriptor(abstractArchive); 293 294 ModuleDescriptor newModule = createModuleDescriptor(descriptor); 295 newModule.setArchiveUri(abstractArchive.getArchiveUri()); 296 297 readWebServicesDescriptor(abstractArchive, descriptor); 298 299 readPersistenceDeploymentDescriptors(abstractArchive, getDescriptor()); 302 303 postStandardDDsRead(descriptor, abstractArchive); 304 305 readAnnotations(abstractArchive, descriptor); 306 postAnnotationProcess(descriptor, abstractArchive); 307 308 readRuntimeDeploymentDescriptor(abstractArchive, descriptor); 310 postRuntimeDDsRead(descriptor, abstractArchive); 311 312 return descriptor; 313 } 314 315 protected void readAnnotations(AbstractArchive abstractArchive, BundleDescriptor descriptor) throws IOException { 316 boolean isFull = false; 321 if (processAnnotationForOldDD) { 322 isFull = descriptor.isFullAttribute(); 323 } else { 324 isFull = descriptor.isFullFlag(); 325 } 326 327 if (!isFull && annotationProcessingRequested 331 && classLoader != null) { 332 try { 333 ProcessingResult result = processAnnotations(descriptor, abstractArchive); 334 if (result != null && 335 ResultType.FAILED.equals(result.getOverallResult())){ 336 DOLUtils.getDefaultLogger().severe(localStrings.getLocalString( 337 "enterprise.deployment.archivist.annotationprocessingfailed", 338 "Annotations processing failed for {0}", 339 new Object []{abstractArchive.getArchiveUri()})); 340 } 341 } catch(NoClassDefFoundError err) { 343 if (DOLUtils.getDefaultLogger().isLoggable(Level.WARNING)) { 344 DOLUtils.getDefaultLogger().warning( 345 "Error in annotation processing: " + err); 346 } 347 } catch(AnnotationProcessorException ex) { 348 DOLUtils.getDefaultLogger().severe(ex.getMessage()); 349 DOLUtils.getDefaultLogger().log(Level.FINE, ex.getMessage(), ex); 350 throw new IllegalStateException (ex); 351 } 352 } else if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 353 DOLUtils.getDefaultLogger().fine("Annotation is not processed for this archive."); 354 } 355 } 356 357 367 public void readPersistenceDeploymentDescriptors( 368 AbstractArchive archive, Descriptor descriptor) 369 throws IOException, SAXParseException { 370 } 373 374 378 public ProcessingResult processAnnotations(BundleDescriptor bundleDesc, 379 AbstractArchive abstractArchive) 380 throws AnnotationProcessorException, IOException { 381 382 AnnotatedElementHandler aeHandler = 383 AnnotatedElementHandlerFactory.createAnnotatedElementHandler( 384 bundleDesc); 385 386 if (aeHandler == null) { 387 return null; 388 } 389 390 Scanner scanner = ScannerFactory.createScanner( 391 bundleDesc, this, abstractArchive, classLoader); 392 if (!scanner.getElements().isEmpty()) { 393 if (bundleDesc.isDDWithNoAnnotationAllowed()) { 394 String ddName = 398 getStandardDDFile().getDeploymentDescriptorPath(); 399 String explodedArchiveName = 400 new File(abstractArchive.getArchiveUri()).getName(); 401 String archiveName = FileUtils.revertFriendlyFilenameExtension( 402 explodedArchiveName); 403 throw new AnnotationProcessorException( 404 localStrings.getLocalString( 405 "enterprise.deployment.oldDDwithAnnotation", 406 "{0} in archive {1} is of version {2}, which cannot support annotations in an application. Please upgrade the deployment descriptor to be a version supported by Java EE 5.0 (or later).", 407 new Object [] {ddName, archiveName, bundleDesc.getSpecVersion()})); 408 } 409 AnnotationProcessor ap = SJSASFactory.getAnnotationProcessor(); 410 ProcessingContext ctx = ap.createContext(); 411 if (annotationErrorHandler != null) { 412 ctx.setErrorHandler(annotationErrorHandler); 413 } 414 ctx.setProcessingInput(scanner); 415 ctx.pushHandler(aeHandler); 416 417 ClassLoader originalBundleClassLoader = null; 420 try { 421 originalBundleClassLoader = bundleDesc.getClassLoader(); 422 } catch(Exception e) { 423 } 425 426 if( originalBundleClassLoader == null ) { 428 bundleDesc.setClassLoader(classLoader); 429 } 430 431 try { 432 return ap.process(ctx); 433 } finally { 434 if( originalBundleClassLoader == null ) { 435 bundleDesc.setClassLoader(null); 436 } 437 } 438 } else { 439 bundleDesc.setFullFlag(true); 440 } 441 return null; 442 } 443 444 453 public Descriptor readStandardDeploymentDescriptor(AbstractArchive abstractArchive) 454 throws IOException, SAXParseException { 455 456 InputStream is = null; 457 458 try { 459 is = abstractArchive.getEntry(getStandardDDFile().getDeploymentDescriptorPath()); 460 if (is!=null) { 461 DeploymentDescriptorFile ddFile = getStandardDDFile(); 462 ddFile.setXMLValidation(getXMLValidation()); 463 ddFile.setXMLValidationLevel(validationLevel); 464 if (abstractArchive.getArchiveUri()!=null) { 465 ddFile.setErrorReportingString(abstractArchive.getArchiveUri()); 466 } 467 Descriptor result = ddFile.read(is); 468 return result; 469 } else { 470 475 return getDefaultBundleDescriptor(); 476 } 477 } finally { 478 if (is != null) { 479 is.close(); 480 } 481 } 482 } 483 484 493 public void readRuntimeDeploymentDescriptor(AbstractArchive abstractArchive, Descriptor descriptor) 494 throws IOException, SAXParseException { 495 496 String ddFileEntryName = getRuntimeDeploymentDescriptorPath(); 498 if (!isHandlingRuntimeInfo() || ddFileEntryName==null) { 499 return; 500 } 501 502 InputStream is = null; 503 try { 504 is = abstractArchive.getEntry(ddFileEntryName); 506 DeploymentDescriptorFile confDD = getConfigurationDDFile(); 507 if (abstractArchive.getArchiveUri()!=null) { 508 confDD.setErrorReportingString(abstractArchive.getArchiveUri()); 509 } 510 511 if (is!=null && confDD!=null) { 512 confDD.setXMLValidation(getRuntimeXMLValidation()); 513 confDD.setXMLValidationLevel(runtimeValidationLevel); 514 confDD.read(descriptor, is); 515 } 516 } finally { 517 if (is != null) { 518 try { 519 is.close(); 520 } catch (IOException ioe) { 521 } 522 } 523 } 524 } 525 526 531 public void readRuntimeDDFromDeploymentPlan( 532 AbstractArchive planArchive, Descriptor descriptor) 533 throws IOException, SAXParseException { 534 535 String runtimeDDPath = getRuntimeDeploymentDescriptorPath(); 537 if (runtimeDDPath==null || planArchive==null) { 538 return; 539 } 540 541 Vector dpEntries = new Vector(); 543 for (Enumeration e = planArchive.entries(); e.hasMoreElements();) { 544 dpEntries.add(e.nextElement()); 545 } 546 547 String entry = runtimeDDPath.substring(runtimeDDPath.lastIndexOf('/')+1); 548 if (dpEntries.contains(entry)) { 549 readRuntimeDDFromDeploymentPlan(entry, planArchive, descriptor); 550 } 551 } 552 553 558 protected void readRuntimeDDFromDeploymentPlan( 559 String entry, AbstractArchive planArchive, Descriptor descriptor) 560 throws IOException, SAXParseException { 561 562 InputStream is = null; 563 try { 564 is = planArchive.getEntry(entry); 565 DeploymentDescriptorFile confDD = getConfigurationDDFile(); 566 if (is!=null && confDD!=null) { 567 if (planArchive.getArchiveUri()!=null) { 568 confDD.setErrorReportingString(planArchive.getArchiveUri()); 569 } 570 confDD.setXMLValidation(getXMLValidation()); 571 confDD.read(descriptor, is); 572 } 573 } finally { 574 if (is != null) { 575 try { 576 is.close(); 577 } catch (IOException ioe) { } 578 } 579 } 580 } 581 582 586 protected void readWebServicesDescriptor(AbstractArchive abstractArchive, 587 Descriptor descriptor) 588 throws IOException, SAXParseException { 589 590 DeploymentDescriptorFile confDD = getWebServicesDDFile(descriptor); 591 if (abstractArchive.getArchiveUri()!=null) { 592 confDD.setErrorReportingString(abstractArchive.getArchiveUri()); 593 } 594 InputStream is = null; 595 try { 596 is = abstractArchive.getEntry(confDD.getDeploymentDescriptorPath()); 597 if (is != null) { 598 confDD.setXMLValidation(getXMLValidation()); 599 confDD.setXMLValidationLevel(validationLevel); 600 confDD.read(descriptor, is); 601 } 602 } finally { 603 if (is != null) { 604 is.close(); 605 } 606 } 607 608 } 609 610 615 public void write() throws IOException { 616 write(path); 617 } 618 619 624 public void write(String outPath) throws IOException { 625 AbstractArchive in = abstractArchiveFactory.openArchive(path); 626 write(in, outPath); 627 in.close(); 628 } 629 630 636 public void write(AbstractArchive in, String outPath) throws IOException { 637 638 AbstractArchive oldArchive=null; 639 try { 640 oldArchive = abstractArchiveFactory.openArchive(outPath); 641 } catch (IOException ioe) { 642 } 645 AbstractArchive out = null; 646 File fOut = new File(outPath); 647 BufferedOutputStream bos=null; 648 try { 649 String tmpName = null; 650 if (oldArchive!=null && oldArchive.exists() && 651 !oldArchive.supportsElementsOverwriting()) { 652 File outputFile = getTempFile(outPath); 655 tmpName = outputFile.getAbsolutePath(); 656 outputFile.delete(); 657 out = abstractArchiveFactory.createArchive(tmpName); 658 oldArchive.close(); 659 } else { 660 out = abstractArchiveFactory.createArchive(outPath); 661 } 662 663 writeContents(in, out); 665 out.close(); 666 in.close(); 667 668 if (tmpName!=null) { 670 AbstractArchive finalArchive = abstractArchiveFactory.openArchive(outPath); 671 finalArchive.delete(); 672 AbstractArchive tmpArchive = abstractArchiveFactory.openArchive(tmpName); 673 tmpArchive.renameTo(outPath); 674 } 675 } catch (IOException ioe) { 676 if (out!=null) { 678 try { 679 out.close(); 680 } catch (IOException outIoe) { 682 } 685 } 686 throw ioe; 688 } 689 } 690 691 public void write(AbstractArchive in, AbstractArchive out) throws IOException { 692 writeContents(in,out); 693 } 694 695 700 protected void writeContents(AbstractArchive out) throws IOException { 701 AbstractArchive in = abstractArchiveFactory.openArchive(path); 702 writeContents(in, out); 703 in.close(); 704 } 705 706 707 712 protected void writeContents(AbstractArchive in, AbstractArchive out) throws IOException { 713 714 writeContents(in, out, null); 715 } 716 717 718 725 protected void writeContents(AbstractArchive in, AbstractArchive out, Vector entriesToSkip) 726 throws IOException { 727 728 if (in!=null && in.exists()) { 730 if (entriesToSkip==null) { 731 entriesToSkip = getListOfFilesToSkip(); 732 } else { 733 entriesToSkip.addAll(getListOfFilesToSkip()); 734 } 735 copyJarElements(in, out, entriesToSkip); 736 } 737 738 writeDeploymentDescriptors(out); 740 741 if (manifest!=null) { 743 OutputStream os = out.putNextEntry(JarFile.MANIFEST_NAME); 744 manifest.write(new DataOutputStream(os)); 745 out.closeEntry(); 746 } 747 } 748 749 755 public void writeDeploymentDescriptors(AbstractArchive out) throws IOException { 756 757 writeStandardDeploymentDescriptors(out); 759 760 writeExtraDeploymentDescriptors(out); 762 } 763 764 769 public void writeStandardDeploymentDescriptors(AbstractArchive out) throws IOException { 770 771 OutputStream os = out.putNextEntry(getDeploymentDescriptorPath()); 772 writeStandardDeploymentDescriptors(os); 773 out.closeEntry(); 774 775 Descriptor desc = getDescriptor(); 776 777 if (desc instanceof BundleDescriptor) { 779 writeWebServicesDescriptors((BundleDescriptor) desc, out); 780 } 781 } 782 783 788 public void writeRuntimeDeploymentDescriptors(AbstractArchive out) throws IOException { 789 790 Descriptor desc = getDescriptor(); 791 792 if (isHandlingRuntimeInfo()) { 794 DeploymentDescriptorFile confDD = getConfigurationDDFile(); 795 if (confDD!=null) { 796 OutputStream os = out.putNextEntry(getRuntimeDeploymentDescriptorPath()); 797 confDD.write(desc, os); 798 out.closeEntry(); 799 } 800 } 801 802 } 803 804 809 protected void writeExtraDeploymentDescriptors(AbstractArchive out) throws IOException { 810 writeRuntimeDeploymentDescriptors(out); 811 } 812 813 818 public void writeStandardDeploymentDescriptors(OutputStream os) throws IOException { 819 getStandardDDFile().write(getDescriptor(), os); 820 } 821 822 827 public void writeRuntimeDeploymentDescriptors(OutputStream os) throws IOException { 828 DeploymentDescriptorFile confDD = getConfigurationDDFile(); 829 if (confDD!=null) { 830 confDD.write(getDescriptor(), os); 831 } 832 } 833 834 838 protected void writeWebServicesDescriptors(BundleDescriptor desc, AbstractArchive out) 839 throws IOException { 840 if (desc.hasWebServices()) { 841 DeploymentDescriptorFile webServicesDD = getWebServicesDDFile(desc); 842 OutputStream os = out.putNextEntry(webServicesDD.getDeploymentDescriptorPath()); 843 webServicesDD.write(desc, os); 844 out.closeEntry(); 845 } 846 } 847 848 852 public String getDeploymentDescriptorPath() { 853 return getStandardDDFile().getDeploymentDescriptorPath(); 854 } 855 856 861 public String getWebServicesDeploymentDescriptorPath() { 862 return null; 863 } 864 865 869 public String getRuntimeDeploymentDescriptorPath() { 870 DeploymentDescriptorFile ddFile = getConfigurationDDFile(); 871 if (ddFile!=null) { 872 return ddFile.getDeploymentDescriptorPath(); 873 } else { 874 return null; 875 } 876 } 877 878 882 public boolean containsRuntimeDeploymentDescriptors(AbstractArchive in) { 883 884 String ddFileName = getRuntimeDeploymentDescriptorPath(); 885 if (ddFileName==null) { 886 return false; 887 } 888 for (Enumeration e = in.entries();e.hasMoreElements();) { 889 String entryName = (String ) e.nextElement(); 890 if (entryName.equals(ddFileName)) { 891 return true; 892 } 893 } 894 return false; 897 } 898 899 904 public void setModuleDescriptor(ModuleDescriptor module) { 905 setDescriptor(module.getDescriptor()); 906 setManifest(module.getManifest()); 907 } 908 909 912 public boolean performOptionalPkgDependenciesCheck(AbstractArchive archive) throws IOException { 913 914 boolean dependenciesSatisfied = true; 915 Manifest m = archive.getManifest(); 916 if (m!=null) { 917 dependenciesSatisfied=OptionalPkgDependency.optionalPkgDependencyLogic(m, archive.getArchiveUri()); 918 } 919 Vector<String > libs = getLibraries(archive); 921 if (libs!=null) { 922 for (String libUri : libs) { 923 JarInputStream jis=null; 924 try { 925 jis = new JarInputStream(archive.getEntry(libUri)); 926 m = jis.getManifest(); 927 if (m!=null) { 928 if (!OptionalPkgDependency.optionalPkgDependencyLogic(m, libUri)) { 929 dependenciesSatisfied=false; 930 } 931 } 932 } finally { 933 if (jis!=null) 934 jis.close(); 935 } 936 } 937 } 938 return dependenciesSatisfied; 939 } 940 941 946 public abstract void setDescriptor(Descriptor descriptor); 947 948 951 public abstract Descriptor getDescriptor(); 952 953 958 public abstract ModuleType getModuleType(); 959 960 964 public abstract DeploymentDescriptorFile getStandardDDFile(); 965 966 970 public abstract DeploymentDescriptorFile getConfigurationDDFile(); 971 972 975 public abstract Descriptor getDefaultBundleDescriptor(); 976 977 982 public DeploymentDescriptorFile getWebServicesDDFile(Descriptor desc) { 983 return new WebServicesDeploymentDescriptorFile(desc); 984 } 985 986 989 protected abstract String getArchiveExtension(); 990 991 994 protected abstract boolean postHandles(AbstractArchive abstractArchive) throws IOException; 995 996 public boolean hasStandardDeploymentDescriptor(AbstractArchive archive) 997 throws IOException { 998 InputStream stIs = archive.getEntry(getDeploymentDescriptorPath()); 999 if (stIs != null) { 1000 stIs.close(); 1001 return true; 1002 } 1003 return false; 1004 } 1005 1006 public boolean hasRuntimeDeploymentDescriptor(AbstractArchive archive) 1007 throws IOException { 1008 1009 if (getConfigurationDDFile() != null) { 1011 InputStream runIs = archive.getEntry( 1012 getConfigurationDDFile().getDeploymentDescriptorPath()); 1013 if (runIs != null) { 1014 runIs.close(); 1015 return true; 1016 } 1017 } 1018 return false; 1019 } 1020 1021 1025 public ModuleDescriptor createModuleDescriptor(BundleDescriptor descriptor) { 1026 ModuleDescriptor newModule = new ModuleDescriptor(); 1027 newModule.setModuleType(getModuleType()); 1028 newModule.setDescriptor(descriptor); 1029 setDescriptor(descriptor); 1030 return newModule; 1031 } 1032 1033 1034 1037 public void printDescriptor() { 1038 getDescriptor().visit((DescriptorVisitor) new TracerVisitor()); 1039 } 1040 1041 1046 public void setHandleRuntimeInfo(boolean handleRuntimeInfo) { 1047 this.handleRuntimeInfo = handleRuntimeInfo; 1048 } 1049 1050 1053 public boolean isHandlingRuntimeInfo() { 1054 return handleRuntimeInfo; 1055 } 1056 1057 1062 public void setAnnotationProcessingRequested( 1063 boolean annotationProcessingRequested) { 1064 this.annotationProcessingRequested = annotationProcessingRequested; 1065 } 1066 1067 1070 public boolean isAnnotationProcessingRequested() { 1071 return annotationProcessingRequested; 1072 } 1073 1074 1079 public void setAnnotationErrorHandler(ErrorHandler annotationErrorHandler) { 1080 this.annotationErrorHandler = annotationErrorHandler; 1081 } 1082 1083 1086 public ErrorHandler getAnnotationErrorHandler() { 1087 return annotationErrorHandler; 1088 } 1089 1090 1095 public void setManifest(Manifest m) { 1096 manifest = m; 1097 } 1098 1099 1102 public Manifest getManifest() { 1103 return manifest; 1104 } 1105 1106 1111 public void setClassPath(String newClassPath) { 1112 1113 if (manifest==null) { 1114 manifest = new Manifest(); 1115 } 1116 1117 Attributes atts = manifest.getMainAttributes(); 1118 atts.putValue(Attributes.Name.CLASS_PATH.toString(), newClassPath); 1119 } 1120 1121 1125 public String getClassPath() { 1126 1127 if (manifest==null) { 1128 return null; 1129 } 1130 1131 Attributes atts = manifest.getMainAttributes(); 1132 return atts.getValue(Attributes.Name.CLASS_PATH); 1133 } 1134 1135 1138 public Vector getLibraries(AbstractArchive archive) { 1139 1140 Enumeration<String > entries = archive.entries(); 1141 if (entries==null) 1142 return null; 1143 1144 Vector libs = new Vector(); 1145 while (entries.hasMoreElements()) { 1146 1147 String entryName = entries.nextElement(); 1148 if (entryName.indexOf('/')!=-1) { 1149 continue; } 1151 if (entryName.endsWith(".jar")) { 1152 libs.add(entryName); 1153 } 1154 } 1155 return libs; 1156 } 1157 1158 1161 protected String getUniqueEntryFilenameFor(AbstractArchive archive, String trialName) throws IOException { 1162 Vector entriesNames = new Vector(); 1163 Enumeration e = archive.entries(); 1164 while (e!=null && e.hasMoreElements()) { 1165 entriesNames.add(e.nextElement()); 1166 } 1167 return Descriptor.createUniqueFilenameAmongst(trialName, entriesNames); 1168 } 1169 1170 1177 protected ModuleDescriptor addToArchive(ApplicationArchivist appArch, String externalDD) 1178 throws IOException, SAXParseException { 1179 1180 1181 AbstractArchive archiveToBeAdded = abstractArchiveFactory.openArchive(path); 1182 File archiveFile = new File(path); 1183 AbstractArchive appArchive = abstractArchiveFactory.openArchive(appArch.getArchiveUri()); 1184 String archiveName = getUniqueEntryFilenameFor(appArchive, archiveFile.getName()); 1185 appArchive.close(); 1186 Descriptor descriptor = null; 1187 if (externalDD != null && !"".equals(externalDD)) { 1188 File externalDescriptorFile = new File(externalDD); 1189 if (externalDescriptorFile.exists()) { 1190 FileInputStream fis = new FileInputStream(externalDescriptorFile); 1191 try { 1192 DeploymentDescriptorFile ddf = getStandardDDFile(); 1193 descriptor = ddf.read(fis); 1194 } catch(SAXParseException pe) { 1195 archiveToBeAdded.close(); 1196 pe.printStackTrace(); 1197 throw pe; 1199 } 1200 } 1201 } 1202 if (descriptor==null) { 1203 descriptor = open(archiveToBeAdded); 1206 } 1207 archiveToBeAdded.close(); 1208 1209 if (descriptor instanceof BundleDescriptor) { 1211 ModuleDescriptor newModule = new ModuleDescriptor(); 1212 newModule.setArchiveUri(archiveName); 1213 newModule.setModuleType(getModuleType()); 1214 newModule.setDescriptor((BundleDescriptor) descriptor); 1215 newModule.setManifest(getManifest()); 1216 if (externalDD != null && !"".equals(externalDD)) { 1217 newModule.setAlternateDescriptor(externalDD); 1218 } 1219 return newModule; 1220 } else { 1221 return null; 1222 } 1223 } 1224 1225 1230 protected void prepareForInclusion(AbstractArchive out) throws IOException { 1231 } 1232 1233 public void saveRuntimeInfo(File output) throws IOException { 1234 File outputFile = output; 1236 if (outputFile==null) { 1237 outputFile = getTempFile(path); 1238 } 1239 1240 AbstractArchive out = abstractArchiveFactory.createArchive(outputFile.getAbsolutePath()); 1243 AbstractArchive in = abstractArchiveFactory.openArchive(path); 1244 Vector skipFiles = new Vector(); 1245 skipFiles.add(getRuntimeDeploymentDescriptorPath()); 1246 copyInto(in, out, skipFiles); 1247 in.close(); 1248 1249 OutputStream os = out.putNextEntry(getRuntimeDeploymentDescriptorPath()); 1251 writeRuntimeDeploymentDescriptors(os); 1252 out.closeEntry(); 1253 out.close(); 1254 1255 if (output==null) { 1257 AbstractArchive finalArchive = abstractArchiveFactory.openArchive(path); 1258 finalArchive.delete(); 1259 AbstractArchive tmpArchive = abstractArchiveFactory.openArchive(outputFile.getAbsolutePath()); 1260 tmpArchive.renameTo(path); 1261 } 1262 1263 } 1264 1265 1268 public void applyRuntimeInfo(File runtimeDD, File output) throws IOException, SAXParseException { 1269 1270 getConfigurationDDFile().read(getDescriptor(), new FileInputStream(runtimeDD)); 1272 1273 saveRuntimeInfo(output); 1275 } 1276 1277 1284 protected static File getTempFile(String fileOrDirPath) throws IOException { 1285 if (fileOrDirPath!=null) { 1286 return getTempFile(new File(fileOrDirPath)); 1287 } else { 1288 return getTempFile((File) null); 1289 } 1290 } 1291 1292 1296 public Vector getListOfFilesToSkip() { 1297 1298 Vector filesToSkip = new Vector(); 1299 filesToSkip.add(getDeploymentDescriptorPath()); 1300 if (manifest!=null) { 1301 filesToSkip.add(JarFile.MANIFEST_NAME); 1302 } 1303 if (getRuntimeDeploymentDescriptorPath()!=null) { 1304 filesToSkip.add(getRuntimeDeploymentDescriptorPath()); 1305 } 1306 1307 filesToSkip.addAll(WebServicesDeploymentDescriptorFile. 1310 getAllDescriptorPaths()); 1311 1312 return filesToSkip; 1313 } 1314 1315 1322 protected static File getTempFile(File fileOrDir) throws IOException { 1323 1324 File dir = null; 1325 if (fileOrDir==null) { 1326 dir=new File(System.getProperty("user.dir")); 1327 } else { 1328 if (!fileOrDir.isDirectory()) { 1329 dir = fileOrDir.getParentFile(); 1330 if (dir==null) { 1331 dir=new File(System.getProperty("user.dir")); 1332 } 1333 } else { 1334 dir = fileOrDir; 1335 } 1336 } 1337 return File.createTempFile("tmp", ".jar", dir); 1338 } 1339 1340 1347 protected static void addFileToArchive(AbstractArchive archive, String filePath, String entryName) 1348 throws IOException { 1349 1350 FileInputStream is = new FileInputStream(new File(filePath)); 1351 OutputStream os = archive.putNextEntry(entryName); 1352 ArchivistUtils.copyWithoutClose(is, os); 1353 is.close(); 1354 archive.closeEntry(); 1355 } 1356 1357 1365 protected void copyJarElements(AbstractArchive in, AbstractArchive out, Vector ignoreList) 1366 throws IOException { 1367 1368 Enumeration entries = in.entries(); 1369 for (Enumeration outEntriesItr = out.entries();outEntriesItr.hasMoreElements();) { 1372 if (ignoreList==null) { 1373 ignoreList = new Vector(); 1374 } 1375 ignoreList.add(outEntriesItr.nextElement()); 1376 } 1377 if (entries!=null) { 1378 for (;entries.hasMoreElements();) { 1379 String anEntry = (String ) entries.nextElement(); 1380 if (ignoreList==null || !ignoreList.contains(anEntry)) { 1381 InputStream is = in.getEntry(anEntry); 1382 OutputStream os = out.putNextEntry(anEntry); 1383 ArchivistUtils.copyWithoutClose(is, os); 1384 is.close(); 1385 out.closeEntry(); 1386 } 1387 } 1388 } 1389 } 1390 1391 1397 protected boolean renameTmp(String from, String to) throws IOException { 1398 1399 AbstractArchive finalArchive = abstractArchiveFactory.openArchive(to); 1400 finalArchive.delete(); 1401 AbstractArchive tmpArchive = abstractArchiveFactory.openArchive(from); 1402 boolean success = tmpArchive.renameTo(to); 1403 if (!success) { 1404 throw new IOException("Error renaming JAR"); 1405 } 1406 return success; 1407 } 1408 1409 1412 public void setArchiveUri(String path) { 1413 this.path = path; 1414 } 1415 1416 1419 public String getArchiveUri() { 1420 return path; 1421 } 1422 1423 1427 public void setClassLoader(ClassLoader classLoader) { 1428 this.classLoader = classLoader; 1429 } 1430 1431 1436 public void setXMLValidation(boolean validate) { 1437 isValidatingXML=validate; 1438 } 1439 1440 1444 public boolean getXMLValidation() { 1445 return isValidatingXML; 1446 } 1447 1448 1453 public void setRuntimeXMLValidation(boolean validate) { 1454 isValidatingRuntimeXML = validate; 1455 } 1456 1457 1461 public boolean getRuntimeXMLValidation() { 1462 return isValidatingRuntimeXML; 1463 } 1464 1465 1473 public void setXMLValidationLevel(String level) { 1474 validationLevel = level; 1475 } 1476 1477 1480 public String getXMLValidationLevel() { 1481 return validationLevel; 1482 } 1483 1484 1492 public void setRuntimeXMLValidationLevel(String level) { 1493 runtimeValidationLevel = level; 1494 } 1495 1496 1499 public String getRuntimeXMLValidationLevel() { 1500 return runtimeValidationLevel; 1501 } 1502 1503 1508 public void validate(ClassLoader aClassLoader) { 1509 } 1510 1511 1515 public void copyInto(AbstractArchive target) throws IOException { 1516 AbstractArchive source = abstractArchiveFactory.openArchive(path); 1517 copyInto(source, target); 1518 } 1519 1520 1528 public void copyInto(AbstractArchive source, AbstractArchive target) throws IOException { 1529 copyInto(source, target, null, true); 1530 } 1531 1532 1540 public void copyInto(AbstractArchive source, AbstractArchive target, boolean overwriteManifest) throws IOException { 1541 copyInto(source, target, null, overwriteManifest); 1542 } 1543 1544 1551 public void copyInto(AbstractArchive source, AbstractArchive target, Vector entriesToSkip) 1552 throws IOException { 1553 copyInto(source, target, entriesToSkip, true); 1554 } 1555 1556 1564 public void copyInto(AbstractArchive source, AbstractArchive target, 1565 Vector entriesToSkip, boolean overwriteManifest) 1566 throws IOException { 1567 1568 copyJarElements(source, target, entriesToSkip); 1569 if (overwriteManifest) { 1570 Manifest m = source.getManifest(); 1571 if (m != null) { 1572 OutputStream os = target.putNextEntry(JarFile.MANIFEST_NAME); 1573 m.write(os); 1574 target.closeEntry(); 1575 } 1576 } 1577 } 1578 1579 1584 public void extractEntry(String entryName, File out) throws IOException { 1585 AbstractArchive archive = abstractArchiveFactory.openArchive(path); 1586 InputStream is = archive.getEntry(entryName); 1587 OutputStream os = new BufferedOutputStream(new FileOutputStream(out)); 1588 ArchivistUtils.copy(new BufferedInputStream(is), os); 1589 archive.close(); 1590 } 1591 1592 1595 public void setPluggableArchivists(PluggableArchivists pa) { 1596 this.pa = pa; 1597 } 1598 1599 1602 public PluggableArchivists getPluggableArchivists() { 1603 if (pa==null) { 1604 return ArchivistFactory.getPluggableArchivists(); 1605 } else { 1606 return pa; 1607 } 1608 } 1609 1610 public static void copyAnEntry(AbstractArchive in, 1611 AbstractArchive out, String entryName) throws IOException { 1612 InputStream is = null; 1613 InputStream is2 = null; 1614 try { 1615 is = in.getEntry(entryName); 1616 is2 = out.getEntry(entryName); 1617 if (is != null && is2 == null) { 1618 OutputStream os = out.putNextEntry(entryName); 1619 ArchivistUtils.copyWithoutClose(is, os); 1620 } 1621 } finally { 1622 1625 if (is != null) { 1626 is.close(); 1627 } 1628 if (is2 != null) { 1629 is2.close(); 1630 } 1631 out.closeEntry(); 1632 } 1633 } 1634 1635 public void copyStandardDeploymentDescriptors(AbstractArchive in, 1636 AbstractArchive out) throws IOException { 1637 String entryName = getDeploymentDescriptorPath(); 1638 copyAnEntry(in, out, entryName); 1639 1640 Descriptor desc = getDescriptor(); 1641 1642 if (desc instanceof BundleDescriptor) { 1644 BundleDescriptor desc2 = (BundleDescriptor)desc; 1645 if (desc2.hasWebServices()) { 1646 DeploymentDescriptorFile webServicesDD = 1647 getWebServicesDDFile((BundleDescriptor)desc2); 1648 String anEntry = webServicesDD.getDeploymentDescriptorPath(); 1649 copyAnEntry(in, out, anEntry); 1650 } 1651 } 1652 } 1653 1654 public static void copyExtraElements(AbstractArchive in, 1656 AbstractArchive out) throws IOException { 1657 Enumeration entries = in.entries(); 1658 if (entries!=null) { 1659 for (;entries.hasMoreElements();) { 1660 String anEntry = (String ) entries.nextElement(); 1661 if(anEntry.endsWith(PERSISTENCE_DD_ENTRY)) { 1662 continue; 1667 } 1668 if (anEntry.indexOf(WSDL) != -1 || 1669 anEntry.indexOf(XML) != -1 || 1670 anEntry.indexOf(XSD) != -1) { 1671 copyAnEntry(in, out, anEntry); 1672 } 1673 } 1674 } 1675 } 1676 1677 protected void readPersistenceDeploymentDescriptor( 1678 Archive subArchive, String puRoot, Descriptor descriptor) 1679 throws IOException, SAXParseException { 1680 final String subArchiveURI = AbstractArchive.class.cast(subArchive).getArchiveUri(); 1681 if (logger.isLoggable(Level.FINE)) { 1682 logger.logp(Level.FINE, "Archivist", 1683 "readPersistenceDeploymentDescriptor", 1684 "PURoot = [{0}] subArchive = {1}", 1685 new Object []{puRoot, subArchiveURI}); 1686 } 1687 final RootDeploymentDescriptor rootDD = 1688 RootDeploymentDescriptor.class.cast(descriptor); 1689 if (rootDD.getPersistenceUnitsDescriptor(puRoot) != null) { 1690 if (logger.isLoggable(Level.FINE)) { 1691 logger.logp(Level.FINE, "Archivist", 1692 "readPersistenceDeploymentDescriptor", 1693 "PU has been already read for = {0}", 1694 subArchiveURI); 1695 } 1696 return; 1697 } 1698 PersistenceDeploymentDescriptorFile persistenceDeploymentDescriptorFile 1699 = new PersistenceDeploymentDescriptorFile(); 1700 persistenceDeploymentDescriptorFile.setErrorReportingString( 1701 subArchiveURI.toString()); 1702 persistenceDeploymentDescriptorFile.setXMLValidation(getXMLValidation()); 1703 persistenceDeploymentDescriptorFile.setXMLValidationLevel( 1704 getXMLValidationLevel()); 1705 InputStream is = subArchive.getEntry( 1706 persistenceDeploymentDescriptorFile.getDeploymentDescriptorPath()); 1707 if (is == null) { 1708 if (logger.isLoggable(Level.FINE)) { 1709 logger.logp(Level.FINE, "Archivist", 1710 "readPersistenceDeploymentDescriptor", 1711 "{0} does not contain {1}, so it is not a PU Root.", 1712 new Object []{subArchiveURI, 1713 persistenceDeploymentDescriptorFile.getDeploymentDescriptorPath()}); 1714 } 1715 return; 1716 } 1717 try { 1718 PersistenceUnitsDescriptor persistenceUnitsDescriptor = 1719 PersistenceUnitsDescriptor.class.cast( 1720 persistenceDeploymentDescriptorFile.read(rootDD, is)); 1721 rootDD.addPersistenceUnitsDescriptor(puRoot, 1722 persistenceUnitsDescriptor); 1723 } finally { 1724 is.close(); 1725 } 1726 } 1727} 1728 | Popular Tags |