Commits

Samuel Le Berrigaud  committed 8ab17c1

WEBHOOKS-25: Adding a module factory for described-module-type

Actually adding the service only if the schema plugin is not
already installed.
Exporting necessary packages.

  • Participants
  • Parent commits 09bd6a8

Comments (0)

Files changed (4)

File plugin/pom.xml

                     <extractDependencies>true</extractDependencies>
                     <instructions>
                         <Import-Package>
-                            com.atlassian.plugin.schema.descriptor;resolution:=optional,
-                            com.atlassian.plugin.schema.spi;resolution:=optional,
+                            com.atlassian.plugin.schema.descriptor;version="2.10";resolution:=optional,
+                            com.atlassian.plugin.schema.spi;version="2.10";resolution:=optional,
                             com.atlassian.plugin.osgi.bridge.external;version="2.8",
+                            com.atlassian.plugin.osgi.external;version="2.8",
+                            com.atlassian.plugin;version="2.8",
                             com.atlassian.plugin*;version="2.8",
                             com.atlassian.sal.api*;version="${sal.version}",
                             com.atlassian.util.concurrent*;version="${atlassian-util-concurrent-api.version}",
                             com.atlassian.httpclient.spi*;version="${project.version}",
                             com.atlassian.webhooks.spi.plugin;version="${spi.version}",
                             com.atlassian.webhooks.spi.provider;version="${spi.version}",
+                            com.atlassian.plugin.schema.descriptor;version="2.10",
+                            com.atlassian.plugin.schema.spi;version="2.10",
                         </Export-Package>
-                        <Private-Package>
-                            com.atlassian.plugin.schema*
-                        </Private-Package>
                         <Spring-Context>*;timeout:=60</Spring-Context>
                     </instructions>
                 </configuration>

File plugin/src/main/java/com/atlassian/webhooks/plugin/plugin/WebHookDescribedModuleDescriptorFactory.java

-package com.atlassian.webhooks.plugin.plugin;
-
-import com.atlassian.plugin.ModuleDescriptor;
-import com.atlassian.plugin.PluginAccessor;
-import com.atlassian.plugin.PluginParseException;
-import com.atlassian.plugin.module.ModuleFactory;
-import com.atlassian.plugin.osgi.external.ListableModuleDescriptorFactory;
-import com.atlassian.plugin.schema.plugin.DescribedModuleTypeModuleDescriptor;
-import com.google.common.base.Supplier;
-import com.google.common.base.Suppliers;
-import com.google.common.collect.ImmutableSet;
-import org.osgi.framework.BundleContext;
-
-import java.util.Set;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-public final class WebHookDescribedModuleDescriptorFactory implements ListableModuleDescriptorFactory
-{
-    @SuppressWarnings("unchecked")
-    private static final Class<ModuleDescriptor<?>> DESCRIBED_MODULE_TYPE_MODULE_DESCRIPTOR_CLASS = (Class) DescribedModuleTypeModuleDescriptor.class;
-
-    private static final String DESCRIBED_MODULE_TYPE = "described-module-type";
-
-    private final PluginAccessor pluginAccessor;
-    private final ModuleFactory moduleFactory;
-    private final BundleContext bundleContext;
-
-    private final Supplier<DescribedModuleTypeModuleDescriptor> moduleDescriptor = Suppliers.memoize(new Supplier<DescribedModuleTypeModuleDescriptor>()
-    {
-        @Override
-        public DescribedModuleTypeModuleDescriptor get()
-        {
-            return new DescribedModuleTypeModuleDescriptor(moduleFactory, bundleContext);
-        }
-    });
-
-    public WebHookDescribedModuleDescriptorFactory(PluginAccessor pluginAccessor, ModuleFactory moduleFactory, BundleContext bundleContext)
-    {
-        this.pluginAccessor = checkNotNull(pluginAccessor);
-        this.moduleFactory = checkNotNull(moduleFactory);
-        this.bundleContext = checkNotNull(bundleContext);
-    }
-
-    @Override
-    public ModuleDescriptor<?> getModuleDescriptor(String s) throws PluginParseException, IllegalAccessException, InstantiationException, ClassNotFoundException
-    {
-        return isValidType(s) ? moduleDescriptor.get() : null;
-    }
-
-    @Override
-    public Class<? extends ModuleDescriptor> getModuleDescriptorClass(String s)
-    {
-        return isValidType(s) ? DESCRIBED_MODULE_TYPE_MODULE_DESCRIPTOR_CLASS : null;
-    }
-
-    @Override
-    public boolean hasModuleDescriptor(String s)
-    {
-        return isValidType(s);
-    }
-
-    @Override
-    public Set<Class<ModuleDescriptor<?>>> getModuleDescriptorClasses()
-    {
-        if (describedModuleDescriptorTypeDoesNotExist())
-        {
-            return ImmutableSet.of(DESCRIBED_MODULE_TYPE_MODULE_DESCRIPTOR_CLASS);
-        }
-        else
-        {
-            return ImmutableSet.of();
-        }
-    }
-
-    private boolean isValidType(String s)
-    {
-        return DESCRIBED_MODULE_TYPE.equals(s) && describedModuleDescriptorTypeDoesNotExist();
-    }
-
-    private boolean describedModuleDescriptorTypeDoesNotExist()
-    {
-        return pluginAccessor.getEnabledModuleDescriptorsByType(DESCRIBED_MODULE_TYPE).isEmpty();
-    }
-}

