1 25 26 package org.objectweb.jorm.util.lib; 27 28 import java.io.File ; 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import org.apache.tools.ant.DirectoryScanner; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.taskdefs.MatchingTask; 38 import org.apache.tools.ant.taskdefs.UpToDate; 39 import org.apache.tools.ant.types.Path; 40 import org.apache.tools.ant.types.Reference; 41 import org.objectweb.jorm.api.PClassMapping; 42 import org.objectweb.jorm.api.PException; 43 import org.objectweb.jorm.api.PMapCluster; 44 import org.objectweb.jorm.api.PMapper; 45 import org.objectweb.jorm.mapper.rdb.lib.MapperJDBC; 46 import org.objectweb.jorm.mapper.rdb.lib.RdbScriptPMSM; 47 48 62 public class AntScriptGenerator extends MatchingTask { 63 public static final String SCRIPT_FILE_NAME = "script.fileName"; 64 public static final String SCRIPT_MAPPER_NAME = "script.mapperName"; 65 public static final String SCRIPT_DELETE_STATEMENT = "script.generate.delete"; 66 public static final String SCRIPT_DROP_STATEMENT = "script.generate.drop"; 67 public static final String SCRIPT_CREATE_STATEMENT = "script.generate.create"; 68 public static final String SCRIPT_CLASS_DIR = "script.class.dir"; 69 public static final String SCRIPT_JAR_DIR = "script.jar.dir"; 70 71 private Path classpath = null; 73 private File src = null; 75 private File destFile = null; 77 private String mapperName; 79 private boolean generateDrop = true; 81 private boolean generateDelete = true; 82 private boolean generateCreate = true; 83 84 private boolean userDefinedIncludes = false; 85 86 public static void main(String [] args) { 87 new AntScriptGenerator().execute(args); 88 } 89 90 public void setClasspath(Path cp) { 91 if (classpath == null) 92 classpath = cp; 93 else 94 classpath.append(cp); 95 } 96 public Path getClasspath() { 97 return classpath; 98 } 99 public Path createClasspath() { 100 if (classpath == null) 101 classpath = new Path(getProject()); 102 return classpath.createPath(); 103 } 104 public void setClasspathRef(Reference r) { 105 createClasspath().setRefid(r); 106 } 107 108 public void setSrc(File src) { 109 this.src = src; 110 } 111 public File getSrc() { 112 return src; 113 } 114 115 public void setDestFile(File destFile) { 116 this.destFile = destFile; 117 } 118 public File getDestFile() { 119 return destFile; 120 } 121 122 public void setGenerateDrop(boolean generate) { 123 this.generateDrop = generate; 124 } 125 public boolean isGenerateDrop() { 126 return generateDrop; 127 } 128 129 public void setGenerateDelete(boolean generate) { 130 this.generateDelete = generate; 131 } 132 public boolean isGenerateDelete() { 133 return generateDelete; 134 } 135 136 public void setGenerateCreate(boolean generate) { 137 this.generateCreate = generate; 138 } 139 public boolean isGenerateCreate() { 140 return generateCreate; 141 } 142 143 public String getMapperName() { 144 return mapperName; 145 } 146 public void setMapperName(String mapperName) { 147 this.mapperName = mapperName; 148 } 149 150 public DirectoryScanner getDirectoryScanner() { 151 return super.getDirectoryScanner(src); 152 } 153 154 public void setIncludes(String s) { 155 super.setIncludes(s); 156 userDefinedIncludes = true; 157 } 158 159 public void execute() { 160 if (!userDefinedIncludes) { 162 super.setIncludes("**/*Mapping.class"); 163 } 164 List classesToProcess = Arrays.asList(getDirectoryScanner().getIncludedFiles()); 165 if (classesToProcess == null || classesToProcess.isEmpty()) { 166 project.log("[WARN] No class files found in " 167 + src.getAbsolutePath() + ".", Project.MSG_WARN); 168 return; 169 } 170 171 UpToDate u = new UpToDate(); 172 u.setProject(project); 173 u.addSrcfiles(fileset); 174 u.setTargetFile(destFile); 175 if (u.eval()) { 176 return; 177 } 178 project.log("[INFO] Generating script for database creation...", Project.MSG_INFO); 179 180 List tmpClasses = new ArrayList (classesToProcess.size()); 181 for (int i=0; i<classesToProcess.size(); i++) { 182 tmpClasses.add(formatElement((String ) classesToProcess.get(i))); 184 } 185 186 187 RdbScriptPMSM msm = new RdbScriptPMSM(destFile.getAbsolutePath(), true); 188 try { 189 MapperJDBC mapper = new MapperJDBC(); 191 mapper.setMapperName(mapperName); 193 mapper.start(); 195 msm.setPMapper(mapper); 197 msm.init(); 199 } catch (PException pe) { 200 project.log( 201 "[ERROR] Impossible to init the PMappingStructuresManager :" 202 + pe.getMessage(), Project.MSG_ERR); 203 } 204 ClassLoader cl = getClass().getClassLoader(); 206 207 classesToProcess = new ArrayList (tmpClasses); 209 for (int i=0; i<classesToProcess.size(); i++) { 211 if(initClass((String )classesToProcess.get(i), msm, cl) == -1) { 214 tmpClasses.remove(classesToProcess.get(i)); 215 } 216 } 217 classesToProcess = new ArrayList (tmpClasses); 219 Collection clusterList = new ArrayList (1); 221 for (int i=0; i<classesToProcess.size(); i++) { 223 try { 224 String classMapping = (String )classesToProcess.get(i); 226 String className = classMapping; 227 int index = classMapping.indexOf(PMapper.PCLASSMAPPINGAPPENDER); 228 if (index != -1) { 229 className = classMapping.substring(0, index); 230 } 231 PMapCluster cluster = msm.getPMapCluster(className); 233 if (cluster == null) { 234 project.log( 235 "[ERROR] JORM cluster is null for class: " + className, 236 Project.MSG_ERR); 237 } 238 if (clusterList.contains(cluster)) { 240 continue; 241 } else { 242 clusterList.add(cluster); 243 } 244 if (!cluster.isDefined()) { 245 Iterator it = cluster.getUnResolvedDependencies().iterator(); 246 while( it.hasNext()) { 247 String classUnresolved = (String ) it.next(); 248 if (className.equals(classUnresolved)) { 249 cluster.classDefined(className); 250 } else { 251 project.log( 252 "[INFO] Cluster for class: " + className + " is not ready : " + 253 cluster.getUnResolvedDependencies().toString(), 254 Project.MSG_INFO); 255 initClass(classUnresolved + PMapper.PCLASSMAPPINGAPPENDER , msm, cl); 256 } 257 } 258 } 259 260 if (generateDrop) { 261 cluster.deleteMappingStructures(); 262 } 263 if (generateCreate) { 264 cluster.createMappingStructures(true); 265 } 266 if (generateDelete) { 267 cluster.deleteData(); 268 } 269 } catch(Exception pe) { 270 project.log( 271 "[ERROR] Cannot generate sql statements : " + pe.getMessage(), 272 Project.MSG_ERR); 273 } 274 } 275 project.log("[INFO] File generated: " + destFile.getAbsolutePath(), Project.MSG_INFO); 276 } 277 278 public void execute(String [] args){ 279 if (args.length == 0) { 281 usage("You must specify classes to generate script for."); 282 } 283 destFile = new File (System.getProperty(SCRIPT_FILE_NAME)); 285 if (destFile == null) { 286 usage("You must specify a destination file."); 287 } 288 mapperName = System.getProperty(SCRIPT_MAPPER_NAME); 290 if (mapperName == null) { 291 usage("You must specify a mapper name."); 292 } 293 String deleteString = System.getProperty(SCRIPT_DELETE_STATEMENT); 295 if (deleteString != null) { 296 generateDelete = new Boolean (deleteString).booleanValue(); 297 } 298 String dropString = System.getProperty(SCRIPT_DROP_STATEMENT); 300 if (dropString != null) { 301 generateDrop = new Boolean (dropString).booleanValue(); 302 } 303 String createString = System.getProperty(SCRIPT_CREATE_STATEMENT); 305 if (createString != null) { 306 generateCreate = new Boolean (createString).booleanValue(); 307 } 308 } 309 310 private int initClass(String className, 311 RdbScriptPMSM msm, 312 ClassLoader classLoader) { 313 java.lang.Class currentClass = null; 314 try { 315 currentClass = classLoader.loadClass(className); 317 Object o = currentClass.newInstance(); 319 PClassMapping pcm = (PClassMapping) o; 321 pcm.init(msm); 322 } catch (ClassNotFoundException e) { 323 project.log( 324 "[ERROR] Impossible to load the persistent class '" 325 + className + "' from the classpath: " + e.getMessage(), Project.MSG_ERR); 326 return -1; 327 } catch (IllegalAccessException iae) { 328 project.log("[ERROR] Impossible to have access to a new instance of the class '" 329 + className + ": " + iae.getMessage(), Project.MSG_ERR); 330 return -1; 331 } catch (InstantiationException ie) { 332 project.log("[ERROR] Impossible to get a new instance of the class '" 333 + className + ": " + ie.getMessage(), Project.MSG_ERR); 334 return -1; 335 } catch (PException pe) { 336 project.log("[ERROR] Impossible to init the PClassMapping class '" 337 + className + ": " + pe.getMessage(), Project.MSG_ERR); 338 return -1; 339 } 340 return 1; 341 } 342 343 private void usage(String msg) { 344 project.log(msg, Project.MSG_ERR); 345 project.log("Usage: ", Project.MSG_ERR); 346 project.log("\tRdbScriptGeneration (<class name to generate>)*", Project.MSG_ERR); 347 project.log("\t\t-D"+SCRIPT_CLASS_DIR+"=<generated classes directory>", Project.MSG_ERR); 348 project.log("\t\t-D"+SCRIPT_JAR_DIR+"=<added jar directory>", Project.MSG_ERR); 349 project.log("\t\t[-D"+SCRIPT_FILE_NAME+"=<file name>]", Project.MSG_ERR); 350 project.log("\t\t[-D"+SCRIPT_MAPPER_NAME+"=<mapper name>]", Project.MSG_ERR); 351 project.log("\t\t[-D"+SCRIPT_DELETE_STATEMENT+"=<true|false>]", Project.MSG_ERR); 352 project.log("\t\t[-D"+SCRIPT_DROP_STATEMENT+"=<true|false>]", Project.MSG_ERR); 353 project.log("\t\t[-D"+SCRIPT_CREATE_STATEMENT+"=<true|false>]", Project.MSG_ERR); 354 System.exit(1); 355 } 356 357 362 public static String formatElement(String element) { 363 int index = element.lastIndexOf("."); 364 if (index != -1) { 365 element = element.substring(0, index); 367 } 368 if (element.indexOf("\\") != -1) { 369 element = replaceString("\\", ".", element); 371 } else { 372 element = replaceString("/", ".", element); 374 } 375 return element; 376 } 377 378 public static String replaceString(String old, String neo, String str) { 379 if (str == null || str.length() == 0) { 380 return str; 381 } 382 int i = 0; 383 int oldSize = old.length(); 384 int neoSize = neo.length(); 385 StringBuffer sb = new StringBuffer (str); 386 int begin = 0; 387 int idx = sb.indexOf(old, begin); 388 while(idx != -1) { 389 sb.delete(idx, idx + oldSize); 390 sb.insert(idx, neo); 391 begin = idx + neoSize; 392 if (begin < sb.length()) { 393 idx = sb.indexOf(old, begin); 394 } else { 395 idx = -1; 396 } 397 } 398 399 return sb.toString(); 400 } 401 } 402 | Popular Tags |