taylor      2004/07/19 13:47:00

  Added:       components/deploy-tool project.properties project.xml
                        maven.xml
               components/deploy-tool/src/java/org/apache/jetspeed/tools/deploy
                        JetspeedDeploy.java
                        JetspeedWebApplicationRewriter.java
  Log:
  Jetspeed command line deploy tool
  Takes a war file and rewrites the web.xml infusing the Jetspeed-specific servlet
  
  CVS: ----------------------------------------------------------------------
  CVS: PR:
  CVS:   If this change addresses a PR in the problem report tracking
  CVS:   database, then enter the PR number(s) here.
  CVS: Obtained from:
  CVS:   If this change has been taken from another system, such as NCSA,
  CVS:   then name the system in this line, otherwise delete it.
  CVS: Submitted by:
  CVS:   If this code has been contributed to Apache by someone else; i.e.,
  CVS:   they sent us a patch or a new module, then include their name/email
  CVS:   address here. If this is your work then delete this line.
  CVS: Reviewed by:
  CVS:   If we are doing pre-commit code reviews and someone else has
  CVS:   reviewed your changes, include their name(s) here.
  CVS:   If you have not had it reviewed then delete this line.
  
  Revision  Changes    Path
  1.1                  jakarta-jetspeed-2/components/deploy-tool/project.properties
  
  Index: project.properties
  ===================================================================
  maven.uberjar.main  org.apache.jetspeed.tools.deploy.JetspeedDeploy
  
  
  
  1.1                  jakarta-jetspeed-2/components/deploy-tool/project.xml
  
  Index: project.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <project>
      <pomVersion>3</pomVersion>
      <id>jsdeploy</id>
      <name>JSDeploy</name>
      <currentVersion>1.0</currentVersion>
  
      <dependencies>
          <dependency>
              <groupId>jdom</groupId>
              <artifactId>jdom</artifactId>
              <version>b10</version>
          </dependency>
                <dependency>
                 <groupId>saxpath</groupId>             
                  <artifactId>saxpath</artifactId>
                  <version>1.0-FCS</version>
                </dependency>
                  <dependency>
                  <id>jaxen</id>
                  <version>1.0-FCS-full</version>
                  <properties>
                        <war.bundle>true</war.bundle>
                  </properties>
                </dependency>
                
      </dependencies>
  
      <build>
          <nagEmailAddress>[EMAIL PROTECTED]</nagEmailAddress>
          <sourceDirectory>${basedir}/src/java</sourceDirectory>
      </build>
  </project>
  
  
  
  1.1                  jakarta-jetspeed-2/components/deploy-tool/maven.xml
  
  Index: maven.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  <project default="uberjar"
      xmlns:j="jelly:core"
      xmlns:ant="jelly:ant" >
  
  </project>
  
  
  
  1.1                  
