KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > QJspFactory


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp;
31
32 import com.caucho.server.webapp.WebApp;
33 import com.caucho.util.FreeList;
34
35 import javax.servlet.Servlet JavaDoc;
36 import javax.servlet.ServletContext JavaDoc;
37 import javax.servlet.ServletRequest JavaDoc;
38 import javax.servlet.ServletResponse JavaDoc;
39 import javax.servlet.http.HttpSession JavaDoc;
40 import javax.servlet.jsp.JspApplicationContext JavaDoc;
41 import javax.servlet.jsp.JspEngineInfo JavaDoc;
42 import javax.servlet.jsp.JspFactory JavaDoc;
43 import javax.servlet.jsp.PageContext JavaDoc;
44
45 public class QJspFactory extends JspFactory JavaDoc {
46   private static JspEngineInfo JavaDoc _engineInfo = new EngineInfo JavaDoc();
47
48   private static FreeList<PageContextImpl> _freePages =
49     new FreeList<PageContextImpl>(32);
50   
51   private static QJspFactory _factory;
52   
53   static public QJspFactory create()
54   {
55     if (_factory == null)
56       _factory = new QJspFactory();
57
58     return _factory;
59   }
60
61   public static PageContextImpl allocatePageContext(Servlet JavaDoc servlet,
62                             ServletRequest JavaDoc request,
63                             ServletResponse JavaDoc response,
64                             String JavaDoc errorPageURL,
65                             boolean needsSession,
66                             int buffer,
67                             boolean autoFlush)
68   {
69     PageContextImpl pc = _freePages.allocate();
70     if (pc == null)
71       pc = new PageContextImpl();
72
73     try {
74       pc.initialize(servlet, request, response, errorPageURL,
75                     needsSession, buffer, autoFlush);
76     } catch (Exception JavaDoc e) {
77     }
78
79     return pc;
80   }
81
82   /**
83    * The jsp page context initialization.
84    */

85   public static PageContextImpl allocatePageContext(Servlet JavaDoc servlet,
86                             WebApp app,
87                             ServletRequest JavaDoc request,
88                             ServletResponse JavaDoc response,
89                             String JavaDoc errorPageURL,
90                             HttpSession JavaDoc session,
91                             int buffer,
92                             boolean autoFlush,
93                             boolean isPrintNullAsBlank)
94   {
95     PageContextImpl pc = _freePages.allocate();
96     if (pc == null)
97       pc = new PageContextImpl();
98
99     pc.initialize(servlet, app, request, response, errorPageURL,
100           session, buffer, autoFlush, isPrintNullAsBlank);
101
102     return pc;
103   }
104
105   public PageContext JavaDoc getPageContext(Servlet JavaDoc servlet,
106                     ServletRequest JavaDoc request,
107                     ServletResponse JavaDoc response,
108                     String JavaDoc errorPageURL,
109                     boolean needsSession,
110                     int buffer,
111                     boolean autoFlush)
112   {
113     return allocatePageContext(servlet, request, response,
114                    errorPageURL, needsSession,
115                    buffer, autoFlush);
116   }
117
118   /**
119    * Frees a page context after the JSP page is done with it.
120    *
121    * @param pc the PageContext to free
122    */

123   public void releasePageContext(PageContext JavaDoc pc)
124   {
125     if (pc != null) {
126       pc.release();
127
128       if (pc instanceof PageContextImpl)
129     _freePages.free((PageContextImpl) pc);
130     }
131   }
132
133   public static void freePageContext(PageContext JavaDoc pc)
134   {
135     if (pc != null) {
136       pc.release();
137
138       if (pc instanceof PageContextImpl)
139     _freePages.free((PageContextImpl) pc);
140     }
141   }
142
143   // This doesn't appear in the spec, but apparantly exists in some jsdk.jar
144
public String JavaDoc getSpecificationVersion()
145   {
146     return getEngineInfo().getSpecificationVersion();
147   }
148   
149   public JspEngineInfo JavaDoc getEngineInfo()
150   {
151     return _engineInfo;
152   }
153   
154   public JspApplicationContext JavaDoc getJspApplicationContext(ServletContext JavaDoc context)
155   {
156     return ((WebApp) context).getJspApplicationContext();
157   }
158
159   static class EngineInfo extends JspEngineInfo JavaDoc {
160     public String JavaDoc getSpecificationVersion()
161     {
162       return "2.0";
163     }
164   }
165 }
166
Popular Tags