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.*; 43 44 import javax.servlet.*; 45 import javax.servlet.http.HttpServlet ; 46 import javax.servlet.http.HttpServletRequest ; 47 import javax.servlet.http.HttpServletResponse ; 48 import javax.sql.DataSource ; 49 import java.io.IOException ; 50 import java.io.OutputStream ; 51 import java.util.logging.Level ; 52 import java.util.logging.Logger ; 53 54 57 public class QuercusServletImpl 58 { 59 private static final L10N L = new L10N(QuercusServletImpl.class); 60 private static final Logger log 61 = Logger.getLogger(QuercusServletImpl.class.getName()); 62 63 protected Quercus _quercus; 64 65 private ServletConfig _config; 66 private ServletContext _servletContext; 67 68 71 public void init(ServletConfig config) 72 throws ServletException 73 { 74 _config = config; 75 _servletContext = config.getServletContext(); 76 77 getQuercus().setPwd(new FilePath(_servletContext.getRealPath("/"))); 78 } 79 80 83 public void service(HttpServletRequest request, 84 HttpServletResponse response) 85 throws ServletException, IOException 86 { 87 try { 88 Path path = getPath(request); 89 90 QuercusPage page = getQuercus().parse(path); 91 92 WriteStream ws; 93 94 OutputStream out = response.getOutputStream(); 95 96 VfsStream s = new VfsStream(null, out); 97 ws = new WriteStream(s); 98 99 Env env = new Env(getQuercus(), page, ws, request, response); 100 try { 101 env.setGlobalValue("request", env.wrapJava(request)); 102 env.setGlobalValue("response", env.wrapJava(request)); 103 104 env.start(); 105 106 String prepend = env.getIniString("auto_prepend_file"); 107 if (prepend != null) { 108 QuercusPage prependPage = getQuercus().parse(env.lookup(prepend)); 109 prependPage.executeTop(env); 110 } 111 112 page.executeTop(env); 113 114 String append = env.getIniString("auto_append_file"); 115 if (append != null) { 116 QuercusPage appendPage = getQuercus().parse(env.lookup(append)); 117 appendPage.executeTop(env); 118 } 119 } 121 catch (QuercusExitException e) { 122 throw e; 123 } 124 catch (QuercusLineRuntimeException e) { 125 log.log(Level.FINE, e.toString(), e); 126 127 } 129 catch (QuercusValueException e) { 130 log.log(Level.FINE, e.toString(), e); 131 132 ws.println(e.toString()); 133 134 } 136 catch (Throwable e) { 137 if (response.isCommitted()) 138 e.printStackTrace(ws.getPrintWriter()); 139 140 ws = null; 141 142 throw e; 143 } 144 finally { 145 env.close(); 146 147 if (ws != null) 149 ws.close(); 150 } 151 } 152 catch (QuercusDieException e) { 153 log.log(Level.FINE, e.toString(), e); 154 } 156 catch (QuercusExitException e) { 157 log.log(Level.FINER, e.toString(), e); 158 } 160 catch (RuntimeException e) { 161 throw e; 162 } 163 catch (Throwable e) { 164 throw new ServletException(e); 165 } 166 } 167 168 Path getPath(HttpServletRequest req) 169 { 170 String scriptPath = req.getServletPath(); 171 String pathInfo = req.getPathInfo(); 172 173 Path pwd = new FilePath(System.getProperty("user.dir")); 174 175 Path path = pwd.lookup(req.getRealPath(scriptPath)); 176 177 if (path.isFile()) 178 return path; 179 180 182 String fullPath; 183 if (pathInfo != null) 184 fullPath = scriptPath + pathInfo; 185 else 186 fullPath = scriptPath; 187 188 return pwd.lookup(req.getRealPath(fullPath)); 189 } 190 191 194 protected Quercus getQuercus() 195 { 196 synchronized (this) { 197 if (_quercus == null) 198 _quercus = new Quercus(); 199 } 200 201 return _quercus; 202 } 203 204 207 public void destroy() 208 { 209 _quercus.close(); 210 } 211 } 212 213 | Popular Tags |