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.

DefaultPluginFactory.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import java.lang.reflect.Constructor;
  20. import java.lang.reflect.Modifier;
  21. /**
  22. * The default implementation for {@link PluginFactory}.
  23. * It uses {@link Constructor#newInstance(Object...)} method.
  24. *
  25. * @author Decebal Suiu
  26. */
  27. public class DefaultPluginFactory implements PluginFactory {
  28. private static final Logger log = LoggerFactory.getLogger(DefaultPluginFactory.class);
  29. /**
  30. * Creates a plugin instance. If an error occurs than that error is logged and the method returns {@code null}.
  31. */
  32. @Override
  33. public Plugin create(final PluginWrapper pluginWrapper) {
  34. String pluginClassName = pluginWrapper.getDescriptor().getPluginClass();
  35. log.debug("Create instance for plugin '{}'", pluginClassName);
  36. Class<?> pluginClass;
  37. try {
  38. pluginClass = pluginWrapper.getPluginClassLoader().loadClass(pluginClassName);
  39. } catch (ClassNotFoundException e) {
  40. log.error(e.getMessage(), e);
  41. return null;
  42. }
  43. // once we have the class, we can do some checks on it to ensure
  44. // that it is a valid implementation of a plugin.
  45. int modifiers = pluginClass.getModifiers();
  46. if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers)
  47. || (!Plugin.class.isAssignableFrom(pluginClass))) {
  48. log.error("The plugin class '{}' is not valid", pluginClassName);
  49. return null;
  50. }
  51. return createInstance(pluginClass, pluginWrapper);
  52. }
  53. protected Plugin createInstance(Class<?> pluginClass, PluginWrapper pluginWrapper) {
  54. try {
  55. Constructor<?> constructor = pluginClass.getConstructor(PluginWrapper.class);
  56. return (Plugin) constructor.newInstance(pluginWrapper);
  57. } catch (NoSuchMethodException e) {
  58. return createUsingNoParametersConstructor(pluginClass);
  59. } catch (Exception e) {
  60. log.error(e.getMessage(), e);
  61. }
  62. return null;
  63. }
  64. protected Plugin createUsingNoParametersConstructor(Class<?> pluginClass) {
  65. try {
  66. Constructor<?> constructor = pluginClass.getConstructor();
  67. return (Plugin) constructor.newInstance();
  68. } catch (Exception e) {
  69. log.error(e.getMessage(), e);
  70. }
  71. return null;
  72. }
  73. }