You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IsolatedLauncherFactory.java 3.8KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * SonarQube Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * sonarqube@googlegroups.com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner.impl;
  21. import java.io.File;
  22. import java.nio.file.Paths;
  23. import java.security.AccessController;
  24. import java.security.PrivilegedAction;
  25. import java.util.List;
  26. import java.util.Properties;
  27. import org.sonar.home.cache.Logger;
  28. import org.sonar.home.cache.PersistentCache;
  29. import org.sonar.home.cache.PersistentCacheBuilder;
  30. import org.sonar.runner.batch.IsolatedLauncher;
  31. public class IsolatedLauncherFactory {
  32. static final String ISOLATED_LAUNCHER_IMPL = "org.sonar.runner.batch.BatchIsolatedLauncher";
  33. private final TempCleaning tempCleaning;
  34. private final String launcherImplClassName;
  35. private final Logger logger;
  36. /**
  37. * For unit tests
  38. */
  39. IsolatedLauncherFactory(String isolatedLauncherClassName, TempCleaning tempCleaning, Logger logger) {
  40. this.tempCleaning = tempCleaning;
  41. this.launcherImplClassName = isolatedLauncherClassName;
  42. this.logger = logger;
  43. }
  44. public IsolatedLauncherFactory(Logger logger) {
  45. this(ISOLATED_LAUNCHER_IMPL, new TempCleaning(logger), logger);
  46. }
  47. private PersistentCache getCache(Properties props) {
  48. PersistentCacheBuilder builder = new PersistentCacheBuilder(logger);
  49. String serverUrl = props.getProperty("sonar.host.url");
  50. String home = props.getProperty("sonar.userHome");
  51. builder.setAreaForGlobal(serverUrl, null);
  52. if (home != null) {
  53. builder.setSonarHome(Paths.get(home));
  54. }
  55. return builder.build();
  56. }
  57. private ClassLoader createClassLoader(List<File> jarFiles, ClassloadRules maskRules) {
  58. IsolatedClassloader classloader = new IsolatedClassloader(getClass().getClassLoader(), maskRules);
  59. classloader.addFiles(jarFiles);
  60. return classloader;
  61. }
  62. public IsolatedLauncher createLauncher(Properties props, ClassloadRules rules) {
  63. if (props.containsKey(InternalProperties.RUNNER_DUMP_TO_FILE)) {
  64. String version = props.getProperty(InternalProperties.RUNNER_VERSION_SIMULATION);
  65. if (version == null) {
  66. version = "5.2";
  67. }
  68. return new SimulatedLauncher(version, logger);
  69. }
  70. ServerConnection serverConnection = ServerConnection.create(props, getCache(props), logger);
  71. JarDownloader jarDownloader = new JarDownloader(serverConnection, logger, props);
  72. return createLauncher(jarDownloader, rules);
  73. }
  74. IsolatedLauncher createLauncher(final JarDownloader jarDownloader, final ClassloadRules rules) {
  75. return AccessController.doPrivileged(new PrivilegedAction<IsolatedLauncher>() {
  76. @Override
  77. public IsolatedLauncher run() {
  78. try {
  79. List<File> jarFiles = jarDownloader.download();
  80. logger.debug("Create isolated classloader...");
  81. ClassLoader cl = createClassLoader(jarFiles, rules);
  82. IsolatedLauncher objProxy = IsolatedLauncherProxy.create(cl, IsolatedLauncher.class, launcherImplClassName, logger);
  83. tempCleaning.clean();
  84. return objProxy;
  85. } catch (Exception e) {
  86. // Catch all other exceptions, which relates to reflection
  87. throw new RunnerException("Unable to execute SonarQube", e);
  88. }
  89. }
  90. });
  91. }
  92. }