vgritsenko    02/02/12 08:18:45

  Modified:    .        build.xml
               src/java/org/apache/cocoon/servlet CocoonServlet.java
  Added:       tools/src ManifestToolTask.java
  Log:
  Make Cocoon work out of unpacked WAR. Ugly, but works.
  
  Revision  Changes    Path
  1.167     +7 -1      xml-cocoon2/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/build.xml,v
  retrieving revision 1.166
  retrieving revision 1.167
  diff -u -r1.166 -r1.167
  --- build.xml 11 Feb 2002 16:39:26 -0000      1.166
  +++ build.xml 12 Feb 2002 16:18:45 -0000      1.167
  @@ -1035,6 +1035,9 @@
         <taskdef name="xconf-tool" classname="XConfToolTask" 
             classpath="${tools.dir}/anttasks"/>
   
  +      <taskdef name="manifest-tool" classname="ManifestToolTask" 
  +          classpath="${tools.dir}/anttasks"/>
  +
         <!-- Invoke the SitemapToolTask to add optional entries -->
         <sitemap-tool directory="${build.src}"
                      extension="sitemap"
  @@ -1044,6 +1047,7 @@
         <xconf-tool directory="${build.src}"
                    extension="xconf"
                    configuration="${build.war}/cocoon.xconf"/>
  +
     </target>
   
     <!-- =================================================================== -->
  @@ -1092,6 +1096,8 @@
       </move>
   
       <copy file="${build.dir}/${name}.jar" 
tofile="${build.war}/WEB-INF/lib/${name}-${version}.jar"/>
  +
  +    <manifest-tool directory="${build.war}/WEB-INF/lib" 
manifest="${build.war}/WEB-INF/Manifest.mf"/>
     </target>
   
     <!-- =================================================================== -->
  @@ -1120,7 +1126,7 @@
     <!-- Creates the war file                                                -->
     <!-- =================================================================== -->
     <target name="webapp" depends="prepare-webapp, prepare-webapp-libs, 
prepare-scratchpad-libs, prepare-xsp" description="* Generates the war package">
  -    <jar jarfile="${build.dir}/${name}.war" basedir="${build.war}" includes="**"/>
  +    <jar jarfile="${build.dir}/${name}.war" basedir="${build.war}" includes="**" 
manifest="${build.war}/WEB-INF/Manifest.mf"/>
     </target>
   
     <!-- =================================================================== -->
  
  
  
  1.1                  xml-cocoon2/tools/src/ManifestToolTask.java
  
  Index: ManifestToolTask.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  import java.io.*;
  import java.util.*;
  import org.apache.tools.ant.*;
  import org.apache.tools.ant.taskdefs.*;
  import org.apache.tools.ant.types.*;
  
  /**
   * Creates Manifest file with the all the JARs and modification dates
   * in the specified directory.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Vadim Gritsenko</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/02/12 16:18:45 $
   */
  
  public final class ManifestToolTask extends Task {
  
      private String directory;
      private String manifest;
  
      public void setDirectory(String directory) {
          this.directory = directory;
      }
  
      public void setManifest(String manifest) {
          this.manifest = manifest;
      }
  
      public void execute() throws BuildException {
          if (this.manifest == null) {
              throw new BuildException("manifest attribute is required", location);
          }
          if (this.directory == null) {
              throw new BuildException("directory attribute is required", location);
          }
  
          try {
              // process recursive
              this.process(new File(this.directory), this.manifest);
          } catch (IOException ioe) {
              throw new BuildException("IOException: " + ioe);
          }
      }
  
      /**
       * Scan recursive
       */
      private void process(final File directoryFile,
                           final String manifest)
      throws IOException, BuildException {
  
          System.out.println("Writing: " + manifest);
          FileWriter w = new FileWriter(manifest);
          w.write("Manifest-Version: 1.0\n");
          w.write("Cocoon-Libs: ");
  
          final File[] files = directoryFile.listFiles();
          for(int i = 0; i < files.length; i++) {
              if (files[i].getName().endsWith(".jar")) {
                  w.write(files[i].getName());
                  w.write(" ");
              }
          }
          w.write("\n");
  
          for(int i = 0; i < files.length; i++) {
              if (files[i].getName().endsWith(".jar")) {
                  w.write("Cocoon-Lib-");
                  String s = files[i].getName().replace('.', '_');
                  w.write(s);
                  w.write(": ");
                  w.write(String.valueOf(files[i].lastModified()));
                  w.write("\n");
              }
          }
  
          w.close();
      }
  }
  
  
  
  1.8       +97 -5     
