001: package jschemeweb;
002:
003: /**
004: * SchemeServlet
005: *
006: * This helper class allows one to define an HttpServlet in which the
007: * public methods are implemented by scheme procedures.
008: * The servlet is initialized using a scheme procedure
009: * specified in the web.xml file defining the servlet.
010: *
011: * @author Timothy J. Hickey Copyright 2000, All Rights Reserved, <tim@cs.brandeis.edu>
012: */
013:
014: import java.io.*;
015: import java.text.*;
016: import java.util.*;
017: import javax.servlet.*;
018: import javax.servlet.http.*;
019:
020: public class SchemeServlet extends HttpServlet {
021:
022: public static jscheme.JScheme js = new jscheme.JScheme();
023:
024: public jsint.Procedure do_service = null, do_delete = null,
025: do_get = null, do_options = null, do_post = null,
026: do_put = null, do_trace = null, do_destroy = null;
027:
028: /**
029: This reads a scheme initialization procedure from the resource
030: specified by the init-param with name "code" in the web.xml The
031: procedure is then evaluated and applied to this servlet. The
032: procedure can optionally set the do_??? variables of this servlet
033: to scheme procedures which will override the defaults.
034: */
035: public void init() throws ServletException {
036: ServletConfig config = this .getServletConfig();
037: if (config == null)
038: return;
039: try {
040: String file = config.getInitParameter("code");
041: ServletContext sc = config.getServletContext();
042: if (!(file == null)) {
043: jsint.InputPort in = new jsint.InputPort(sc
044: .getResourceAsStream(file));
045: js.apply((jsint.Procedure) js.eval(in.read()), js
046: .list(this ));
047: System.out
048: .println("No code parameter for SchemeServlet\n ");
049: }
050: } catch (Exception e) {
051: System.out.println("During SchemeServlet.init(): ");
052: e.printStackTrace();
053: }
054: }
055:
056: public void doDelete(HttpServletRequest request,
057: HttpServletResponse response) throws IOException,
058: ServletException {
059: if (do_delete != null)
060: js.apply(do_delete, js.list(request, response));
061: else
062: super .doDelete(request, response);
063: }
064:
065: public void doGet(HttpServletRequest request,
066: HttpServletResponse response) throws IOException,
067: ServletException {
068: if (do_get != null)
069: js.apply(do_get, js.list(request, response));
070: else
071: super .doGet(request, response);
072: }
073:
074: public void doOptions(HttpServletRequest request,
075: HttpServletResponse response) throws IOException,
076: ServletException {
077: if (do_options != null)
078: js.apply(do_options, js.list(request, response));
079: else
080: super .doOptions(request, response);
081: }
082:
083: public void doPut(HttpServletRequest request,
084: HttpServletResponse response) throws IOException,
085: ServletException {
086: if (do_put != null)
087: js.apply(do_put, js.list(request, response));
088: else
089: super .doPut(request, response);
090: }
091:
092: public void doTrace(HttpServletRequest request,
093: HttpServletResponse response) throws IOException,
094: ServletException {
095: if (do_trace != null)
096: js.apply(do_trace, js.list(request, response));
097: else
098: super .doTrace(request, response);
099: }
100:
101: public void doPost(HttpServletRequest request,
102: HttpServletResponse response) throws IOException,
103: ServletException {
104: if (do_post != null)
105: js.apply(do_post, js.list(request, response));
106:
107: else
108: super .doPost(request, response);
109: }
110:
111: public void service(HttpServletRequest request,
112: HttpServletResponse response) throws IOException,
113: ServletException {
114: if (do_service != null)
115: js.apply(do_service, js.list(request, response));
116: else
117: super .service(request, response);
118: }
119:
120: public void destroy() {
121: if (do_destroy != null)
122: js.apply(do_destroy, js.list());
123: else
124: super.destroy();
125: }
126:
127: }
|