1 29 30 package com.caucho.quercus.servlet; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.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.server.connection.CauchoResponse; 42 import com.caucho.server.session.SessionManager; 43 import com.caucho.server.webapp.*; 44 import com.caucho.util.L10N; 45 import com.caucho.vfs.*; 46 47 import javax.servlet.*; 48 import javax.servlet.http.HttpServlet ; 49 import javax.servlet.http.HttpServletRequest ; 50 import javax.servlet.http.HttpServletResponse ; 51 import javax.sql.DataSource ; 52 import java.io.IOException ; 53 import java.io.OutputStream ; 54 import java.util.logging.Level ; 55 import java.util.logging.Logger ; 56 57 60 public class ResinQuercusServlet extends QuercusServletImpl 61 { 62 private static final L10N L = new L10N(ResinQuercusServlet.class); 63 private static final Logger log 64 = Logger.getLogger(ResinQuercusServlet.class.getName()); 65 66 private WebApp _webApp; 67 68 71 @Override 72 public void init(ServletConfig config) 73 throws ServletException 74 { 75 super.init(config); 76 77 _webApp = (WebApp) config.getServletContext(); 78 79 ResinQuercus quercus = (ResinQuercus) getQuercus(); 80 81 quercus.setWebApp(_webApp); 82 getQuercus().setPwd(Vfs.lookup()); 83 } 84 85 88 public void service(HttpServletRequest request, 89 HttpServletResponse response) 90 throws ServletException, IOException 91 { 92 try { 93 Path path = getPath(request); 94 95 QuercusPage page = getQuercus().parse(path); 96 97 WriteStream ws; 98 99 if (response instanceof CauchoResponse) { 102 ws = Vfs.openWrite(((CauchoResponse) response).getResponseStream()); 103 } else { 104 OutputStream out = response.getOutputStream(); 105 106 ws = Vfs.openWrite(out); 107 } 108 109 Env env = new Env(getQuercus(), page, ws, request, response); 110 try { 111 env.setGlobalValue("request", env.wrapJava(request)); 112 env.setGlobalValue("response", env.wrapJava(request)); 113 114 env.start(); 115 116 String prepend = env.getIniString("auto_prepend_file"); 117 if (prepend != null) { 118 QuercusPage prependPage = getQuercus().parse(env.lookup(prepend)); 119 prependPage.executeTop(env); 120 } 121 122 page.executeTop(env); 123 124 String append = env.getIniString("auto_append_file"); 125 if (append != null) { 126 QuercusPage appendPage = getQuercus().parse(env.lookup(append)); 127 appendPage.executeTop(env); 128 } 129 } 131 catch (QuercusExitException e) { 132 throw e; 133 } 134 catch (QuercusLineRuntimeException e) { 135 log.log(Level.FINE, e.toString(), e); 136 137 } 139 catch (QuercusValueException e) { 140 log.log(Level.FINE, e.toString(), e); 141 142 ws.println(e.toString()); 143 144 } 146 catch (Throwable e) { 147 if (response.isCommitted()) 148 e.printStackTrace(ws.getPrintWriter()); 149 150 ws = null; 151 152 throw e; 153 } 154 finally { 155 env.close(); 156 157 if (ws != null) 159 ws.close(); 160 } 161 } 162 catch (QuercusDieException e) { 163 log.log(Level.FINE, e.toString(), e); 164 } 166 catch (QuercusExitException e) { 167 log.log(Level.FINER, e.toString(), e); 168 } 170 catch (RuntimeException e) { 171 throw e; 172 } 173 catch (Throwable e) { 174 throw new ServletException(e); 175 } 176 } 177 178 Path getPath(HttpServletRequest req) 179 { 180 String scriptPath = req.getServletPath(); 181 String pathInfo = req.getPathInfo(); 182 183 Path pwd = Vfs.lookup(); 184 185 Path path = pwd.lookup(req.getRealPath(scriptPath)); 186 187 if (path.isFile()) 188 return path; 189 190 192 String fullPath; 193 if (pathInfo != null) 194 fullPath = scriptPath + pathInfo; 195 else 196 fullPath = scriptPath; 197 198 return Vfs.lookup().lookup(req.getRealPath(fullPath)); 199 } 200 201 204 @Override 205 protected Quercus getQuercus() 206 { 207 synchronized (this) { 208 if (_quercus == null) 209 _quercus = new ResinQuercus(); 210 } 211 212 return _quercus; 213 } 214 } 215 216 | Popular Tags |