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

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