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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. Plugin plugin; // cache
  32. public PluginWrapper(PluginManager pluginManager, PluginDescriptor descriptor, Path pluginPath, ClassLoader pluginClassLoader) {
  33. this.pluginManager = pluginManager;
  34. this.descriptor = descriptor;
  35. this.pluginPath = pluginPath;
  36. this.pluginClassLoader = pluginClassLoader;
  37. pluginState = PluginState.CREATED;
  38. }
  39. /**
  40. * Returns the plugin manager.
  41. */
  42. public PluginManager getPluginManager() {
  43. return pluginManager;
  44. }
  45. /**
  46. * Returns the plugin descriptor.
  47. */
  48. public PluginDescriptor getDescriptor() {
  49. return descriptor;
  50. }
  51. /**
  52. * Returns the path of this plugin.
  53. */
  54. public Path getPluginPath() {
  55. return pluginPath;
  56. }
  57. /**
  58. * Returns the plugin class loader used to load classes and resources
  59. * for this plug-in. The class loader can be used to directly access
  60. * plug-in resources and classes.
  61. */
  62. public ClassLoader getPluginClassLoader() {
  63. return pluginClassLoader;
  64. }
  65. public Plugin getPlugin() {
  66. if (plugin == null) {
  67. plugin = pluginFactory.create(this);
  68. }
  69. return plugin;
  70. }
  71. public PluginState getPluginState() {
  72. return pluginState;
  73. }
  74. public RuntimeMode getRuntimeMode() {
  75. return runtimeMode;
  76. }
  77. /**
  78. * Shortcut
  79. */
  80. public String getPluginId() {
  81. return getDescriptor().getPluginId();
  82. }
  83. @Override
  84. public int hashCode() {
  85. final int prime = 31;
  86. int result = 1;
  87. result = prime * result + descriptor.getPluginId().hashCode();
  88. return result;
  89. }
  90. @Override
  91. public boolean equals(Object obj) {
  92. if (this == obj) {
  93. return true;
  94. }
  95. if (obj == null) {
  96. return false;
  97. }
  98. if (getClass() != obj.getClass()) {
  99. return false;
  100. }
  101. PluginWrapper other = (PluginWrapper) obj;
  102. if (!descriptor.getPluginId().equals(other.descriptor.getPluginId())) {
  103. return false;
  104. }
  105. return true;
  106. }
  107. @Override
  108. public String toString() {
  109. return "PluginWrapper [descriptor=" + descriptor + ", pluginPath=" + pluginPath + "]";
  110. }
  111. void setPluginState(PluginState pluginState) {
  112. this.pluginState = pluginState;
  113. }
  114. void setRuntimeMode(RuntimeMode runtimeMode) {
  115. this.runtimeMode = runtimeMode;
  116. }
  117. void setPluginFactory(PluginFactory pluginFactory) {
  118. this.pluginFactory = pluginFactory;
  119. }
  120. }