PageRenderTime 388ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/installer/src/main/java/org/appfuse/command/NewCommand.java

http://github.com/myabc/appfuse
Java | 169 lines | 82 code | 27 blank | 60 comment | 5 complexity | 5e98ebba051e4ed8b99a5aa14bb860cf MD5 | raw file
  1. package org.appfuse.command;
  2. import org.appfuse.utility.PropertyUtility;
  3. import org.appfuse.engine.ApplicationData;
  4. import org.appfuse.ui.SplashScreen;
  5. import java.io.IOException;
  6. import java.io.File;
  7. import org.apache.log4j.Logger;
  8. import org.apache.log4j.xml.DOMConfigurator;
  9. import static org.apache.log4j.Logger.*;
  10. /**
  11. * <p> This program is open software. It is licensed using the Apache Software
  12. * Foundation, version 2.0 January 2004
  13. * </p>
  14. * <a
  15. * href="mailto:dlwhitehurst@gmail.com">dlwhitehurst@gmail.com</a>
  16. *
  17. * @author David L Whitehurst
  18. */
  19. public class NewCommand extends Command implements Runnable {
  20. private static SplashScreen splash;
  21. private static Logger log;
  22. /**
  23. * Construction data used by command
  24. */
  25. private ApplicationData data;
  26. /**
  27. * Constructor takes java bean with all data needed to create
  28. * and run this threaded command
  29. * @param data
  30. */
  31. public NewCommand(ApplicationData data) {
  32. super("NewCommand");
  33. this.data = data;
  34. // configure log4j using resource URL APF-548
  35. DOMConfigurator.configure(getClass().getResource("/META-INF/log4j.xml"));
  36. log = getLogger(NewCommand.class);
  37. }
  38. /**
  39. * Run method for command execution
  40. */
  41. public void run() {
  42. /**
  43. * let the user the know what is going to happen
  44. */
  45. showExecutionStart();
  46. /**
  47. * create an archetype for the new application
  48. */
  49. executeArchetypeCommand();
  50. /**
  51. * Provide some delay to allow thread to create the archetype
  52. */
  53. splash = new SplashScreen();
  54. for (int i = 0; i < 7; i++) {
  55. updateStatus(i);
  56. try {
  57. Thread.sleep(500);
  58. } catch (InterruptedException ie) {
  59. ie.printStackTrace();
  60. }
  61. }
  62. hideSplashScreen();
  63. splash = null;
  64. /**
  65. * let the user the know what is going to happen
  66. */
  67. showExecutionEnd();
  68. }
  69. /**
  70. * Method to hide Splash screen
  71. */
  72. private static void hideSplashScreen() {
  73. if (splash != null) {
  74. splash.dispose();
  75. splash = null;
  76. }
  77. }
  78. /**
  79. * Method to increment Splash progress indicator
  80. */
  81. private static void updateStatus(final int increment) {
  82. if (splash != null) {
  83. splash.advance();
  84. }
  85. }
  86. /**
  87. * Execute maven archetype create goal for determined operating system
  88. */
  89. private void executeArchetypeCommand() {
  90. /*
  91. * Maven embedder:doit
  92. *
  93. */
  94. String[] commands = {"mvn","archetype:create",
  95. "-DarchetypeGroupId=org.appfuse",
  96. "-DarchetypeArtifactId=appfuse-archetype-basic-struts",
  97. "-DarchetypeVersion=1.0-m2",
  98. "-DgroupId=" + data.getPackageName(),
  99. "-DartifactId=" + data.getApplicationName(),
  100. "-DremoteRepositories=http://oss.sonatype.org/content/repositories/appfuse-releases"};
  101. String path = PropertyUtility.getInstance().getSystemProperties().getProperty("user.dir");
  102. log.info(path);
  103. runCommand(commands, null, new File("../"));
  104. //buffer.append("firefox http://localhost/mediawiki");
  105. }
  106. /**
  107. * Execute command using Java Runtime object
  108. * @param commandArray
  109. * @param envP
  110. * @param location
  111. */
  112. private void runCommand(String[] commandArray, String[] envP, File location) {
  113. try {
  114. Runtime.getRuntime().exec(commandArray, envP, location);
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119. /**
  120. * Send console message to acknowledge command start.
  121. *
  122. */
  123. private void showExecutionStart() {
  124. log.info("##############################################################");
  125. log.info("Creating a new web application with the following parameters: ");
  126. log.info("##############################################################");
  127. log.info("Name: " + data.getApplicationName());
  128. log.info("Package: " + data.getPackageName());
  129. log.info("Database Choice: " + data.getDatabaseChoice());
  130. log.info("Database Name: " + data.getDatabaseName());
  131. log.info("Persistence Module: " + data.getPersistenceChoice());
  132. log.info("Web Module: " + data.getWebChoice());
  133. }
  134. /**
  135. * Send console message to acknowledge command end.
  136. */
  137. private void showExecutionEnd() {
  138. log.info("##############################################################");
  139. log.info("Application created: Press Cancel to end dialog");
  140. log.info("##############################################################");
  141. }
  142. }