stefano 00/08/31 09:00:54
Modified: src/org/apache/cocoon/servlet Tag: xml-cocoon2
CocoonServlet.java
Log:
added support for error notification
Revision Changes Path
No revision
No revision
1.1.4.18 +143 -121
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.17
retrieving revision 1.1.4.18
diff -u -r1.1.4.17 -r1.1.4.18
--- CocoonServlet.java 2000/08/17 17:07:06 1.1.4.17
+++ CocoonServlet.java 2000/08/31 16:00:53 1.1.4.18
@@ -9,7 +9,6 @@
package org.apache.cocoon.servlet;
import java.util.Date;
-import java.util.Enumeration;
import java.io.File;
import java.io.IOException;
@@ -23,13 +22,16 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.xml.sax.SAXException;
+
import org.apache.avalon.ConfigurationException;
import org.apache.avalon.ComponentNotAccessibleException;
import org.apache.cocoon.Cocoon;
+import org.apache.cocoon.Notifier;
+import org.apache.cocoon.Notification;
import org.apache.cocoon.environment.http.HttpEnvironment;
-import org.xml.sax.SAXException;
/**
* This is the entry point for Cocoon execution as an HTTP Servlet.
@@ -37,44 +39,76 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Pierpaolo Fumagalli</a>
* (Apache Software Foundation, Exoffice Technologies)
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
- * @version CVS $Revision: 1.1.4.17 $ $Date: 2000/08/17 17:07:06 $
+ * @author <a href="mailto:[EMAIL PROTECTED]">Nicola Ken Barozzi</a> Aisa
+ * @version CVS $Revision: 1.1.4.18 $ $Date: 2000/08/31 16:00:53 $
*/
+
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 {
-
+
super.init(conf);
-
+
this.context = conf.getServletContext();
+
String configFile = conf.getInitParameter("configurations");
- this.context.log("this.configurationFile: " +
this.configurationFile);
-
+
+ this.context.log("this.configurationFile: " + configFile);
+
if (configFile == null) {
- throw new ServletException("Servlet initialization argument "+
- "'configurations' not specified");
+ ServletException fatalException =
+ new ServletException("Servlet initialization argument "
+ + "'configurations' not specified");
+ Notification n = new Notification(this, fatalException);
+ n.setType("cocoon-init-error");
+ n.setTitle("Cocoon error upon init.");
+ // FIXME (SM) We should use Servlet log channels for this
+ // since otherwise it might get lost or end up being in bad
+ // places if we are run under server frameworks such as avalon
+ // since our servlet container might hook to Avalon own loggin
+ // methods.
+ Notifier.notify(n, System.out);
+
+ throw fatalException;
}
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 "
+ ServletException fatalException =
+ new ServletException("Servlet initialization argument "
+ + "'configurations' not found at "
+ this.configurationFile);
+ Notification n = new Notification(this, fatalException);
+ n.setType("cocoon-init-error");
+ n.setTitle("Cocoon error upon init.");
+ n.addExtraDescription("requested-configuration-file",
+ this.configurationFile);
+ // FIXME (SM) see above
+ Notifier.notify(n, System.out);
+
+ throw fatalException;
}
this.cocoon = this.create();
-
- System.setProperty(Cocoon.TEMPDIR_PROPERTY, ((File)
this.context.getAttribute("javax.servlet.context.tempdir")).toString());
+
+ System.setProperty(Cocoon.TEMPDIR_PROPERTY,
+ ((File) this.context
+
.getAttribute("javax.servlet.context.tempdir"))
+ .toString());
}
/**
@@ -82,10 +116,10 @@
* on the specified <code>HttpServletResponse</code>.
*/
public void service(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException {
+ throws ServletException, IOException {
long start = new Date().getTime();
- long end = 0;
+ long end = 0;
// Reload cocoon if configuration changed or we are reloading
boolean reloaded = false;
@@ -94,132 +128,124 @@
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(Cocoon.RELOAD_PARAM) != null)) {
+ 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;
+ reloaded = true;
}
- } else if ((req.getPathInfo() == null) &&
- (req.getParameter(Cocoon.RELOAD_PARAM) != null)) {
+ } else if ((req.getPathInfo() == null)
+ && (req.getParameter(Cocoon.RELOAD_PARAM) != null)) {
this.context.log("Invalid configurations reload");
+
this.cocoon = this.create();
- reloaded = true;
+ reloaded = true;
}
}
- ServletOutputStream out = res.getOutputStream();
-
// Check if cocoon was initialized
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 " + Cocoon.VERSION + ": not
initialized</title>");
- out.println("<body>");
- 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("?" + 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();
- out.println("<hr>");
- this.printException(out,nested);
- } else if (exception instanceof ComponentNotAccessibleException)
{
- out.println("Component not accessible<br>");
- Exception nested = this.exception;
-
nested=((ComponentNotAccessibleException)nested).getException();
- out.println("<hr>");
- this.printException(out,nested);
- }
- out.println("<hr></body></html>");
- out.flush();
+
+ Notification n = new Notification(this);
+ n.setType("internal-servlet-error");
+ n.setTitle("Internal servlet error");
+ n.setSource("Cocoon servlet");
+ n.setMessage("Internal servlet error");
+ n.setDescription("Cocoon was not initialized.");
+ n.addExtraDescription("request-uri", req.getRequestURI());
+ Notifier.notify(n, req, res);
+
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)
-//-----> FIXME (SM) Check what this means now that we support only Servlet
2.2 and above!
- 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);
- HttpEnvironment env = new HttpEnvironment (uri, req, res,
context);
+ if (uri.charAt(0) == '/') {
+ uri = uri.substring(1);
+ }
+
+ HttpEnvironment env = new HttpEnvironment(uri, req, res,
+ context);
+
if (!this.cocoon.process(env)) {
+
+ //-----> FIXME (NKB) It is not true
that!this.cocoon.process(env) means only SC_NOT_FOUND!
res.setStatus(res.SC_NOT_FOUND);
- res.setContentType("text/html");
- out.println("<html><head>");
- out.println("<title>Cocoon " + Cocoon.VERSION + ": not
Found</title>");
- out.println("<body>");
- out.println("<center><h1>Cocoon " + Cocoon.VERSION + ":
not Found</h1></center>");
- out.println("<hr>");
- out.print("The requested URI \"" + req.getRequestURI());
- out.print("\" was not found.");
- out.println("<!-- PATH_INFO=\"" + uri + "\" -->");
- out.println("<hr></body></html>");
+
+ Notification n = new Notification(this);
+ n.setType("resource-not-found");
+ n.setTitle("Resource not found");
+ n.setSource("Cocoon servlet");
+ n.setMessage("Resource not found");
+ n.setDescription("The requested URI \""
+ + req.getRequestURI()
+ + "\" was not found.");
+ n.addExtraDescription("request-uri",
req.getRequestURI());
+ n.addExtraDescription("path-info", uri);
+ Notifier.notify(n, req, res);
}
} catch (Exception e) {
- //res.setStatus(res.SC_INTERNAL_SERVER_ERROR);
- res.setContentType("text/html");
- out.println("<html><head>");
- out.println("<title>Cocoon " + Cocoon.VERSION + ":
exception</title>");
- out.println("<body>");
- out.println("<center><h1>Cocoon " + Cocoon.VERSION + ":
exception</h1></center>");
- out.println("<hr>");
- this.printException(out,e);
- if (e instanceof SAXException) {
- out.println("<hr>");
- out.println("SAX processing exception<br>");
- Exception nested=((SAXException)e).getException();
- this.printException(out,nested);
- } else if (e instanceof ComponentNotAccessibleException) {
- out.println("<hr>");
- out.println("Component not accessible<br>");
- Exception nested = e;
-
nested=((ComponentNotAccessibleException)nested).getException();
- this.printException(out,nested);
- }
- out.println("<hr></body></html>");
+ //res.setStatus(res.SC_INTERNAL_SERVER_ERROR);
+ Notification n = new Notification(this, e);
+ n.setType("internal-server-error");
+ n.setTitle("Internal server error");
+ n.setSource("Cocoon servlet");
+ n.addExtraDescription("request-uri", req.getRequestURI());
+ n.addExtraDescription("path-info", uri);
+ Notifier.notify(n, req, res);
}
} else {
- res.setContentType("text/html");
- out.println("<html><head>");
- out.println("<title>Cocoon " + Cocoon.VERSION + "</title>");
- out.println("<body>");
- 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("<hr></body></html>");
+ Notification n = new Notification(this);
+ n.setType("information");
+ n.setTitle(Cocoon.VERSION + " :)");
+ n.setSource("Cocoon servlet");
+ n.setMessage("Ready to process requests...");
+
+ String OkDescription = "Ready to process requests...";
+
+ if (reloaded) {
+ OkDescription += "configurations reloaded.";
+ }
+
+ n.setDescription(OkDescription);
+ n.addExtraDescription("request-uri", req.getRequestURI());
+ n.addExtraDescription("path-info", uri);
+ Notifier.notify(n, req, res);
}
+ ServletOutputStream out = res.getOutputStream();
+
end = new Date().getTime();
+
String showTime = req.getParameter(Cocoon.SHOWTIME_PARAM);
- if ((showTime != null) && !showTime.equalsIgnoreCase("no")) {
- float time = (float) (end - start);
+
+ if ((showTime != null) &&!showTime.equalsIgnoreCase("no")) {
+ float time = (float) (end - start);
float second = (float) 1000;
float minute = (float) 60 * second;
- float hour = (float) 60 * minute;
+ float hour = (float) 60 * minute;
if (showTime.equalsIgnoreCase("hide")) {
out.print("<!-- ");
} else {
out.print("<p>");
}
-
+
out.print("Processed by Cocoon " + Cocoon.VERSION + " in ");
if (time > hour) {
@@ -248,26 +274,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;
} catch (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>");
- out.println("<pre>");
- e.printStackTrace(out);
- out.println("</pre>");
- out.flush();
+ return (null);
}
}
+