1 23 package org.infoglue.deliver.portal.deploy; 24 25 import java.io.BufferedInputStream ; 26 import java.io.BufferedOutputStream ; 27 import java.io.ByteArrayInputStream ; 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.OutputStream ; 35 import java.net.URL ; 36 import java.util.Enumeration ; 37 import java.util.Vector ; 38 import java.util.jar.JarEntry ; 39 import java.util.jar.JarFile ; 40 import java.util.jar.JarOutputStream ; 41 import java.util.zip.ZipEntry ; 42 import java.util.zip.ZipOutputStream ; 43 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 import org.apache.pluto.PortletContainerServices; 47 import org.apache.pluto.descriptors.services.PortletAppDescriptorService; 48 import org.apache.pluto.descriptors.services.WebAppDescriptorService; 49 import org.apache.pluto.descriptors.services.impl.AbstractWebAppDescriptorService; 50 import org.apache.pluto.descriptors.services.impl.StreamPortletAppDescriptorServiceImpl; 51 import org.apache.pluto.om.portlet.PortletApplicationDefinition; 52 import org.apache.pluto.portalImpl.om.portlet.impl.PortletApplicationDefinitionImpl; 53 import org.apache.pluto.portalImpl.services.ServiceManager; 54 import org.apache.xerces.parsers.DOMParser; 55 import org.exolab.castor.mapping.Mapping; 56 import org.exolab.castor.xml.Unmarshaller; 57 import org.infoglue.deliver.portal.ServletConfigContainer; 58 import org.w3c.dom.Document ; 59 import org.xml.sax.InputSource ; 60 61 66 public class Deploy { 67 68 public static class StreamWebAppDescriptorServiceImpl extends AbstractWebAppDescriptorService { 69 70 private InputStream in; 71 private OutputStream out; 72 73 public StreamWebAppDescriptorServiceImpl(String contextPath, 74 InputStream in, 75 OutputStream out) { 76 super(contextPath); 77 this.in = in; 78 this.out = out; 79 } 80 81 protected InputStream getInputStream() throws IOException { 82 return in; 83 } 84 85 protected OutputStream getOutputStream() throws IOException { 86 return out; 87 } 88 } 89 90 private static final Log log = LogFactory.getLog(Deploy.class); 91 92 private static final int MAX_POLL_DEPLOY = 30; 93 94 private static final String PORTLET_XML = "WEB-INF/portlet.xml"; 95 96 private static final String WEB_XML = "WEB-INF/web.xml"; 97 98 private static final String PORTLET_MAPPING = "portletdefinitionmapping.xml"; 99 100 private static final String SERVLET_MAPPING = "servletdefinitionmapping.xml"; 101 102 119 public static boolean deployArchive(String webappsDir, String warName, InputStream is, 120 String containerName) throws FileNotFoundException , IOException { 121 File webapps = new File (webappsDir); 122 if (!webapps.exists() || !webapps.isDirectory()) { 123 throw new FileNotFoundException ("Webapps directory not found: " + webappsDir); 124 } 125 String appName = warName; 126 int dot = appName.lastIndexOf("."); 127 if (dot > 0) { 128 appName = appName.substring(0, dot); 129 } else { 130 warName += ".war"; 131 } 132 133 File war = new File (webapps, warName); 135 if (war.exists()) { 136 log.info(".war-file already exists: " + war.getAbsolutePath()); 138 } else { 139 FileOutputStream os = new FileOutputStream (war); 140 BufferedOutputStream bos = new BufferedOutputStream (os); 141 int num = copyStream(is, bos); 142 bos.close(); 143 os.close(); 144 log.info(num + " bytes written to " + war.getAbsolutePath()); 145 } 146 147 158 159 File appDir = new File (webapps, appName); 161 File webInfDir = new File (appDir, "WEB-INF"); 162 File portletXml = new File (webInfDir, "portlet.xml"); 163 for (int i = 0; !portletXml.exists() && i < MAX_POLL_DEPLOY; i++) { 164 log.debug("Waiting for servlet container to deploy portlet, try " + i + "/" 165 + MAX_POLL_DEPLOY); 166 try { 167 Thread.sleep(1000); 168 } catch (InterruptedException e) { 169 log.warn("Deployment paus interrupted", e); 170 } 171 } 172 if (!portletXml.exists()) { 173 log.error("Failed to deploy portlet: " + war.getAbsolutePath()); 174 } 175 176 log.info("Updating pluto service manager: " + containerName); 178 try { 179 PortletContainerServices.prepare(containerName); 180 ServiceManager.init(ServletConfigContainer.getContainer().getServletConfig()); 181 } catch (Exception e) { 182 log.error("Error during update of pluto service manager", e); 183 } 184 return true; 185 } 186 187 196 public static PortletApplicationDefinition prepareArchive(File file, File tmp, String appName) throws IOException 197 { 198 PortletApplicationDefinitionImpl portletApp = null; 199 try 200 { 201 Mapping pdmXml = new Mapping(); 202 try 203 { 204 URL url = Deploy.class.getResource("/" + PORTLET_MAPPING); 205 pdmXml.loadMapping(url); 206 } 207 catch (Exception e) 208 { 209 throw new IOException ("Failed to load mapping file " + PORTLET_MAPPING); 210 } 211 212 JarFile jar = new JarFile (file); 214 215 ZipEntry portletEntry = jar.getEntry(PORTLET_XML); 217 if (portletEntry == null) 218 { 219 throw new IOException ("Unable to find portlet.xml"); 220 } 221 222 InputStream pisDebug = jar.getInputStream(portletEntry); 223 StringBuffer sb = new StringBuffer (); 224 int i; 225 while((i = pisDebug.read()) > -1) 226 { 227 sb.append((char)i); 228 } 229 pisDebug.close(); 230 231 InputSource xmlSource = new InputSource (new ByteArrayInputStream (sb.toString().getBytes("UTF-8"))); 232 DOMParser parser = new DOMParser(); 233 parser.parse(xmlSource); 234 Document portletDocument = parser.getDocument(); 235 236 InputStream pis = jar.getInputStream(portletEntry); 240 241 ZipEntry webEntry = jar.getEntry(WEB_XML); 242 InputStream wis = null; 243 if (webEntry != null) 244 { 245 wis = jar.getInputStream(webEntry); 246 249 } 250 251 Unmarshaller unmarshaller = new Unmarshaller(pdmXml); 252 unmarshaller.setWhitespacePreserve(true); 253 unmarshaller.setIgnoreExtraElements(true); 254 unmarshaller.setIgnoreExtraAttributes(true); 255 256 portletApp = (PortletApplicationDefinitionImpl) unmarshaller.unmarshal(portletDocument); 257 258 Vector structure = new Vector (); 260 structure.add(appName); 261 structure.add(null); 262 structure.add(null); 263 portletApp.preBuild(structure); 264 265 410 411 416 417 418 FileOutputStream fos = new FileOutputStream (tmp); 420 JarOutputStream tempJar = new JarOutputStream (fos); 421 byte[] buffer = new byte[1024]; 422 int bytesRead; 423 for (Enumeration entries = jar.entries(); entries.hasMoreElements();) 424 { 425 JarEntry entry = (JarEntry ) entries.nextElement(); 426 JarEntry newEntry = new JarEntry (entry.getName()); 428 tempJar.putNextEntry(newEntry); 429 430 if (entry.getName().equals(WEB_XML)) { 431 445 446 PortletAppDescriptorService portletAppDescriptorService = new StreamPortletAppDescriptorServiceImpl(appName, pis, null); 447 File tmpf = File.createTempFile("infoglue-web-xml", null); 448 WebAppDescriptorService webAppDescriptorService = new StreamWebAppDescriptorServiceImpl(appName, wis, new FileOutputStream (tmpf)); 449 450 org.apache.pluto.driver.deploy.Deploy d = new org.apache.pluto.driver.deploy.Deploy(webAppDescriptorService, portletAppDescriptorService); 451 d.updateDescriptors(); 452 FileInputStream fis = new FileInputStream (tmpf); 453 while ((bytesRead = fis.read(buffer)) != -1) 454 { 455 tempJar.write(buffer, 0, bytesRead); 456 } 457 tmpf.delete(); 458 } 459 else 460 { 461 InputStream entryStream = jar.getInputStream(entry); 462 if(entryStream != null) 463 { 464 while ((bytesRead = entryStream.read(buffer)) != -1) 465 { 466 tempJar.write(buffer, 0, bytesRead); 467 } 468 } 469 } 470 } 471 tempJar.flush(); 472 tempJar.close(); 473 fos.flush(); 474 fos.close(); 475 481 } catch (Exception e) { 482 log.error("Failed to prepare archive", e); 483 throw new IOException (e.getMessage()); 484 } 485 486 return portletApp; 487 } 488 489 private static void copy(String from, String to) throws IOException { 490 File f = new File (to); 491 f.getParentFile().mkdirs(); 492 493 InputStream fis = new FileInputStream (from); 494 FileOutputStream fos = new FileOutputStream (f); 495 copyStream(fis, fos); 496 fos.close(); 497 } 498 499 private static int copyStream(InputStream is, OutputStream os) throws IOException { 500 int total = 0; 501 byte[] buffer = new byte[1024]; 502 int length = 0; 503 504 while ((length = is.read(buffer)) >= 0) { 505 os.write(buffer, 0, length); 506 total += length; 507 } 508 os.flush(); 509 return total; 510 } 511 512 private static int expandArchive(File warFile, File destDir) throws IOException { 513 int numEntries = 0; 514 515 if (!destDir.exists()) { 516 destDir.mkdirs(); 517 } 518 JarFile jarFile = new JarFile (warFile); 519 Enumeration files = jarFile.entries(); 520 while (files.hasMoreElements()) { 521 JarEntry entry = (JarEntry ) files.nextElement(); 522 String fileName = entry.getName(); 523 524 File file = new File (destDir, fileName); 525 File dirF = new File (file.getParent()); 526 dirF.mkdirs(); 527 528 if (entry.isDirectory()) { 529 file.mkdirs(); 530 } else { 531 InputStream fis = jarFile.getInputStream(entry); 532 FileOutputStream fos = new FileOutputStream (file); 533 copyStream(fis, fos); 534 fos.close(); 535 } 536 numEntries++; 537 } 538 return numEntries; 539 } 540 541 private static int createArchive(File dir, File archive) throws IOException { 542 int BUFFER = 2048; 543 BufferedInputStream origin = null; 544 FileOutputStream dest = new FileOutputStream (archive); 545 ZipOutputStream out = new ZipOutputStream (new BufferedOutputStream (dest)); 546 byte data[] = new byte[BUFFER]; 548 File files[] = dir.listFiles(); 550 551 for (int i = 0; i < files.length; i++) { 552 File curr = files[i]; 553 if (curr.isDirectory()) { 554 } else { 556 FileInputStream fi = new FileInputStream (curr); 557 origin = new BufferedInputStream (fi, BUFFER); 558 ZipEntry entry = new ZipEntry (curr.getName()); 559 out.putNextEntry(entry); 560 int count; 561 while ((count = origin.read(data, 0, BUFFER)) != -1) { 562 out.write(data, 0, count); 563 } 564 origin.close(); 565 } 566 } 567 out.close(); 568 return files.length; 569 } 570 571 private static void removeAll(File file) { 572 if (file.isDirectory()) { 573 File [] files = file.listFiles(); 574 for (int i = 0; i < files.length; i++) { 575 removeAll(files[i]); 576 } 577 } 578 file.delete(); 579 } 580 581 public static void main(String [] args) { 582 try { 583 File file = new File ("c:\\infoglueHome\\RssPortlet.war"); 584 File tmp = new File ("c:\\infoglueHome\\RssPortlet2.war"); 585 prepareArchive(file, tmp, "test-portlet.war"); 586 } catch (IOException e) { 587 e.printStackTrace(); 589 } 590 } 591 } | Popular Tags |