jakarta-jetspeed-2/components/deploy-tool/src/java/org/apache/jetspeed/tools/deploy/JetspeedDeploy.java
  
  Index: JetspeedDeploy.java
  ===================================================================
  package org.apache.jetspeed.tools.deploy;
  
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.File;
  import java.util.jar.JarInputStream;
  import java.util.jar.JarOutputStream;
  import java.util.zip.ZipEntry;
  
  import org.jdom.Document;
  import org.jdom.input.SAXBuilder;
  import org.jdom.output.Format;
  import org.jdom.output.XMLOutputter;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2004/07/19 20:47:00 $
   */
  public class JetspeedDeploy 
  {
      public static void main(String[] args) throws Exception 
      {
          if (args.length != 2) {
              System.out.println("Usage: java -jar jsdeploy.jar INPUT OUTPUT");
              System.exit(1);
              return;
          }
  
          new JetspeedDeploy(args[0], args[1]);
      }
  
      private final byte[] buffer = new byte[4096];
  
      public JetspeedDeploy(String inputName, String outputName) throws Exception 
      {
          JarInputStream jin = null;
          JarOutputStream jout = null;
          try 
          {
              jin = new JarInputStream(new FileInputStream(inputName));
              jout = new JarOutputStream(new FileOutputStream(outputName), 
jin.getManifest());
  
              // copy over all of the files in the input war to the output
              // war except for web.xml and portlet.xml, which we parse for
              // use later
              Document webXml = null;
              Document portletXml = null;
              ZipEntry src;
              while ((src = jin.getNextEntry()) != null) 
              {
                  String target = src.getName();
                  if ("WEB-INF/web.xml".equals(target)) 
                  {
                      System.out.println("Found web.xml");
                      webXml = parseXml(jin);
                  } 
                  else if ("WEB-INF/portlet.xml".equals(target)) 
                  {
                      System.out.println("Found WEB-INF/portlet.xml");
                      portletXml = parseXml(jin);
                  } 
                  else 
                  {
                      addFile(target, jin, jout);
                  }
              }
  
              if (webXml == null) 
              {
                  throw new IllegalArgumentException("WEB-INF/web.xml");
              }
              if (portletXml == null) 
              {
                  throw new IllegalArgumentException("WEB-INF/portlet.xml");
              }
              
              JetspeedWebApplicationRewriter rewriter = new 
JetspeedWebApplicationRewriter(webXml, true);
              rewriter.processWebXML();
              
              // mung the web.xml
              //webXml.getRootElement().setAttribute("foo", "bar");
  
              // write the web.xml and portlet.xml files
              addFile("WEB-INF/web.xml", webXml, jout);
              addFile("WEB-INF/portlet.xml", portletXml, jout);
          } 
          catch (IOException e) 
          {
              e.printStackTrace();
  
              if(jin != null) 
              {
                  try 
                  {
                      jin.close();
                      jin = null;
                  } 
                  catch (IOException e1) 
                  {
                      // ignore
                  }
              }
              if(jout != null) {
                  try {
                      jout.close();
                      jout = null;
                  } catch (IOException e1) {
                      // ignore
                  }
              }
              new File(outputName).delete();
          }
      }
  
      private Document parseXml(InputStream jin) throws Exception {
          SAXBuilder saxBuilder = new SAXBuilder();
          Document document = saxBuilder.build(new UncloseableInputStream(jin));
          return document;
      }
  
      public void addFile(String path, InputStream source, JarOutputStream jos) throws 
IOException 
      {
          jos.putNextEntry(new ZipEntry(path));
          try {
              int count;
              while ((count = source.read(buffer)) > 0) {
                  jos.write(buffer, 0, count);
              }
          } finally {
              jos.closeEntry();
          }
      }
  
      public void addFile(String path, Document source, JarOutputStream jos) throws 
IOException {
          jos.putNextEntry(new ZipEntry(path));
  
          XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
          try {
              xmlOutputter.output(source, jos);
          } finally {
              jos.closeEntry();
          }
      }
  
      private class UncloseableInputStream extends InputStream {
          private final InputStream in;
  
          public UncloseableInputStream(InputStream in) {
              this.in = in;
          }
  
          public int read() throws IOException {
              return in.read();
          }
  
          public int read(byte b[]) throws IOException {
              return in.read(b);
          }
  
          public int read(byte b[], int off, int len) throws IOException {
              return in.read(b, off, len);
          }
  
          public long skip(long n) throws IOException {
              return in.skip(n);
          }
  
          public int available() throws IOException {
              return in.available();
          }
  
          public void close() throws IOException {
              // not closeable
          }
  
          public void mark(int readlimit) {
              in.mark(readlimit);
          }
  
          public void reset() throws IOException {
              in.reset();
          }
  
          public boolean markSupported() {
              return in.markSupported();
          }
      }
  }
  
  
  
  1.1                  
