001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.jasper.runtime;
017:
018: import java.security.AccessController;
019: import java.security.PrivilegedAction;
020:
021: import javax.servlet.Servlet;
022: import javax.servlet.ServletRequest;
023: import javax.servlet.ServletResponse;
024: import javax.servlet.jsp.JspFactory;
025: import javax.servlet.jsp.JspEngineInfo;
026: import javax.servlet.jsp.PageContext;
027:
028: import org.apache.jasper.util.SimplePool;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: /**
033: * Implementation of JspFactory.
034: *
035: * @author Anil K. Vijendran
036: */
037: public class JspFactoryImpl extends JspFactory {
038:
039: // Logger
040: private static Log log = LogFactory.getLog(JspFactoryImpl.class);
041:
042: private static final String SPEC_VERSION = "2.0";
043: private static final boolean USE_POOL = true;
044:
045: private SimplePool pool = new SimplePool(100);
046:
047: public PageContext getPageContext(Servlet servlet,
048: ServletRequest request, ServletResponse response,
049: String errorPageURL, boolean needsSession, int bufferSize,
050: boolean autoflush) {
051:
052: if (System.getSecurityManager() != null) {
053: PrivilegedGetPageContext dp = new PrivilegedGetPageContext(
054: (JspFactoryImpl) this , servlet, request, response,
055: errorPageURL, needsSession, bufferSize, autoflush);
056: return (PageContext) AccessController.doPrivileged(dp);
057: } else {
058: return internalGetPageContext(servlet, request, response,
059: errorPageURL, needsSession, bufferSize, autoflush);
060: }
061: }
062:
063: public void releasePageContext(PageContext pc) {
064: if (pc == null)
065: return;
066: if (System.getSecurityManager() != null) {
067: PrivilegedReleasePageContext dp = new PrivilegedReleasePageContext(
068: (JspFactoryImpl) this , pc);
069: AccessController.doPrivileged(dp);
070: } else {
071: internalReleasePageContext(pc);
072: }
073: }
074:
075: public JspEngineInfo getEngineInfo() {
076: return new JspEngineInfo() {
077: public String getSpecificationVersion() {
078: return SPEC_VERSION;
079: }
080: };
081: }
082:
083: private PageContext internalGetPageContext(Servlet servlet,
084: ServletRequest request, ServletResponse response,
085: String errorPageURL, boolean needsSession, int bufferSize,
086: boolean autoflush) {
087: try {
088: PageContext pc;
089: if (USE_POOL) {
090: pc = (PageContext) pool.get();
091: if (pc == null) {
092: pc = new PageContextImpl(this );
093: }
094: } else {
095: pc = new PageContextImpl(this );
096: }
097: pc.initialize(servlet, request, response, errorPageURL,
098: needsSession, bufferSize, autoflush);
099: return pc;
100: } catch (Throwable ex) {
101: /* FIXME: need to do something reasonable here!! */
102: log.fatal("Exception initializing page context", ex);
103: return null;
104: }
105: }
106:
107: private void internalReleasePageContext(PageContext pc) {
108: pc.release();
109: if (USE_POOL && (pc instanceof PageContextImpl)) {
110: pool.put(pc);
111: }
112: }
113:
114: private class PrivilegedGetPageContext implements PrivilegedAction {
115:
116: private JspFactoryImpl factory;
117: private Servlet servlet;
118: private ServletRequest request;
119: private ServletResponse response;
120: private String errorPageURL;
121: private boolean needsSession;
122: private int bufferSize;
123: private boolean autoflush;
124:
125: PrivilegedGetPageContext(JspFactoryImpl factory,
126: Servlet servlet, ServletRequest request,
127: ServletResponse response, String errorPageURL,
128: boolean needsSession, int bufferSize, boolean autoflush) {
129: this .factory = factory;
130: this .servlet = servlet;
131: this .request = request;
132: this .response = response;
133: this .errorPageURL = errorPageURL;
134: this .needsSession = needsSession;
135: this .bufferSize = bufferSize;
136: this .autoflush = autoflush;
137: }
138:
139: public Object run() {
140: return factory.internalGetPageContext(servlet, request,
141: response, errorPageURL, needsSession, bufferSize,
142: autoflush);
143: }
144: }
145:
146: private class PrivilegedReleasePageContext implements
147: PrivilegedAction {
148:
149: private JspFactoryImpl factory;
150: private PageContext pageContext;
151:
152: PrivilegedReleasePageContext(JspFactoryImpl factory,
153: PageContext pageContext) {
154: this .factory = factory;
155: this .pageContext = pageContext;
156: }
157:
158: public Object run() {
159: factory.internalReleasePageContext(pageContext);
160: return null;
161: }
162: }
163: }
|