1 2 12 package com.versant.core.jdo.tools.enhancer; 13 14 import com.versant.core.common.config.ConfigInfo; 15 import com.versant.core.common.config.ConfigParser; 16 import com.versant.core.jdo.tools.enhancer.info.ClassInfo; 17 import com.versant.core.jdo.tools.enhancer.info.FieldInfo; 18 import com.versant.core.jdo.tools.enhancer.utils.MetaDataToInfoMapper; 19 import com.versant.core.jdo.tools.enhancer.utils.SwapFieldHelper; 20 import com.versant.core.metadata.ClassMetaData; 21 import com.versant.core.metadata.ModelMetaData; 22 import com.versant.core.storagemanager.StorageManagerFactory; 23 import com.versant.core.storagemanager.StorageManagerFactoryBuilder; 24 import com.versant.core.util.BeanUtils; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.util.*; 29 30 35 public class Enhancer { 36 37 private ClassEnhancer classEnhancer; 38 private FieldRefEnhancer fieldRefEnhancer; 39 private List classes = new ArrayList(); 40 private HashMap classResourceMap = new HashMap(); private MetaDataToInfoMapper classInfoUtil; 42 private HashMap classMetaDataMap = new HashMap(); 43 private ModelMetaData metaData = null; 44 protected Config cfg = new Config(); 45 46 private Set scopeFiles = new HashSet(); 47 private Set queriesFiles = new HashSet(); 48 private File outputDirectory; 49 private ClassLoader classLoader; 50 private String propertiesResourceName; 51 private File propertiesFile; 52 private boolean genHyper = false; 53 private boolean makeFieldsPrivate = false; 54 private boolean detached = true; 55 private File hyperdriveDir; 56 private File srcOutDir; 57 private boolean genSrc; 58 59 public Enhancer() {} 60 61 public static class Config { 62 63 public String outputDir; 64 public String inputDir; 65 public boolean h = false; 66 public String hyperDir; 67 public boolean genSrc = false; 68 public String srcOutputDir; 69 public boolean d = true; 70 71 72 public static final String HELP_outputDir = 73 "The output directory where the enhanced classes must be writen to " + 74 "(If not set then the classes will be written where they are found)."; 75 public static final String HELP_hyperDir = 76 "The output directory where the hyperdrive classes must be writen to."; 77 public static final String HELP_inputDir = 78 "The Persistence Aware files. " + 79 "These are normal classes (i.e. NOT Persistence Capable classes) that " + 80 "reference the persistence capable classes fields directly."; 81 public static final String HELP_h = 82 "generate hyperdrive classes at compile time."; 83 public static final String HELP_srcOutputDir = 84 "The place where the hyperdrive src files must be written to."; 85 public static final String HELP_d = 86 "enhanced for detach (true by default)."; 87 public static final String HELP_genSrc = 88 "The directory where the hyperdrive src files must be written to."; 89 90 91 } 92 93 public static void main(String [] args) { 94 Enhancer server = new Enhancer(); 95 BeanUtils.CmdLineResult res = null; 96 try { 97 res = BeanUtils.processCmdLine(args, 98 server.getClass().getClassLoader(), server.cfg, null, 99 "Enhancer", "The JDO class enhancer", 100 true); 101 } catch (IllegalArgumentException e) { 102 System.exit(1); 104 } 105 try { 106 107 ArrayList scopeFiles = new ArrayList(); 108 if (server.cfg.inputDir != null) { 109 StringTokenizer tokenizer = new StringTokenizer(server.cfg.inputDir,",",false); 110 while(tokenizer.hasMoreTokens()){ 111 scopeFiles.add(new File (tokenizer.nextToken())); 112 } 113 } 114 115 if (server.cfg.srcOutputDir != null){ 116 server.cfg.genSrc = true; 117 } 118 119 server.rumCommandLine(res.properties, server.cfg.outputDir, scopeFiles, 120 null, server.cfg.h, false, server.cfg.d, server.cfg.hyperDir, 121 server.cfg.genSrc, server.cfg.srcOutputDir); 122 } catch (Exception e) { 123 e.printStackTrace(); 124 System.exit(1); 125 } 126 } 127 128 129 134 public ClassLoader getClassLoader() { 135 return classLoader; 136 } 137 138 143 public void setClassLoader(ClassLoader classLoader) { 144 this.classLoader = classLoader; 145 } 146 147 152 public boolean isDetached() { 153 return detached; 154 } 155 156 161 public void setDetached(boolean detached) { 162 this.detached = detached; 163 } 164 165 170 public boolean isGenHyper() { 171 return genHyper; 172 } 173 174 179 public void setGenHyper(boolean genHyper) { 180 this.genHyper = genHyper; 181 } 182 187 public boolean isGenSrc() { 188 return genSrc; 189 } 190 195 public void setGenSrc(boolean genSrc) { 196 this.genSrc = genSrc; 197 } 198 199 public File getSrcOutDir() { 200 return srcOutDir; 201 } 202 203 public void setSrcOutDir(File srcOutDir) { 204 this.srcOutDir = srcOutDir; 205 } 206 207 213 public boolean isMakeFieldsPrivate() { 214 return makeFieldsPrivate; 215 } 216 217 223 public void setMakeFieldsPrivate(boolean makeFieldsPrivate) { 224 this.makeFieldsPrivate = makeFieldsPrivate; 225 } 226 227 233 public File getOutputDir() { 234 return outputDirectory; 235 } 236 237 243 public void setOutputDir(File outputDir) { 244 this.outputDirectory = outputDir; 245 } 246 247 252 public File getPropertiesFile() { 253 return propertiesFile; 254 } 255 256 261 public void setPropertiesFile(File propertiesFile) { 262 this.propertiesFile = propertiesFile; 263 } 264 265 270 public String getPropertiesResourceName() { 271 return propertiesResourceName; 272 } 273 274 279 public void setPropertiesResourceName(String propertiesResourceName) { 280 this.propertiesResourceName = propertiesResourceName; 281 } 282 283 291 296 public Set getPCAwareFiles() { 297 return scopeFiles; 298 } 299 300 309 public void setPCAwareFiles(Set scopeFiles) { 310 this.scopeFiles = scopeFiles; 311 } 312 313 public void setHyperdriveDir(File hyperdriveDir){ 314 this.hyperdriveDir = hyperdriveDir; 315 } 316 317 323 public void enhance() throws Exception { 324 if (propertiesFile == null && propertiesResourceName == null) { 325 throw new Exception ( 326 "Either the propertiesFile or propertiesResourceName is required."); 327 } 328 329 setClassLoader(getPrivateClassLoader()); 330 334 if (propertiesFile != null) { 335 start(scopeFiles, queriesFiles, outputDirectory, classLoader, 336 propertiesFile, genHyper, makeFieldsPrivate, detached, 337 genSrc, srcOutDir); 338 } else { 339 start(scopeFiles, queriesFiles, outputDirectory, classLoader, 340 propertiesResourceName, genHyper, makeFieldsPrivate, 341 detached, genSrc, srcOutDir); 342 } 343 344 } 345 346 private void rumCommandLine(Properties props, String outputDir, 347 ArrayList scopeFiles, ArrayList queries, boolean genHyper, 348 boolean makeFieldsPrivate, boolean detached, String hyperdriveDir, 349 boolean genSrc, String hyperSrcDir) throws Exception { 350 ClassLoader callingClassLoader = Enhancer.class.getClassLoader(); 351 GrepFile grepFile = null; 352 353 try { 354 grepFile = new GrepFile(scopeFiles); 355 } catch (IOException e) { 356 e.printStackTrace(); } 358 359 if (outputDir != null){ 360 this.outputDirectory = new File (outputDir); 361 } 362 363 if (hyperdriveDir != null) { 364 this.hyperdriveDir = new File (hyperdriveDir); 365 } 366 367 if (hyperSrcDir != null) { 368 this.srcOutDir = new File (hyperSrcDir); 369 } 370 this.genSrc = genSrc; 371 372 start(grepFile.getJdoFiles(), null, outputDirectory, 373 callingClassLoader, props, genHyper, 374 makeFieldsPrivate, detached, genSrc, srcOutDir); 375 376 } 377 378 private void start(Set scopeFiles, Set queriesFiles, File outputDir, 379 ClassLoader callingClassLoader, 380 File configFile, boolean genHyper, boolean makeFieldsPrivate, 381 boolean detached, boolean genSrc, File srcOutDir) throws Exception { 382 this.genHyper = genHyper; 383 metaData = getMetaData(configFile, callingClassLoader); 384 startImp(scopeFiles, queriesFiles, outputDir, callingClassLoader, 385 genHyper, makeFieldsPrivate, detached, genSrc, srcOutDir); 386 } 387 388 private void start(Set scopeFiles, Set queriesFiles, File outputDir, 389 ClassLoader callingClassLoader, 390 Properties props, boolean genHyper, boolean makeFieldsPrivate, 391 boolean detached, boolean genSrc, File srcOutDir) throws Exception { 392 this.genHyper = genHyper; 393 metaData = getMetaData(props, callingClassLoader); 394 startImp(scopeFiles, queriesFiles, outputDir, callingClassLoader, 395 genHyper, makeFieldsPrivate, detached, genSrc, srcOutDir); 396 } 397 398 private void start(Set scopeFiles, Set queriesFiles, File outputDir, 399 ClassLoader callingClassLoader, 400 String configFilename, boolean genHyper, boolean makeFieldsPrivate, 401 boolean detached, boolean genSrc, File srcOutDir) throws Exception { 402 this.genHyper = genHyper; 403 metaData = getMetaData(configFilename, callingClassLoader); 404 startImp(scopeFiles, queriesFiles, outputDir, callingClassLoader, 405 genHyper, makeFieldsPrivate, detached, genSrc, srcOutDir); 406 407 } 408 409 private void startImp(Set scopeFiles, Set queriesFiles, File outputDir, 410 ClassLoader callingClassLoader, boolean genHyper, 411 boolean makeFieldsPrivate, boolean detached, boolean genSrc, File srcOutDir) 412 throws Exception { 413 classEnhancer = new ClassEnhancer(outputDir, callingClassLoader); 414 fieldRefEnhancer = new FieldRefEnhancer(outputDir, callingClassLoader); 415 classInfoUtil = new MetaDataToInfoMapper(classResourceMap, 416 callingClassLoader); 417 for (int k = 0; k < metaData.classes.length; k++) { 418 ClassMetaData classMetaData = metaData.classes[k]; 419 classMetaDataMap.put(classMetaData.qname, classMetaData); 420 classInfoUtil.setClassInfo(classMetaData.jdoClass, classMetaData); 421 } 422 423 classes = classInfoUtil.getClassInfoList(); 424 long start = System.currentTimeMillis(); 425 classEnhancer.setGetAndSettersMap(getMapOfGetAndSetters(classes)); 426 ListIterator classIter = classes.listIterator(); 427 int classCount = 0; 428 while (classIter.hasNext()) { 429 ClassInfo classInfo = (ClassInfo)classIter.next(); 430 fieldRefEnhancer.setPersistentCapable(classInfo.getClassName()); 431 ClassMetaData classMeta = (ClassMetaData)classMetaDataMap.get( 433 classInfo.getClassName()); 434 if (classEnhancer.enhance(classInfo, classMeta, makeFieldsPrivate, 435 detached)) { 436 classCount++; 437 } 438 } 439 440 long end = System.currentTimeMillis(); 441 long time = end - start; 442 System.out.println( 443 "[Enhanced (" + classCount + ")] time = " + time + " ms"); 444 445 start = System.currentTimeMillis(); 446 447 classCount = classes.size(); 448 fieldRefEnhancer.setFieldsToEnhanceMap( 449 getNonPrivateMapOfGetAndSetters(classes)); 450 fieldRefEnhancer.enhance(scopeFiles); 451 452 end = System.currentTimeMillis(); 453 time = end - start; 454 System.out.println( 455 "[Looked through " + scopeFiles.size() + " class files and found " + fieldRefEnhancer.getAwareNum() + " Persistence Aware Classes] time = " + time + " ms"); 456 457 } 466 467 private ClassLoader getPrivateClassLoader() { 468 ClassLoader classLoader = getClassLoader(); 469 if (classLoader != null) { 470 return classLoader; 471 } 472 ClassLoader taskClassLoader = getClass().getClassLoader(); 473 if (taskClassLoader == null) { 474 taskClassLoader = ClassLoader.getSystemClassLoader(); 475 } 476 return taskClassLoader; 477 } 478 479 ArrayList orderList = new ArrayList(); 480 481 private void getOrder(ClassMetaData meta) { 482 orderList.add(meta.qname); 483 ClassMetaData[] mySub = meta.pcSubclasses; 484 if (mySub == null) { 485 return; 486 } else { 487 for (int j = 0; j < mySub.length; j++) { 488 ClassMetaData data = mySub[j]; 489 getOrder(data); 490 } 491 } 492 } 493 494 ArrayList hierList = new ArrayList(); 495 496 private void getHier(ClassMetaData meta) { 497 hierList.add(meta.qname); 498 ClassMetaData[] mySub = meta.pcSubclasses; 499 if (mySub == null) { 500 return; 501 } else { 502 for (int j = 0; j < mySub.length; j++) { 503 ClassMetaData data = mySub[j]; 504 getHier(data); 505 } 506 } 507 } 508 509 private HashMap getMapOfGetAndSetters(List classes) { 510 HashMap map = new HashMap(); 511 512 ClassMetaData[] allMetas = metaData.classes; 513 for (int i = 0; i < allMetas.length; i++) { 514 ClassMetaData meta = allMetas[i]; 515 if (!meta.isInHeirachy()) { 516 orderList.add(meta.qname); 517 } else if (meta.top.equals(meta)) { 518 getOrder(meta); 519 } 520 } 521 522 for (Iterator iterator = orderList.iterator(); iterator.hasNext();) { 523 String s = (String )iterator.next(); 524 Iterator classIter = classes.iterator(); 525 while (classIter.hasNext()) { 526 ClassInfo classInfo = (ClassInfo)classIter.next(); 527 String className = classInfo.getClassName(); 528 if (className.equals(s)) { 529 Iterator iter = classInfo.getFieldList().iterator(); 530 while (iter.hasNext()) { 531 FieldInfo fieldInfo = (FieldInfo)iter.next(); 532 String fieldName = fieldInfo.getFieldName(); 533 SwapFieldHelper helper = new SwapFieldHelper(); 534 helper.className = className; 535 helper.fieldName = fieldName; 536 helper.jdoGetName = fieldInfo.getJdoGetName(); 537 helper.jdoSetName = fieldInfo.getJdoSetName(); 538 helper.type = fieldInfo.getType(); 539 map.put(className + "|" + fieldName, helper); 540 541 if (!fieldInfo.isPrivate()) { 542 ClassMetaData clMeta = metaData.getClassMetaData( 543 className); 544 hierList = new ArrayList(); 545 getHier(clMeta); 546 for (Iterator iterator1 = hierList.iterator(); 547 iterator1.hasNext();) { 548 String clName = (String )iterator1.next(); 549 map.put(clName + "|" + fieldName, helper); 550 } 551 } 552 } 553 554 } 555 } 556 557 } 558 return map; 559 } 560 561 private HashMap getNonPrivateMapOfGetAndSetters(List classes) { 562 HashMap map = new HashMap(); 563 564 for (Iterator iterator = orderList.iterator(); iterator.hasNext();) { 565 String s = (String )iterator.next(); 566 Iterator classIter = classes.iterator(); 567 while (classIter.hasNext()) { 568 ClassInfo classInfo = (ClassInfo)classIter.next(); 569 String className = classInfo.getClassName(); 570 if (className.equals(s)) { 571 Iterator iter = classInfo.getFieldList().iterator(); 572 while (iter.hasNext()) { 573 FieldInfo fieldInfo = (FieldInfo)iter.next(); 574 if (fieldInfo.isPrivate()) continue; 575 String fieldName = fieldInfo.getFieldName(); 576 SwapFieldHelper helper = new SwapFieldHelper(); 577 helper.className = className; 578 helper.fieldName = fieldName; 579 helper.jdoGetName = fieldInfo.getJdoGetName(); 580 helper.jdoSetName = fieldInfo.getJdoSetName(); 581 helper.type = fieldInfo.getType(); 582 map.put(className + "|" + fieldName, helper); 583 584 ClassMetaData clMeta = metaData.getClassMetaData( 585 className); 586 hierList = new ArrayList(); 587 getHier(clMeta); 588 for (Iterator iterator1 = hierList.iterator(); 589 iterator1.hasNext();) { 590 String clName = (String )iterator1.next(); 591 map.put(clName + "|" + fieldName, helper); 592 } 593 } 594 } 595 } 596 } 597 return map; 598 } 599 600 private ModelMetaData getMetaData(File configFile, ClassLoader loader) 601 throws Exception { 602 ConfigInfo config = new ConfigParser().parseResource(configFile); 604 config.validate(); 605 return getMetaDataImp(config, loader); 606 } 607 608 private ModelMetaData getMetaData(String configFilename, ClassLoader loader) 609 throws Exception { 610 ConfigInfo config = new ConfigParser().parseResource(configFilename, 612 loader); 613 config.validate(); 614 return getMetaDataImp(config, loader); 615 } 616 617 private ModelMetaData getMetaData(Properties props, ClassLoader loader) 618 throws Exception { 619 ConfigInfo config = new ConfigParser().parse(props); 621 config.validate(); 622 return getMetaDataImp(config, loader); 623 } 624 625 private ModelMetaData getMetaDataImp(ConfigInfo config, ClassLoader loader) 626 throws Exception { 627 628 if (srcOutDir != null) { 629 config.hyperdriveSrcDir = srcOutDir.toString(); 630 } 631 if (hyperdriveDir != null) { 632 config.hyperdriveClassDir = hyperdriveDir.toString(); 633 } else if (genHyper) { 634 config.hyperdriveClassDir = outputDirectory.toString(); 635 } 636 config.hyperdrive = srcOutDir != null || hyperdriveDir != null; 637 if (this.genHyper && !config.hyperdrive){ 638 config.hyperdrive = true; 639 } 640 641 StorageManagerFactoryBuilder b = new StorageManagerFactoryBuilder(); 642 b.setConfig(config); 643 b.setLoader(loader); 644 b.setOnlyMetaData(true); 645 StorageManagerFactory smf = b.createStorageManagerFactory(); 646 metaData = smf.getModelMetaData(); 647 648 Thread.currentThread().setContextClassLoader(loader); 649 return metaData; 650 } 651 652 } 653 | Popular Tags |