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.

JarPluginManager.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 2016 Decebal Suiu
  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 org.pf4j.util.AndFileFilter;
  18. import org.pf4j.util.DirectoryFileFilter;
  19. import org.pf4j.util.HiddenFilter;
  20. import org.pf4j.util.JarFileFilter;
  21. import org.pf4j.util.NotFileFilter;
  22. import org.pf4j.util.OrFileFilter;
  23. import org.pf4j.util.NameFileFilter;
  24. import java.io.FileFilter;
  25. import java.io.IOException;
  26. import java.nio.file.Path;
  27. import java.util.jar.JarFile;
  28. import java.util.jar.Manifest;
  29. /**
  30. * It's a {@link PluginManager} that loads plugin from a jar file.
  31. * Actually, a plugin is a fat jar, a jar which contains classes from all the libraries,
  32. * on which your project depends and, of course, the classes of current project.
  33. *
  34. * @author Decebal Suiu
  35. */
  36. public class JarPluginManager extends DefaultPluginManager {
  37. @Override
  38. protected PluginRepository createPluginRepository() {
  39. return new JarPluginRepository(getPluginsRoot(), isDevelopment());
  40. }
  41. @Override
  42. protected PluginDescriptorFinder createPluginDescriptorFinder() {
  43. return isDevelopment() ? new PropertiesPluginDescriptorFinder() : new JarPluginDescriptorFinder();
  44. }
  45. @Override
  46. protected PluginLoader createPluginLoader() {
  47. return new JarPluginLoader(this, pluginClasspath);
  48. }
  49. class JarPluginRepository extends BasePluginRepository {
  50. public JarPluginRepository(Path pluginsRoot, boolean development) {
  51. super(pluginsRoot);
  52. if (development) {
  53. AndFileFilter pluginsFilter = new AndFileFilter(new DirectoryFileFilter());
  54. pluginsFilter.addFileFilter(new NotFileFilter(createHiddenPluginFilter(development)));
  55. setFilter(pluginsFilter);
  56. } else {
  57. setFilter(new JarFileFilter());
  58. }
  59. }
  60. protected FileFilter createHiddenPluginFilter(boolean development) {
  61. OrFileFilter hiddenPluginFilter = new OrFileFilter(new HiddenFilter());
  62. if (development) {
  63. hiddenPluginFilter.addFileFilter(new NameFileFilter("target"));
  64. }
  65. return hiddenPluginFilter;
  66. }
  67. }
  68. class JarPluginDescriptorFinder extends ManifestPluginDescriptorFinder {
  69. @Override
  70. public Manifest readManifest(Path pluginPath) throws PluginException {
  71. try {
  72. return new JarFile(pluginPath.toFile()).getManifest();
  73. } catch (IOException e) {
  74. throw new PluginException(e);
  75. }
  76. }
  77. }
  78. class JarPluginLoader extends DefaultPluginLoader {
  79. public JarPluginLoader(PluginManager pluginManager, PluginClasspath pluginClasspath) {
  80. super(pluginManager, pluginClasspath);
  81. }
  82. @Override
  83. public ClassLoader loadPlugin(Path pluginPath, PluginDescriptor pluginDescriptor) {
  84. if (isDevelopment()) {
  85. return super.loadPlugin(pluginPath, pluginDescriptor);
  86. }
  87. PluginClassLoader pluginClassLoader = new PluginClassLoader(pluginManager, pluginDescriptor, getClass().getClassLoader());
  88. pluginClassLoader.addFile(pluginPath.toFile());
  89. return pluginClassLoader;
  90. }
  91. }
  92. }