KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.ConfigException;
33 import com.caucho.config.types.FileSetType;
34 import com.caucho.config.types.PathPatternType;
35 import com.caucho.java.JavaCompiler;
36 import com.caucho.java.LineMap;
37 import com.caucho.lifecycle.Lifecycle;
38 import com.caucho.log.Log;
39 import com.caucho.server.webapp.WebApp;
40 import com.caucho.util.CompileException;
41 import com.caucho.util.L10N;
42 import com.caucho.vfs.Path;
43 import com.caucho.vfs.Vfs;
44
45 import javax.annotation.PostConstruct;
46 import javax.servlet.jsp.JspFactory JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.logging.Level JavaDoc;
49 import java.util.logging.Logger JavaDoc;
50
51 /**
52  * Resource for precompiling all the *.jsp files on startup.
53  */

54 public class JspPrecompileResource {
55   private static final Logger JavaDoc log = Log.open(JspPrecompileResource.class);
56   private static final L10N L = new L10N(JspPrecompileResource.class);
57
58   private FileSetType _fileSet;
59   
60   private WebApp _webApp;
61
62   private final Lifecycle _lifecycle = new Lifecycle();
63
64   /**
65    * Sets the webApp.
66    */

67   public void setWebApp(WebApp app)
68   {
69     _webApp = app;
70   }
71
72   /**
73    * Add a pattern.
74    */

75   public FileSetType createFileset()
76   {
77     if (_fileSet == null) {
78       _fileSet = new FileSetType();
79       _fileSet.setDir(Vfs.lookup());
80     }
81
82     return _fileSet;
83   }
84
85   /**
86    * @deprecated
87    */

88   public FileSetType createFileSet()
89   {
90     return createFileset();
91   }
92
93   /**
94    * Initialize the resource.
95    */

96   @PostConstruct
97   public void init()
98     throws ConfigException
99   {
100     Path pwd = Vfs.lookup();
101
102     if (_fileSet == null) {
103       createFileset().addInclude(new PathPatternType("**/*.jsp"));
104     }
105
106     if (_webApp == null) {
107       _webApp = WebApp.getLocal();
108     }
109
110     if (_webApp == null)
111       throw new ConfigException(L.l("JspPrecompileResource must be used in a web-app context."));
112
113     start();
114   }
115
116   /**
117    * Starts the resource.
118    */

119   public void start()
120   {
121     if (! _lifecycle.toActive())
122       return;
123     
124     // JspManager manager = new JspManager(_webApp);
125

126     if (JspFactory.getDefaultFactory() == null)
127       JspFactory.setDefaultFactory(new QJspFactory());
128     
129     JspCompiler compiler = new JspCompiler();
130     compiler.setWebApp(_webApp);
131
132     ArrayList JavaDoc<Path> paths = _fileSet.getPaths();
133     ArrayList JavaDoc<String JavaDoc> classes = new ArrayList JavaDoc<String JavaDoc>();
134
135     String JavaDoc contextPath = _webApp.getContextPath();
136     if (! contextPath.endsWith("/"))
137       contextPath = contextPath + "/";
138     
139     Path pwd = Vfs.lookup();
140     
141     for (int i = 0; i < paths.size(); i++) {
142       Path path = paths.get(i);
143
144       String JavaDoc uri = path.getPath().substring(pwd.getPath().length());
145
146       if (_webApp.getContext(contextPath + uri) != _webApp)
147     continue;
148
149       String JavaDoc className = JspCompiler.urlToClassName(uri);
150
151       try {
152     CauchoPage page = (CauchoPage) compiler.loadClass(className, true);
153
154     page.init(pwd);
155
156     if (! page._caucho_isModified()) {
157       log.fine("pre-loaded " + uri);
158       continue;
159     }
160       } catch (ClassNotFoundException JavaDoc e) {
161       } catch (Throwable JavaDoc e) {
162     log.log(Level.FINER, e.toString(), e);
163       }
164
165       log.fine("compiling " + uri);
166       
167       try {
168     JspCompilerInstance compilerInst;
169     compilerInst = compiler.getCompilerInstance(path, uri, className);
170
171     JspGenerator generator = compilerInst.generate();
172     
173     if (generator.isStatic())
174       continue;
175     
176     LineMap lineMap = generator.getLineMap();
177
178     classes.add(className.replace('.', '/') + ".java");
179       } catch (Exception JavaDoc e) {
180     if (e instanceof CompileException)
181       log.warning(e.getMessage());
182     else
183       log.log(Level.WARNING, e.toString(), e);
184
185     continue;
186       }
187     }
188
189     if (classes.size() == 0)
190       return;
191
192     try {
193       JavaCompiler javaCompiler = JavaCompiler.create(null);
194       javaCompiler.setClassDir(compiler.getClassDir());
195
196       String JavaDoc files[] = new String JavaDoc[classes.size()];
197       classes.toArray(files);
198
199       javaCompiler.compileBatch(files);
200     } catch (Exception JavaDoc e) {
201       if (e instanceof CompileException)
202     log.warning(e.getMessage());
203       else
204     log.log(Level.WARNING, e.toString(), e);
205     }
206   }
207 }
208
Popular Tags