001: package org.jucas.addon.pageflow;
002:
003: import java.io.IOException;
004: import java.net.URI;
005:
006: import javax.servlet.ServletConfig;
007: import javax.servlet.ServletException;
008: import javax.servlet.http.HttpServlet;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: import org.jaul.DocumentScript;
013: import org.jaul.IScriptCache;
014: import org.jaul.IURIResolver;
015: import org.jaul.JaulUtil;
016: import org.jaul.ScriptStore;
017: import org.jaul.utils.ServletURIResolver;
018: import org.jaul.utils.SimpleScriptCache;
019:
020: /*
021: * License
022: *
023: *
024: * Copyright (c) 2003 Essl Christian. All rights
025: * reserved.
026: *
027: * This Licence is based on the Apache Software Licence Version 1.1.
028: *
029: *
030: * Redistribution and use in source and binary forms, with or without
031: * modification, are permitted provided that the following conditions
032: * are met:
033: *
034: * 1. Redistributions of source code must retain the above copyright
035: * notice, this list of conditions and the following disclaimer.
036: *
037: * 2. Redistributions in binary form must reproduce the above copyright
038: * notice, this list of conditions and the following disclaimer in
039: * the documentation and/or other materials provided with the
040: * distribution.
041: *
042: * 3. The end-user documentation included with the redistribution,
043: * if any, must include the following acknowledgment:
044: * "This product includes software developed by Christian Essl
045: * and others for project Jucas (http://www.jucas.org/)."
046: * Alternately, this acknowledgment may appear in the software itself,
047: * if and wherever such third-party acknowledgments normally appear.
048: *
049: * 4. The names "Jucas" and "Christian Essl" must not be used to endorse or
050: * promote products derived from this software without prior written
051: * permission. For written permission, please contact essl_christian@jucas.
052: * org.
053: *
054: * 5. Products derived from this software may not be called "Jucas"
055: * or "Christian Essl", nor may "Jucas" or "Christian Essl"
056: * appear in their name, without prior written permission
057: * of Christian Essl (essl_christian@jucas.org).
058: *
059: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
060: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
061: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
062: * DISCLAIMED. IN NO EVENT SHALL CHRISTIAN ESSL OR
063: * OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
064: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
065: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
066: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
067: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
068: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
069: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
070: * SUCH DAMAGE.
071: * ====================================================================
072: *
073: *
074: */
075:
076: /**
077: *
078: *
079: * @author chris
080: */
081: public class FlowServlet extends HttpServlet {
082: /** the logger for this class */
083: public static transient final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
084: .getLog(FlowServlet.class);
085:
086: private ScriptStore scriptStore;
087:
088: /**
089: * Constructor for FlowServlet.
090: */
091: public FlowServlet() {
092: super ();
093: }
094:
095: /**
096: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
097: */
098: protected void doGet(HttpServletRequest req, HttpServletResponse res)
099: throws ServletException, IOException {
100: this .doAction(req, res);
101: }
102:
103: /**
104: * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
105: */
106: protected void doPost(HttpServletRequest req,
107: HttpServletResponse res) throws ServletException,
108: IOException {
109: this .doAction(req, res);
110: }
111:
112: /**
113: * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
114: */
115: public void init(ServletConfig config) throws ServletException {
116: super .init(config);
117:
118: String cSize = config.getInitParameter("cache-size");
119: int cSizeInt = 10;
120: if (cSize != null) {
121: try {
122: cSizeInt = Integer.parseInt(cSize);
123: } catch (Exception e) {
124: cSizeInt = 10;
125: }
126: }
127:
128: URI baseURI = ServletURIResolver.SERVLET_ROOT_URI;
129: IScriptCache cache = new SimpleScriptCache(cSizeInt);
130: IURIResolver[] resolvers = { new ServletURIResolver(config
131: .getServletContext(), "xml", null) };
132:
133: ClassLoader classLoader = this .getClass().getClassLoader();
134:
135: this .scriptStore = new ScriptStore(baseURI, resolvers, cache,
136: classLoader, null);
137: }
138:
139: /**
140: * Method doAction.
141: * @param req
142: * @param res
143: */
144: private void doAction(HttpServletRequest req,
145: HttpServletResponse res) {
146: String path = req.getPathInfo();
147: String target;
148: String source;
149: int cut = path.lastIndexOf('/');
150: if (cut == 0) {
151: source = "/WEB-INF/main.jucasflow.xml";
152: target = path.substring(1);
153: } else {
154: target = path.substring(cut + 1);
155: source = path.substring(0, cut) + ".jucasflow.xml";
156: }
157:
158: URI uri = URI.create(source);
159: //laod the source
160: DocumentScript script;
161: try {
162: script = this .scriptStore.getDocumentScript(uri);
163: JaulFlowContext ctxt = new JaulFlowContext(script, req,
164: res, this .getServletContext(), target);
165: script.execute(ctxt, JaulUtil.NULL_WRITER);
166: } catch (Exception e) {
167: log.debug("Error in executing target: " + target
168: + " in script script: " + source, e);
169: }
170:
171: }
172:
173: }
|