1 package org.jahia.clipbuilder.html.util; 2 import java.io.*; 3 import java.util.zip.*; 4 import java.util.jar.JarOutputStream ; 5 import java.util.jar.JarEntry ; 6 import java.io.FileInputStream ; 7 import java.io.FileOutputStream ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.io.File ; 11 import java.nio.channels.FileChannel ; 12 import java.util.Enumeration ; 13 import java.util.jar.JarFile ; 14 import java.util.jar.JarOutputStream ; 15 import java.util.zip.ZipEntry ; 16 17 import org.jdom.*; 18 import org.jdom.input.SAXBuilder; 19 import org.jdom.output.Format; 20 import org.jdom.output.XMLOutputter; 21 36 43 public class DeployUtilities { 44 45 private final byte[] buffer = new byte[4096]; 46 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DeployUtilities.class); 47 48 49 59 public void deploy(String inputName, String outputName, String portletName, String portletDescription, Document clipperXML) throws Exception { 60 File tempFile = null; 61 JarFile jin = null; 62 JarOutputStream jout = null; 63 FileChannel srcChannel = null; 64 FileChannel dstChannel = null; 65 66 try { 67 String portletApplicationName = getPortletApplicationName(outputName); 68 tempFile = File.createTempFile(portletApplicationName, ""); 69 tempFile.deleteOnExit(); 70 71 jin = new JarFile (inputName); 72 jout = new JarOutputStream (new FileOutputStream (tempFile)); 73 74 Document webXml = null; 78 Document portletXml = null; 79 ZipEntry src; 80 InputStream source; 81 Enumeration zipEntries = jin.entries(); 82 while (zipEntries.hasMoreElements()) { 83 src = (ZipEntry ) zipEntries.nextElement(); 84 source = jin.getInputStream(src); 85 try { 86 String target = src.getName(); 87 if ("WEB-INF/web.xml".equals(target)) { 88 System.out.println("Found web.xml"); 89 webXml = parseXml(source); 90 } 91 else if ("WEB-INF/portlet.xml".equals(target)) { 92 System.out.println("Found WEB-INF/portlet.xml"); 93 portletXml = parseXml(source); 94 Element rootElement = portletXml.getRootElement(); 95 Element portletEle = rootElement.getChild("portlet", rootElement.getNamespace()); 96 Element portletNameEle = portletEle.getChild("portlet-name", rootElement.getNamespace()); 97 portletNameEle.setText(portletName); 98 Element displayNameEle = portletEle.getChild("display-name", rootElement.getNamespace()); 99 displayNameEle.setText(portletName); 100 Element descriptionEle = portletEle.getChild("description", rootElement.getNamespace()); 101 descriptionEle.setText(portletDescription); 102 } 103 104 else { 105 106 addFile(target, source, jout); 107 } 108 } 109 finally { 110 source.close(); 111 } 112 } 113 114 if (webXml == null) { 115 throw new IllegalArgumentException ("WEB-INF/web.xml"); 116 } 117 if (portletXml == null) { 118 throw new IllegalArgumentException ("WEB-INF/portlet.xml"); 119 } 120 121 addFile("WEB-INF/web.xml", webXml, jout); 123 addFile("WEB-INF/portlet.xml", portletXml, jout); 124 addFile("WEB-INF/clipper.xml", clipperXML, jout); 125 126 jout.close(); 127 jin.close(); 128 jin = null; 129 jout = null; 130 131 System.out.println("Creating war " + outputName + " ..."); 132 System.out.flush(); 133 srcChannel = new FileInputStream (tempFile).getChannel(); 135 dstChannel = new FileOutputStream (outputName).getChannel(); 136 dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); 137 srcChannel.close(); 138 srcChannel = null; 139 dstChannel.close(); 140 dstChannel = null; 141 tempFile.delete(); 142 tempFile = null; 143 System.out.println("War " + outputName + " created"); 144 System.out.flush(); 145 } 146 finally { 147 if (srcChannel != null && srcChannel.isOpen()) { 148 try { 149 srcChannel.close(); 150 } 151 catch (IOException e1) { 152 } 154 } 155 if (dstChannel != null && dstChannel.isOpen()) { 156 try { 157 dstChannel.close(); 158 } 159 catch (IOException e1) { 160 } 162 } 163 if (jin != null) { 164 try { 165 jin.close(); 166 jin = null; 167 } 168 catch (IOException e1) { 169 } 171 } 172 if (jout != null) { 173 try { 174 jout.close(); 175 jout = null; 176 } 177 catch (IOException e1) { 178 } 180 } 181 if (tempFile != null && tempFile.exists()) { 182 tempFile.delete(); 183 } 184 } 185 } 186 187 188 194 protected String getPortletApplicationName(String path) { 195 File file = new File (path); 196 String name = file.getName(); 197 String portletApplicationName = name; 198 199 int index = name.lastIndexOf("."); 200 if (index > -1) { 201 portletApplicationName = name.substring(0, index); 202 } 203 return portletApplicationName; 204 } 205 206 207 214 protected Document parseXml(InputStream source) throws Exception { 215 SAXBuilder saxBuilder = new SAXBuilder(); 218 Document document = saxBuilder.build(source); 219 return document; 220 } 221 222 223 231 protected void addFile(String path, InputStream source, JarOutputStream jos) throws IOException { 232 jos.putNextEntry(new ZipEntry (path)); 233 try { 234 int count; 235 while ((count = source.read(buffer)) > 0) { 236 jos.write(buffer, 0, count); 237 } 238 } 239 finally { 240 jos.closeEntry(); 241 } 242 } 243 244 245 253 protected void addFile(String path, Document source, JarOutputStream jos) throws IOException { 254 if (source != null) { 255 jos.putNextEntry(new ZipEntry (path)); 256 XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat()); 257 try { 258 xmlOutputter.output(source, jos); 259 } 260 finally { 261 jos.closeEntry(); 262 } 263 } 264 } 265 266 267 272 public static DeployUtilities getInstance() { 273 return new DeployUtilities(); 274 } 275 276 } 277 278 | Popular Tags |