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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 final PluginManager pluginManager;
  25. private final PluginDescriptor descriptor;
  26. private final Path pluginPath;
  27. private final ClassLoader pluginClassLoader;
  28. private PluginFactory pluginFactory;
  29. private PluginState pluginState;
  30. private final 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. * @return the plugin manager
  45. */
  46. public PluginManager getPluginManager() {
  47. return pluginManager;
  48. }
  49. /**
  50. * Returns the plugin descriptor.
  51. *
  52. * @return the plugin descriptor
  53. */
  54. public PluginDescriptor getDescriptor() {
  55. return descriptor;
  56. }
  57. /**
  58. * Returns the path of this plugin.
  59. *
  60. * @return the path of this plugin
  61. */
  62. public Path getPluginPath() {
  63. return pluginPath;
  64. }
  65. /**
  66. * Returns the plugin {@link ClassLoader} used to load classes and resources for this plug-in.
  67. * <p>
  68. * The class loader can be used to directly access plug-in resources and classes.
  69. *
  70. * @return the plugin class loader
  71. */
  72. public ClassLoader getPluginClassLoader() {
  73. return pluginClassLoader;
  74. }
  75. /**
  76. * Returns the plugin instance.
  77. *
  78. * @return the plugin instance
  79. */
  80. public Plugin getPlugin() {
  81. if (plugin == null) {
  82. plugin = pluginFactory.create(this);
  83. }
  84. return plugin;
  85. }
  86. /**
  87. * Returns the plugin factory.
  88. *
  89. * @return the plugin factory
  90. */
  91. public PluginState getPluginState() {
  92. return pluginState;
  93. }
  94. /**
  95. * Returns the runtime mode.
  96. *
  97. * @return the runtime mode
  98. */
  99. public RuntimeMode getRuntimeMode() {
  100. return runtimeMode;
  101. }
  102. /**
  103. * Shortcut for {@code getDescriptor().getPluginId()}.
  104. */
  105. public String getPluginId() {
  106. return getDescriptor().getPluginId();
  107. }
  108. @Override
  109. public int hashCode() {
  110. final int prime = 31;
  111. int result = 1;
  112. result = prime * result + descriptor.getPluginId().hashCode();
  113. return result;
  114. }
  115. @Override
  116. public boolean equals(Object obj) {
  117. if (this == obj) {
  118. return true;
  119. }
  120. if (obj == null) {
  121. return false;
  122. }
  123. if (getClass() != obj.getClass()) {
  124. return false;
  125. }
  126. PluginWrapper other = (PluginWrapper) obj;
  127. return descriptor.getPluginId().equals(other.descriptor.getPluginId());
  128. }
  129. @Override
  130. public String toString() {
  131. return "PluginWrapper [descriptor=" + descriptor + ", pluginPath=" + pluginPath + "]";
  132. }
  133. /**
  134. * Used internally by the framework to set the plugin factory.
  135. *
  136. * @param pluginState the plugin state
  137. */
  138. public void setPluginState(PluginState pluginState) {
  139. this.pluginState = pluginState;
  140. }
  141. /**
  142. * Used internally by the framework to set the plugin factory.
  143. *
  144. * @param pluginFactory the plugin factory
  145. */
  146. public void setPluginFactory(PluginFactory pluginFactory) {
  147. this.pluginFactory = pluginFactory;
  148. }
  149. /**
  150. * Returns the exception with which the plugin fails to start.
  151. * See @{link PluginStatus#FAILED}.
  152. *
  153. * @return the exception with which the plugin fails to start
  154. */
  155. public Throwable getFailedException() {
  156. return failedException;
  157. }
  158. /**
  159. * Used internally by the framework to set the exception with which the plugin fails to start.
  160. *
  161. * @param failedException the exception with which the plugin fails to start
  162. */
  163. public void setFailedException(Throwable failedException) {
  164. this.failedException = failedException;
  165. }
  166. }