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.

Plugin.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.plugins;
  23. import com.dmdirc.ClientModule;
  24. import com.dmdirc.util.validators.ValidationResponse;
  25. import dagger.Module;
  26. import dagger.ObjectGraph;
  27. /**
  28. * Defines the standard methods that should be implemented by plugins.
  29. */
  30. public interface Plugin {
  31. /**
  32. * Check any further Prerequisites for this plugin to load that can not be checked using
  33. * metainfo.
  34. *
  35. * @return ValidationResponse detailing if the plugin passes any extra checks that plugin.info
  36. * can't handle
  37. */
  38. ValidationResponse checkPrerequisites();
  39. /**
  40. * Called when the plugin is loaded.
  41. */
  42. void onLoad();
  43. /**
  44. * Called when the plugin is about to be unloaded.
  45. */
  46. void onUnload();
  47. /**
  48. * Loads the plugin. This method is called when the plugin has been activated by the client,
  49. * either in response to a direct user request, or automatically as part of client startup or
  50. * during the course of another plugin being loaded.
  51. *
  52. * <p>
  53. * Plugins are provided with an {@link ObjectGraph} which they may extend and use to inflate
  54. * their classes. This is the only supported way of obtaining instances of core DMDirc classes,
  55. * or objects shared by this plugin's parent.
  56. *
  57. * <p>
  58. * Plugins wishing to use this form of dependency injection should define a new
  59. * {@link Module} which specifies an {@link Module#addsTo()} argument of either:
  60. *
  61. * <ul>
  62. * <li>For plugins with no parents, or plugins which do not have a dependency on their parent,
  63. * {@link ClientModule}.</li>
  64. * <li>For plugins with dependencies on a parent plugin, that plugin's own {@link Module}
  65. * implementation.</li>
  66. * </ul>
  67. *
  68. * <p>
  69. * The implementation of this method should then call
  70. * {@link ObjectGraph#plus(Object[])} with an instance of the plugin's own module.
  71. *
  72. * <p>
  73. * To expose dependencies to child plugins, the relevant {@link ObjectGraph} should be returned
  74. * from the {@link #getObjectGraph()} method.
  75. *
  76. * <p>
  77. * While both this method and {@link #onLoad()} are defined, this method is guaranteed to be
  78. * called first. Implementations that support this method of loading can simply treat
  79. * {@link #onLoad()} as a no-op.
  80. *
  81. * @param pluginInfo The information object corresponding to this plugin.
  82. * @param graph The dependency-injection graph that may be used to instantiate plugin
  83. * classes.
  84. */
  85. void load(PluginInfo pluginInfo, ObjectGraph graph);
  86. /**
  87. * Returns an {@link ObjectGraph} that may be used by subplugins to inject dependencies provided
  88. * by this class.
  89. *
  90. * <p>
  91. * This should always be an extension of the {@link ObjectGraph} provided to the
  92. * {@link #load(PluginInfo, ObjectGraph)} method. If the plugin has no
  93. * dependencies it wishes to expose, it may return {@code null} and any subplugins will be given
  94. * the global {@link ObjectGraph}.
  95. *
  96. * <p>
  97. * It is recommended that implementations separate their internal dependencies from those that
  98. * will be published. This can be accomplished by using two modules, e.g.:
  99. *
  100. * <pre><code>
  101. * ObjectGraph external = graph.plus(new ExternalModule());
  102. * ObjectGraph internal = external.plus(new InternalModule());
  103. * </code></pre>
  104. *
  105. * <p>
  106. * The plugin can then inflate its own classes using the 'internal' graph, while only exposing
  107. * relevant dependencies in the 'external' graph.
  108. *
  109. * @return A graph to be used by child plugins, or {@code null} for the default graph.
  110. */
  111. ObjectGraph getObjectGraph();
  112. }