1 2 24 package org.enhydra.tool.archive; 25 26 import org.enhydra.tool.common.ResUtil; 28 import org.enhydra.tool.archive.xml.WarDescriptorHandler; 29 30 import java.io.IOException ; 32 import java.io.File ; 33 import java.util.ResourceBundle ; 34 import java.util.jar.Manifest ; 35 import java.util.jar.Attributes ; 36 37 public class WarBuilder extends JarBuilder { 39 40 private final String WEB_INF = "WEB-INF"; private final String CLASSES = "classes"; 44 public WarBuilder() { 46 super(); 47 } 48 49 public WarPlan getWarPlan() { 50 WarPlan wp = null; 51 52 if (getPlan() instanceof WarPlan) { 53 wp = (WarPlan) getPlan(); 54 } 55 return wp; 56 } 57 58 public File buildArchive() throws ArchiveException { 60 File war = null; 61 String root = null; 62 File [] files = new File [0]; 63 Descriptor[] dd = new Descriptor[0]; 64 65 war = openJarStream(getWarPlan().getArchivePath()); 66 root = getWarPlan().getContentRoot(); 67 dd = getWarPlan().getDescriptors(); 68 files = getWarPlan().getContentFileArray(); 69 addWarClasses(); 70 addLibraries(); 71 addContent(); 72 for (int i = 0; i < dd.length; i++) { 73 addDescriptor(dd[i], root, files); 74 } 75 closeJarStream(); 76 return war; 77 } 78 79 protected Manifest buildManifest(Manifest m) { 80 Manifest manifest = super.buildManifest(m); 81 82 manifest.getMainAttributes().put(new Attributes.Name (CONTENT_ROOT), 83 getWarPlan().getContentRoot()); 84 return manifest; 85 } 86 87 private void addContent() throws ArchiveException { 88 File [] files = getWarPlan().getContentFileArray(); 89 90 for (int i = 0; i < files.length; i++) { 91 addContentFile(files[i]); 92 } 93 } 94 95 private void addContentFile(File source) throws ArchiveException { 96 StringBuffer entryPath = new StringBuffer (); 97 String contentRoot = null; 98 String relative = null; 99 100 contentRoot = getWarPlan().getContentRoot(); 101 relative = source.getAbsolutePath().substring(contentRoot.length() 102 + 1); 103 relative = relative.replace('\\', '/'); 104 entryPath.append(relative); 105 try { 106 createEntry(source, entryPath.toString()); 107 } catch (IOException e) { 108 throw new ArchiveException(e, 109 ResUtil.format(res.getString("WarBuilder_Unable_to"), 110 source)); 111 } 112 } 113 114 private void addLibraries() throws ArchiveException { 115 File [] files = getWarPlan().getLibFileArray(); 116 117 for (int i = 0; i < files.length; i++) { 118 addLibFile(files[i]); 119 } 120 } 121 122 private void addLibFile(File source) throws ArchiveException { 123 StringBuffer entryPath = new StringBuffer (); 124 125 entryPath.append("WEB-INF"); 127 entryPath.append('/'); 128 entryPath.append("lib"); 129 entryPath.append('/'); 130 entryPath.append(source.getName()); 131 132 try { 134 createEntry(source, entryPath.toString()); 135 } catch (IOException e) { 136 String exMess = res.getString("WarBuilder_Unable_to"); 137 138 exMess = ResUtil.format(exMess, source); 139 throw new ArchiveException(e, exMess); 140 } 141 } 142 143 private void addWarClasses() throws ArchiveException { 144 File [] files = getWarPlan().getClassFileArray(); 145 146 for (int i = 0; i < files.length; i++) { 147 addWarClassesFile(files[i]); 148 } 149 } 150 151 private void addWarClassesFile(File source) throws ArchiveException { 152 StringBuffer entryPath = new StringBuffer (); 153 String classRoot = null; 154 String relative = null; 155 156 classRoot = getWarPlan().getClassRoot(); 158 relative = source.getAbsolutePath().substring(classRoot.length() + 1); 159 relative = relative.replace('\\', '/'); 160 161 entryPath.append(WEB_INF); 163 entryPath.append('/'); 164 entryPath.append(CLASSES); 165 entryPath.append('/'); 166 entryPath.append(relative); 167 168 try { 170 createEntry(source, entryPath.toString()); 171 } catch (IOException e) { 172 throw new ArchiveException(e, 173 ResUtil.format(res.getString("WarBuilder_Unable_to1"), 174 source)); 175 } 176 } 177 178 } 179 | Popular Tags |