PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/plugin/src/main/java/com/atlassian/labs/speakeasy/manager/convention/ConventionDescriptorGeneratorServiceFactory.java

https://github.com/mrdon/speakeasy-plugin
Java | 183 lines | 140 code | 26 blank | 17 comment | 15 complexity | 72534a074a690015f5f6f00930ffbbeb MD5 | raw file
  1. package com.atlassian.labs.speakeasy.manager.convention;
  2. import com.atlassian.labs.speakeasy.descriptor.DescriptorGeneratorManagerImpl;
  3. import com.atlassian.labs.speakeasy.descriptor.external.SpeakeasyWebResourceModuleDescriptor;
  4. import com.atlassian.labs.speakeasy.commonjs.descriptor.SpeakeasyCommonJsModulesDescriptor;
  5. import com.atlassian.labs.speakeasy.manager.PluginOperationFailedException;
  6. import com.atlassian.labs.speakeasy.descriptor.external.webfragment.SpeakeasyWebItemModuleDescriptor;
  7. import com.atlassian.labs.speakeasy.manager.convention.external.ConventionDescriptorGenerator;
  8. import com.atlassian.labs.speakeasy.model.JsonManifest;
  9. import com.atlassian.labs.speakeasy.util.WebResourceUtil;
  10. import com.atlassian.plugin.*;
  11. import com.atlassian.plugin.event.PluginEventManager;
  12. import com.atlassian.plugin.hostcontainer.HostContainer;
  13. import com.atlassian.plugin.module.ModuleFactory;
  14. import com.atlassian.plugin.osgi.util.OsgiHeaderUtil;
  15. import com.atlassian.plugin.webresource.WebResourceManager;
  16. import com.atlassian.plugin.webresource.WebResourceModuleDescriptor;
  17. import org.dom4j.DocumentFactory;
  18. import org.dom4j.Element;
  19. import org.osgi.framework.Bundle;
  20. import org.osgi.framework.BundleContext;
  21. import org.osgi.framework.ServiceFactory;
  22. import org.osgi.framework.ServiceRegistration;
  23. import static com.google.common.collect.Sets.newHashSet;
  24. /**
  25. *
  26. */
  27. public class ConventionDescriptorGeneratorServiceFactory implements ServiceFactory
  28. {
  29. private final ModuleFactory moduleFactory;
  30. private final BundleContext bundleContext;
  31. private final PluginAccessor pluginAccessor;
  32. private final HostContainer hostContainer;
  33. private final DescriptorGeneratorManagerImpl descriptorGeneratorManager;
  34. private final PluginEventManager pluginEventManager;
  35. private final JsonToElementParser jsonToElementParser;
  36. private final WebResourceManager webResourceManager;
  37. private final JsonManifestHandler jsonManifestHandler;
  38. private final PluginController pluginController;
  39. //private final Set<String> trackedPlugins = new CopyOnWriteArraySet<String>();
  40. public ConventionDescriptorGeneratorServiceFactory(final ModuleFactory moduleFactory, final BundleContext bundleContext, final PluginAccessor pluginAccessor, HostContainer hostContainer, DescriptorGeneratorManagerImpl descriptorGeneratorManager, JsonToElementParser jsonToElementParser, WebResourceManager webResourceManager, PluginEventManager pluginEventManager, final PluginController pluginController, JsonManifestHandler jsonManifestHandler)
  41. {
  42. this.moduleFactory = moduleFactory;
  43. this.bundleContext = bundleContext;
  44. this.pluginAccessor = pluginAccessor;
  45. this.hostContainer = hostContainer;
  46. this.descriptorGeneratorManager = descriptorGeneratorManager;
  47. this.jsonToElementParser = jsonToElementParser;
  48. this.pluginEventManager = pluginEventManager;
  49. this.webResourceManager = webResourceManager;
  50. this.pluginController = pluginController;
  51. this.jsonManifestHandler = jsonManifestHandler;
  52. }
  53. public Object getService(Bundle bundle, ServiceRegistration registration)
  54. {
  55. DocumentFactory factory = DocumentFactory.getInstance();
  56. String pluginKey = OsgiHeaderUtil.getPluginKey(bundle);
  57. Plugin plugin = pluginAccessor.getPlugin(pluginKey);
  58. if (bundle.getEntry("atlassian-extension.json") != null)
  59. {
  60. JsonManifest mf = jsonManifestHandler.read(plugin);
  61. if (mf.getScreenshot() != null)
  62. {
  63. registerScreenshotWebResourceDescriptor(bundle, factory, plugin, mf.getScreenshot());
  64. }
  65. }
  66. if (bundle.getEntry("js/") != null)
  67. {
  68. SpeakeasyCommonJsModulesDescriptor descriptor = new SpeakeasyCommonJsModulesDescriptor(
  69. moduleFactory, bundleContext, hostContainer, descriptorGeneratorManager, pluginAccessor);
  70. Element modules = factory.createElement("scoped-modules")
  71. .addAttribute("key", "modules")
  72. .addAttribute("location", "js");
  73. if (bundle.getEntry("css/") != null)
  74. {
  75. modules.addElement("dependency").setText("css");
  76. }
  77. descriptor.init(plugin, modules);
  78. bundle.getBundleContext().registerService(ModuleDescriptor.class.getName(), descriptor, null);
  79. }
  80. if (bundle.getEntry("images/") != null)
  81. {
  82. registerSpeakeasyWebResourceDescriptor(bundle, factory, plugin, "images");
  83. }
  84. if (bundle.getEntry("css") != null)
  85. {
  86. registerSpeakeasyWebResourceDescriptor(bundle, factory, plugin, "css");
  87. }
  88. if (bundle.getEntry("ui/web-items.json") != null)
  89. {
  90. registerSpeakeasyWebItems(bundle, plugin);
  91. }
  92. //trackedPlugins.add(pluginKey);
  93. return new ConventionDescriptorGenerator()
  94. {
  95. };
  96. }
  97. private void registerSpeakeasyWebItems(Bundle bundle, Plugin plugin)
  98. {
  99. try
  100. {
  101. for (Element element : jsonToElementParser.createWebItems(plugin.getResourceAsStream("ui/web-items.json")))
  102. {
  103. SpeakeasyWebItemModuleDescriptor descriptor = new SpeakeasyWebItemModuleDescriptor(moduleFactory, bundleContext, descriptorGeneratorManager, webResourceManager);
  104. descriptor.init(plugin, element);
  105. bundle.getBundleContext().registerService(ModuleDescriptor.class.getName(), descriptor, null);
  106. }
  107. }
  108. catch (PluginOperationFailedException e)
  109. {
  110. e.setPluginKey(plugin.getKey());
  111. throw e;
  112. }
  113. }
  114. private void registerScreenshotWebResourceDescriptor(Bundle bundle, DocumentFactory factory, Plugin plugin, String screenshotPath)
  115. {
  116. WebResourceModuleDescriptor descriptor = WebResourceUtil.instantiateDescriptor(moduleFactory, hostContainer);
  117. Element element = factory.createElement("web-resource")
  118. .addAttribute("key", "screenshot");
  119. element.addElement("resource")
  120. .addAttribute("type", "download")
  121. .addAttribute("name", "screenshot.png")
  122. .addAttribute("location", screenshotPath);
  123. descriptor.init(plugin, element);
  124. bundle.getBundleContext().registerService(ModuleDescriptor.class.getName(), descriptor, null);
  125. }
  126. private void registerSpeakeasyWebResourceDescriptor(Bundle bundle, DocumentFactory factory, Plugin plugin, String type)
  127. {
  128. SpeakeasyWebResourceModuleDescriptor descriptor = new SpeakeasyWebResourceModuleDescriptor(moduleFactory, hostContainer, bundleContext, descriptorGeneratorManager);
  129. Element element = factory.createElement("scoped-web-resource")
  130. .addAttribute("key", type)
  131. .addAttribute("scan", "/" + type);
  132. element.addElement("transformation")
  133. .addAttribute("extension", "css")
  134. .addElement("transformer")
  135. .addAttribute("key", "cssVariables")
  136. .addAttribute("imagesModuleKey", plugin.getKey() + ":" + "images-" + bundle.getLastModified())
  137. .addAttribute("fullModuleKey", plugin.getKey() + ":" + "css-" + bundle.getLastModified());
  138. descriptor.init(plugin, element);
  139. bundle.getBundleContext().registerService(ModuleDescriptor.class.getName(), descriptor, null);
  140. }
  141. public void ungetService(Bundle bundle, ServiceRegistration registration, Object service)
  142. {
  143. final String pluginKey = OsgiHeaderUtil.getPluginKey(bundle);
  144. // Plugin plugin = pluginAccessor.getPlugin(pluginKey);
  145. // if (trackedPlugins.contains(plugin.getKey()))
  146. // {
  147. // for (ModuleDescriptor descriptor : plugin.getModuleDescriptors())
  148. // {
  149. // if (descriptor instanceof StateAware)
  150. // {
  151. // ((StateAware)descriptor).disabled();
  152. // }
  153. // }
  154. // }
  155. //trackedPlugins.remove(pluginKey);
  156. }
  157. }