File plugin/src/main/java/com/atlassian/webhooks/plugin/plugin/WebHookDescriptorFactoryRegistration.java

+package com.atlassian.webhooks.plugin.plugin;
+
+import com.atlassian.plugin.ModuleDescriptorFactory;
+import com.atlassian.plugin.PluginAccessor;
+import com.atlassian.plugin.osgi.external.ListableModuleDescriptorFactory;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.InitializingBean;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+public final class WebHookDescriptorFactoryRegistration implements InitializingBean, DisposableBean
+{
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    private final PluginAccessor pluginAccessor;
+    private final ListableModuleDescriptorFactory moduleDescriptorFactory;
+    private final BundleContext bundleContext;
+
+    private ServiceRegistration serviceRegistration;
+
+    public WebHookDescriptorFactoryRegistration(ListableModuleDescriptorFactory moduleDescriptorFactory, PluginAccessor pluginAccessor, BundleContext bundleContext)
+    {
+        this.moduleDescriptorFactory = checkNotNull(moduleDescriptorFactory);
+        this.pluginAccessor = checkNotNull(pluginAccessor);
+        this.bundleContext = checkNotNull(bundleContext);
+    }
+
+    @Override
+    public void afterPropertiesSet() throws Exception
+    {
+        if (!isSchemaPluginInstalled())
+        {
+            logger.debug("Schema plugin is not installed. Registering {}.", moduleDescriptorFactory.getModuleDescriptorClasses());
+
+            serviceRegistration = bundleContext.registerService(
+                    new String[]{ListableModuleDescriptorFactory.class.getName(), ModuleDescriptorFactory.class.getName()},
+                    moduleDescriptorFactory,
+                    null);
+        }
+        else
+        {
+            logger.debug("Schema plugin is installed. Not registering {}.", moduleDescriptorFactory.getModuleDescriptorClasses());
+        }
+    }
+
+    @Override
+    public void destroy() throws Exception
+    {
+        if (serviceRegistration != null)
+        {
+            serviceRegistration.unregister();
+        }
+    }
+
+    private boolean isSchemaPluginInstalled()
+    {
+        // we check whether the plugin is installed, not whether it's enabled since it could get enabled after the
+        // webhook plugin
+        return pluginAccessor.getPlugin("com.atlassian.plugins.schema") != null;
+    }
+}

File plugin/src/main/resources/META-INF/spring/atlassian-plugins-module-types.xml

              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                                  http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"
              default-autowire="autodetect" osgi:default-timeout="30000">
-    <osgi:service id="moduleType-described-module-type_osgiService" auto-export="interfaces">
-        <beans:bean id="moduleType-described-module-type"
-                    class="com.atlassian.webhooks.plugin.plugin.WebHookDescribedModuleDescriptorFactory">
-            <beans:constructor-arg index="0" ref="pluginAccessor" />
-            <beans:constructor-arg index="1" ref="moduleFactory" />
-            <beans:constructor-arg index="2" ref="bundleContext"/>
-        </beans:bean>
-    </osgi:service>
+
+    <beans:bean id="descriptorFactoryRegistration" class="com.atlassian.webhooks.plugin.plugin.WebHookDescriptorFactoryRegistration">
+        <beans:constructor-arg index="0">
+            <beans:bean class="com.atlassian.plugin.osgi.external.SingleModuleDescriptorFactory">
+                <beans:constructor-arg index="0">
+                    <beans:bean id="springHostContainer"
+                                class="com.atlassian.plugin.osgi.bridge.external.SpringHostContainer"/>
+                </beans:constructor-arg>
+                <beans:constructor-arg index="1" value="described-module-type"/>
+                <beans:constructor-arg index="2"
+                                       value="com.atlassian.plugin.schema.plugin.DescribedModuleTypeModuleDescriptor"/>
+            </beans:bean>
+        </beans:constructor-arg>
+        <beans:constructor-arg index="1" ref="pluginAccessor"/>
+        <beans:constructor-arg index="2" ref="bundleContext"/>
+    </beans:bean>
 </beans:beans>