PageRenderTime 76ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/atlassian/bamboo-tomcat-plugin/
Java | 84 lines | 66 code | 10 blank | 8 comment | 1 complexity | 6b6f7227481714570c33a133366d6f5e 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.log4j.Logger;
  17. import org.jetbrains.annotations.NotNull;
  18. import java.io.IOException;
  19. public class StopAppTask implements CommonTaskType
  20. {
  21. private static final Logger log = Logger.getLogger(StopAppTask.class);
  22. // ------------------------------------------------------------------------------------------------------- Constants
  23. // ------------------------------------------------------------------------------------------------- Type Properties
  24. // ---------------------------------------------------------------------------------------------------- Dependencies
  25. private final EncryptionService encryptionService;
  26. private final CustomVariableContext customVariableContext;
  27. // ---------------------------------------------------------------------------------------------------- Constructors
  28. public StopAppTask(final EncryptionService encryptionService, final CustomVariableContext customVariableContext)
  29. {
  30. this.encryptionService = encryptionService;
  31. this.customVariableContext = customVariableContext;
  32. }
  33. // ----------------------------------------------------------------------------------------------- Interface Methods
  34. @NotNull
  35. @Override
  36. public TaskResult execute(@NotNull final CommonTaskContext taskContext) throws TaskException
  37. {
  38. final BuildLogger buildLogger = taskContext.getBuildLogger();
  39. final TomcatConnection connection = new TaskTomcatConnection(taskContext, encryptionService);
  40. final TomcatApplicationManager tomcatManager = new TomcatApplicationManagerImpl(connection, taskContext, customVariableContext, buildLogger);
  41. final TaskResultBuilder taskResultBuilder = TaskResultBuilder.newBuilder(taskContext);
  42. final String context = taskContext.getConfigurationMap().get(DeployAppConfigurator.APP_CONTEXT);
  43. try
  44. {
  45. buildLogger.addBuildLogEntry("Stopping application at context '"
  46. + context
  47. + "' on server '"
  48. + connection.getURL()
  49. + "'");
  50. final TomcatResult result = tomcatManager.stopApplication(context);
  51. if (result.isSuccessful())
  52. {
  53. buildLogger.addBuildLogEntry("The application has been stopped");
  54. taskResultBuilder.success();
  55. }
  56. else
  57. {
  58. final String message = "The application could not be stopped: " + result.getReason();
  59. buildLogger.addErrorLogEntry(message);
  60. taskResultBuilder.failed();
  61. }
  62. }
  63. catch (IOException e)
  64. {
  65. final String log = "Could not stop application: " + e.getMessage();
  66. buildLogger.addErrorLogEntry(log);
  67. taskResultBuilder.failedWithError();
  68. }
  69. return taskResultBuilder.build();
  70. }
  71. // -------------------------------------------------------------------------------------------------- Action Methods
  72. // -------------------------------------------------------------------------------------------------- Public Methods
  73. // -------------------------------------------------------------------------------------- Basic Accessors / Mutators
  74. }