Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

BootstrapClassLoader.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Sonar Runner - API
  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;
  21. import java.io.IOException;
  22. import java.net.URL;
  23. import java.net.URLClassLoader;
  24. import java.util.Enumeration;
  25. /**
  26. * Special {@link URLClassLoader} to execute Sonar, which restricts loading from parent.
  27. */
  28. class BootstrapClassLoader extends URLClassLoader {
  29. private final String[] unmaskedPackages;
  30. BootstrapClassLoader(ClassLoader parent, String... unmaskedPackages) {
  31. super(new URL[0], parent);
  32. this.unmaskedPackages = unmaskedPackages;
  33. }
  34. /**
  35. * {@inheritDoc} Visibility of a method has been relaxed to public.
  36. */
  37. @Override
  38. public void addURL(URL url) {
  39. super.addURL(url);
  40. }
  41. /**
  42. * {@inheritDoc} Visibility of a method has been relaxed to public.
  43. */
  44. @Override
  45. public Class<?> findClass(String name) throws ClassNotFoundException {
  46. return super.findClass(name);
  47. }
  48. /**
  49. * @return true, if class can be loaded from parent ClassLoader
  50. */
  51. boolean canLoadFromParent(String name) {
  52. if (name.startsWith("org.sonar.runner.") && !name.startsWith("org.sonar.runner.internal.batch.")) {
  53. return true;
  54. }
  55. for (String pkg : unmaskedPackages) {
  56. if (name.startsWith(pkg + ".")) {
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. /**
  63. * Same behavior as in {@link URLClassLoader#loadClass(String, boolean)}, except loading from parent.
  64. */
  65. @Override
  66. protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
  67. // First, check if the class has already been loaded
  68. Class<?> c = findLoadedClass(name);
  69. if (c == null) {
  70. try {
  71. // Load from parent
  72. if (getParent() != null && canLoadFromParent(name)) {
  73. c = getParent().loadClass(name);
  74. } else {
  75. // Load from system
  76. // I don't know for other vendors, but for Oracle JVM :
  77. // - ClassLoader.getSystemClassLoader() is sun.misc.Launcher$AppClassLoader. It contains app classpath.
  78. // - ClassLoader.getSystemClassLoader().getParent() is sun.misc.Launcher$ExtClassLoader. It contains core JVM
  79. ClassLoader systemClassLoader = getSystemClassLoader();
  80. if (systemClassLoader.getParent() != null) {
  81. systemClassLoader = systemClassLoader.getParent();
  82. }
  83. c = systemClassLoader.loadClass(name);
  84. }
  85. } catch (ClassNotFoundException e) {
  86. // If still not found, then invoke findClass in order
  87. // to find the class.
  88. c = findClass(name);
  89. }
  90. }
  91. if (resolve) {
  92. resolveClass(c);
  93. }
  94. return c;
  95. }
  96. /**
  97. * Unlike {@link URLClassLoader#getResource(String)} don't return resource from parent.
  98. * See http://jira.codehaus.org/browse/SONAR-2276
  99. */
  100. @Override
  101. public URL getResource(String name) {
  102. return findResource(name);
  103. }
  104. /**
  105. * Unlike {@link URLClassLoader#getResources(String)} don't return resources from parent.
  106. * See http://jira.codehaus.org/browse/SONAR-2276
  107. */
  108. @Override
  109. public Enumeration<URL> getResources(String name) throws IOException {
  110. return findResources(name);
  111. }
  112. }