stefano     00/08/17 10:07:06

  Modified:    src/org/apache/cocoon/servlet Tag: xml-cocoon2
                        CocoonServlet.java
  Log:
  major cleanup and allowed XSP to use the temp directory provided by the 
servlet container
  which eases installation a lot!!!
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.4.17  +85 -67    
xml-cocoon/src/org/apache/cocoon/servlet/Attic/CocoonServlet.java
  
  Index: CocoonServlet.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon/src/org/apache/cocoon/servlet/Attic/CocoonServlet.java,v
  retrieving revision 1.1.4.16
  retrieving revision 1.1.4.17
  diff -u -r1.1.4.16 -r1.1.4.17
  --- CocoonServlet.java        2000/08/04 21:12:10     1.1.4.16
  +++ CocoonServlet.java        2000/08/17 17:07:06     1.1.4.17
  @@ -9,9 +9,11 @@
   package org.apache.cocoon.servlet;
   
   import java.util.Date;
  +import java.util.Enumeration;
  +
  +import java.io.File;
   import java.io.IOException;
   import java.io.PrintWriter;
  -import java.util.Enumeration;
   
   import javax.servlet.ServletConfig;
   import javax.servlet.ServletContext;
  @@ -25,46 +27,54 @@
   import org.apache.avalon.ComponentNotAccessibleException;
   
   import org.apache.cocoon.Cocoon;
  -import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.environment.http.HttpEnvironment;
   
   import org.xml.sax.SAXException;
   
   /**
  + * This is the entry point for Cocoon execution as an HTTP Servlet.
    *
  - *
    * @author <a href="mailto:[EMAIL PROTECTED]">Pierpaolo Fumagalli</a>
    *         (Apache Software Foundation, Exoffice Technologies)
  - * @version CVS $Revision: 1.1.4.16 $ $Date: 2000/08/04 21:12:10 $
  + * @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
  + * @version CVS $Revision: 1.1.4.17 $ $Date: 2000/08/17 17:07:06 $
    */
   public class CocoonServlet extends HttpServlet {
  -    private Cocoon cocoon=null;
  -    private long creationTime=0;
  -    private String configurationFile=null;
  -    private Exception exception=null;
  -    private ServletContext context=null;
  +        
  +    private Cocoon cocoon = null;
  +    private long creationTime = 0;
  +    private String configurationFile = null;
  +    private Exception exception = null;
  +    private ServletContext context = null;
   
       /**
        * Initialize this <code>CocoonServlet</code> instance.
        */
  -    public void init(ServletConfig conf)
  -    throws ServletException {
  +    public void init(ServletConfig conf) throws ServletException {
  +        
           super.init(conf);
  -        this.context=conf.getServletContext();
  -        String configFile=conf.getInitParameter("configurations");
  -        this.context.log ("this.configurationFile: "+this.configurationFile);
  -        if (configFile==null) {
  +        
  +        this.context = conf.getServletContext();
  +        String configFile = conf.getInitParameter("configurations");
  +        this.context.log("this.configurationFile: " + 
this.configurationFile);
  +        
  +        if (configFile == null) {
               throw new ServletException("Servlet initialization argument "+
                                          "'configurations' not specified");
           }
  +
           try {
  -            
this.configurationFile=this.context.getResource(configFile).getFile();
  -            this.context.log ("this.configurationFile: 
"+this.configurationFile);
  +            this.configurationFile = 
this.context.getResource(configFile).getFile();
  +            this.context.log("this.configurationFile: " + 
this.configurationFile);
           } catch (java.net.MalformedURLException mue) {
  -            throw new ServletException("Servlet initialization argument "+
  -                                       "'configurations' not found at 
"+this.configurationFile);
  +            throw new ServletException("Servlet initialization argument " 
  +                                     + "'configurations' not found at " 
  +                                     + this.configurationFile);
           }
  -        this.cocoon=this.create();
  +
  +        this.cocoon = this.create();
  +        
  +        System.setProperty(Cocoon.TEMPDIR_PROPERTY, ((File) 
this.context.getAttribute("javax.servlet.context.tempdir")).toString());
       }
   
       /**
  @@ -73,55 +83,59 @@
        */
       public void service(HttpServletRequest req, HttpServletResponse res)
       throws ServletException, IOException {
  +
           long start = new Date().getTime();
           long end = 0;
  +
           // Reload cocoon if configuration changed or we are reloading
  -        boolean reloaded=false;
  +        boolean reloaded = false;
  +
           synchronized (this) {
  -            if (this.cocoon!=null) {
  +            if (this.cocoon != null) {
                   if (this.cocoon.modifiedSince(this.creationTime)) {
                       this.context.log("Configuration changed reload attempt");
  -                    this.cocoon=this.create();
  -                    reloaded=true;
  -                } else if ((req.getPathInfo()==null) &&
  -                           (req.getParameter("reload")!=null)) {
  +                    this.cocoon = this.create();
  +                    reloaded = true;
  +                } else if ((req.getPathInfo() == null) &&
  +                           (req.getParameter(Cocoon.RELOAD_PARAM) != null)) {
                       this.context.log("Forced reload attempt");
  -                    this.cocoon=this.create();
  -                    reloaded=true;
  +                    this.cocoon = this.create();
  +                    reloaded = true;
                   }
  -            } else if ((req.getPathInfo()==null) &&
  -                       (req.getParameter("reload")!=null)) {
  +            } else if ((req.getPathInfo() == null) &&
  +                       (req.getParameter(Cocoon.RELOAD_PARAM) != null)) {
                   this.context.log("Invalid configurations reload");
  -                this.cocoon=this.create();
  -                reloaded=true;
  +                this.cocoon = this.create();
  +                reloaded = true;
               }
           }
   
  -        ServletOutputStream out=res.getOutputStream();
  +        ServletOutputStream out = res.getOutputStream();
   
           // Check if cocoon was initialized
  -        if (this.cocoon==null) {
  +        if (this.cocoon == null) {
  +// ----> FIXME (SM) Yuck! What happens if the requesting client doesn't 
handle HTML?
               res.setStatus(res.SC_INTERNAL_SERVER_ERROR);
               res.setContentType("text/html");
               out.println("<html><head>");
  -            out.println("<title>Cocoon Version 2.0: Not 
initialized</title>");
  +            out.println("<title>Cocoon " + Cocoon.VERSION + ": not 
initialized</title>");
               out.println("<body>");
  -            out.println("<center><h1>Cocoon 2.0: Not 
initialized</h1></center>");
  +            out.println("<center><h1>Cocoon " + Cocoon.VERSION + ": not 
initialized</h1></center>");
               out.println("<hr>");
               out.print("Try to <a href=\"");
  -            out.print(req.getScheme()+"://"+req.getServerName()+":");
  -            out.print(req.getServerPort()+req.getServletPath());
  -            out.print("?reload=true\">Reload</a>");
  -            out.println("<!-- PATH_INFO=\""+req.getPathInfo()+"\" -->");
  +            out.print(req.getScheme() + "://" + req.getServerName() + ":");
  +            out.print(req.getServerPort() + req.getServletPath());
  +            out.print("?" + Cocoon.RELOAD_PARAM + "=true\">Reload</a>");
  +            out.println("<!-- PATH_INFO=\"" + req.getPathInfo() + "\" -->");
               out.println("<hr>");
               this.printException(out,this.exception);
               if (exception instanceof SAXException) {
  -                Exception 
nested=((SAXException)this.exception).getException();
  +                Exception nested=((SAXException) 
this.exception).getException();
                   out.println("<hr>");
                   this.printException(out,nested);
               } else if (exception instanceof ComponentNotAccessibleException) 
{
                   out.println("Component not accessible<br>");
  -                Exception nested=this.exception;
  +                Exception nested = this.exception;
                   
nested=((ComponentNotAccessibleException)nested).getException();
                   out.println("<hr>");
                   this.printException(out,nested);
  @@ -130,35 +144,38 @@
               out.flush();
               return;
           }
  +        
           // We got it... Process the request
           // We should use getRequestURI(), minus the Context path otherwise
           // we break compatability with Tomcat and other Servlet 2.2 engines
           // (like Gefion LWS and Orion)
  -        String 
uri=req.getRequestURI().substring(req.getContextPath().length());
  +//-----> FIXME (SM) Check what this means now that we support only Servlet 
2.2 and above!
  +        String uri = 
req.getRequestURI().substring(req.getContextPath().length());
  +        
           if (!uri.equals("")) {
               try {
  -                if (uri.charAt(0)=='/') uri=uri.substring(1);
  +                if (uri.charAt(0)=='/') uri = uri.substring(1);
                   HttpEnvironment env = new HttpEnvironment (uri, req, res, 
context);
                   if (!this.cocoon.process(env)) {
                       res.setStatus(res.SC_NOT_FOUND);
                       res.setContentType("text/html");
                       out.println("<html><head>");
  -                    out.println("<title>Cocoon Version 2.0: Not 
Found</title>");
  +                    out.println("<title>Cocoon " + Cocoon.VERSION + ": not 
Found</title>");
                       out.println("<body>");
  -                    out.println("<center><h1>Cocoon 2.0: Not 
Found</h1></center>");
  +                    out.println("<center><h1>Cocoon " + Cocoon.VERSION + ": 
not Found</h1></center>");
                       out.println("<hr>");
  -                    out.print("The requested URI \""+req.getRequestURI());
  +                    out.print("The requested URI \"" + req.getRequestURI());
                       out.print("\" was not found.");
  -                    out.println("<!-- PATH_INFO=\""+uri+"\" -->");
  +                    out.println("<!-- PATH_INFO=\"" + uri + "\" -->");
                       out.println("<hr></body></html>");
                   }
               } catch (Exception e) {
                   //res.setStatus(res.SC_INTERNAL_SERVER_ERROR);
                   res.setContentType("text/html");
                   out.println("<html><head>");
  -                out.println("<title>Cocoon Version 2.0: Exception</title>");
  +                out.println("<title>Cocoon " + Cocoon.VERSION + ": 
exception</title>");
                   out.println("<body>");
  -                out.println("<center><h1>Cocoon 2.0: 
Exception</h1></center>");
  +                out.println("<center><h1>Cocoon " + Cocoon.VERSION + ": 
exception</h1></center>");
                   out.println("<hr>");
                   this.printException(out,e);
                   if (e instanceof SAXException) {
  @@ -169,7 +186,7 @@
                   } else if (e instanceof ComponentNotAccessibleException) {
                       out.println("<hr>");
                       out.println("Component not accessible<br>");
  -                    Exception nested=e;
  +                    Exception nested = e;
                       
nested=((ComponentNotAccessibleException)nested).getException();
                       this.printException(out,nested);
                   }
  @@ -179,18 +196,18 @@
           } else {
               res.setContentType("text/html");
               out.println("<html><head>");
  -            out.println("<title>Cocoon Version 2.0</title>");
  +            out.println("<title>Cocoon " + Cocoon.VERSION + "</title>");
               out.println("<body>");
  -            out.println("<center><h1>Cocoon 2.0: Version 2.0</h1></center>");
  +            out.println("<center><h1>Cocoon " + Cocoon.VERSION + 
"</h1></center>");
               out.println("<hr>");
               if (reloaded) out.println("Configurations reloaded.<br>");
               out.println("Ready to process requests...");
  -            out.println("<!-- PATH_INFO=\""+uri+"\" -->");
  +            out.println("<!-- PATH_INFO=\"" + uri + "\" -->");
               out.println("<hr></body></html>");
           }
   
           end = new Date().getTime();
  -        String showTime = req.getParameter("showtime");
  +        String showTime = req.getParameter(Cocoon.SHOWTIME_PARAM);
           if ((showTime != null) && !showTime.equalsIgnoreCase("no")) {
               float time = (float) (end - start);
               float second = (float) 1000;
  @@ -198,11 +215,12 @@
               float hour = (float) 60 * minute;
   
               if (showTime.equalsIgnoreCase("hide")) {
  -                out.print("<!--");
  +                out.print("<!-- ");
               } else {
                   out.print("<p>");
               }
  -            out.print("Processed by Cocoon in ");
  +            
  +            out.print("Processed by Cocoon " + Cocoon.VERSION + " in ");
   
               if (time > hour) {
                   out.print(time / hour);
  @@ -218,7 +236,7 @@
                   out.print(" milliseconds.");
               }
   
  -            if (showTime == "hide") {
  +            if (showTime.equalsIgnoreCase("hide")) {
                   out.print("-->");
               } else {
                   out.print("</p>");
  @@ -231,22 +249,22 @@
       /** Create a new <code>Cocoon</code> object. */
       private Cocoon create() {
           try {
  -            this.context.log("Reloading from: "+this.configurationFile);
  -            Cocoon c=new Cocoon(this.configurationFile);
  -            this.creationTime=System.currentTimeMillis();
  -            return(c);
  +            this.context.log("Reloading from: " + this.configurationFile);
  +            Cocoon c = new Cocoon(this.configurationFile);
  +            this.creationTime = System.currentTimeMillis();
  +            return c;
           } catch (Exception e) {
  -            this.context.log("Exception reloading: "+e.getMessage());
  -            this.exception=e;
  +            this.context.log("Exception reloading: " + e.getMessage());
  +            this.exception = e;
           }
           return(null);
       }
   
       /** Dump an exception to the specified <code>ServletOutputStream</code> 
*/
       private void printException(ServletOutputStream o, Exception e) {
  -        PrintWriter out=new PrintWriter(o);
  -        out.println("Class: <b>"+e.getClass().getName()+"</b><br>");
  -        out.println("Message: <i>"+e.getMessage()+"</i><br>");
  +        PrintWriter out = new PrintWriter(o);
  +        out.println("Class: <b>" + e.getClass().getName() + "</b><br>");
  +        out.println("Message: <i>" + e.getMessage() + "</i><br>");
           out.println("<pre>");
           e.printStackTrace(out);
           out.println("</pre>");
  
  
  

Reply via email to