PageRenderTime 6ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/worldguard-legacy/src/main/java/com/sk89q/worldguard/util/logging/ClassSourceValidator.java

https://gitlab.com/igserfurtmcschulserver/CustomWorldGuard
Java | 124 lines | 67 code | 17 blank | 40 comment | 7 complexity | 28a7e8f2158c636e4f4a90086ad26a77 MD5 | raw file
  1. /*
  2. * WorldGuard, a suite of tools for Minecraft
  3. * Copyright (C) sk89q <http://www.sk89q.com>
  4. * Copyright (C) WorldGuard team and contributors
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldguard.util.logging;
  20. import com.google.common.base.Strings;
  21. import com.google.common.collect.Maps;
  22. import org.bukkit.plugin.Plugin;
  23. import javax.annotation.Nullable;
  24. import java.security.CodeSource;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import static com.google.common.base.Preconditions.checkNotNull;
  30. /**
  31. * Validates that certain specified classes came from the same source as
  32. * a plugin.
  33. */
  34. public class ClassSourceValidator {
  35. private static final Logger log = Logger.getLogger(ClassSourceValidator.class.getCanonicalName());
  36. private static final String separatorLine = Strings.repeat("*", 46);
  37. private final Plugin plugin;
  38. @Nullable
  39. private final CodeSource expectedCodeSource;
  40. /**
  41. * Create a new instance.
  42. *
  43. * @param plugin The plugin
  44. */
  45. public ClassSourceValidator(Plugin plugin) {
  46. checkNotNull(plugin, "plugin");
  47. this.plugin = plugin;
  48. this.expectedCodeSource = plugin.getClass().getProtectionDomain().getCodeSource();
  49. }
  50. /**
  51. * Return a map of classes that been loaded from a different source.
  52. *
  53. * @param classes A list of classes to check
  54. * @return The results
  55. */
  56. public Map<Class<?>, CodeSource> findMismatches(List<Class<?>> classes) {
  57. checkNotNull(classes, "classes");
  58. Map<Class<?>, CodeSource> mismatches = Maps.newHashMap();
  59. if (expectedCodeSource != null) {
  60. for (Class<?> testClass : classes) {
  61. CodeSource testSource = testClass.getProtectionDomain().getCodeSource();
  62. if (!expectedCodeSource.equals(testSource)) {
  63. mismatches.put(testClass, testSource);
  64. }
  65. }
  66. }
  67. return mismatches;
  68. }
  69. /**
  70. * Reports classes that have come from a different source.
  71. *
  72. * <p>The warning is emitted to the log.</p>
  73. *
  74. * @param classes The list of classes to check
  75. */
  76. public void reportMismatches(List<Class<?>> classes) {
  77. Map<Class<?>, CodeSource> mismatches = findMismatches(classes);
  78. if (!mismatches.isEmpty()) {
  79. StringBuilder builder = new StringBuilder("\n");
  80. builder.append(separatorLine).append("\n");
  81. builder.append("** /!\\ SEVERE WARNING /!\\\n");
  82. builder.append("** \n");
  83. builder.append("** A plugin developer has copied and pasted a portion of \n");
  84. builder.append("** ").append(plugin.getName()).append(" into their own plugin, so rather than using\n");
  85. builder.append("** the version of ").append(plugin.getName()).append(" that you downloaded, you\n");
  86. builder.append("** will be using a broken mix of old ").append(plugin.getName()).append(" (that came\n");
  87. builder.append("** with the plugin) and your downloaded version. THIS MAY\n");
  88. builder.append("** SEVERELY BREAK ").append(plugin.getName().toUpperCase()).append(" AND ALL OF ITS FEATURES.\n");
  89. builder.append("**\n");
  90. builder.append("** This may have happened because the developer is using\n");
  91. builder.append("** the ").append(plugin.getName()).append(" API and thinks that including\n");
  92. builder.append("** ").append(plugin.getName()).append(" is necessary. However, it is not!\n");
  93. builder.append("**\n");
  94. builder.append("** Here are some files that have been overridden:\n");
  95. builder.append("** \n");
  96. for (Map.Entry<Class<?>, CodeSource> entry : mismatches.entrySet()) {
  97. CodeSource codeSource = entry.getValue();
  98. String url = codeSource != null ? codeSource.getLocation().toExternalForm() : "(unknown)";
  99. builder.append("** '").append(entry.getKey().getSimpleName()).append("' came from '").append(url).append("'\n");
  100. }
  101. builder.append("**\n");
  102. builder.append("** Please report this to the plugins' developers.\n");
  103. builder.append(separatorLine).append("\n");
  104. log.log(Level.SEVERE, builder.toString());
  105. }
  106. }
  107. }