01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.jsp;
18:
19: import java.io.IOException;
20:
21: import javax.servlet.Servlet;
22: import javax.servlet.ServletContext;
23: import javax.servlet.ServletException;
24: import javax.servlet.http.HttpServletRequest;
25: import javax.servlet.http.HttpServletResponse;
26:
27: import org.apache.avalon.framework.logger.AbstractLogEnabled;
28: import org.apache.avalon.framework.parameters.Parameterizable;
29: import org.apache.avalon.framework.parameters.Parameters;
30: import org.apache.avalon.framework.thread.ThreadSafe;
31:
32: /**
33: * Allows Servlets and JSPs to be used as a generator.
34: *
35: * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
36: * @version CVS $Id: JSPEngineImpl.java 433543 2006-08-22 06:22:54Z crossley $
37: */
38: public class JSPEngineImpl extends AbstractLogEnabled implements
39: JSPEngine, Parameterizable, ThreadSafe {
40:
41: /** The Default Servlet Class Name for Tomcat 3.X and 4.X */
42: public static final String DEFAULT_SERVLET_CLASS = "org.apache.jasper.servlet.JspServlet";
43:
44: /** Servlet Class Name */
45: public String jspServletClass = DEFAULT_SERVLET_CLASS;
46:
47: /**
48: * @param params The configuration parameters
49: */
50: public void parameterize(Parameters params) {
51: this .jspServletClass = params.getParameter("servlet-class",
52: DEFAULT_SERVLET_CLASS);
53: }
54:
55: /**
56: * Execute the Servlet/JSP and return the output in UTF8 encoding.
57: */
58: public byte[] executeJSP(String url,
59: HttpServletRequest servletRequest,
60: HttpServletResponse servletResponse, ServletContext context)
61: throws IOException, ServletException, Exception {
62:
63: JSPEngineServletOutputStream output = new JSPEngineServletOutputStream();
64: JSPEngineServletRequest request = new JSPEngineServletRequest(
65: servletRequest, url);
66: JSPEngineServletResponse response = new JSPEngineServletResponse(
67: servletResponse, output);
68:
69: byte[] bytes = null;
70:
71: // start the servlet
72: Class clazz = Thread.currentThread().getContextClassLoader()
73: .loadClass(this .jspServletClass);
74: Servlet servlet = (Servlet) clazz.newInstance();
75: servlet.init(new JSPEngineServletConfig(context,
76: "JSPEngineImpl"));
77:
78: try {
79: servlet.service(request, response);
80: bytes = output.toByteArray();
81: } finally {
82: // clean up
83: servlet.destroy();
84: }
85:
86: return bytes;
87: }
88: }
|