KickJava   Java API By Example, From Geeks To Geeks.

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


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

57 public class QuercusServletImpl
58 {
59   private static final L10N L = new L10N(QuercusServletImpl.class);
60   private static final Logger JavaDoc log
61     = Logger.getLogger(QuercusServletImpl.class.getName());
62
63   protected Quercus _quercus;
64
65   private ServletConfig _config;
66   private ServletContext _servletContext;
67
68   /**
69    * initialize the script manager.
70    */

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   /**
81    * Service.
82    */

83   public void service(HttpServletRequest JavaDoc request,
84                       HttpServletResponse JavaDoc response)
85     throws ServletException, IOException JavaDoc
86   {
87     try {
88       Path path = getPath(request);
89
90       QuercusPage page = getQuercus().parse(path);
91
92       WriteStream ws;
93       
94       OutputStream JavaDoc 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 JavaDoc 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 JavaDoc append = env.getIniString("auto_append_file");
115     if (append != null) {
116       QuercusPage appendPage = getQuercus().parse(env.lookup(append));
117       appendPage.executeTop(env);
118     }
119      // return;
120
}
121       catch (QuercusExitException e) {
122         throw e;
123       }
124       catch (QuercusLineRuntimeException e) {
125         log.log(Level.FINE, e.toString(), e);
126
127       // return;
128
}
129       catch (QuercusValueException e) {
130         log.log(Level.FINE, e.toString(), e);
131     
132     ws.println(e.toString());
133
134       // return;
135
}
136       catch (Throwable JavaDoc 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         // don't want a flush for an exception
148
if (ws != null)
149           ws.close();
150       }
151     }
152     catch (QuercusDieException e) {
153       log.log(Level.FINE, e.toString(), e);
154       // normal exit
155
}
156     catch (QuercusExitException e) {
157       log.log(Level.FINER, e.toString(), e);
158       // normal exit
159
}
160     catch (RuntimeException JavaDoc e) {
161       throw e;
162     }
163     catch (Throwable JavaDoc e) {
164       throw new ServletException(e);
165     }
166   }
167
168   Path getPath(HttpServletRequest JavaDoc req)
169   {
170     String JavaDoc scriptPath = req.getServletPath();
171     String JavaDoc 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     // XXX: include
181

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

194   protected Quercus getQuercus()
195   {
196     synchronized (this) {
197       if (_quercus == null)
198     _quercus = new Quercus();
199     }
200
201     return _quercus;
202   }
203
204   /**
205    * Gets the script manager.
206    */

207   public void destroy()
208   {
209     _quercus.close();
210   }
211 }
212
213
Popular Tags