KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > backend > JSPCompiler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Indentation Information:
26  * 0. Please (try to) preserve these settings.
27  * 1. Tabs are preferred over spaces.
28  * 2. In vi/vim -
29  * :set tabstop=4 :set shiftwidth=4 :set softtabstop=4
30  * 3. In S1 Studio -
31  * 1. Tools->Options->Editor Settings->Java Editor->Tab Size = 4
32  * 2. Tools->Options->Indentation Engines->Java Indentation Engine->Expand Tabs to Spaces = False.
33  * 3. Tools->Options->Indentation Engines->Java Indentation Engine->Number of Spaces per Tab = 4.
34  */

35
36 /*
37  * @author byron.nevins@sun.com
38  */

39
40 package com.sun.enterprise.deployment.backend;
41   
42 import java.io.*;
43 import java.util.*;
44 import java.util.logging.*;
45 import org.apache.jasper.JspC;
46 import com.sun.enterprise.util.io.FileUtils;
47 import com.sun.enterprise.util.i18n.StringManager;
48 import com.sun.enterprise.deployment.WebBundleDescriptor;
49 import com.sun.enterprise.deployment.WebComponentDescriptor;
50 import com.sun.enterprise.deployment.web.InitializationParameter;
51 import com.sun.enterprise.deployment.runtime.web.SunWebApp;
52 import com.sun.enterprise.deployment.runtime.web.WebProperty;
53 import com.sun.enterprise.deployment.runtime.web.JspConfig;
54 import com.sun.appserv.server.util.ASClassLoaderUtil;
55
56 public class JSPCompiler
57 {
58     public static void compile(File inWebDir, File outWebDir,
59                                    WebBundleDescriptor wbd)
60             throws IASDeploymentException
61     {
62         compile(inWebDir, outWebDir, wbd, null);
63     }
64
65     ////////////////////////////////////////////////////////////////////////////
66

67     public static void compile(File inWebDir, File outWebDir,
68                                    WebBundleDescriptor wbd, List classpathList)
69             throws IASDeploymentException
70     {
71         JspC jspc = new JspC();
72
73         if (classpathList != null)
74         {
75             String JavaDoc classpath = getClasspath(classpathList);
76         
77             if (classpath != null)
78                 jspc.setClassPath(classpath);
79         }
80
81                 // START SJSAS 6311155
82
String JavaDoc appName = wbd.getApplication().getName();
83                 String JavaDoc sysClassPath = ASClassLoaderUtil.getWebModuleClassPath(appName);
84                 jspc.setSystemClassPath(sysClassPath);
85                 // END SJSAS 6311155
86

87         verify(inWebDir, outWebDir);
88
89         configureJspc(jspc, wbd);
90         jspc.setOutputDir(outWebDir.getAbsolutePath());
91         jspc.setUriroot(inWebDir.getAbsolutePath());
92         jspc.setCompile(true);
93         logger.info(startMessage);
94
95         try
96         {
97             jspc.execute();
98         }
99         catch (Exception JavaDoc je)
100         {
101             throw new IASDeploymentException("JSP Compilation Error: " + je, je);
102         }
103         finally
104         {
105             // bnevins 9-9-03 -- There may be no jsp files in this web-module
106
// in such a case the code above will create a useless, and possibly
107
// problematic empty directory. If the directory is empty -- delete
108
// the directory.
109

110             String JavaDoc[] files = outWebDir.list();
111             
112             if(files == null || files.length <= 0)
113                 outWebDir.delete();
114             
115             logger.info(finishMessage);
116         }
117     }
118
119     ////////////////////////////////////////////////////////////////////////////
120

121     private static void verify(File inWebDir, File outWebDir) throws IASDeploymentException
122     {
123         // inWebDir must exist, outWebDir must either exist or be creatable
124
if (!FileUtils.safeIsDirectory(inWebDir))
125         {
126             throw new IASDeploymentException("inWebDir is not a directory: " + inWebDir);
127         }
128      
129         if (!FileUtils.safeIsDirectory(outWebDir))
130         {
131             outWebDir.mkdirs();
132         
133             if (!FileUtils.safeIsDirectory(outWebDir))
134             {
135                 throw new IASDeploymentException("outWebDir is not a directory, and it can't be created: " + outWebDir);
136             }
137         }
138     }
139
140     ////////////////////////////////////////////////////////////////////////////
141

142     private static String JavaDoc getClasspath(List paths)
143     {
144         if(paths == null)
145             return null;
146         
147         String JavaDoc classpath = null;
148
149         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
150         boolean first = true;
151
152         for (Iterator it = paths.iterator(); it.hasNext(); )
153         {
154             String JavaDoc path = (String JavaDoc)it.next();
155
156             if (first)
157                 first = false;
158             else
159                 sb.append(File.pathSeparatorChar);
160
161             sb.append(path);
162         }
163
164         if (sb.length() > 0)
165             classpath = sb.toString();
166
167         return classpath;
168     }
169
170     ////////////////////////////////////////////////////////////////////////////
171

172         /*
173          * Configures the given JspC instance with the jsp-config properties
174      * specified in the sun-web.xml of the web module represented by the
175      * given WebBundleDescriptor.
176      *
177      * @param jspc JspC instance to be configured
178      * @param wbd WebBundleDescriptor of the web module whose sun-web.xml
179          * is used to configure the given JspC instance
180      */

181         private static void configureJspc(JspC jspc, WebBundleDescriptor wbd) {
182
183         SunWebApp sunWebApp = wbd.getSunDescriptor();
184         if (sunWebApp == null) {
185         return;
186         }
187
188             // START SJSAS 6384538
189
if (sunWebApp.sizeWebProperty() > 0) {
190                 WebProperty[] props = sunWebApp.getWebProperty();
191                 for (int i = 0; i < props.length; i++) {
192                     String JavaDoc pName = props[i].getAttributeValue("name");
193                     String JavaDoc pValue = props[i].getAttributeValue("value");
194                     if (pName == null || pValue == null) {
195                         throw new IllegalArgumentException JavaDoc(
196                             "Missing sun-web-app property name or value");
197                     }
198                     if ("enableTldValidation".equals(pName)) {
199                         jspc.setIsTldValidationEnabled(
200                             Boolean.valueOf(pValue).booleanValue());
201                     }
202                 }
203             }
204             // END SJSAS 6384538
205

206             // START SJSAS 6170435
207
/*
208              * Configure JspC with the init params of the JspServlet
209              */

210             Set set = wbd.getWebComponentDescriptorsSet();
211             if (!set.isEmpty()) {
212                 Iterator<WebComponentDescriptor> iterator = set.iterator();
213                 while (iterator.hasNext()) {
214                     WebComponentDescriptor webComponentDesc = iterator.next();
215                     if ("jsp".equals(webComponentDesc.getCanonicalName())) {
216                         Enumeration<InitializationParameter> en
217                             = webComponentDesc.getInitializationParameters();
218                         if (en != null) {
219                             while (en.hasMoreElements()) {
220                                 InitializationParameter initP = en.nextElement();
221                                 configureJspc(jspc,
222                                               initP.getName(),
223                                               initP.getValue());
224                             }
225                         }
226                         break;
227                     }
228                 }
229             }
230             // END SJSAS 6170435
231

232         JspConfig jspConfig = sunWebApp.getJspConfig();
233         if (jspConfig == null) {
234         return;
235         }
236
237             WebProperty[] props = jspConfig.getWebProperty();
238             for (int i=0; props!=null && i<props.length; i++) {
239                 configureJspc(jspc,
240                               props[i].getAttributeValue("name"),
241                               props[i].getAttributeValue("value"));
242             }
243     }
244
245
246         /*
247          * Configures the given JspC instance with the given property name
248          * and value.
249          *
250          * @jspc The JspC instance to configure
251          * @pName The property name
252          * @pValue The property value
253          */

254         private static void configureJspc(JspC jspc, String JavaDoc pName,
255                                           String JavaDoc pValue) {
256
257             if (pName == null || pValue == null) {
258                 throw new IllegalArgumentException JavaDoc(
259                     "Null property name or value");
260             }
261
262             if ("xpoweredBy".equals(pName)) {
263                 jspc.setXpoweredBy(Boolean.valueOf(pValue).booleanValue());
264             } else if ("classdebuginfo".equals(pName)) {
265                 jspc.setClassDebugInfo(Boolean.valueOf(pValue).booleanValue());
266             } else if ("enablePooling".equals(pName)) {
267                 jspc.setPoolingEnabled(Boolean.valueOf(pValue).booleanValue());
268             } else if ("ieClassId".equals(pName)) {
269                 jspc.setIeClassId(pValue);
270             } else if ("trimSpaces".equals(pName)) {
271                 jspc.setTrimSpaces(Boolean.valueOf(pValue).booleanValue());
272             } else if ("genStrAsCharArray".equals(pName)) {
273                 jspc.setGenStringAsCharArray(
274                     Boolean.valueOf(pValue).booleanValue());
275             } else if ("errorOnUseBeanInvalidClassAttribute".equals(pName)) {
276                 jspc.setErrorOnUseBeanInvalidClassAttribute(
277                     Boolean.valueOf(pValue).booleanValue());
278             }
279         }
280
281
282     ////////////////////////////////////////////////////////////////////////////
283

284     private static String JavaDoc startMessage;
285     private static String JavaDoc finishMessage;
286     private static Logger logger;
287
288     ////////////////////////////////////////////////////////////////////////////
289

290     static
291     {
292         logger = DeploymentLogger.get();
293         StringManager localStrings = StringManager.getManager( JSPCompiler.class );
294         startMessage = localStrings.getStringWithDefault("enterprise.deployment.backend.start_jspc", "Beginning JSP Precompile...");
295         finishMessage = localStrings.getStringWithDefault("enterprise.deployment.backend.finish_jspc", "Finished JSP Precompile");
296     }
297 }
298
Popular Tags