PageRenderTime 4248ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/eng/tools/jacob/src/com/mu/jacob/core/ant/JacobTask.java

http://mu-labs.googlecode.com/
Java | 212 lines | 107 code | 30 blank | 75 comment | 16 complexity | ca791a18324b71ff89dc0d09a6c866ee 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.ant;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import org.apache.log4j.Logger;
  31. import org.apache.tools.ant.BuildException;
  32. import org.apache.tools.ant.types.Reference;
  33. import com.google.inject.Binder;
  34. import com.google.inject.Guice;
  35. import com.google.inject.Injector;
  36. import com.google.inject.Module;
  37. import com.mu.jacob.core.ant.ConfigModule.BuilderFctory;
  38. import com.mu.jacob.core.builder.IBuilder;
  39. import com.mu.jacob.core.generator.Config;
  40. import com.mu.jacob.core.generator.DecoratorModule;
  41. import com.mu.jacob.core.generator.DefaultConfig;
  42. import com.mu.jacob.core.generator.ElementModule;
  43. import com.mu.jacob.core.generator.JacobProcessor;
  44. /**
  45. * Main Jacob task, initializes and runs Jacob processor
  46. * @author Adam Smyczek
  47. */
  48. public class JacobTask extends GeneratorTask {
  49. /* Model type containing all model classes */
  50. private ModelType modelType = null;
  51. /* Builder task list */
  52. private final List<BuilderTask> builderTasks = new ArrayList<BuilderTask>();
  53. /* Custom module for DI */
  54. private String module = null;
  55. /**
  56. * @return create model task, only one can be configured
  57. */
  58. public ModelType createModel() {
  59. if (modelType != null) {
  60. throw new BuildException("Only a single descriptor configuration is allowed.");
  61. }
  62. modelType = new ModelType();
  63. return modelType;
  64. }
  65. /**
  66. * @return custom builder task
  67. */
  68. public BuilderTask createBuilder() {
  69. return addBuilderTask(new CustomBuilderTask());
  70. }
  71. /**
  72. * @return file builder task
  73. */
  74. public BuilderTask createFileBuilder() {
  75. return addBuilderTask(new DefaultBuilderTask(BuilderTask.BuilderType.FILE));
  76. }
  77. /**
  78. * @return element builder task
  79. */
  80. public BuilderTask createElementBuilder() {
  81. return addBuilderTask(new DefaultBuilderTask(BuilderTask.BuilderType.ELEMENT));
  82. }
  83. /**
  84. * Internal add builder task method
  85. * @param builderTask
  86. * @return the builder task
  87. */
  88. private BuilderTask addBuilderTask(BuilderTask builderTask) {
  89. builderTasks.add(builderTask);
  90. return builderTask;
  91. }
  92. /**
  93. * Set descriptor task as reference
  94. * @param model reference called by ant
  95. */
  96. public void setModelRef(Reference reference) {
  97. Object refObject = reference.getReferencedObject(getProject());
  98. if (refObject instanceof ModelType) {
  99. if (modelType != null) {
  100. throw new BuildException("Only a single model configuration is allowed.", getLocation());
  101. } else {
  102. modelType = (ModelType)refObject;
  103. }
  104. } else {
  105. throw new BuildException("Referenced model is not a model type!", getLocation());
  106. }
  107. }
  108. /**
  109. * Sets custom module used for dependency injection
  110. * @param module
  111. */
  112. public void setModule(String module) {
  113. this.module = module;
  114. }
  115. /**
  116. * Main execute method
  117. */
  118. @Override
  119. public void execute() throws BuildException {
  120. // validate configuration
  121. logger.debug("Validating configuration");
  122. validateConfiguration();
  123. try {
  124. // Configure class loader
  125. setThreadContextLoader();
  126. // Bootstrap jacob
  127. logger.debug("Bootstrapping application");
  128. DecoratorModule decoratorModule = new DecoratorModule();
  129. ConfigModule configModule = new ConfigModule();
  130. ElementModule elementModule = (modelType.getElementModule() == null)? new ElementModule() :
  131. newInstance(modelType.getElementModule(), ElementModule.class);
  132. Module customModule = (module == null)? emptyModule : newInstance(module, Module.class);
  133. Injector injector = Guice.createInjector(configModule, decoratorModule, elementModule, customModule);
  134. // Create configuration
  135. logger.debug("Configuring jacob");
  136. DefaultConfig config = (DefaultConfig)injector.getInstance(Config.class);
  137. config.initialize(injector);
  138. config.addModelSets(modelType.getModelSets());
  139. config.setClassPath(getPath().toString());
  140. decoratorModule.setConfig(config);
  141. // Add builders
  142. logger.debug("Creating builders");
  143. List<IBuilder> builders = new ArrayList<IBuilder>();
  144. for (BuilderTask builderTask : builderTasks) {
  145. builders.add(injector.getInstance(BuilderFctory.class).createBuilder(builderTask, config));
  146. }
  147. // Create and run processor
  148. logger.debug("Generating");
  149. JacobProcessor processor = injector.getInstance(JacobProcessor.class);
  150. processor.process(builders);
  151. logger.debug("Done");
  152. } catch (RuntimeException re) {
  153. re.printStackTrace();
  154. } finally {
  155. // reset loader
  156. resetThreadContextLoader();
  157. }
  158. }
  159. /**
  160. * Validate all task configuration before execute
  161. * @throws BuildException
  162. */
  163. protected void validateConfiguration() throws BuildException {
  164. // validate model configuration
  165. if (modelType == null) {
  166. throw new BuildException("No model defined.");
  167. } else {
  168. modelType.validateConfiguration();
  169. }
  170. // validate builders
  171. if (builderTasks.isEmpty()) {
  172. throw new BuildException("No builders defined.");
  173. } else {
  174. for (BuilderTask builderTask : builderTasks) {
  175. builderTask.validateConfiguration();
  176. }
  177. }
  178. }
  179. /* Empty module */
  180. private final static Module emptyModule = new Module() { public void configure(Binder binder) { }};
  181. private final static Logger logger = Logger.getLogger(JacobTask.class);
  182. }