1 package org.jahia.clipbuilder.sql.util; 2 3 import java.io.*; 4 import java.util.zip.*; 5 import java.util.jar.JarOutputStream ; 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 22 37 44 public class DeployUtilities { 45 46 private final byte[] buffer = new byte[4096]; 47 private static org.apache.log4j.Logger logger = 48 org.apache.log4j.Logger.getLogger(DeployUtilities.class); 49 50 51 54 55 71 public void deploy(String inputName, String outputName, String portletName, 72 String portletDescription, String driver, String url, 73 String databaseName, String username, String password, 74 String title, String query) throws Exception { 75 File tempFile = null; 76 JarFile jin = null; 77 JarOutputStream jout = null; 78 FileChannel srcChannel = null; 79 FileChannel dstChannel = null; 80 81 try { 82 String portletApplicationName = getPortletApplicationName(outputName); 83 tempFile = File.createTempFile(portletApplicationName, ""); 84 tempFile.deleteOnExit(); 85 86 jin = new JarFile (inputName); 87 jout = new JarOutputStream (new FileOutputStream (tempFile)); 88 89 Document webXml = null; 93 Document portletXml = null; 94 ZipEntry src; 95 InputStream source; 96 Enumeration zipEntries = jin.entries(); 97 while (zipEntries.hasMoreElements()) { 98 src = (ZipEntry ) zipEntries.nextElement(); 99 source = jin.getInputStream(src); 100 try { 101 String target = src.getName(); 102 if ("WEB-INF/web.xml".equals(target)) { 103 System.out.println("Found web.xml"); 104 webXml = parseXml(source); 105 } 106 else if ("WEB-INF/portlet.xml".equals(target)) { 107 System.out.println("Found WEB-INF/portlet.xml"); 108 portletXml = parseXml(source); 109 110 Element rootElement = portletXml.getRootElement(); 111 Element portletEle = rootElement.getChild("portlet", rootElement.getNamespace()); 112 Element portletNameEle = portletEle.getChild("portlet-name", rootElement.getNamespace()); 114 portletNameEle.setText(portletName); 115 Element displayNameEle = portletEle.getChild("display-name", rootElement.getNamespace()); 116 displayNameEle.setText(portletName); 117 Element descriptionEle = portletEle.getChild("description", rootElement.getNamespace()); 118 descriptionEle.setText(portletDescription); 119 120 Element portletPreferencesEle = portletEle.getChild("portlet-preferences", rootElement.getNamespace()); 122 123 setDriverPref(driver, portletPreferencesEle); 125 126 setUrlPref(url, portletPreferencesEle); 128 129 setDatabaseNamePref(databaseName, portletPreferencesEle); 131 132 setUserNamePref(username, portletPreferencesEle); 134 135 setUserpassPref(password, portletPreferencesEle); 137 138 setTitlePref(title, portletPreferencesEle); 140 141 setSqlQueryPref(query, portletPreferencesEle); 143 144 } 145 146 else { 147 148 addFile(target, source, jout); 149 } 150 } 151 finally { 152 source.close(); 153 } 154 } 155 156 if (webXml == null) { 157 throw new IllegalArgumentException ("WEB-INF/web.xml"); 158 } 159 if (portletXml == null) { 160 throw new IllegalArgumentException ("WEB-INF/portlet.xml"); 161 } 162 163 addFile("WEB-INF/web.xml", webXml, jout); 165 addFile("WEB-INF/portlet.xml", portletXml, jout); 166 167 jout.close(); 168 jin.close(); 169 jin = null; 170 jout = null; 171 172 System.out.println("Creating war " + outputName + " ..."); 173 System.out.flush(); 174 srcChannel = new FileInputStream (tempFile).getChannel(); 176 dstChannel = new FileOutputStream (outputName).getChannel(); 177 dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); 178 srcChannel.close(); 179 srcChannel = null; 180 dstChannel.close(); 181 dstChannel = null; 182 tempFile.delete(); 183 tempFile = null; 184 System.out.println("War " + outputName + " created"); 185 System.out.flush(); 186 } 187 finally { 188 if (srcChannel != null && srcChannel.isOpen()) { 189 try { 190 srcChannel.close(); 191 } 192 catch (IOException e1) { 193 } 195 } 196 if (dstChannel != null && dstChannel.isOpen()) { 197 try { 198 dstChannel.close(); 199 } 200 catch (IOException e1) { 201 } 203 } 204 if (jin != null) { 205 try { 206 jin.close(); 207 jin = null; 208 } 209 catch (IOException e1) { 210 } 212 } 213 if (jout != null) { 214 try { 215 jout.close(); 216 jout = null; 217 } 218 catch (IOException e1) { 219 } 221 } 222 if (tempFile != null && tempFile.exists()) { 223 tempFile.delete(); 224 } 225 } 226 } 227 228 229 235 protected String getPortletApplicationName(String path) { 236 File file = new File (path); 237 String name = file.getName(); 238 String portletApplicationName = name; 239 240 int index = name.lastIndexOf("."); 241 if (index > -1) { 242 portletApplicationName = name.substring(0, index); 243 } 244 return portletApplicationName; 245 } 246 247 248 255 protected Document parseXml(InputStream source) throws Exception { 256 SAXBuilder saxBuilder = new SAXBuilder(); 259 Document document = saxBuilder.build(source); 260 return document; 261 } 262 263 264 272 protected void addFile(String path, InputStream source, JarOutputStream jos) throws 273 IOException { 274 jos.putNextEntry(new ZipEntry (path)); 275 try { 276 int count; 277 while ((count = source.read(buffer)) > 0) { 278 jos.write(buffer, 0, count); 279 } 280 } 281 finally { 282 jos.closeEntry(); 283 } 284 } 285 286 287 295 protected void addFile(String path, Document source, JarOutputStream jos) throws 296 IOException { 297 if (source != null) { 298 jos.putNextEntry(new ZipEntry (path)); 299 XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat()); 300 try { 301 xmlOutputter.output(source, jos); 302 } 303 finally { 304 jos.closeEntry(); 305 } 306 } 307 } 308 309 310 316 private void setSqlQueryPref(String query, Element portletPreferencesEle) { 317 Element queryPrefEle = new Element("preference"); 318 Element queryPrefNameEle = new Element("name"); 319 queryPrefNameEle.setText("query"); 320 Element queryPrefValueEle = new Element("value"); 321 queryPrefValueEle.setText(query); 322 portletPreferencesEle.addContent(queryPrefEle); 323 queryPrefEle.addContent(queryPrefNameEle); 324 queryPrefEle.addContent(queryPrefValueEle); 325 } 326 327 328 334 private void setTitlePref(String title, Element portletPreferencesEle) { 335 Element titlePrefEle = new Element("preference"); 336 Element titlePrefNameEle = new Element("name"); 337 titlePrefNameEle.setText("title"); 338 Element titlePrefValueEle = new Element("value"); 339 titlePrefValueEle.setText(title); 340 portletPreferencesEle.addContent(titlePrefEle); 341 titlePrefEle.addContent(titlePrefNameEle); 342 titlePrefEle.addContent(titlePrefValueEle); 343 } 344 345 346 352 private void setUserpassPref(String password, Element portletPreferencesEle) { 353 Element userpasswordPrefEle = new Element("preference"); 354 Element userpasswordPrefNameEle = new Element("name"); 355 userpasswordPrefNameEle.setText("userpassword"); 356 Element userpasswordPrefValueEle = new Element("value"); 357 userpasswordPrefValueEle.setText(password); 358 portletPreferencesEle.addContent(userpasswordPrefEle); 359 userpasswordPrefEle.addContent(userpasswordPrefNameEle); 360 userpasswordPrefEle.addContent(userpasswordPrefValueEle); 361 } 362 363 364 370 private void setUserNamePref(String username, Element portletPreferencesEle) { 371 Element userNamePrefEle = new Element("preference"); 372 Element userNamePrefNameEle = new Element("name"); 373 userNamePrefNameEle.setText("username"); 374 Element userNamePrefValueEle = new Element("value"); 375 userNamePrefValueEle.setText(username); 376 portletPreferencesEle.addContent(userNamePrefEle); 377 userNamePrefEle.addContent(userNamePrefNameEle); 378 userNamePrefEle.addContent(userNamePrefValueEle); 379 } 380 381 382 388 private void setDatabaseNamePref(String databaseName, 389 Element portletPreferencesEle) { 390 Element databaseNamePrefEle = new Element("preference"); 391 Element databaseNamePrefNameEle = new Element("name"); 392 databaseNamePrefNameEle.setText("databasename"); 393 Element databaseNamePrefValueEle = new Element("value"); 394 databaseNamePrefValueEle.setText(databaseName); 395 portletPreferencesEle.addContent(databaseNamePrefEle); 396 databaseNamePrefEle.addContent(databaseNamePrefNameEle); 397 databaseNamePrefEle.addContent(databaseNamePrefValueEle); 398 } 399 400 401 407 private void setUrlPref(String url, Element portletPreferencesEle) { 408 Element urlPrefEle = new Element("preference"); 409 Element urlPrefNameEle = new Element("name"); 410 urlPrefNameEle.setText("url"); 411 Element urlPrefValueEle = new Element("value"); 412 urlPrefValueEle.setText(url); 413 portletPreferencesEle.addContent(urlPrefEle); 414 urlPrefEle.addContent(urlPrefNameEle); 415 urlPrefEle.addContent(urlPrefValueEle); 416 } 417 418 419 425 private void setDriverPref(String driver, Element portletPreferencesEle) { 426 Element driverPrefEle = new Element("preference"); 428 Element driverPrefNameEle = new Element("name"); 429 driverPrefNameEle.setText("driver"); 430 Element driverPrefValueEle = new Element("value"); 431 driverPrefValueEle.setText(driver); 432 portletPreferencesEle.addContent(driverPrefEle); 433 driverPrefEle.addContent(driverPrefNameEle); 434 driverPrefEle.addContent(driverPrefValueEle); 435 } 436 437 438 443 public static DeployUtilities getInstance() { 444 return new DeployUtilities(); 445 } 446 447 } 448 | Popular Tags |