KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > servlet > ResinQuercusServlet


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
49 import javax.servlet.http.HttpServletRequest JavaDoc;
50 import javax.servlet.http.HttpServletResponse JavaDoc;
51 import javax.sql.DataSource JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.io.OutputStream JavaDoc;
54 import java.util.logging.Level JavaDoc;
55 import java.util.logging.Logger JavaDoc;
56
57 /**
58  * Servlet to call PHP through javax.script.
59  */

60 public class ResinQuercusServlet extends QuercusServletImpl
61 {
62   private static final L10N L = new L10N(ResinQuercusServlet.class);
63   private static final Logger JavaDoc log
64     = Logger.getLogger(ResinQuercusServlet.class.getName());
65
66   private WebApp _webApp;
67
68   /**
69    * initialize the script manager.
70    */

71   @Override JavaDoc
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   /**
86    * Service.
87    */

88   public void service(HttpServletRequest JavaDoc request,
89                       HttpServletResponse JavaDoc response)
90     throws ServletException, IOException JavaDoc
91   {
92     try {
93       Path path = getPath(request);
94
95       QuercusPage page = getQuercus().parse(path);
96
97       WriteStream ws;
98       
99       // XXX: check if correct. PHP doesn't expect the lower levels
100
// to deal with the encoding, so this may be okay
101
if (response instanceof CauchoResponse) {
102     ws = Vfs.openWrite(((CauchoResponse) response).getResponseStream());
103       } else {
104     OutputStream JavaDoc 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 JavaDoc 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 JavaDoc append = env.getIniString("auto_append_file");
125     if (append != null) {
126       QuercusPage appendPage = getQuercus().parse(env.lookup(append));
127       appendPage.executeTop(env);
128     }
129      // return;
130
}
131       catch (QuercusExitException e) {
132         throw e;
133       }
134       catch (QuercusLineRuntimeException e) {
135         log.log(Level.FINE, e.toString(), e);
136
137       // return;
138
}
139       catch (QuercusValueException e) {
140         log.log(Level.FINE, e.toString(), e);
141     
142     ws.println(e.toString());
143
144       // return;
145
}
146       catch (Throwable JavaDoc 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         // don't want a flush for an exception
158
if (ws != null)
159           ws.close();
160       }
161     }
162     catch (QuercusDieException e) {
163       log.log(Level.FINE, e.toString(), e);
164       // normal exit
165
}
166     catch (QuercusExitException e) {
167       log.log(Level.FINER, e.toString(), e);
168       // normal exit
169
}
170     catch (RuntimeException JavaDoc e) {
171       throw e;
172     }
173     catch (Throwable JavaDoc e) {
174       throw new ServletException(e);
175     }
176   }
177
178   Path getPath(HttpServletRequest JavaDoc req)
179   {
180     String JavaDoc scriptPath = req.getServletPath();
181     String JavaDoc 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     // XXX: include
191

192     String JavaDoc 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   /**
202    * Returns the Quercus instance.
203    */

204   @Override JavaDoc
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