KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > runtime > JspFactoryImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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.jasper.runtime;
18
19 import java.security.AccessController JavaDoc;
20 import java.security.PrivilegedAction JavaDoc;
21
22 import javax.servlet.Servlet JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.ServletResponse JavaDoc;
26 import javax.servlet.jsp.JspApplicationContext JavaDoc;
27 import javax.servlet.jsp.JspFactory JavaDoc;
28 import javax.servlet.jsp.JspEngineInfo JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.jasper.util.SimplePool;
34
35 /**
36  * Implementation of JspFactory.
37  *
38  * @author Anil K. Vijendran
39  */

40 public class JspFactoryImpl extends JspFactory JavaDoc {
41
42     // Logger
43
private Log log = LogFactory.getLog(JspFactoryImpl.class);
44
45     private static final String JavaDoc SPEC_VERSION = "2.0";
46     private static final boolean USE_POOL =
47         Boolean.valueOf(System.getProperty("org.apache.jasper.runtime.JspFactoryImpl.USE_POOL", "true")).booleanValue();
48
49     private SimplePool pool = new SimplePool( 100 );
50     
51     public PageContext JavaDoc getPageContext(Servlet JavaDoc servlet,
52                       ServletRequest JavaDoc request,
53                                       ServletResponse JavaDoc response,
54                                       String JavaDoc errorPageURL,
55                                       boolean needsSession,
56                       int bufferSize,
57                                       boolean autoflush) {
58
59     if( System.getSecurityManager() != null ) {
60         PrivilegedGetPageContext dp = new PrivilegedGetPageContext(
61         (JspFactoryImpl)this, servlet, request, response, errorPageURL,
62                 needsSession, bufferSize, autoflush);
63         return (PageContext JavaDoc)AccessController.doPrivileged(dp);
64     } else {
65         return internalGetPageContext(servlet, request, response,
66                       errorPageURL, needsSession,
67                       bufferSize, autoflush);
68     }
69     }
70
71     public void releasePageContext(PageContext JavaDoc pc) {
72     if( pc == null )
73         return;
74         if( System.getSecurityManager() != null ) {
75             PrivilegedReleasePageContext dp = new PrivilegedReleasePageContext(
76                 (JspFactoryImpl)this,pc);
77             AccessController.doPrivileged(dp);
78         } else {
79             internalReleasePageContext(pc);
80     }
81     }
82
83     public JspEngineInfo JavaDoc getEngineInfo() {
84         return new JspEngineInfo JavaDoc() {
85         public String JavaDoc getSpecificationVersion() {
86             return SPEC_VERSION;
87         }
88         };
89     }
90
91     private PageContext JavaDoc internalGetPageContext(Servlet JavaDoc servlet,
92                            ServletRequest JavaDoc request,
93                            ServletResponse JavaDoc response,
94                            String JavaDoc errorPageURL,
95                            boolean needsSession,
96                            int bufferSize,
97                            boolean autoflush) {
98         try {
99             PageContext JavaDoc pc;
100             if( USE_POOL ) {
101                 pc = (PageContext JavaDoc) pool.get();
102                 if( pc == null ) {
103                     pc = new PageContextImpl();
104                 }
105             } else {
106                 pc = new PageContextImpl();
107             }
108             pc.initialize(servlet, request, response, errorPageURL,
109                     needsSession, bufferSize, autoflush);
110             return pc;
111         } catch (Throwable JavaDoc ex) {
112             /* FIXME: need to do something reasonable here!! */
113             log.fatal("Exception initializing page context", ex);
114             return null;
115         }
116     }
117
118     private void internalReleasePageContext(PageContext JavaDoc pc) {
119         pc.release();
120     if (USE_POOL && (pc instanceof PageContextImpl)) {
121         pool.put( pc );
122     }
123     }
124
125     private class PrivilegedGetPageContext implements PrivilegedAction JavaDoc {
126
127     private JspFactoryImpl factory;
128     private Servlet JavaDoc servlet;
129     private ServletRequest JavaDoc request;
130     private ServletResponse JavaDoc response;
131     private String JavaDoc errorPageURL;
132     private boolean needsSession;
133     private int bufferSize;
134     private boolean autoflush;
135
136     PrivilegedGetPageContext(JspFactoryImpl factory,
137                  Servlet JavaDoc servlet,
138                  ServletRequest JavaDoc request,
139                  ServletResponse JavaDoc response,
140                  String JavaDoc errorPageURL,
141                  boolean needsSession,
142                  int bufferSize,
143                  boolean autoflush) {
144         this.factory = factory;
145         this.servlet = servlet;
146         this.request = request;
147         this.response = response;
148         this.errorPageURL = errorPageURL;
149         this.needsSession = needsSession;
150         this.bufferSize = bufferSize;
151         this.autoflush = autoflush;
152     }
153  
154     public Object JavaDoc run() {
155         return factory.internalGetPageContext(servlet,
156                           request,
157                           response,
158                           errorPageURL,
159                           needsSession,
160                           bufferSize,
161                           autoflush);
162     }
163     }
164
165     private class PrivilegedReleasePageContext implements PrivilegedAction JavaDoc {
166
167         private JspFactoryImpl factory;
168     private PageContext JavaDoc pageContext;
169
170         PrivilegedReleasePageContext(JspFactoryImpl factory,
171                      PageContext JavaDoc pageContext) {
172             this.factory = factory;
173             this.pageContext = pageContext;
174         }
175
176         public Object JavaDoc run() {
177             factory.internalReleasePageContext(pageContext);
178         return null;
179         }
180     }
181     
182     public JspApplicationContext JavaDoc getJspApplicationContext(ServletContext JavaDoc context) {
183         return JspApplicationContextImpl.getInstance(context);
184     }
185 }
186
Popular Tags