KickJava   Java API By Example, From Geeks To Geeks.

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


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.Path;
43 import com.caucho.vfs.WriteStream;
44
45 import javax.servlet.*;
46 import javax.servlet.http.*;
47 import javax.sql.DataSource JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.OutputStream JavaDoc;
50 import java.util.logging.Level JavaDoc;
51 import java.util.logging.Logger JavaDoc;
52
53 /**
54  * Servlet to call PHP through javax.script.
55  */

56 public class QuercusServlet
57   extends HttpServlet
58 {
59   private static final L10N L = new L10N(QuercusServlet.class);
60   private static final Logger JavaDoc 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 JavaDoc cl = Class.forName("com.caucho.quercus.servlet.ProQuercusServlet");
73     _impl = (QuercusServletImpl) cl.newInstance();
74       } catch (Exception JavaDoc e) {
75     log.log(Level.FINEST, e.toString(), e);
76       }
77     }
78     
79     if (_impl == null) {
80       try {
81     Class JavaDoc cl = Class.forName("com.caucho.quercus.servlet.ResinQuercusServlet");
82     _impl = (QuercusServletImpl) cl.newInstance();
83       } catch (Exception JavaDoc e) {
84     log.log(Level.FINEST, e.toString(), e);
85       }
86     }
87     
88     if (_impl == null)
89       _impl = new QuercusServletImpl();
90   }
91   
92   /**
93    * Set true if quercus should be compiled into Java.
94    */

95   public void setCompile(String JavaDoc 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   /**
117    * Set the default data source.
118    */

119   public void setDatabase(DataSource JavaDoc 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   /**
129    * Sets the strict mode.
130    */

131   public void setStrict(boolean isStrict)
132   {
133     getQuercus().setStrict(isStrict);
134   }
135
136   /**
137    * Adds a quercus module.
138    */

139   public void addModule(QuercusModule module)
140     throws ConfigException
141   {
142     getQuercus().addModule(module);
143   }
144
145   /**
146    * Adds a quercus class.
147    */

148   public void addClass(PhpClassConfig classConfig)
149     throws ConfigException
150   {
151     getQuercus().addJavaClass(classConfig.getName(), classConfig.getType());
152   }
153
154   /**
155    * Adds a quercus class.
156    */

157   public void addImplClass(PhpClassConfig classConfig)
158     throws ConfigException
159   {
160     getQuercus().addImplClass(classConfig.getName(), classConfig.getType());
161   }
162
163   /**
164    * Adds a quercus.ini configuration
165    */

166   public PhpIni createPhpIni()
167     throws ConfigException
168   {
169     return new PhpIni(getQuercus());
170   }
171
172   /**
173    * Adds a $_SERVER configuration
174    */

175   public ServerEnv createServerEnv()
176     throws ConfigException
177   {
178     return new ServerEnv(getQuercus());
179   }
180
181   /**
182    * Adds a quercus.ini configuration
183    */

184   public void setIniFile(Path path)
185   {
186     getQuercus().setIniFile(path);
187   }
188
189   /**
190    * Sets the script encoding.
191    */

192   public void setScriptEncoding(String JavaDoc encoding)
193     throws ConfigException
194   {
195     getQuercus().setScriptEncoding(encoding);
196   }
197
198   /**
199    * Gets the script manager.
200    */

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       // XXX: for validating QA
218
// throw new ServletException("compile must be set.");
219
}
220
221     _impl.init(config);
222   }
223
224   /**
225    * Service.
226    */

227   public void service(HttpServletRequest request,
228                       HttpServletResponse response)
229     throws ServletException, IOException JavaDoc
230   {
231     _impl.service(request, response);
232   }
233
234   /**
235    * Returns the Quercus instance.
236    */

237   private Quercus getQuercus()
238   {
239     synchronized (this) {
240       if (_quercus == null)
241     _quercus = _impl.getQuercus();
242     }
243
244     return _quercus;
245   }
246
247   /**
248    * Gets the script manager.
249    */

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     /**
265      * Sets an arbitrary property.
266      */

267     public void put(String JavaDoc key, String JavaDoc 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     /**
282      * Sets an arbitrary property.
283      */

284     public void put(String JavaDoc key, String JavaDoc value)
285     {
286       _quercus.setServerEnv(key, value);
287     }
288   }
289 }
290
291
Popular Tags