1 18 19 package org.apache.tools.ant.taskdefs.optional.ejb; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import javax.xml.parsers.ParserConfigurationException ; 24 import javax.xml.parsers.SAXParser ; 25 import javax.xml.parsers.SAXParserFactory ; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.Task; 28 import org.apache.tools.ant.types.Path; 29 import org.xml.sax.SAXException ; 30 31 87 public class IPlanetEjbcTask extends Task { 88 89 90 private File ejbdescriptor; 91 private File iasdescriptor; 92 private File dest; 93 private Path classpath; 94 private boolean keepgenerated = false; 95 private boolean debug = false; 96 private File iashome; 97 98 104 public void setEjbdescriptor(File ejbdescriptor) { 105 this.ejbdescriptor = ejbdescriptor; 106 } 107 108 115 public void setIasdescriptor (File iasdescriptor) { 116 this.iasdescriptor = iasdescriptor; 117 } 118 119 126 public void setDest(File dest) { 127 this.dest = dest; 128 } 129 130 135 public void setClasspath(Path classpath) { 136 if (this.classpath == null) { 137 this.classpath = classpath; 138 } else { 139 this.classpath.append(classpath); 140 } 141 } 142 143 147 public Path createClasspath() { 148 if (classpath == null) { 149 classpath = new Path(getProject()); 150 } 151 return classpath.createPath(); 152 } 153 154 160 public void setKeepgenerated(boolean keepgenerated) { 161 this.keepgenerated = keepgenerated; 162 } 163 164 170 public void setDebug(boolean debug) { 171 this.debug = debug; 172 } 173 174 181 public void setIashome(File iashome) { 182 this.iashome = iashome; 183 } 184 185 189 public void execute() throws BuildException { 190 checkConfiguration(); 191 192 executeEjbc(getParser()); 193 } 194 195 200 private void checkConfiguration() throws BuildException { 201 202 if (ejbdescriptor == null) { 203 String msg = "The standard EJB descriptor must be specified using " 204 + "the \"ejbdescriptor\" attribute."; 205 throw new BuildException(msg, getLocation()); 206 } 207 if ((!ejbdescriptor.exists()) || (!ejbdescriptor.isFile())) { 208 String msg = "The standard EJB descriptor (" + ejbdescriptor 209 + ") was not found or isn't a file."; 210 throw new BuildException(msg, getLocation()); 211 } 212 213 if (iasdescriptor == null) { 214 String msg = "The iAS-speific XML descriptor must be specified using" 215 + " the \"iasdescriptor\" attribute."; 216 throw new BuildException(msg, getLocation()); 217 } 218 if ((!iasdescriptor.exists()) || (!iasdescriptor.isFile())) { 219 String msg = "The iAS-specific XML descriptor (" + iasdescriptor 220 + ") was not found or isn't a file."; 221 throw new BuildException(msg, getLocation()); 222 } 223 224 if (dest == null) { 225 String msg = "The destination directory must be specified using " 226 + "the \"dest\" attribute."; 227 throw new BuildException(msg, getLocation()); 228 } 229 if ((!dest.exists()) || (!dest.isDirectory())) { 230 String msg = "The destination directory (" + dest + ") was not " 231 + "found or isn't a directory."; 232 throw new BuildException(msg, getLocation()); 233 } 234 235 if ((iashome != null) && (!iashome.isDirectory())) { 236 String msg = "If \"iashome\" is specified, it must be a valid " 237 + "directory (it was set to " + iashome + ")."; 238 throw new BuildException(msg, getLocation()); 239 } 240 } 241 242 248 private SAXParser getParser() throws BuildException { 249 250 SAXParser saxParser = null; 251 try { 252 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 253 saxParserFactory.setValidating(true); 254 saxParser = saxParserFactory.newSAXParser(); 255 } catch (SAXException e) { 256 String msg = "Unable to create a SAXParser: " + e.getMessage(); 257 throw new BuildException(msg, e, getLocation()); 258 } catch (ParserConfigurationException e) { 259 String msg = "Unable to create a SAXParser: " + e.getMessage(); 260 throw new BuildException(msg, e, getLocation()); 261 } 262 263 return saxParser; 264 } 265 266 274 private void executeEjbc(SAXParser saxParser) throws BuildException { 275 IPlanetEjbc ejbc = new IPlanetEjbc(ejbdescriptor, 276 iasdescriptor, 277 dest, 278 getClasspath().toString(), 279 saxParser); 280 ejbc.setRetainSource(keepgenerated); 281 ejbc.setDebugOutput(debug); 282 if (iashome != null) { 283 ejbc.setIasHomeDir(iashome); 284 } 285 286 try { 287 ejbc.execute(); 288 } catch (IOException e) { 289 String msg = "An IOException occurred while trying to read the XML " 290 + "descriptor file: " + e.getMessage(); 291 throw new BuildException(msg, e, getLocation()); 292 } catch (SAXException e) { 293 String msg = "A SAXException occurred while trying to read the XML " 294 + "descriptor file: " + e.getMessage(); 295 throw new BuildException(msg, e, getLocation()); 296 } catch (IPlanetEjbc.EjbcException e) { 297 String msg = "An exception occurred while trying to run the ejbc " 298 + "utility: " + e.getMessage(); 299 throw new BuildException(msg, e, getLocation()); 300 } 301 } 302 303 309 private Path getClasspath() { 310 Path cp = null; 311 if (classpath == null) { 312 cp = (new Path(getProject())).concatSystemClasspath("last"); 313 } else { 314 cp = classpath.concatSystemClasspath("ignore"); 315 } 316 317 return cp; 318 } 319 } 320 | Popular Tags |