您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

IsolatedLauncherFactory.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * SonarQube Runner - Implementation
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  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 org.sonar.runner.batch.IsolatedLauncher;
  22. import org.sonar.home.cache.PersistentCacheBuilder;
  23. import org.sonar.home.cache.PersistentCache;
  24. import java.io.File;
  25. import java.security.AccessController;
  26. import java.security.PrivilegedAction;
  27. import java.util.List;
  28. import java.util.Properties;
  29. public class IsolatedLauncherFactory {
  30. static final String ISOLATED_LAUNCHER_IMPL = "org.sonar.runner.batch.BatchIsolatedLauncher";
  31. private final TempCleaning tempCleaning;
  32. private final String launcherImplClassName;
  33. /**
  34. * For unit tests
  35. */
  36. IsolatedLauncherFactory(String isolatedLauncherClassName, TempCleaning tempCleaning) {
  37. this.tempCleaning = tempCleaning;
  38. this.launcherImplClassName = isolatedLauncherClassName;
  39. }
  40. public IsolatedLauncherFactory() {
  41. this(ISOLATED_LAUNCHER_IMPL, new TempCleaning());
  42. }
  43. private static PersistentCache getCache(Properties props) {
  44. PersistentCacheBuilder builder = new PersistentCacheBuilder();
  45. if (!"true".equals(props.getProperty("sonar.enableHttpCache"))) {
  46. builder.forceUpdate(true);
  47. }
  48. return builder.build();
  49. }
  50. static String[][] getMaskRules(final Properties props) {
  51. String maskRulesProp = props.getProperty(InternalProperties.RUNNER_MASK_RULES, null);
  52. String[] maskRulesConcat = maskRulesProp != null ? maskRulesProp.split(",") : (new String[0]);
  53. String[][] maskRules = new String[maskRulesConcat.length][2];
  54. for (int i = 0; i < maskRulesConcat.length; i++) {
  55. String[] splitted = maskRulesConcat[i].split("\\|");
  56. maskRules[i][0] = splitted[0];
  57. maskRules[i][1] = splitted.length > 1 ? splitted[1] : "";
  58. }
  59. return maskRules;
  60. }
  61. private static void addIsolatedLauncherMaskRule(Properties props) {
  62. String unmask = "UNMASK|org.sonar.runner.batch.IsolatedLauncher,UNMASK|org.sonar.home.log.LogListener";
  63. String currentRules = (String) props.get(InternalProperties.RUNNER_MASK_RULES);
  64. if (currentRules == null) {
  65. props.put(InternalProperties.RUNNER_MASK_RULES, unmask);
  66. } else {
  67. props.put(InternalProperties.RUNNER_MASK_RULES, currentRules + "," + unmask);
  68. }
  69. }
  70. private ClassLoader createClassLoader(List<File> jarFiles, final Properties props) {
  71. Properties copy = new Properties();
  72. copy.putAll(props);
  73. addIsolatedLauncherMaskRule(copy);
  74. String[][] maskRules = getMaskRules(copy);
  75. IsolatedClassloader classloader = new IsolatedClassloader(getClass().getClassLoader(), maskRules);
  76. classloader.addFiles(jarFiles);
  77. return classloader;
  78. }
  79. public IsolatedLauncher createLauncher(Properties props) {
  80. ServerConnection serverConnection = ServerConnection.create(props, getCache(props));
  81. JarDownloader jarDownloader = new JarDownloader(serverConnection);
  82. return createLauncher(jarDownloader, props);
  83. }
  84. IsolatedLauncher createLauncher(final JarDownloader jarDownloader, final Properties props) {
  85. return AccessController.doPrivileged(new PrivilegedAction<IsolatedLauncher>() {
  86. @Override
  87. public IsolatedLauncher run() {
  88. try {
  89. List<File> jarFiles = jarDownloader.download();
  90. Logs.debug("Create isolated classloader...");
  91. ClassLoader cl = createClassLoader(jarFiles, props);
  92. IsolatedLauncher objProxy = IsolatedLauncherProxy.create(cl, IsolatedLauncher.class, launcherImplClassName);
  93. tempCleaning.clean();
  94. return objProxy;
  95. } catch (Exception e) {
  96. // Catch all other exceptions, which relates to reflection
  97. throw new RunnerException("Unable to execute SonarQube", e);
  98. }
  99. }
  100. });
  101. }
  102. }