1 23 24 29 30 package org.apache.tools.ant.taskdefs.optional.sun.appserv; 31 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.taskdefs.MatchingTask; 34 import org.apache.tools.ant.types.Reference; 35 import org.apache.tools.ant.DirectoryScanner; 36 import org.apache.tools.ant.BuildException; 37 import org.apache.tools.ant.types.Path; 38 import org.apache.tools.ant.taskdefs.Java; 39 40 import java.io.File ; 41 import java.util.StringTokenizer ; 42 import java.util.ArrayList ; 43 44 85 86 public class SunJspc extends MatchingTask 87 { 88 private File destDir = null; 89 private File srcDir; 90 private String packageName; 91 private String verboseLevel; 92 private File uriRoot; 93 private File uriBase; 94 private Path classPath; 95 private int compileListLength; 96 private boolean failOnError = true; 97 private File webAppBaseDir; 98 99 private File sunoneHome; 100 private File asinstalldir; 101 102 103 private static final String [] CLASSPATH_ELEMENTS = { 104 "lib", 105 "lib/appserv-rt.jar", 106 "lib/javaee.jar", 107 "lib/appserv-ext.jar"}; 108 109 LocalStringsManager lsm = new LocalStringsManager(); 110 111 public void setSunonehome(File sunoneHome) 112 { 113 final String msg = lsm.getString("DeprecatedAttribute", new Object [] {"sunonehome", 114 "asinstalldir"}); 115 log(msg, Project.MSG_WARN); 116 this.asinstalldir = sunoneHome; 117 } 118 119 120 128 public void setAsinstalldir(File asinstalldir) { 129 this.asinstalldir = asinstalldir; 130 } 131 132 133 144 protected File getAsinstalldir() throws ClassNotFoundException { 145 if (asinstalldir == null) { 146 String home = getProject().getProperty("asinstall.dir"); 147 if (home != null) { 148 asinstalldir = new File (home); 149 } 150 else { 151 home = getProject().getProperty("sunone.home"); 152 if (home != null) 153 { 154 final String msg = lsm.getString("DeprecatedProperty", new Object [] {"sunone.home", "asinstall.dir"}); 155 log(msg, Project.MSG_WARN); 156 asinstalldir = new File (home); 157 } 158 159 } 160 } 161 if (asinstalldir!=null) verifyAsinstalldir(asinstalldir); 162 return asinstalldir; 163 } 164 165 166 173 private boolean verifyAsinstalldir(File home) throws ClassNotFoundException { 174 if (home!= null && home.isDirectory()) { 175 if ( new File (home, "config").isDirectory() ) { 176 return true; 177 } 178 } 179 throw new ClassNotFoundException ("ClassCouldNotBeFound"); 180 } 181 182 183 public void setDestdir(File dest) 184 { 185 destDir = dest; 186 } 187 188 public File getDestdir() 189 { 190 return destDir; 191 } 192 193 public void setSrcdir(File src) 194 { 195 srcDir = src; 196 } 197 198 public File getSrcdir() 199 { 200 return srcDir; 201 } 202 203 public void setPackage(String name) 204 { 205 packageName = name; 206 } 207 208 public String getPackage() 209 { 210 return packageName; 211 } 212 213 public void setVerbose(String level) 214 { 215 verboseLevel = level; 216 } 217 218 public String getVerbose() 219 { 220 return verboseLevel; 221 } 222 223 public void setFailonerror(boolean fail) 224 { 225 failOnError = fail; 226 } 227 228 public boolean getFailonerror() 229 { 230 return failOnError; 231 } 232 233 public void setUribase(File base) 234 { 235 uriBase = base; 236 } 237 238 public File getUribase() 239 { 240 if(uriBase!=null) 241 return uriBase; 242 return uriRoot; 243 } 244 245 public void setUriroot(File root) 246 { 247 uriRoot = root; 248 } 249 250 public File getUriroot() 251 { 252 return uriRoot; 253 } 254 255 public void setClasspath(Path cp) 256 { 257 if(classPath == null) 258 classPath = cp; 259 else 260 classPath.append(cp); 261 } 262 263 264 public Path createClasspath() 265 { 266 if(classPath==null) 267 classPath = new Path(project); 268 return classPath.createPath(); 269 } 270 271 272 public void setClasspathref(Reference ref) 273 { 274 createClasspath().setRefid(ref); 275 } 276 277 public void setWebapp(File baseDir) 278 { 279 webAppBaseDir = baseDir; 280 } 281 282 public File getWebapp() 283 { 284 return webAppBaseDir; 285 } 286 287 288 public void execute()throws BuildException 289 { 290 291 CheckForMutuallyExclusiveAttribute(); 292 293 if(webAppBaseDir==null) 294 { 295 if(srcDir==null) 296 throw new BuildException(lsm.getString("SourceDirectoryProviced"), location); 297 if(!srcDir.exists() || !srcDir.isDirectory()) 298 throw new BuildException(lsm.getString("SourceDirectoryDoesNotExist", 299 new Object [] {srcDir.getAbsolutePath()}), 300 location); 301 } 302 else 303 { 304 if(!webAppBaseDir.exists() || !webAppBaseDir.isDirectory()) 305 throw new BuildException(lsm.getString("WebAppDirectoryDoesNotExist", 306 new Object [] {webAppBaseDir.getAbsolutePath()}), 307 location); 308 } 309 310 if(destDir!=null) 311 { 312 if(!destDir.exists()) 313 throw new BuildException(lsm.getString("DestinationDirectoryDoesNotExist", 314 new Object [] {destDir})); 315 if(!destDir.isDirectory()) 316 throw new BuildException(lsm.getString("InvalidDestinationDirectory", 317 new Object [] {destDir})); 318 } 319 else 320 { 321 throw new BuildException(lsm.getString("DestinationDirectoryNoProvided")); 322 } 323 324 325 String args[] = getCommandString(); 326 if(srcDir!=null) 327 log(lsm.getString("PreCompilation", new Object [] {String.valueOf(compileListLength), 328 destDir.getAbsolutePath()})); 329 if(!doCompilation(args)) 330 throw new BuildException(lsm.getString("CompilationFailed")); 331 } 332 333 334 339 private void CheckForMutuallyExclusiveAttribute() throws BuildException 340 { 341 if(webAppBaseDir!=null && srcDir!=null) { 342 final String msg = lsm.getString("MutuallyExclusivelyAttribute", 343 new Object [] {"srcdir", 344 "webapp"}); 345 throw new BuildException(msg, getLocation()); 346 } 347 } 348 349 350 protected boolean doCompilation(String args[]) 351 { 352 try 353 { 354 Java java = (Java)project.createTask("java"); 355 java.setClasspath(constructPath()); 356 java.setClassname("org.apache.jasper.JspC"); 357 for(int i=0;i<args.length;i++) 358 { 359 java.createArg().setValue(args[i]); 360 } 361 java.setFailonerror(failOnError); 362 java.setFork(true); 363 log("Executing Jasper Compiler"); 364 int returnCode = java.executeJava(); 365 if(returnCode == 1) 366 { 367 log(lsm.getString("SetVerbose")); 368 return false; 369 } 370 return true; 371 } 372 catch(Exception ex) 373 { 374 log(lsm.getString("ExceptionMessage", new Object [] {ex.toString()})); 375 return false; 376 } 377 } 378 379 protected String [] getCommandString() 380 { 381 ArrayList commandList = new ArrayList (); 382 383 commandList.add("-d"); 384 commandList.add(destDir.getAbsolutePath()); 385 386 if(packageName!=null && packageName.length()>0) 387 { 388 commandList.add("-p"); 389 commandList.add(packageName); 390 } 391 392 if(verboseLevel!=null) 393 commandList.add("-v".concat(verboseLevel)); 394 395 if(uriRoot!=null && uriRoot.exists()) 396 { 397 commandList.add("-uriroot"); 398 commandList.add(uriRoot.getAbsolutePath()); 399 } 400 401 if(uriBase!=null && uriBase.exists()) 402 { 403 commandList.add("-uribase"); 404 commandList.add(uriBase.getAbsolutePath()); 405 } 406 else if(uriRoot!=null && uriRoot.exists()) 407 { 408 commandList.add("-uribase"); 409 commandList.add(uriRoot.getAbsolutePath()); 410 } 411 412 commandList.add("-dtds"); 414 commandList.add("/dtds/"); 415 416 commandList.add("-schemas"); 417 commandList.add("/schemas/"); 418 420 commandList.add("-die1"); 421 if(webAppBaseDir!=null) 422 { 423 commandList.add("-webapp"); 424 commandList.add(webAppBaseDir.getAbsolutePath()); 425 } 426 else 427 { 428 DirectoryScanner ds = super.getDirectoryScanner(srcDir); 429 String files[] = ds.getIncludedFiles(); 430 compileListLength = files.length; 431 for(int i=0;i<files.length;i++) 432 { 433 File tempFile = new File (srcDir,files[i]); 434 commandList.add(tempFile.getAbsolutePath()); 435 } 436 } 437 438 String args[] = (String [])commandList.toArray(new String [commandList.size()]); 439 return args; 440 } 441 442 private Path constructPath() throws ClassNotFoundException 443 { 444 StringBuffer classPathBuffer = new StringBuffer (); 445 if(getAsinstalldir()!=null) 446 { 447 for(int i=0;i<CLASSPATH_ELEMENTS.length;i++) 448 { 449 classPathBuffer.append((new File (getAsinstalldir(),CLASSPATH_ELEMENTS[i])).getPath()); 450 classPathBuffer.append(":"); 451 } 452 } 453 if(classPath!=null) 454 { 455 classPathBuffer.append(classPath); 456 classPathBuffer.append(":"); 457 } 458 classPathBuffer.append(Path.systemClasspath); 459 return new Path(getProject(),classPathBuffer.toString()); 460 } 461 } 462 | Popular Tags |