1 17 package org.apache.jasper.runtime; 18 19 import java.security.AccessController ; 20 import java.security.PrivilegedAction ; 21 22 import javax.servlet.Servlet ; 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.ServletResponse ; 26 import javax.servlet.jsp.JspApplicationContext ; 27 import javax.servlet.jsp.JspFactory ; 28 import javax.servlet.jsp.JspEngineInfo ; 29 import javax.servlet.jsp.PageContext ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.jasper.util.SimplePool; 34 35 40 public class JspFactoryImpl extends JspFactory { 41 42 private Log log = LogFactory.getLog(JspFactoryImpl.class); 44 45 private static final String 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 getPageContext(Servlet servlet, 52 ServletRequest request, 53 ServletResponse response, 54 String 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 )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 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 getEngineInfo() { 84 return new JspEngineInfo () { 85 public String getSpecificationVersion() { 86 return SPEC_VERSION; 87 } 88 }; 89 } 90 91 private PageContext internalGetPageContext(Servlet servlet, 92 ServletRequest request, 93 ServletResponse response, 94 String errorPageURL, 95 boolean needsSession, 96 int bufferSize, 97 boolean autoflush) { 98 try { 99 PageContext pc; 100 if( USE_POOL ) { 101 pc = (PageContext ) 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 ex) { 112 113 log.fatal("Exception initializing page context", ex); 114 return null; 115 } 116 } 117 118 private void internalReleasePageContext(PageContext pc) { 119 pc.release(); 120 if (USE_POOL && (pc instanceof PageContextImpl)) { 121 pool.put( pc ); 122 } 123 } 124 125 private class PrivilegedGetPageContext implements PrivilegedAction { 126 127 private JspFactoryImpl factory; 128 private Servlet servlet; 129 private ServletRequest request; 130 private ServletResponse response; 131 private String errorPageURL; 132 private boolean needsSession; 133 private int bufferSize; 134 private boolean autoflush; 135 136 PrivilegedGetPageContext(JspFactoryImpl factory, 137 Servlet servlet, 138 ServletRequest request, 139 ServletResponse response, 140 String 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 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 { 166 167 private JspFactoryImpl factory; 168 private PageContext pageContext; 169 170 PrivilegedReleasePageContext(JspFactoryImpl factory, 171 PageContext pageContext) { 172 this.factory = factory; 173 this.pageContext = pageContext; 174 } 175 176 public Object run() { 177 factory.internalReleasePageContext(pageContext); 178 return null; 179 } 180 } 181 182 public JspApplicationContext getJspApplicationContext(ServletContext context) { 183 return JspApplicationContextImpl.getInstance(context); 184 } 185 } 186 | Popular Tags |