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.

PluginWrapper.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j;
  17. import java.nio.file.Path;
  18. /**
  19. * A wrapper over plugin instance.
  20. *
  21. * @author Decebal Suiu
  22. */
  23. public class PluginWrapper {
  24. private PluginManager pluginManager;
  25. private PluginDescriptor descriptor;
  26. private Path pluginPath;
  27. private ClassLoader pluginClassLoader;
  28. private PluginFactory pluginFactory;
  29. private PluginState pluginState;
  30. private RuntimeMode runtimeMode;
  31. private Throwable failedException;
  32. Plugin plugin; // cache
  33. public PluginWrapper(PluginManager pluginManager, PluginDescriptor descriptor, Path pluginPath, ClassLoader pluginClassLoader) {
  34. this.pluginManager = pluginManager;
  35. this.descriptor = descriptor;
  36. this.pluginPath = pluginPath;
  37. this.pluginClassLoader = pluginClassLoader;
  38. pluginState = PluginState.CREATED;
  39. runtimeMode = pluginManager.getRuntimeMode();
  40. }
  41. /**
  42. * Returns the plugin manager.
  43. */
  44. public PluginManager getPluginManager() {
  45. return pluginManager;
  46. }
  47. /**
  48. * Returns the plugin descriptor.
  49. */
  50. public PluginDescriptor getDescriptor() {
  51. return descriptor;
  52. }
  53. /**
  54. * Returns the path of this plugin.
  55. */
  56. public Path getPluginPath() {
  57. return pluginPath;
  58. }
  59. /**
  60. * Returns the plugin class loader used to load classes and resources
  61. * for this plug-in. The class loader can be used to directly access
  62. * plug-in resources and classes.
  63. */
  64. public ClassLoader getPluginClassLoader() {
  65. return pluginClassLoader;
  66. }
  67. public Plugin getPlugin() {
  68. if (plugin == null) {
  69. plugin = pluginFactory.create(this);
  70. }
  71. return plugin;
  72. }
  73. public PluginState getPluginState() {
  74. return pluginState;
  75. }
  76. public RuntimeMode getRuntimeMode() {
  77. return runtimeMode;
  78. }
  79. /**
  80. * Shortcut
  81. */
  82. public String getPluginId() {
  83. return getDescriptor().getPluginId();
  84. }
  85. @Override
  86. public int hashCode() {
  87. final int prime = 31;
  88. int result = 1;
  89. result = prime * result + descriptor.getPluginId().hashCode();
  90. return result;
  91. }
  92. @Override
  93. public boolean equals(Object obj) {
  94. if (this == obj) {
  95. return true;
  96. }
  97. if (obj == null) {
  98. return false;
  99. }
  100. if (getClass() != obj.getClass()) {
  101. return false;
  102. }
  103. PluginWrapper other = (PluginWrapper) obj;
  104. return descriptor.getPluginId().equals(other.descriptor.getPluginId());
  105. }
  106. @Override
  107. public String toString() {
  108. return "PluginWrapper [descriptor=" + descriptor + ", pluginPath=" + pluginPath + "]";
  109. }
  110. public void setPluginState(PluginState pluginState) {
  111. this.pluginState = pluginState;
  112. }
  113. public void setPluginFactory(PluginFactory pluginFactory) {
  114. this.pluginFactory = pluginFactory;
  115. }
  116. /**
  117. * Returns the exception with which the plugin fails to start.
  118. * See @{link PluginStatus#FAILED}.
  119. */
  120. public Throwable getFailedException() {
  121. return failedException;
  122. }
  123. public void setFailedException(Throwable failedException) {
  124. this.failedException = failedException;
  125. }
  126. }