1 27 28 package org.objectweb.speedo.ant; 29 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.DirectoryScanner; 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.Task; 34 import org.apache.tools.ant.taskdefs.Javac; 35 import org.apache.tools.ant.taskdefs.MatchingTask; 36 import org.apache.tools.ant.types.DTDLocation; 37 import org.apache.tools.ant.types.Path; 38 import org.apache.tools.ant.types.Reference; 39 import org.objectweb.speedo.api.ExceptionHelper; 40 import org.objectweb.speedo.api.SpeedoException; 41 import org.objectweb.speedo.generation.SpeedoCompiler; 42 import org.objectweb.speedo.generation.api.SpeedoCompilerParameter; 43 44 import java.io.File ; 45 import java.util.Arrays ; 46 import java.util.Collection ; 47 48 68 public class AntSpeedo extends Task { 69 70 private Path classpath = null; 71 private Path jormclasspath = null; 72 private Description jdo = null; 73 private Description jorm = null; 74 private Description awareFiles = null; 75 76 private SpeedoCompilerParameter scp = null; 77 private SpeedoCompiler sc = null; 78 private File src = null; 79 private File output = null; 80 81 public void init() throws BuildException { 82 super.init(); 83 sc = new SpeedoCompiler(); 84 scp = sc.getSpeedoCompilerParameter(); 85 } 86 87 90 public void setConfFile(File confFile) { 91 } 92 93 public void setLogPropFile(File logPropFile) { 94 scp.logPropFile = logPropFile.getAbsolutePath(); 95 } 96 97 public void setProjectname(String projectName) { 98 scp.projectName = projectName; 99 } 100 101 public void setMappername(String mapperName) { 102 scp.mapperName = mapperName; 103 } 104 105 public void setSrc(File src) { 106 this.src = src; 107 } 108 109 public void setKeepsrc(boolean keepsrc) { 110 scp.keepsrc = keepsrc; 111 } 112 113 120 public DTDLocation createDTD() { 121 DTDLocation dtdLocation = new DTDLocation(); 122 scp.dtdLocations.add(dtdLocation); 123 return dtdLocation; 124 } 125 126 public void setOutput(File output) { 127 this.output = output; 128 scp.output = output.getAbsolutePath(); 129 } 130 131 public void setInput(File intput) { 132 scp.input = intput.getAbsolutePath(); 133 } 134 135 public void setClasspath(Path cp) { 136 if (classpath == null) 137 classpath = cp; 138 else 139 classpath.append(cp); 140 } 141 142 public Path getClasspath() { 143 return classpath; 144 } 145 146 public Path createClasspath() { 147 if (classpath == null) 148 classpath = new Path(getProject()); 149 return classpath.createPath(); 150 } 151 152 public void setClasspathRef(Reference r) { 153 createClasspath().setRefid(r); 154 } 155 156 public Description createJdopath() { 157 Description d = new Description(); 158 this.jdo = d; 159 d.setProject(getProject()); 160 return d; 161 } 162 public MatchingTask createJavac() { 163 if(scp.javac == null) { 164 scp.javac = new Javac(); 165 scp.javac.setProject(getProject()); 166 } 167 return scp.javac; 168 } 169 170 public Description createJormpath() { 171 Description d = new Description(); 172 this.jorm = d; 173 d.setProject(getProject()); 174 return d; 175 } 176 177 public void setJormClasspath(Path cp) { 178 if (jormclasspath == null) 179 jormclasspath = cp; 180 else 181 jormclasspath.append(cp); 182 } 183 184 public Path getJormClasspath() { 185 return jormclasspath; 186 } 187 188 public Path createJormClasspath() { 189 if (jormclasspath == null) 190 jormclasspath = new Path(getProject()); 191 return jormclasspath.createPath(); 192 } 193 194 public void setJormClasspathRef(Reference r) { 195 createJormClasspath().setRefid(r); 196 } 197 198 public Description createPersistenceaware() { 199 Description d = new Description(); 200 this.awareFiles = d; 201 d.setProject(getProject()); 202 return d; 203 } 204 205 public void setGenerateJormFile(boolean val) { 206 scp.generateNeededJormFile = val; 207 } 208 209 public boolean getGenerateJormFile() { 210 return scp.generateNeededJormFile; 211 } 212 213 217 public void execute() throws BuildException { 218 if (getClass().getClassLoader().getResourceAsStream("jorm.properties") == null) { 219 throw new BuildException("Speedo ant task ERROR: Impossible to " + 220 "find the 'jorm.properties' file in the classpath, classloader=" 221 + getClass().getClassLoader()); 222 } 223 if (scp.mapperName == null) { 224 scp.mapperName = "rdb"; 225 } 226 scp.input = src.getAbsolutePath(); 227 if (jdo == null) { 228 jdo = createJdopath(); 229 jdo.setDir(src); 230 jdo.setIncludes("**/*.jdo"); 231 } 232 scp.jdoDir = jdo.dir.getAbsolutePath(); 233 scp.jdo = getSelectedFiles(jdo); 234 log("scp.jdo.size=" + scp.jdo.size(), Project.MSG_DEBUG); 235 236 237 if (jorm == null) { 238 jorm = createJormpath(); 239 jorm.setDir(src); 240 jorm.setIncludes("**/*.pd"); 241 } 242 scp.jormDir = jorm.dir.getAbsolutePath(); 243 scp.jorm = getSelectedFiles(jorm); 244 245 if (awareFiles == null) { 246 awareFiles = createJdopath(); 247 awareFiles.setDir(output); 248 awareFiles.setIncludes("**/*.class"); 249 } 250 scp.awareFilesDir = awareFiles.dir.getAbsolutePath(); 251 scp.awareFiles = getSelectedFiles(awareFiles); 252 253 if (jormclasspath == null) { 254 jormclasspath = classpath; 255 } 256 scp.jormclasspath = Arrays.asList(jormclasspath.list()); 257 scp.classpath = classpath; 258 259 log("scp.jormclasspath=" + scp.jormclasspath, Project.MSG_DEBUG); 260 261 if (scp.dtdLocations.size() == 0) { 262 DTDLocation dl = new DTDLocation(); 263 dl.setPublicId("-//ObjectWeb Consortium//DTD JORM DEFINITIONS 2.0//EN"); 264 dl.setLocation("jorm2.dtd"); 265 scp.dtdLocations.add(dl); 266 dl = new DTDLocation(); 267 dl.setPublicId("-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN"); 268 dl.setLocation("jdo.dtd"); 269 scp.dtdLocations.add(dl); 270 } 271 classpath = null; 272 jormclasspath = null; 273 jdo = null; 274 jorm = null; 275 awareFiles = null; 276 scp = null; 277 src = null; 278 output = null; 279 try { 280 log("SpeedoCompiler init", Project.MSG_DEBUG); 281 sc.init(); 282 log("SpeedoCompiler process", Project.MSG_DEBUG); 283 sc.process(); 284 log("SpeedoCompiler end", Project.MSG_DEBUG); 285 } catch (SpeedoException e) { 286 e.printStackTrace(); 287 throw new BuildException(ExceptionHelper.getNested(e)); 288 } catch (Throwable e) { 289 e.printStackTrace(); 290 throw new BuildException(e); 291 } 292 } 293 294 299 private Collection getSelectedFiles(Description desc) { 300 Collection result = Arrays.asList( 301 desc.getDirectoryScanner(desc.dir).getIncludedFiles()); 302 Collection toDeselct = Arrays.asList( 303 desc.getDirectoryScanner(desc.dir).getExcludedFiles()); 304 log(toDeselct.toString(), Project.MSG_DEBUG); 305 result.removeAll(toDeselct); 306 return result; 307 } 308 309 312 public class Description extends MatchingTask { 313 314 public File dir = null; 315 316 public void setDir(File dir) { 317 this.dir = dir; 318 } 319 320 public DirectoryScanner getDirectoryScanner(File p) { 321 return super.getDirectoryScanner(p); 322 } 323 } 324 325 } | Popular Tags |