1 23 24 35 36 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 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 classpath = getClasspath(classpathList); 76 77 if (classpath != null) 78 jspc.setClassPath(classpath); 79 } 80 81 String appName = wbd.getApplication().getName(); 83 String sysClassPath = ASClassLoaderUtil.getWebModuleClassPath(appName); 84 jspc.setSystemClassPath(sysClassPath); 85 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 je) 100 { 101 throw new IASDeploymentException("JSP Compilation Error: " + je, je); 102 } 103 finally 104 { 105 110 String [] files = outWebDir.list(); 111 112 if(files == null || files.length <= 0) 113 outWebDir.delete(); 114 115 logger.info(finishMessage); 116 } 117 } 118 119 121 private static void verify(File inWebDir, File outWebDir) throws IASDeploymentException 122 { 123 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 142 private static String getClasspath(List paths) 143 { 144 if(paths == null) 145 return null; 146 147 String classpath = null; 148 149 StringBuffer sb = new StringBuffer (); 150 boolean first = true; 151 152 for (Iterator it = paths.iterator(); it.hasNext(); ) 153 { 154 String path = (String )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 172 181 private static void configureJspc(JspC jspc, WebBundleDescriptor wbd) { 182 183 SunWebApp sunWebApp = wbd.getSunDescriptor(); 184 if (sunWebApp == null) { 185 return; 186 } 187 188 if (sunWebApp.sizeWebProperty() > 0) { 190 WebProperty[] props = sunWebApp.getWebProperty(); 191 for (int i = 0; i < props.length; i++) { 192 String pName = props[i].getAttributeValue("name"); 193 String pValue = props[i].getAttributeValue("value"); 194 if (pName == null || pValue == null) { 195 throw new IllegalArgumentException ( 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 206 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 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 254 private static void configureJspc(JspC jspc, String pName, 255 String pValue) { 256 257 if (pName == null || pValue == null) { 258 throw new IllegalArgumentException ( 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 284 private static String startMessage; 285 private static String finishMessage; 286 private static Logger logger; 287 288 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 |