xml-cocoon2/src/java/org/apache/cocoon/servlet/CocoonServlet.java
  
  Index: CocoonServlet.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/servlet/CocoonServlet.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- CocoonServlet.java        4 Feb 2002 13:51:27 -0000       1.7
  +++ CocoonServlet.java        12 Feb 2002 16:18:45 -0000      1.8
  @@ -97,11 +97,17 @@
   import java.io.InputStream;
   import java.io.IOException;
   import java.io.OutputStream;
  +import java.io.FileOutputStream;
   import java.lang.reflect.Constructor;
   import java.net.URL;
   import java.util.Arrays;
   import java.util.HashMap;
   import java.util.StringTokenizer;
  +import java.util.List;
  +import java.util.Iterator;
  +import java.util.ArrayList;
  +import java.util.jar.Manifest;
  +import java.util.jar.Attributes;
   
   /**
    * This is the entry point for Cocoon execution as an HTTP Servlet.
  @@ -113,7 +119,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Berin Loritsch</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Leo Sutic</a>
  - * @version CVS $Id: CocoonServlet.java,v 1.7 2002/02/04 13:51:27 cziegeler Exp $
  + * @version CVS $Id: CocoonServlet.java,v 1.8 2002/02/12 16:18:45 vgritsenko Exp $
    */
   public class CocoonServlet extends HttpServlet {
   
  @@ -149,6 +155,7 @@
       private static final boolean SAVE_UPLOADED_FILES_TO_DISK = true;
       private static final int MAX_UPLOAD_SIZE = 10000000; // 10Mb
       private int maxUploadSize = MAX_UPLOAD_SIZE; // 10Mb
  +
       private File uploadDir;
       private File workDir;
       private File cacheDir;
  @@ -196,6 +203,7 @@
           this.appContext.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, new 
HttpContext(this.servletContext));
   
           this.servletContextPath = this.servletContext.getRealPath("/");
  +
           // first init the work-directory for the logger.
           // this is required if we are running inside a war file!
           final String workDirParam = conf.getInitParameter("work-directory");
  @@ -399,12 +407,10 @@
        *
        * @throws ServletException
        */
  -     protected String getClassPath()
  -     throws ServletException {
  +     protected String getClassPath() throws ServletException {
           StringBuffer buildClassPath = new StringBuffer();
   
           File root = null;
  -
           if (servletContextPath != null) {
               // Old method.  There *MUST* be a better method than this...
   
  @@ -450,7 +456,7 @@
               }
   
               if (libDirURL != null && 
libDirURL.toExternalForm().startsWith("file:")) {
  -                root = new File(libDirURL.toExternalForm().substring(5));
  +                root = new 
File(libDirURL.toExternalForm().substring("file:".length()));
               }
   
               if (classDirURL != null) {
  @@ -468,6 +474,11 @@
               }
           }
   
  +        // Unable to find lib directory. Going the hard way.
  +        if (root == null) {
  +            root = extractLibraries();
  +        }
  +
           if (root != null && root.isDirectory()) {
               File[] libraries = root.listFiles();
               Arrays.sort(libraries);
  @@ -494,6 +505,87 @@
                         .append(getExtraClassPath());
           return buildClassPath.toString();
        }
  +
  +    private File extractLibraries() {
  +        try {
  +            URL manifestURL = 
this.servletContext.getResource("/META-INF/MANIFEST.MF");
  +            if (manifestURL == null) {
  +                this.log.fatalError("Unable to get Manifest");
  +                return null;
  +            }
  +
  +            Manifest mf = new Manifest(manifestURL.openStream());
  +            Attributes attr = mf.getMainAttributes();
  +            String libValue = (String)attr.getValue("Cocoon-Libs");
  +            if (libValue == null) {
  +                this.log.fatalError("Unable to get 'Cocoon-Libs' attribute from the 
Manifest");
  +                return null;
  +            }
  +
  +            List libList = new ArrayList();
  +            for(StringTokenizer st = new StringTokenizer(libValue, " "); 
st.hasMoreTokens();) {
  +                libList.add(st.nextToken());
  +            }
  +
  +            File root = new File(this.workDir, "lib");
  +            root.mkdirs();
  +
  +            File[] oldLibs = root.listFiles();
  +            for (int i=0; i<oldLibs.length; i++) {
  +                String oldLib = oldLibs[i].getName();
  +                if (!libList.contains(oldLib)) {
  +                    this.log.debug("Removing old library " + oldLibs[i]);
  +                    oldLibs[i].delete();
  +                }
  +            }
  +
  +            this.log.warn("Extracting libraries into " + root);
  +            byte[] buffer = new byte[65536];
  +            for (Iterator i = libList.iterator(); i.hasNext();) {
  +                String libName = (String)i.next();
  +
  +                long lastModified = -1;
  +                try {
  +                    lastModified = Long.parseLong(attr.getValue("Cocoon-Lib-" + 
libName.replace('.', '_')));
  +                } catch (Exception e) {
  +                    this.log.debug("Failed to parse lastModified: " + 
attr.getValue("Cocoon-Lib-" + libName.replace('.', '_')));
  +                }
  +
  +                File lib = new File(root, libName);
  +                if (lib.exists() && lib.lastModified() != lastModified) {
  +                    this.log.debug("Removing modified library " + lib);
  +                    lib.delete();
  +                }
  +
  +                InputStream is = 
this.servletContext.getResourceAsStream("/WEB-INF/lib/" + libName);
  +                if (is == null) {
  +                    this.log.warn("Skipping " + libName);
  +                } else {
  +                    this.log.debug("Extracting " + libName);
  +                    OutputStream os = null;
  +                    try {
  +                        os = new FileOutputStream(lib);
  +                        int count;
  +                        while((count = is.read(buffer)) > 0) {
  +                            os.write(buffer, 0, count);
  +                        }
  +                    } finally {
  +                        if (is != null) is.close();
  +                        if (os != null) os.close();
  +                    }
  +                }
  +
  +                if (lastModified != -1) {
  +                    lib.setLastModified(lastModified);
  +                }
  +            }
  +
  +            return root;
  +        } catch (IOException e) {
  +            this.log.fatalError("Exception while processing Manifest file", e);
  +            return null;
  +        }
  +    }
   
   
       /**
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to