PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/bamboo/plugins/tomcat/tasks/DeployAppTask.java

https://bitbucket.org/atlassian/bamboo-tomcat-plugin/
Java | 117 lines | 93 code | 13 blank | 11 comment | 4 complexity | 30ca6e4c5825a00480acf02ddd043e37 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.bamboo.plugins.tomcat.tasks;
  2. import com.atlassian.bamboo.build.logger.BuildLogger;
  3. import com.atlassian.bamboo.plugins.tomcat.configuration.DeployAppConfigurator;
  4. import com.atlassian.bamboo.plugins.tomcat.manager.TaskTomcatConnection;
  5. import com.atlassian.bamboo.plugins.tomcat.manager.TomcatApplicationManager;
  6. import com.atlassian.bamboo.plugins.tomcat.manager.TomcatApplicationManagerImpl;
  7. import com.atlassian.bamboo.plugins.tomcat.manager.TomcatConnection;
  8. import com.atlassian.bamboo.plugins.tomcat.manager.TomcatResult;
  9. import com.atlassian.bamboo.security.EncryptionService;
  10. import com.atlassian.bamboo.task.CommonTaskContext;
  11. import com.atlassian.bamboo.task.CommonTaskType;
  12. import com.atlassian.bamboo.task.TaskException;
  13. import com.atlassian.bamboo.task.TaskResult;
  14. import com.atlassian.bamboo.task.TaskResultBuilder;
  15. import com.atlassian.bamboo.variable.CustomVariableContext;
  16. import org.apache.commons.lang.StringUtils;
  17. import org.apache.log4j.Logger;
  18. import org.jetbrains.annotations.NotNull;
  19. import java.io.File;
  20. import java.io.IOException;
  21. /**
  22. * Deploys a war on the local file system to Tomcat. Will stop
  23. */
  24. public class DeployAppTask implements CommonTaskType
  25. {
  26. private static final Logger log = Logger.getLogger(DeployAppTask.class);
  27. // ------------------------------------------------------------------------------------------------------- Constants
  28. // ------------------------------------------------------------------------------------------------- Type Properties
  29. // ---------------------------------------------------------------------------------------------------- Dependencies
  30. private final EncryptionService encryptionService;
  31. private final CustomVariableContext customVariableContext;
  32. // ---------------------------------------------------------------------------------------------------- Constructors
  33. public DeployAppTask(final EncryptionService encryptionService, final CustomVariableContext customVariableContext)
  34. {
  35. this.encryptionService = encryptionService;
  36. this.customVariableContext = customVariableContext;
  37. }
  38. // ----------------------------------------------------------------------------------------------- Interface Methods
  39. @NotNull
  40. @java.lang.Override
  41. public TaskResult execute(@NotNull final CommonTaskContext taskContext) throws TaskException
  42. {
  43. final BuildLogger buildLogger = taskContext.getBuildLogger();
  44. final TomcatConnection connection = new TaskTomcatConnection(taskContext, encryptionService);
  45. final TomcatApplicationManager tomcatManager = new TomcatApplicationManagerImpl(connection, taskContext, customVariableContext, buildLogger);
  46. final TaskResultBuilder taskResultBuilder = TaskResultBuilder.newBuilder(taskContext);
  47. final String context = taskContext.getConfigurationMap().get(DeployAppConfigurator.APP_CONTEXT);
  48. final String version = taskContext.getConfigurationMap().get(DeployAppConfigurator.APP_VERSION);
  49. final String warFilePath = taskContext.getConfigurationMap().get(DeployAppConfigurator.WAR_FILE_PATH);
  50. final String deploymentTag = taskContext.getConfigurationMap().get(DeployAppConfigurator.DEPLOYMENT_TAG);
  51. final File file = new File(taskContext.getRootDirectory(), warFilePath);
  52. if (file.isFile())
  53. {
  54. try
  55. {
  56. final StringBuilder deployMessage = new StringBuilder()
  57. .append("Deploying application with war file '")
  58. .append(warFilePath)
  59. .append("' to context '")
  60. .append(context)
  61. .append("' to server '")
  62. .append(connection.getURL())
  63. .append("'");
  64. if (StringUtils.isNotEmpty(version))
  65. {
  66. deployMessage.append(" with version '" + version + "'");
  67. }
  68. if (StringUtils.isNotEmpty(deploymentTag))
  69. {
  70. deployMessage.append(" with tag '" + deploymentTag + "'");
  71. }
  72. buildLogger.addBuildLogEntry(deployMessage.toString());
  73. final TomcatResult result = tomcatManager.deployApplication(context, version, deploymentTag, file);
  74. if (result.isSuccessful())
  75. {
  76. buildLogger.addBuildLogEntry("Application was successfully deployed.");
  77. taskResultBuilder.success();
  78. }
  79. else
  80. {
  81. final String message = "Application failed to deploy: " + result.getReason();
  82. buildLogger.addErrorLogEntry(message);
  83. taskResultBuilder.failed();
  84. }
  85. }
  86. catch (IOException e)
  87. {
  88. final String log = "Could not deploy application: " + e.getMessage();
  89. buildLogger.addErrorLogEntry(log, e);
  90. taskResultBuilder.failedWithError();
  91. }
  92. }
  93. else
  94. {
  95. final String noWarMessage = "Could not find war file at " + file.getAbsolutePath() + ". The application could not be deployed.";
  96. buildLogger.addErrorLogEntry(noWarMessage);
  97. taskResultBuilder.failedWithError();
  98. }
  99. return taskResultBuilder.build();
  100. }
  101. // -------------------------------------------------------------------------------------------------- Action Methods
  102. // -------------------------------------------------------------------------------------------------- Public Methods
  103. // -------------------------------------------------------------------------------------- Basic Accessors / Mutators
  104. }