PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-core/src/main/java/com/atlassian/jira/plugin/PluginFactoryAndLoaderRegistrar.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 230 lines | 172 code | 23 blank | 35 comment | 9 complexity | eb22d105d439d1e8ebf94dcd1cf1f399 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.plugin;
  2. import com.atlassian.jira.config.properties.JiraProperties;
  3. import com.atlassian.jira.util.BuildUtilsInfo;
  4. import com.atlassian.plugin.Application;
  5. import com.atlassian.plugin.PluginAccessor;
  6. import com.atlassian.plugin.event.PluginEventManager;
  7. import com.atlassian.plugin.factories.PluginFactory;
  8. import com.atlassian.plugin.factories.XmlDynamicPluginFactory;
  9. import com.atlassian.plugin.loaders.BundledPluginLoader;
  10. import com.atlassian.plugin.loaders.DirectoryPluginLoader;
  11. import com.atlassian.plugin.loaders.PluginLoader;
  12. import com.atlassian.plugin.loaders.RosterFilePluginLoader;
  13. import com.atlassian.plugin.loaders.SinglePluginLoader;
  14. import com.atlassian.plugin.osgi.container.OsgiContainerManager;
  15. import com.atlassian.plugin.osgi.factory.OsgiBundleFactory;
  16. import com.atlassian.plugin.osgi.factory.OsgiPluginFactory;
  17. import com.atlassian.plugin.osgi.factory.RemotablePluginFactory;
  18. import com.atlassian.plugin.osgi.factory.UnloadableStaticPluginFactory;
  19. import com.atlassian.plugin.servlet.ServletContextFactory;
  20. import com.google.common.collect.ImmutableList;
  21. import com.google.common.collect.ImmutableSet;
  22. import com.google.common.collect.Lists;
  23. import org.apache.commons.io.FileUtils;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.net.MalformedURLException;
  29. import java.net.URL;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import java.util.Set;
  33. import java.util.regex.Pattern;
  34. /**
  35. * A simple registrar of plugin factories and plugin loaders
  36. *
  37. * @since v4.4
  38. */
  39. public class PluginFactoryAndLoaderRegistrar {
  40. private static final Logger log = LoggerFactory.getLogger(PluginFactoryAndLoaderRegistrar.class);
  41. private static final String BUNDLED_PLUGIN_LOCATION = "/WEB-INF/atlassian-bundled-plugins";
  42. private final PluginEventManager pluginEventManager;
  43. private final OsgiContainerManager osgiContainerManager;
  44. private final PluginPath pathFactory;
  45. private final ServletContextFactory servletContextFactory;
  46. private final BuildUtilsInfo buildUtilsInfo;
  47. private final JiraFailedPluginTracker jiraFailedPluginTracker;
  48. private final JiraProperties jiraSystemProperties;
  49. public PluginFactoryAndLoaderRegistrar(PluginEventManager pluginEventManager, OsgiContainerManager osgiContainerManager,
  50. PluginPath pathFactory, ServletContextFactory servletContextFactory, BuildUtilsInfo buildUtilsInfo,
  51. JiraFailedPluginTracker jiraFailedPluginTracker, final JiraProperties jiraSystemProperties) {
  52. this.pluginEventManager = pluginEventManager;
  53. this.osgiContainerManager = osgiContainerManager;
  54. this.pathFactory = pathFactory;
  55. this.servletContextFactory = servletContextFactory;
  56. this.buildUtilsInfo = buildUtilsInfo;
  57. this.jiraFailedPluginTracker = jiraFailedPluginTracker;
  58. this.jiraSystemProperties = jiraSystemProperties;
  59. }
  60. /**
  61. * This allows every plugin found to be loaded
  62. *
  63. * @return a list of plugin factories which is in fact a singleton of the {@link MasterPluginFactory}
  64. */
  65. public List<PluginFactory> getDefaultPluginFactories() {
  66. final ArrayList<Pattern> everyPluginWhiteList = Lists.newArrayList(Pattern.compile(".*"));
  67. return getDefaultPluginFactories(everyPluginWhiteList);
  68. }
  69. /**
  70. * This allows only a select list of plugins found to be loaded
  71. *
  72. * @param pluginWhitelist the whitelist of plugins deployment units that are allowed to be loaded
  73. * @return a list of plugin factories which is in fact a singleton of the {@link MasterPluginFactory}
  74. */
  75. public List<PluginFactory> getDefaultPluginFactories(final List<Pattern> pluginWhitelist) {
  76. final Set<Application> jiraApplications = jiraApplications(buildUtilsInfo);
  77. // this loads Atlassian Plugins (as OSGi bundles) transforming them into full OSGi bundles if necessary
  78. final PluginFactory osgiPluginFactory = new OsgiPluginFactory(
  79. PluginAccessor.Descriptor.FILENAME,
  80. jiraApplications,
  81. pathFactory.getOsgiPersistentCache(),
  82. osgiContainerManager,
  83. pluginEventManager);
  84. // this loads OSGi bundles
  85. final PluginFactory osgiBundleFactory = new OsgiBundleFactory(osgiContainerManager);
  86. // this loads version 3 plugins
  87. final RemotablePluginFactory remotablePluginFactory = new RemotablePluginFactory(
  88. PluginAccessor.Descriptor.FILENAME,
  89. jiraApplications,
  90. osgiContainerManager,
  91. pluginEventManager);
  92. // this loads just-XML-files that describe a plugin
  93. final PluginFactory xmlDynamicFactory = new XmlDynamicPluginFactory(jiraApplications);
  94. // this loads "UnloadablePlugins" in the case that the user drops a Plugins 1 plugin into the plugins 2 installation directory.
  95. final UnloadableStaticPluginFactory unloadableStaticPluginFactory = new UnloadableStaticPluginFactory(PluginAccessor.Descriptor.FILENAME);
  96. final List<PluginFactory> pluginFactories = ImmutableList.of(osgiPluginFactory, osgiBundleFactory, remotablePluginFactory, xmlDynamicFactory, unloadableStaticPluginFactory);
  97. final MasterPluginFactory masterPluginFactory = new MasterPluginFactory(pluginFactories, pluginWhitelist, jiraFailedPluginTracker);
  98. return ImmutableList.<PluginFactory>of(masterPluginFactory);
  99. }
  100. private Set<Application> jiraApplications(BuildUtilsInfo buildUtilsInfo) {
  101. return ImmutableSet.<Application>of(
  102. new JiraApplication("jira", buildUtilsInfo),
  103. new JiraApplication("com.atlassian.jira", buildUtilsInfo));
  104. }
  105. public PluginLoader getBundledPluginsLoader(List<PluginFactory> pluginFactories) {
  106. final String bundledPluginOverride = jiraSystemProperties.getProperty("jira.dev.bundledplugins.url");
  107. final String bundledPluginUrlString;
  108. if (bundledPluginOverride != null) {
  109. bundledPluginUrlString = bundledPluginOverride;
  110. log.warn("Bundled plugins being loaded from override " + bundledPluginUrlString);
  111. } else {
  112. String bundledPluginPath = servletContextFactory.getServletContext().getRealPath(BUNDLED_PLUGIN_LOCATION);
  113. // JiraWebappStartupCheck ensures that getRealPath does not return null for /
  114. bundledPluginUrlString = new File(bundledPluginPath).toURI().toString();
  115. }
  116. // Directory prior version exploded atlassian-bundled-plugins.zip into. We continue to use
  117. // this as the directory required by the current BundledPluginLoader constructor.
  118. final File legacyBundledPluginsDirectory = pathFactory.getBundledPluginsDirectory();
  119. try {
  120. // Clean out old copies of plugins from prior explosions
  121. FileUtils.cleanDirectory(legacyBundledPluginsDirectory);
  122. } catch (IOException eio) {
  123. // We don't use the directory any more, and there's not much we can do if we can't clean it,
  124. // so see if we can get a admin's attention.
  125. log.warn("Cannot clean '" + legacyBundledPluginsDirectory + "': " + eio.getMessage());
  126. }
  127. try {
  128. final URL bundledPluginUrl = new URL(bundledPluginUrlString);
  129. // Note legacyBundlePluginsDirectory is unused since bundledPluginUrl is never a .zip
  130. // anymore. Once we have a more appropriate constructor (PLUGDEV-43) we should use that
  131. // and remove usage of legacyBundledPluginsDirectory, moving the cleanup code to an
  132. // upgrade task.
  133. // TODO: https://jdog.jira-dev.com/browse/JDEV-27508
  134. return new BundledPluginLoader(bundledPluginUrl, legacyBundledPluginsDirectory, pluginFactories, pluginEventManager);
  135. } catch (MalformedURLException e) {
  136. throw new IllegalStateException("Can't form url to bundled plugins directory at: " + BUNDLED_PLUGIN_LOCATION, e);
  137. }
  138. }
  139. public PluginLoader getCustomDirectoryPluginLoader(List<PluginFactory> pluginFactories) {
  140. File customPluginPath = pathFactory.getCustomPluginsDirectory();
  141. if (customPluginPath != null) {
  142. return new DirectoryPluginLoader(customPluginPath, pluginFactories, pluginEventManager);
  143. } else {
  144. return null;
  145. }
  146. }
  147. public PluginLoader getRosterFilePluginLoader(final List<PluginFactory> pluginFactories) {
  148. final File rosterFile = pathFactory.getPluginsRosterFile();
  149. if (rosterFile != null) {
  150. return new RosterFilePluginLoader(rosterFile, pluginFactories, pluginEventManager);
  151. } else {
  152. return null;
  153. }
  154. }
  155. public List<PluginLoader> getDefaultSystemPluginLoaders() {
  156. return Lists.<PluginLoader>newArrayList(
  157. new SinglePluginLoader("system-workflow-plugin.xml"),
  158. new SinglePluginLoader("system-customfieldtypes-plugin.xml"),
  159. //load the link resolvers and renderer components before the renderers get loaded.
  160. new SinglePluginLoader("system-contentlinkresolvers-plugin.xml"),
  161. new SinglePluginLoader("system-renderercomponentfactories-plugin.xml"),
  162. new SinglePluginLoader("system-renderers-plugin.xml"),
  163. new SinglePluginLoader("system-macros-plugin.xml"),
  164. new SinglePluginLoader("system-issueoperations-plugin.xml"),
  165. new SinglePluginLoader("system-issuetabpanels-plugin.xml"),
  166. new SinglePluginLoader("system-comment-field-renderer.xml"),
  167. new SinglePluginLoader("webfragment/system-user-nav-bar-sections.xml"),
  168. new SinglePluginLoader("webfragment/system-admin-sections.xml"),
  169. new SinglePluginLoader("webfragment/system-preset-filters-sections.xml"),
  170. new SinglePluginLoader("webfragment/system-view-project-operations-sections.xml"),
  171. new SinglePluginLoader("webfragment/system-user-profile-links.xml"),
  172. new SinglePluginLoader("webfragment/system-hints.xml"),
  173. new SinglePluginLoader("system-issueviews-plugin.xml"),
  174. new SinglePluginLoader("system-projectroleactors-plugin.xml"),
  175. new SinglePluginLoader("system-webresources-plugin.xml"),
  176. new SinglePluginLoader("system-top-navigation-plugin.xml"),
  177. new SinglePluginLoader("system-feature-keys-plugin.xml"),
  178. new SinglePluginLoader("system-footer-plugin.xml"),
  179. new SinglePluginLoader("system-filter-deletion-warning-plugin.xml"),
  180. new SinglePluginLoader("system-user-format-plugin.xml"),
  181. new SinglePluginLoader("system-user-profile-panels.xml"),
  182. new SinglePluginLoader("system-jql-function-plugin.xml"),
  183. new SinglePluginLoader("system-keyboard-shortcuts-plugin.xml"),
  184. new SinglePluginLoader("system-global-permissions.xml"),
  185. new SinglePluginLoader("system-project-permissions.xml"),
  186. new SinglePluginLoader("webfragment/system-browse-project-operations-sections.xml"),
  187. new SinglePluginLoader("webfragment/system-workflowtransitiontabs-links.xml"),
  188. new SinglePluginLoader("system-helppaths-plugin.xml"),
  189. new SinglePluginLoader("system-attachment-processor-plugin.xml"),
  190. new SinglePluginLoader("system-webpanels-plugin.xml"),
  191. new SinglePluginLoader("system-soyfunction-plugin.xml"),
  192. new SinglePluginLoader("system-entity-property-conditions.xml"),
  193. new SinglePluginLoader("system-icontypes-plugin.xml")
  194. );
  195. }
  196. public List<PluginLoader> getBootstrapSystemPluginLoaders() {
  197. // Atlassian Plugins Webresource Plugin has to be mocked for setup - for more information
  198. // see the comment in the xml file
  199. return ImmutableList.of(
  200. new SinglePluginLoader("system-webresources-plugin.xml"),
  201. new SinglePluginLoader("system-helppaths-plugin.xml"),
  202. new SinglePluginLoader("atlassian-plugins-jira-issue-nav-plugin-mock.xml"),
  203. new SinglePluginLoader("atlassian-plugins-webresource-plugin-mock.xml"),
  204. new SinglePluginLoader("atlassian-plugins-webresource-rest-mock.xml")
  205. );
  206. }
  207. }