您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LegacyServiceLocator.java 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.plugins;
  18. import java.lang.reflect.Method;
  19. import java.util.Collection;
  20. import java.util.HashSet;
  21. import javax.annotation.Nullable;
  22. import javax.inject.Inject;
  23. import javax.inject.Singleton;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import static com.dmdirc.util.LogUtils.USER_ERROR;
  27. import static com.google.common.base.Preconditions.checkNotNull;
  28. /**
  29. * A service locator backed by a {@link PluginManager} and exported methods.
  30. */
  31. @Singleton
  32. public class LegacyServiceLocator implements ServiceLocator {
  33. private static final Logger LOG = LoggerFactory.getLogger(LegacyServiceLocator.class);
  34. /** The plugin manager to use to find services. */
  35. private final PluginManager pluginManager;
  36. @Inject
  37. public LegacyServiceLocator(final PluginManager pluginManager) {
  38. this.pluginManager = checkNotNull(pluginManager);
  39. }
  40. @Override
  41. public <T> Collection<T> getAllServices(final Class<T> serviceType) {
  42. return getServices(serviceType, null);
  43. }
  44. @Override
  45. public <T> T getService(final Class<T> serviceType, final String implementation) {
  46. final Collection<T> services = getServices(serviceType, implementation);
  47. return services.isEmpty() ? null : services.iterator().next();
  48. }
  49. @SuppressWarnings("unchecked")
  50. private <T> Collection<T> getServices(final Class<T> serviceType,
  51. @Nullable final String implementation) {
  52. checkNotNull(serviceType);
  53. final Collection<T> services = new HashSet<>();
  54. pluginManager.getPluginInfos().stream().filter(PluginInfo::isLoaded)
  55. .forEach(pluginInfo -> {
  56. final Plugin plugin = pluginInfo.getPlugin();
  57. for (Method method : plugin.getClass().getMethods()) {
  58. if (method.isAnnotationPresent(Exported.class) &&
  59. method.getParameterTypes().length == 0 &&
  60. serviceType.isAssignableFrom(method.getReturnType())) {
  61. try {
  62. method.setAccessible(true);
  63. final Object object = method.invoke(plugin);
  64. if (object != null && (implementation == null ||
  65. implementation.equals(object.getClass().getName()))) {
  66. services.add((T) object);
  67. }
  68. } catch (ReflectiveOperationException ex) {
  69. LOG.info(USER_ERROR,
  70. "Unable to execute exported method {} in plugin {}",
  71. method.getName(), pluginInfo.getMetaData().getName(), ex);
  72. }
  73. }
  74. }
  75. });
  76. return services;
  77. }
  78. }