KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > server > jetty > JspPrecompiler


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.server.jetty;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.jasper.JasperException;
30 import org.apache.jasper.JspC;
31 import org.mortbay.http.ResourceCache;
32
33 import com.sslexplorer.boot.ContextHolder;
34 import com.sslexplorer.boot.Util;
35
36 /**
37  * Precompiles Jsp for all extensions and the core.
38  *
39  * NOTE This is imcomplete
40  *
41  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
42  */

43 public class JspPrecompiler {
44
45     final static Log log = LogFactory.getLog(JspPrecompiler.class);
46
47     /**
48      * Constructor.
49      *
50      */

51     public JspPrecompiler() {
52     }
53
54
55     /**
56      * Compile.
57      *
58      * @param context context
59      * @throws Exception
60      */

61     public void compile(CustomWebApplicationContext context) throws Exception JavaDoc {
62
63         
64         // Clean up the old web app applications directory if it exists
65
File JavaDoc oldWebAppDir = new File JavaDoc("webapp" + File.separator + "WEB-INF" + File.separator + "applications");
66         if(oldWebAppDir.exists()) {
67             Util.delTree(oldWebAppDir);
68         }
69         
70         /* Scan through the core and every additional resource backwards. The first place a
71          * a file exists is the one that gets compile
72          */

73         List JavaDoc<String JavaDoc> uris = new ArrayList JavaDoc<String JavaDoc>();
74         List JavaDoc<ResourceCache> resourceCaches = new ArrayList JavaDoc<ResourceCache>(context.getResourceCaches());
75         Collections.reverse(resourceCaches);
76         for(ResourceCache c : resourceCaches) {
77             scanRoot(c.getBaseResource().getFile().getAbsolutePath(), uris);
78         }
79         File JavaDoc coreWebapp = new File JavaDoc("webapp");
80         scanRoot(coreWebapp.getAbsolutePath(), uris);
81         
82         /* Precompile */
83         
84         JspC jspc = new JspC() {
85             public void scanFiles( File JavaDoc base ) throws JasperException {
86                 addExtension("jsp");
87                 addExtension("jspx");
88                 addExtension("jspf");
89                 super.scanFiles(base);
90             };
91         };
92         jspc.setClassPath(Util.getClassPath(context.getClassLoader()));
93         jspc.setCompile(true);
94         File JavaDoc dir = ContextHolder.getContext().getTempDirectory();
95         jspc.setOutputDir(dir.getAbsolutePath());
96         jspc.setAddWebXmlMappings(false);
97         
98         /* For the moment, we cannot pre-compile any JSP's that exist
99          * in plugins. This is because JspC does not know how to
100          * find custom tag lib definitions or web.xml
101          */

102         coreOnlyPrecompile(uris, coreWebapp);
103         
104         jspc.setJspFiles(asCommaSeparatedList(uris));
105         
106         jspc.setListErrors(true);
107         jspc.setVerbose(9);
108         jspc.execute();
109     }
110     
111     void coreOnlyPrecompile(List JavaDoc<String JavaDoc> uris, File JavaDoc coreWebapp) {
112         for(String JavaDoc uri : new ArrayList JavaDoc<String JavaDoc>(uris)) {
113             if(!uri.startsWith(coreWebapp.getAbsolutePath())) {
114                 uris.remove(uri);
115             }
116         }
117     }
118
119
120     String JavaDoc asCommaSeparatedList(List JavaDoc<String JavaDoc> uris) {
121         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
122         for(String JavaDoc uri : uris) {
123             if(buf.length() != 0) {
124                 buf.append(",");
125             }
126             buf.append(uri);
127         }
128         return buf.toString();
129     }
130     
131     String JavaDoc asList(List JavaDoc<String JavaDoc> uris) {
132         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
133         for(String JavaDoc uri : uris) {
134             if(buf.length() != 0) {
135                 buf.append("\n");
136             }
137             buf.append(uri);
138         }
139         return buf.toString();
140     }
141
142
143     void scanRoot(String JavaDoc root, List JavaDoc<String JavaDoc> uris) {
144         List JavaDoc<String JavaDoc> paths = new ArrayList JavaDoc<String JavaDoc>();
145         File JavaDoc dir = new File JavaDoc(root);
146         scanDir(dir, uris, paths, "");
147     }
148     
149     void scanDir(File JavaDoc file, List JavaDoc<String JavaDoc> uris, List JavaDoc<String JavaDoc> paths, String JavaDoc path) {
150         if(file.isDirectory()) {
151             File JavaDoc[] files = file.listFiles();
152             for(int i = 0 ; i < files.length; i++) {
153                 String JavaDoc newPath = path.equals("") ? files[i].getName() : ( path + File.separator + files[i].getName() ) ;
154                 scanDir(files[i], uris, paths, newPath);
155             }
156         }
157         else {
158             if(file.getName().endsWith(".jsp") || file.getName().endsWith(".jspf")) {
159                 String JavaDoc uri = file.getAbsolutePath();
160                 if(!paths.contains(path)) {
161                     uris.add(uri);
162                     paths.add(path);
163                 }
164             }
165         }
166     }
167
168 }
169
Popular Tags