KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > JspManager


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.jsp;
31
32 import com.caucho.java.LineMap;
33 import com.caucho.loader.DynamicClassLoader;
34 import com.caucho.loader.SimpleLoader;
35 import com.caucho.log.Log;
36 import com.caucho.server.connection.CauchoRequest;
37 import com.caucho.server.connection.CauchoResponse;
38 import com.caucho.server.util.CauchoSystem;
39 import com.caucho.server.webapp.WebApp;
40 import com.caucho.util.L10N;
41 import com.caucho.vfs.Depend;
42 import com.caucho.vfs.Path;
43 import com.caucho.vfs.PersistentDependency;
44
45 import javax.servlet.SingleThreadModel JavaDoc;
46 import javax.servlet.jsp.HttpJspPage JavaDoc;
47 import javax.servlet.jsp.JspFactory JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.logging.Level JavaDoc;
51 import java.util.logging.Logger JavaDoc;
52
53 /**
54  * Manages JSP templates.
55  */

56 public class JspManager extends PageManager {
57   private final static L10N L = new L10N(JspManager.class);
58   private final static Logger JavaDoc log = Log.open(JspManager.class);
59   
60   private static int _count;
61
62   private boolean _isXml;
63   private boolean _isLoadTldOnInit;
64   private boolean _precompile = true;
65
66   public JspManager()
67   {
68     if (JspFactory.getDefaultFactory() == null)
69       JspFactory.setDefaultFactory(new QJspFactory());
70   }
71
72   /**
73    * Returns true if JSP pages are precompiled.
74    */

75   public boolean getPrecompile()
76   {
77     return _precompile;
78   }
79
80   /**
81    * Set true if xml is default.
82    */

83   public void setXml(boolean isXml)
84   {
85     _isXml = isXml;
86   }
87
88   /**
89    * Set true if tld should be loaded on init.
90    */

91   public void setLoadTldOnInit(boolean isLoadOnInit)
92   {
93     _isLoadTldOnInit = isLoadOnInit;
94   }
95
96   /**
97    * Initialize the manager.
98    */

99   public void init()
100   {
101     if (_isLoadTldOnInit) {
102       try {
103     WebApp app = getWebApp();
104     
105     TldManager tld = TldManager.create(new AppResourceManager(app), app);
106
107     // must be initialized at startup for <listeners>, e.g. JSF
108
tld.init();
109       } catch (Exception JavaDoc e) {
110     log.log(Level.WARNING, e.toString(), e);
111       }
112     }
113   }
114
115   /**
116    * Creates a JSP page. The calling servlet will execute the page by
117    * calling the service method.
118    *
119    * @param path path to the JSP file
120    * @param uri the JSP's uri
121    * @param uriPwd the parent of the JSP's uri
122    *
123    * @return the compiled JSP page
124    */

125   Page createGeneratedPage(Path path, String JavaDoc uri, String JavaDoc className,
126                ArrayList JavaDoc<PersistentDependency> dependList)
127     throws Exception JavaDoc
128   {
129     return createPage(path, uri, className, dependList, true);
130   }
131
132   /**
133    * Creates a JSP page. The calling servlet will execute the page by
134    * calling the service method.
135    *
136    * @param path path to the JSP file
137    * @param uri the JSP's uri
138    * @param uriPwd the parent of the JSP's uri
139    *
140    * @return the compiled JSP page
141    */

142   Page createPage(Path path, String JavaDoc uri, String JavaDoc className,
143           ArrayList JavaDoc<PersistentDependency> dependList)
144     throws Exception JavaDoc
145   {
146     return createPage(path, uri, className, dependList, false);
147   }
148
149   /**
150    * Creates a JSP page. The calling servlet will execute the page by
151    * calling the service method.
152    *
153    * @param path path to the JSP file
154    * @param uri the JSP's uri
155    * @param uriPwd the parent of the JSP's uri
156    *
157    * @return the compiled JSP page
158    */

159   private Page createPage(Path path, String JavaDoc uri, String JavaDoc className,
160           ArrayList JavaDoc<PersistentDependency> dependList,
161           boolean isGenerated)
162     throws Exception JavaDoc
163   {
164     Class JavaDoc jspClass = null;
165
166     Page page = compile(path, uri, className, dependList, isGenerated);
167
168     if (page == null) {
169       return null;
170     }
171
172     // need to load class, too
173

174     //Page page = loadPage(jspClass, parseState.getLineMap(), req);
175
page = loadPage(page, null);
176
177     boolean alwaysModified = false;
178     if (alwaysModified)
179       page._caucho_setAlwaysModified();
180
181     return page;
182   }
183
184   protected void initPageManager()
185   {
186     JspCompiler compiler = new JspCompiler();
187
188     compiler.setWebApp(_webApp);
189     compiler.setXml(_isXml);
190
191     try {
192       compiler.init();
193     } catch (Exception JavaDoc e) {
194       throw new RuntimeException JavaDoc(e);
195     }
196   }
197
198   Page compile(Path path, String JavaDoc uri, String JavaDoc className,
199            ArrayList JavaDoc<PersistentDependency> dependList,
200            boolean isGenerated)
201     throws Exception JavaDoc
202   {
203     WebApp app = getWebApp();
204     JspCompiler compiler = new JspCompiler();
205
206     compiler.setWebApp(_webApp);
207     compiler.setXml(_isXml);
208
209     Page page = null;
210
211     try {
212       if (_precompile || _autoCompile)
213     page = preload(className, app.getClassLoader(), app.getAppDir());
214     } catch (Throwable JavaDoc e) {
215       log.log(Level.WARNING, e.toString(), e);
216     }
217
218     if (page != null) {
219       if (log.isLoggable(Level.FINE))
220         log.fine("loading pre-compiled page for " + uri);
221       
222       return page;
223     }
224
225     if (path == null || ! path.canRead() || path.isDirectory() ||
226         ! _autoCompile) {
227       return null;
228     }
229
230     JspCompilerInstance compilerInst =
231       compiler.getCompilerInstance(path, uri, className);
232
233     compilerInst.setGeneratedSource(isGenerated);
234     compilerInst.addDependList(dependList);
235
236     page = compilerInst.compile();
237
238     Path classPath = getClassDir().lookup(className.replace('.', '/') +
239                       ".class");
240
241     if (classPath.canRead())
242       page._caucho_addDepend(new Depend(classPath));
243
244     return page;
245   }
246
247   void killPage(CauchoRequest request, CauchoResponse response, Page page)
248   {
249   }
250
251   /**
252    * True if the pre-load would load a valid class.
253    */

254   Page preload(String JavaDoc className,
255            ClassLoader JavaDoc parentLoader,
256            Path appDir)
257     throws Exception JavaDoc
258   {
259     DynamicClassLoader loader;
260
261     String JavaDoc fullClassName = className;
262     String JavaDoc mangledName = fullClassName.replace('.', '/');
263     
264     Path classPath = getClassDir().lookup(mangledName + ".class");
265
266     /*
267     if (! classPath.exists())
268       return preloadStatic(mangledName);
269     */

270
271     loader = SimpleLoader.create(parentLoader, getClassDir(), null);
272
273     Class JavaDoc cl = null;
274
275     // If the loading fails, remove the class because it may be corrupted
276
try {
277       cl = CauchoSystem.loadClass(fullClassName, false, loader);
278     } catch (ClassNotFoundException JavaDoc e) {
279       log.finest(e.toString());
280
281       return null;
282     } catch (NoClassDefFoundError JavaDoc e) {
283       log.finest(e.toString());
284
285       return null;
286     } catch (OutOfMemoryError JavaDoc e) {
287       throw e;
288     } catch (Throwable JavaDoc e) {
289       log.log(Level.FINEST, e.toString(), e);
290       
291       if (_autoCompile) {
292         try {
293       log.warning("removing generated file " +
294               classPath + " due to " + e.toString());
295       
296       classPath.remove();
297         } catch (IOException JavaDoc e1) {
298           log.log(Level.FINE, e1.toString(), e1);
299         }
300       }
301
302       return null;
303     }
304
305     HttpJspPage JavaDoc jspPage = (HttpJspPage JavaDoc) cl.newInstance();
306     Page page = null;
307     
308     if (jspPage instanceof CauchoPage) {
309       CauchoPage cPage = (CauchoPage) jspPage;
310
311       cPage.init(appDir);
312
313       if (cPage instanceof Page)
314     ((Page) cPage)._caucho_setJspManager(this);
315
316       if (_autoCompile && cPage._caucho_isModified())
317         return null;
318       else if (cPage instanceof Page)
319     page = (Page) cPage;
320       else
321     page = new WrapperPage(jspPage);
322     }
323     else if (jspPage instanceof SingleThreadModel JavaDoc)
324       page = new SingleThreadWrapperPage((HttpJspPage JavaDoc) jspPage);
325     else
326       page = new WrapperPage(jspPage);
327
328     page._caucho_addDepend(new Depend(classPath));
329
330     return page;
331   }
332
333   // XXX: disable static pages. The slight memory gain is probably
334
// not worth the separate code paths.
335
private Page preloadStatic(String JavaDoc mangledName)
336   {
337     String JavaDoc staticName = mangledName + ".static";
338     String JavaDoc dependName = mangledName + ".depend";
339     
340     Path staticPath = getClassDir().lookup(staticName);
341
342     if (! staticPath.canRead())
343       return null;
344
345     Path dependPath = getClassDir().lookup(dependName);
346     
347     if (! dependPath.canRead())
348       return null;
349
350     try {
351       ArrayList JavaDoc<Depend> dependList = StaticPage.parseDepend(dependPath);
352
353       if (dependList == null)
354     return null;
355       
356       StaticPage page = new StaticPage(staticPath, true);
357
358       for (int i = 0; i < dependList.size(); i++) {
359     Depend depend = dependList.get(i);
360
361     if (depend.isModified())
362       return null;
363
364     page._caucho_addDepend(depend);
365       }
366
367       return page;
368     } catch (Throwable JavaDoc e) {
369       log.log(Level.FINER, e.toString(), e);
370     }
371
372     return null;
373   }
374
375
376   /**
377    * Loads an already-compiled JSP class.
378    *
379    * @param jspClass the class object of the JSP file.
380    * @param lineMap the java to JSP line map.
381    */

382   private Page loadPage(Page page, LineMap lineMap)
383     throws Exception JavaDoc
384   {
385     page.init(_webApp.getAppDir());
386
387     /*
388     ServletConfig config = null;
389
390     if (req != null)
391       config = (ServletConfig) req.getAttribute("caucho.jsp.servlet-config");
392
393     if (config == null)
394       config = new JspServletConfig(_webApp, null,
395                                     RequestAdapter.getPageServletPath(req));
396
397     if (config == null)
398       config = new JspServletConfig(_webApp, null, "/foo");
399     */

400
401     page._caucho_setJspManager(this);
402
403     return page;
404   }
405
406   void unload(Page page)
407   {
408   }
409 }
410
Popular Tags