PageRenderTime 869ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/eng/tools/jacob/src/com/mu/jacob/core/generator/JacobRunner.java

http://mu-labs.googlecode.com/
Java | 111 lines | 43 code | 16 blank | 52 comment | 4 complexity | 573a198703c09b460f96cb7fab79e31d MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. /**
  2. * Copyright (c) 2008, Mu Dynamics.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * - Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation and/or
  12. * other materials provided with the distribution.
  13. * - Neither the name of the "Mu Dynamics" nor the names of its contributors may be used
  14. * to endorse or promote products derived from this software without specific prior
  15. * written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  20. * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  22. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.mu.jacob.core.generator;
  28. import java.util.Arrays;
  29. import org.apache.log4j.Logger;
  30. import com.google.inject.Binder;
  31. import com.google.inject.Guice;
  32. import com.google.inject.Injector;
  33. import com.google.inject.Module;
  34. import com.mu.jacob.core.ant.ConfigModule;
  35. import com.mu.jacob.core.builder.IBuilder;
  36. /**
  37. * Jacob runner used for integration into a Java build app.
  38. * @author Adam Smyczek
  39. *
  40. */
  41. public class JacobRunner {
  42. /* Element module */
  43. private ElementModule elementModule = new ElementModule();
  44. /* Custom module for Guice DI */
  45. private Module module = null;
  46. /**
  47. * Element module setter
  48. * @param module
  49. */
  50. public void setElementModule(final ElementModule module) {
  51. this.elementModule = module;
  52. }
  53. /**
  54. * Custom module setter
  55. * @param module
  56. */
  57. public void setCustomModule(final Module module) {
  58. this.module = module;
  59. }
  60. /**
  61. * Run code generation
  62. * @param modelSets containing all model classes
  63. * @param builders list of builders
  64. * @param classPath optional class path for the compiler processor
  65. */
  66. public void run(final IBuilder[] builders, final String classPath, final ModelSet... modelSets) {
  67. logger.debug("Bootstrapping application");
  68. DecoratorModule decoratorModule = new DecoratorModule();
  69. ConfigModule configModule = new ConfigModule();
  70. Module customModule = (module != null)? module : emptyModule;
  71. Injector injector = Guice.createInjector(configModule, decoratorModule, elementModule, customModule);
  72. // Create configuration
  73. logger.debug("Configuring jacob");
  74. DefaultConfig config = (DefaultConfig)injector.getInstance(Config.class);
  75. config.initialize(injector);
  76. config.addModelSets(Arrays.asList(modelSets));
  77. // Set class path
  78. if (classPath != null) {
  79. config.setClassPath(classPath);
  80. }
  81. decoratorModule.setConfig(config);
  82. // Initialize builders
  83. for (IBuilder builder : builders) {
  84. injector.injectMembers(builder);
  85. }
  86. // Create and run processor
  87. logger.debug("Generating");
  88. JacobProcessor processor = injector.getInstance(JacobProcessor.class);
  89. processor.process(Arrays.asList(builders));
  90. logger.debug("Done");
  91. }
  92. /* Empty module */
  93. private final static Module emptyModule = new Module() { public void configure(Binder binder) { }};
  94. private static final Logger logger = Logger.getLogger(JacobRunner.class);
  95. }