KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > jsp > JSPEngineImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.jsp;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.servlet.Servlet JavaDoc;
21 import javax.servlet.ServletContext JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.avalon.framework.parameters.Parameterizable;
28 import org.apache.avalon.framework.parameters.Parameters;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30
31 /**
32  * Allows Servlets and JSPs to be used as a generator.
33  *
34  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
35  * @version CVS $Id: JSPEngineImpl.java 30932 2004-07-29 17:35:38Z vgritsenko $
36  */

37 public class JSPEngineImpl extends AbstractLogEnabled
38     implements JSPEngine, Parameterizable, ThreadSafe {
39
40     /** The Default Servlet Class Name for Tomcat 3.X and 4.X */
41     public static final String JavaDoc DEFAULT_SERVLET_CLASS = "org.apache.jasper.servlet.JspServlet";
42
43     /** Servlet Class Name */
44     public String JavaDoc jspServletClass = DEFAULT_SERVLET_CLASS;
45     
46     /**
47      * @param params The configuration parameters
48      */

49     public void parameterize(Parameters params) {
50         this.jspServletClass = params.getParameter("servlet-class", DEFAULT_SERVLET_CLASS);
51     }
52     
53     /**
54      * Execute the Servlet/JSP and return the output in UTF8 encoding.
55      */

56     public byte[] executeJSP(String JavaDoc url,
57                              HttpServletRequest JavaDoc servletRequest,
58                              HttpServletResponse JavaDoc servletResponse,
59                              ServletContext JavaDoc context)
60         throws IOException JavaDoc, ServletException JavaDoc, Exception JavaDoc {
61         
62         JSPEngineServletOutputStream output = new JSPEngineServletOutputStream();
63         JSPEngineServletRequest request = new JSPEngineServletRequest(servletRequest, url);
64         JSPEngineServletResponse response = new JSPEngineServletResponse(servletResponse,output);
65         
66         byte[] bytes = null;
67
68         // start the servlet
69
Class JavaDoc clazz = Thread.currentThread().getContextClassLoader().loadClass(this.jspServletClass);
70         Servlet JavaDoc servlet = (Servlet JavaDoc) clazz.newInstance();
71         servlet.init(new JSPEngineServletConfig(context,"JSPEngineImpl"));
72         
73         try {
74             servlet.service(request, response);
75             bytes = output.toByteArray();
76         } finally {
77             // clean up
78
servlet.destroy();
79         }
80         
81         return bytes;
82     }
83 }
84
Popular Tags