jakarta-jetspeed-2/components/deploy-tool/src/java/org/apache/jetspeed/tools/deploy/JetspeedWebApplicationRewriter.java
  
  Index: JetspeedWebApplicationRewriter.java
  ===================================================================
  /*
   * Copyright 2000-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
   * use this file except in compliance with the License. You may obtain a copy of
   * the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   * License for the specific language governing permissions and limitations under
   * the License.
   */
  package org.apache.jetspeed.tools.deploy;
  
  import java.io.InputStream;
  import java.io.Writer;
  import java.util.Arrays;
  import java.util.List;
  
  import org.jdom.Document;
  import org.jdom.Element;
  import org.jdom.input.SAXBuilder;
  import org.jdom.xpath.XPath;
  import org.xml.sax.EntityResolver;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  
  /**
   * Utilities for manipulating the web.xml deployment descriptor
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Scott T. Weaver </a>
   * @author <a href="mailto:[EMAIL PROTECTED]">Matt Avery </a>
   * @version $Id: WebDescriptorUtilities.java,v 1.2 2004/05/12 22:25:04 taylor
   *                Exp $
   */
  public class JetspeedWebApplicationRewriter
  {
      public static final String JETSPEED_SERVLET_XPATH = 
"/web-app/servlet/servlet-name[contains(child::text(), \"JetspeedContainer\")]";
      public static final String JETSPEED_SERVLET_MAPPING_XPATH = 
"/web-app/servlet-mapping/servlet-name[contains(child::text(), 
\"JetspeedContainer\")]";
      protected static final String WEB_XML_PATH = "WEB-INF/web.xml";
  
      protected static final String[] ELEMENTS_BEFORE_SERVLET = new String[]{"icon", 
"display-name", "description",
              "distributable", "context-param", "filter", "filter-mapping", 
"listener", "servlet"};
      protected static final String[] ELEMENTS_BEFORE_SERVLET_MAPPING = new 
String[]{"icon", "display-name",
              "description", "distributable", "context-param", "filter", 
"filter-mapping", "listener", "servlet",
              "servlet-mapping"};
          
      private Document document;
      private boolean changed = false;
      private boolean registerAtInit = false;
      
      public JetspeedWebApplicationRewriter(Document doc, boolean registerAtInit)
      {
                this.document = doc;
              this.registerAtInit = registerAtInit;
      }
  
      public JetspeedWebApplicationRewriter(Document doc)
      {
              this.document = doc;
      }
      
        /**
         * 
         * <p>
         * processWebXML
         * </p>
         * 
         * Infuses this PortletApplicationWar's web.xml file with
         * <code>servlet</code> and a <code>servlet-mapping</code> element for
         * the JetspeedContainer servlet. This is only done if the descriptor does
         * not already contain these items.
         * 
         * @throws MetaDataException
         *             if there is a problem infusing
         */
        public void processWebXML()
      throws Exception
        {
            SAXBuilder builder = new SAXBuilder();
            Writer webXmlWriter = null;
            InputStream webXmlIn = null;
        
            try
            {
                // Use the local dtd instead of remote dtd. This
                // allows to deploy the application offline
                builder.setEntityResolver(new EntityResolver()
                {
                    public InputSource resolveEntity( java.lang.String publicId, 
java.lang.String systemId )
                            throws SAXException, java.io.IOException
                    {
        
                        if (systemId.equals("http://java.sun.com/dtd/web-app_2_3.dtd";))
                        {
                            return new 
InputSource(getClass().getResourceAsStream("web-app_2_3.dtd"));
                        }
                        else return null;
                    }
                });
        
        
                Element root = document.getRootElement();
                
                Object jetspeedServlet = XPath.selectSingleNode(document, 
JETSPEED_SERVLET_XPATH);
                Object jetspeedServletMapping = XPath.selectSingleNode(document, 
JETSPEED_SERVLET_MAPPING_XPATH);
                if (document.getRootElement().getChildren().size() == 0)
                {
                    throw new Exception("Source web.xml has no content!!!");
                }
                
                if (jetspeedServlet == null)
                {
                    Element jetspeedServletElement = new Element("servlet");
                    Element servletName = (Element) new 
Element("servlet-name").addContent("JetspeedContainer");
                    Element servletDspName = (Element) new 
Element("display-name").addContent("Jetspeed Container");
                    Element servletDesc = (Element) new Element("description")
                            .addContent("MVC Servlet for Jetspeed Portlet 
Applications");
                    Element servletClass = (Element) new Element("servlet-class")
                            
.addContent("org.apache.jetspeed.container.JetspeedContainerServlet");
                    jetspeedServletElement.addContent(servletName);
                    jetspeedServletElement.addContent(servletDspName);
                    jetspeedServletElement.addContent(servletDesc);
                    jetspeedServletElement.addContent(servletClass);
                  if (this.registerAtInit)
                  {
                      Element paramName = (Element) new 
Element("param-name").addContent("registerAtInit");
                      Element paramValue = (Element) new 
Element("param-value").addContent("1"); 
                      Element initParam = new Element("init-param");
                      initParam.addContent(paramName);
                      initParam.addContent(paramValue);
                      jetspeedServletElement.addContent(initParam);                    
                      Element loadOnStartup = (Element) new 
Element("load-on-startup").addContent("-1");
                      jetspeedServletElement.addContent(loadOnStartup);
                  }
                    insertElementCorrectly(root, jetspeedServletElement, 
ELEMENTS_BEFORE_SERVLET);
                    changed = true;
                }
        
                if (jetspeedServletMapping == null)
                {
        
                    Element jetspeedServletMappingElement = new 
Element("servlet-mapping");
        
                    Element servletMapName = (Element) new 
Element("servlet-name").addContent("JetspeedContainer");
                    Element servletUrlPattern = (Element) new 
Element("url-pattern").addContent("/container/*");
        
                    jetspeedServletMappingElement.addContent(servletMapName);
                    jetspeedServletMappingElement.addContent(servletUrlPattern);
        
                    insertElementCorrectly(root, jetspeedServletMappingElement, 
ELEMENTS_BEFORE_SERVLET_MAPPING);
                    changed = true;
                }               
            }
            catch (Exception e)
            {
                throw new Exception("Unable to process web.xml for infusion " + 
e.toString(), e);
            }
        
        }
        
      public boolean isChanged()
      {
          return changed;
      }
      
        /**
         * 
         * <p>
         * insertElementCorrectly
         * </p>
         * 
         * @param root
         *            JDom element representing the &lt; web-app &gt;
         * @param toInsert
         *            JDom element to insert into the web.xml hierarchy.
         * @param elementsBefore
         *            an array of web.xml elements that should be defined before the
         *            element we want to insert. This order should be the order
         *            defined by the web.xml's DTD type definition.
         */
        protected void insertElementCorrectly( Element root, Element toInsert, 
String[] elementsBefore )
      throws Exception
        {
            List allChildren = root.getChildren();
            List elementsBeforeList = Arrays.asList(elementsBefore);
            toInsert.detach();
            int insertAfter = 0;
            for (int i = 0; i < allChildren.size(); i++)
            {
                Element element = (Element) allChildren.get(i);
                if (elementsBeforeList.contains(element.getName()))
                {
                    // determine the Content index of the element to insert after
                    insertAfter = root.indexOf(element);
                }
            }
        
            try
            {
                root.addContent((insertAfter + 1), toInsert);
            }
            catch (ArrayIndexOutOfBoundsException e)
            {
                root.addContent(toInsert);
            }
        }
        
   
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to