001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.config.types.FileSetType;
034: import com.caucho.config.types.PathPatternType;
035: import com.caucho.java.JavaCompiler;
036: import com.caucho.java.LineMap;
037: import com.caucho.lifecycle.Lifecycle;
038: import com.caucho.log.Log;
039: import com.caucho.server.webapp.WebApp;
040: import com.caucho.util.CompileException;
041: import com.caucho.util.L10N;
042: import com.caucho.vfs.Path;
043: import com.caucho.vfs.Vfs;
044:
045: import javax.annotation.PostConstruct;
046: import javax.servlet.jsp.JspFactory;
047: import java.util.ArrayList;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050:
051: /**
052: * Resource for precompiling all the *.jsp files on startup.
053: */
054: public class JspPrecompileResource {
055: private static final Logger log = Log
056: .open(JspPrecompileResource.class);
057: private static final L10N L = new L10N(JspPrecompileResource.class);
058:
059: private FileSetType _fileSet;
060:
061: private WebApp _webApp;
062:
063: private final Lifecycle _lifecycle = new Lifecycle();
064:
065: /**
066: * Sets the webApp.
067: */
068: public void setWebApp(WebApp app) {
069: _webApp = app;
070: }
071:
072: /**
073: * Add a pattern.
074: */
075: public FileSetType createFileset() {
076: if (_fileSet == null) {
077: _fileSet = new FileSetType();
078: _fileSet.setDir(Vfs.lookup());
079: }
080:
081: return _fileSet;
082: }
083:
084: /**
085: * @deprecated
086: */
087: public FileSetType createFileSet() {
088: return createFileset();
089: }
090:
091: /**
092: * Initialize the resource.
093: */
094: @PostConstruct
095: public void init() throws ConfigException {
096: Path pwd = Vfs.lookup();
097:
098: if (_fileSet == null) {
099: createFileset().addInclude(new PathPatternType("**/*.jsp"));
100: }
101:
102: if (_webApp == null) {
103: _webApp = WebApp.getLocal();
104: }
105:
106: if (_webApp == null)
107: throw new ConfigException(
108: L
109: .l("JspPrecompileResource must be used in a web-app context."));
110:
111: start();
112: }
113:
114: /**
115: * Starts the resource.
116: */
117: public void start() {
118: if (!_lifecycle.toActive())
119: return;
120:
121: // JspManager manager = new JspManager(_webApp);
122:
123: if (JspFactory.getDefaultFactory() == null)
124: JspFactory.setDefaultFactory(new QJspFactory());
125:
126: JspCompiler compiler = new JspCompiler();
127: compiler.setWebApp(_webApp);
128:
129: ArrayList<Path> paths = _fileSet.getPaths();
130: ArrayList<String> classes = new ArrayList<String>();
131:
132: String contextPath = _webApp.getContextPath();
133: if (!contextPath.endsWith("/"))
134: contextPath = contextPath + "/";
135:
136: Path pwd = Vfs.lookup();
137:
138: for (int i = 0; i < paths.size(); i++) {
139: Path path = paths.get(i);
140:
141: String uri = path.getPath().substring(
142: pwd.getPath().length());
143:
144: if (_webApp.getContext(contextPath + uri) != _webApp)
145: continue;
146:
147: String className = JspCompiler.urlToClassName(uri);
148:
149: try {
150: CauchoPage page = (CauchoPage) compiler.loadClass(
151: className, true);
152:
153: page.init(pwd);
154:
155: if (!page._caucho_isModified()) {
156: log.fine("pre-loaded " + uri);
157: continue;
158: }
159: } catch (ClassNotFoundException e) {
160: } catch (Throwable e) {
161: log.log(Level.FINER, e.toString(), e);
162: }
163:
164: log.fine("compiling " + uri);
165:
166: try {
167: JspCompilerInstance compilerInst;
168: compilerInst = compiler.getCompilerInstance(path, uri,
169: 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 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 files[] = new String[classes.size()];
197: classes.toArray(files);
198:
199: javaCompiler.compileBatch(files);
200: } catch (Exception e) {
201: if (e instanceof CompileException)
202: log.warning(e.getMessage());
203: else
204: log.log(Level.WARNING, e.toString(), e);
205: }
206: }
207: }
|