1 29 30 package com.caucho.quercus.servlet; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.quercus.Quercus; 34 import com.caucho.quercus.QuercusDieException; 35 import com.caucho.quercus.QuercusExitException; 36 import com.caucho.quercus.QuercusLineRuntimeException; 37 import com.caucho.quercus.env.Env; 38 import com.caucho.quercus.env.QuercusValueException; 39 import com.caucho.quercus.module.QuercusModule; 40 import com.caucho.quercus.page.QuercusPage; 41 import com.caucho.util.L10N; 42 import com.caucho.vfs.Path; 43 import com.caucho.vfs.WriteStream; 44 45 import javax.servlet.*; 46 import javax.servlet.http.*; 47 import javax.sql.DataSource ; 48 import java.io.IOException ; 49 import java.io.OutputStream ; 50 import java.util.logging.Level ; 51 import java.util.logging.Logger ; 52 53 56 public class QuercusServlet 57 extends HttpServlet 58 { 59 private static final L10N L = new L10N(QuercusServlet.class); 60 private static final Logger log 61 = Logger.getLogger(QuercusServlet.class.getName()); 62 63 private Quercus _quercus; 64 private QuercusServletImpl _impl; 65 66 private boolean _isCompileSet; 67 68 public QuercusServlet() 69 { 70 if (_impl == null) { 71 try { 72 Class cl = Class.forName("com.caucho.quercus.servlet.ProQuercusServlet"); 73 _impl = (QuercusServletImpl) cl.newInstance(); 74 } catch (Exception e) { 75 log.log(Level.FINEST, e.toString(), e); 76 } 77 } 78 79 if (_impl == null) { 80 try { 81 Class cl = Class.forName("com.caucho.quercus.servlet.ResinQuercusServlet"); 82 _impl = (QuercusServletImpl) cl.newInstance(); 83 } catch (Exception e) { 84 log.log(Level.FINEST, e.toString(), e); 85 } 86 } 87 88 if (_impl == null) 89 _impl = new QuercusServletImpl(); 90 } 91 92 95 public void setCompile(String isCompile) 96 throws ConfigException 97 { 98 _isCompileSet = true; 99 100 Quercus quercus = getQuercus(); 101 102 if ("true".equals(isCompile) || "".equals(isCompile)) { 103 quercus.setCompile(true); 104 quercus.setLazyCompile(false); 105 } else if ("false".equals(isCompile)) { 106 quercus.setCompile(false); 107 quercus.setLazyCompile(false); 108 } else if ("lazy".equals(isCompile)) { 109 quercus.setLazyCompile(true); 110 } else 111 throw new ConfigException(L.l( 112 "'{0}' is an unknown compile value. Values are 'true', 'false', or 'lazy'.", 113 isCompile)); 114 } 115 116 119 public void setDatabase(DataSource database) 120 throws ConfigException 121 { 122 if (database == null) 123 throw new ConfigException(L.l("invalid database")); 124 125 getQuercus().setDatabase(database); 126 } 127 128 131 public void setStrict(boolean isStrict) 132 { 133 getQuercus().setStrict(isStrict); 134 } 135 136 139 public void addModule(QuercusModule module) 140 throws ConfigException 141 { 142 getQuercus().addModule(module); 143 } 144 145 148 public void addClass(PhpClassConfig classConfig) 149 throws ConfigException 150 { 151 getQuercus().addJavaClass(classConfig.getName(), classConfig.getType()); 152 } 153 154 157 public void addImplClass(PhpClassConfig classConfig) 158 throws ConfigException 159 { 160 getQuercus().addImplClass(classConfig.getName(), classConfig.getType()); 161 } 162 163 166 public PhpIni createPhpIni() 167 throws ConfigException 168 { 169 return new PhpIni(getQuercus()); 170 } 171 172 175 public ServerEnv createServerEnv() 176 throws ConfigException 177 { 178 return new ServerEnv(getQuercus()); 179 } 180 181 184 public void setIniFile(Path path) 185 { 186 getQuercus().setIniFile(path); 187 } 188 189 192 public void setScriptEncoding(String encoding) 193 throws ConfigException 194 { 195 getQuercus().setScriptEncoding(encoding); 196 } 197 198 201 public void init(ServletConfig config) 202 throws ServletException 203 { 204 super.init(config); 205 206 initImpl(config); 207 } 208 209 private void initImpl(ServletConfig config) 210 throws ServletException 211 { 212 getQuercus(); 213 214 if (! _isCompileSet) { 215 getQuercus().setLazyCompile(true); 216 217 } 220 221 _impl.init(config); 222 } 223 224 227 public void service(HttpServletRequest request, 228 HttpServletResponse response) 229 throws ServletException, IOException 230 { 231 _impl.service(request, response); 232 } 233 234 237 private Quercus getQuercus() 238 { 239 synchronized (this) { 240 if (_quercus == null) 241 _quercus = _impl.getQuercus(); 242 } 243 244 return _quercus; 245 } 246 247 250 public void destroy() 251 { 252 _quercus.close(); 253 _impl.destroy(); 254 } 255 256 public static class PhpIni { 257 private Quercus _quercus; 258 259 PhpIni(Quercus quercus) 260 { 261 _quercus = quercus; 262 } 263 264 267 public void put(String key, String value) 268 { 269 _quercus.setIni(key, value); 270 } 271 } 272 273 public static class ServerEnv { 274 private Quercus _quercus; 275 276 ServerEnv(Quercus quercus) 277 { 278 _quercus = quercus; 279 } 280 281 284 public void put(String key, String value) 285 { 286 _quercus.setServerEnv(key, value); 287 } 288 } 289 } 290 291 | Popular Tags |