/atlassian-browsers-auto/src/main/java/com/atlassian/browsers/BrowserInstaller.java

https://bitbucket.org/atlassian/atlassian-browsers · Java · 216 lines · 158 code · 29 blank · 29 comment · 14 complexity · 535127e37dd09ff2afaf5c2c37535730 MD5 · raw file

  1. package com.atlassian.browsers;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import javax.annotation.Nullable;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import static com.atlassian.browsers.Utils.extractZip;
  8. import static com.atlassian.browsers.Utils.resourceExists;
  9. import static com.google.common.base.Preconditions.checkNotNull;
  10. import static org.apache.commons.io.FileUtils.readFileToString;
  11. /**
  12. * Defines the browser version, OS and the executable path for each browser version.
  13. */
  14. enum BrowserInstaller
  15. {
  16. FIREFOX_OSX(BrowserType.FIREFOX, OS.OSX, "Contents/MacOS/firefox-bin"),
  17. CHROME_OSX(BrowserType.CHROME, OS.OSX, "Google Chrome.app/Contents/MacOS/Google Chrome"),
  18. FIREFOX_LINUX(BrowserType.FIREFOX, OS.LINUX, "firefox-bin"),
  19. CHROME_LINUX(BrowserType.CHROME, OS.LINUX, "chrome"),
  20. FIREFOX_LINUX64(BrowserType.FIREFOX, OS.LINUX64, "firefox-bin"),
  21. CHROME_LINUX64(BrowserType.CHROME, OS.LINUX64, "chrome"),
  22. FIREFOX_WINDOWS(BrowserType.FIREFOX, OS.WINDOWS, "firefox.exe"),
  23. CHROME_WINDOWS(BrowserType.CHROME, OS.WINDOWS, "chrome.exe"),
  24. IE_WINDOWS(BrowserType.IE, OS.WINDOWS, null);
  25. private static final Logger log = LoggerFactory.getLogger(BrowserInstaller.class);
  26. private static final String PACKAGE_MANIFEST = "profile.package";
  27. private static final String BROWSER_MANIFEST = "browser.package";
  28. private final BrowserType browser;
  29. private final OS os;
  30. private final String binaryPath;
  31. private BrowserInstaller(BrowserType browser, @Nullable OS os, @Nullable String binaryPath)
  32. {
  33. this.browser = checkNotNull(browser, "browser");
  34. this.os = checkNotNull(os, "os");
  35. this.binaryPath = binaryPath;
  36. }
  37. public BrowserType getBrowser()
  38. {
  39. return browser;
  40. }
  41. /**
  42. * OS this installer applies to. {@code null} signifies an that all operating systems are supported.
  43. *
  44. * @return {@code OS} supported by this installer
  45. */
  46. @Nullable
  47. public OS getOS()
  48. {
  49. return os;
  50. }
  51. public String getOsDirName()
  52. {
  53. return os == null ? OS.ALL : os.getName();
  54. }
  55. /**
  56. * Path to the browser executable within the browser package. May be {@literal null}, which means that this
  57. * installer does not support actually starting the target browser.
  58. *
  59. * @return path to the browser executable
  60. */
  61. @Nullable
  62. public String getBinaryPath()
  63. {
  64. return binaryPath;
  65. }
  66. /**
  67. * Determines the BrowserInstaller based on the browserStr passed and the current OS.
  68. * @param browserStr the browserStr that defines the BrowserInstaller that is needed.
  69. * @return The browser installer if it exists otherwise null.
  70. */
  71. public static BrowserInstaller typeOf(String browserStr)
  72. {
  73. OS os = OS.getType();
  74. BrowserType browserType = BrowserType.typeOf(browserStr);
  75. for (BrowserInstaller installer: BrowserInstaller.values())
  76. {
  77. if (installer.getOS().getName().equals(os.getName()) &&
  78. installer.getBrowser().equals(browserType))
  79. {
  80. // Check it's not a more specific browser string such as firefox:path=/path/to/firefox-binary
  81. if (browserStr.equals(installer.getBrowser().getName()) ||
  82. browserStr.matches("^" + installer.getBrowser().getName() + "-[0-9.]+$"))
  83. {
  84. log.debug("Using {}", installer);
  85. return installer;
  86. }
  87. }
  88. }
  89. log.info("There are no browser installers for browser string {}", browserStr);
  90. return null;
  91. }
  92. /**
  93. * Installs the current browser into the destination directory specified by
  94. * extracting the browser zip file.
  95. * If there is a profile zip for the browser it will also extract this.
  96. * Then the correct permissions are applied to the required files.
  97. *
  98. * @param destination the location to extract the browser into. This is the parent directory for the browser.
  99. * @param installConfigurator a default install configurator
  100. */
  101. public void install(File destination, BuiltInConfigurator installConfigurator)
  102. {
  103. String browserName = browser.getName();
  104. String binaryPath = getBinaryPath();
  105. String osDirName = getOsDirName();
  106. String profileName = browserName + "-profile";
  107. final String browserResource = "/" + browserName + "-" + osDirName + ".zip";
  108. final String profileResource = "/" + profileName + ".zip";
  109. final String osProfileResource = "/" + profileName + "-" + osDirName + ".zip";
  110. try
  111. {
  112. final File browserDir = extractBrowser(destination, browserResource);
  113. final File browserBinary = getBrowserBinary(binaryPath, browserDir);
  114. final File browserProfile = extractProfile(destination, profileResource, osProfileResource);
  115. BrowserConfig browserConfig = new BrowserConfig(browserDir, browserBinary, browserProfile, browser);
  116. installConfigurator.setupBrowser(browserConfig);
  117. }
  118. catch(IOException e)
  119. {
  120. throw new RuntimeException(e);
  121. }
  122. }
  123. private File extractProfile(File destination, String profileResource, String osProfileResource) throws IOException
  124. {
  125. if (resourceExists(osProfileResource))
  126. {
  127. return extractZipProfileAndLog(destination, osProfileResource);
  128. }
  129. else if (resourceExists(profileResource))
  130. {
  131. return extractZipProfileAndLog(destination, profileResource);
  132. }
  133. else
  134. {
  135. log.info("Profile for {}:{} is not supported", getBrowser().name(), getOsDirName());
  136. return null;
  137. }
  138. }
  139. private File extractZipProfileAndLog(File destination, String osProfileResource) throws IOException
  140. {
  141. final File profileDir = extractZip(destination, osProfileResource);
  142. if (log.isInfoEnabled())
  143. {
  144. log.info("Installed profile {}: {}", profileDir, readManifestInfo(new File(profileDir, PACKAGE_MANIFEST)));
  145. }
  146. return profileDir;
  147. }
  148. private File extractBrowser(File destination, String browserResource) throws IOException
  149. {
  150. if (!resourceExists(browserResource))
  151. {
  152. log.info("Browser {}:{} is not supported", getBrowser().name(), getOsDirName());
  153. return null;
  154. }
  155. final File browserDir = extractZip(destination, browserResource);
  156. if (log.isInfoEnabled())
  157. {
  158. log.info("Installed browser {}: {}", browserDir, readManifestInfo(new File(browserDir, BROWSER_MANIFEST)));
  159. }
  160. return browserDir;
  161. }
  162. private File getBrowserBinary(String binaryPath, File browserDir) throws IOException
  163. {
  164. if (browserDir == null)
  165. {
  166. return null;
  167. }
  168. File browserBinary = new File(browserDir, binaryPath);
  169. if (!OsValidator.isWindows())
  170. {
  171. Utils.make755(browserBinary);
  172. }
  173. return browserBinary;
  174. }
  175. private static String readManifestInfo(File manifestFile) throws IOException
  176. {
  177. if( manifestFile.exists() )
  178. {
  179. String contents = readFileToString(manifestFile);
  180. return contents.trim().replaceAll("\n", ", ");
  181. }
  182. else
  183. {
  184. return "Manifest file not found " + manifestFile;
  185. }
  186. }
  187. }