PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/bamboo/plugin/dotnet/visualstudio/RunnerExtractor.java

https://bitbucket.org/atlassian/bamboo-dotnet-plugin/
Java | 95 lines | 67 code | 13 blank | 15 comment | 2 complexity | 3f20c925b5ad34cb9b14a9e9d96422ab MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.atlassian.bamboo.plugin.dotnet.visualstudio;
  2. import com.atlassian.bamboo.build.fileserver.BuildDirectoryManager;
  3. import com.atlassian.spring.container.LazyComponentReference;
  4. import com.atlassian.util.concurrent.LazyReference;
  5. import net.jcip.annotations.ThreadSafe;
  6. import org.apache.commons.io.IOUtils;
  7. import org.apache.log4j.Logger;
  8. import org.jetbrains.annotations.NotNull;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. /**
  15. * Extracts various wrapper scripts from the plugin classpath in a thread safe way
  16. */
  17. @ThreadSafe
  18. public class RunnerExtractor
  19. {
  20. @SuppressWarnings("unused")
  21. private static final Logger log = Logger.getLogger(RunnerExtractor.class);
  22. // ------------------------------------------------------------------------------------------------------- Constants
  23. private static final LazyComponentReference<BuildDirectoryManager> BUILD_DIRECTORY_MANAGER_REFERENCE = new LazyComponentReference<BuildDirectoryManager>("buildDirectoryManager");
  24. private static final LazyReference<String> DOTNET_RUNNERS_HOME = new LazyReference<String>()
  25. {
  26. @Override
  27. protected String create() throws Exception
  28. {
  29. File file = new File(BUILD_DIRECTORY_MANAGER_REFERENCE.get().getApplicationHome(), "DotNetSupport");
  30. file.mkdirs();
  31. return file.getAbsolutePath();
  32. }
  33. };
  34. private static final LazyReference<String> DEVENV_RUNNER_REFERENCE = new LazyReference<String>()
  35. {
  36. @Override
  37. protected String create() throws Exception
  38. {
  39. return extract("/com/atlassian/bamboo/plugin/dotnet/visualstudio/devenvrunner.bat", "devenvrunner.bat");
  40. }
  41. };
  42. // ------------------------------------------------------------------------------------------------- Type Properties
  43. // ---------------------------------------------------------------------------------------------------- Dependencies
  44. // ---------------------------------------------------------------------------------------------------- Constructors
  45. private RunnerExtractor()
  46. {
  47. }
  48. // ----------------------------------------------------------------------------------------------- Interface Methods
  49. // -------------------------------------------------------------------------------------------------- Action Methods
  50. // -------------------------------------------------------------------------------------------------- Public Methods
  51. /**
  52. * Get the path to Bamboo's external devenv wrapper batch file
  53. * @return path
  54. */
  55. public static String getDevenvRunnerPath()
  56. {
  57. return DEVENV_RUNNER_REFERENCE.get();
  58. }
  59. // -------------------------------------------------------------------------------------- Basic Accessors / Mutators
  60. private static String extract(@NotNull String resourceName, String filename) throws IOException
  61. {
  62. InputStream inputStream = null;
  63. OutputStream outputStream = null;
  64. try
  65. {
  66. inputStream = RunnerExtractor.class.getResourceAsStream(resourceName);
  67. if (inputStream == null)
  68. {
  69. throw new IllegalStateException("Could not find '" + resourceName + "' on classpath");
  70. }
  71. File file = new File(DOTNET_RUNNERS_HOME.get(), filename);
  72. outputStream = new FileOutputStream(file);
  73. IOUtils.copy(inputStream, outputStream);
  74. return file.getAbsolutePath();
  75. }
  76. finally
  77. {
  78. IOUtils.closeQuietly(inputStream);
  79. IOUtils.closeQuietly(outputStream);
  80. }
  81. }
  82. }