Commits

Samuel Le Berrigaud  committed 09bd6a8

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

This will only trigger if the module type doesn't already
exist. Providing the necessary backward compatibility.

  • Participants
  • Parent commits b4fbb9c

Comments (0)

Files changed (6)

File plugin/pom.xml

         <spi.version>0.13</spi.version>
     </properties>
 
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>com.atlassian.security</groupId>
+                <artifactId>atlassian-secure-xml</artifactId>
+                <version>3.1.4</version>
+                <scope>provided</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
     <dependencies>
         <dependency>
             <groupId>com.atlassian.webhooks</groupId>
         <dependency>
             <groupId>com.atlassian.plugins</groupId>
             <artifactId>atlassian-plugins-schema</artifactId>
-            <version>${atlassian-plugins.version}</version>
-            <scope>provided</scope>
+            <version>${atlassian-plugins-schema.version}</version>
+            <!--<scope>provided</scope>-->
+        </dependency>
+        <dependency>
+            <groupId>com.atlassian.plugins</groupId>
+            <artifactId>atlassian-plugins-schema-plugin</artifactId>
+            <version>${atlassian-plugins-schema.version}</version>
+            <!--<scope>provided</scope>-->
         </dependency>
         <dependency>
             <groupId>dom4j</groupId>
                     <extractDependencies>true</extractDependencies>
                     <instructions>
                         <Import-Package>
-                            com.atlassian.plugin.*;version="2.8",
+                            com.atlassian.plugin.schema.descriptor;resolution:=optional,
+                            com.atlassian.plugin.schema.spi;resolution:=optional,
+                            com.atlassian.plugin.osgi.bridge.external;version="2.8",
+                            com.atlassian.plugin*;version="2.8",
                             com.atlassian.sal.api*;version="${sal.version}",
-                            com.atlassian.plugin.schema.spi,
                             com.atlassian.util.concurrent*;version="${atlassian-util-concurrent-api.version}",
                             com.atlassian.webhooks.spi.provider;version="${spi.version}",
                             com.google.common*;version="${guava.version}",
                             com.atlassian.httpclient.api*;version="${project.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.webhooks.spi.provider;version="${spi.version}",
                         </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/resources/META-INF/spring/atlassian-plugins-module-types.xml

+<?xml version="1.0" encoding="UTF-8"?>
+
+<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
+             xmlns:osgi="http://www.springframework.org/schema/osgi"
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+             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:beans>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <atlassian-plugins.version>2.14.0-m5</atlassian-plugins.version>
         <atlassian-plugins-schema.version>3.0.0-m11</atlassian-plugins-schema.version>
+        <atlassian-plugins-schema-api.version>3.0</atlassian-plugins-schema-api.version>
         <sal.version>2.7.0</sal.version>
         <slf4j.version>1.6.4</slf4j.version>
         <atlassian-util-concurrent.version>2.4.0</atlassian-util-concurrent.version>

File test-plugin/pom.xml

                             <artifactId>org.json</artifactId>
                             <version>${org.json.version}</version>
                         </libArtifact>
-                        <!-- for described module descriptor support,
-                             remove when product have fully moved to Atlassian Plugins 3 -->
-                        <libArtifact>
-                            <groupId>com.atlassian.plugins</groupId>
-                            <artifactId>atlassian-plugins-schema</artifactId>
-                            <version>${atlassian-plugins-schema.version}</version>
-                        </libArtifact>
                     </libArtifacts>
                     <pluginArtifacts>
                         <pluginArtifact>
                             <artifactId>atlassian-util-concurrent</artifactId>
                             <version>${atlassian-util-concurrent.version}</version>
                         </pluginArtifact>
-                        <!-- for described module descriptor support,
-                             remove when product have fully moved to Atlassian Plugins 3 -->
-                        <pluginArtifact>
-                            <groupId>com.atlassian.plugins</groupId>
-                            <artifactId>atlassian-plugins-schema-plugin</artifactId>
-                            <version>${atlassian-plugins-schema.version}</version>
-                        </pluginArtifact>
                     </pluginArtifacts>
                     <products>
                         <product>
                             <log4jProperties>src/test/aps/log4j-refapp.properties</log4jProperties>
                         </product>
                         <product>
+                            <id>refapp</id>
+                            <instanceId>refapp_schema</instanceId>
+                            <version>${refapp.version}</version>
+                            <log4jProperties>src/test/aps/log4j-refapp.properties</log4jProperties>
+                            <libArtifacts>
+                                <libArtifact>
+                                    <groupId>com.atlassian.webhooks</groupId>
+                                    <artifactId>atlassian-webhooks-provider-spi</artifactId>
+                                    <version>${project.version}</version>
+                                </libArtifact>
+                                <libArtifact>
+                                    <groupId>org.json</groupId>
+                                    <artifactId>org.json</artifactId>
+                                    <version>${org.json.version}</version>
+                                </libArtifact>
+                                <!-- for described module descriptor support,
+                                     remove when product have fully moved to Atlassian Plugins 3 -->
+                                <libArtifact>
+                                    <groupId>com.atlassian.plugins</groupId>
+                                    <artifactId>atlassian-plugins-schema</artifactId>
+                                    <version>${atlassian-plugins-schema.version}</version>
+                                </libArtifact>
+                            </libArtifacts>
+                            <pluginArtifacts>
+                                <pluginArtifact>
+                                    <groupId>com.atlassian.functest</groupId>
+                                    <artifactId>functest-plugin</artifactId>
+                                    <version>${functest-plugin.version}</version>
+                                </pluginArtifact>
+                                <pluginArtifact>
+                                    <groupId>com.atlassian.webhooks</groupId>
+                                    <artifactId>atlassian-webhooks-plugin</artifactId>
+                                    <version>${project.version}</version>
+                                </pluginArtifact>
+                                <pluginArtifact>
+                                    <groupId>com.atlassian.util.concurrent</groupId>
+                                    <artifactId>atlassian-util-concurrent</artifactId>
+                                    <version>${atlassian-util-concurrent.version}</version>
+                                </pluginArtifact>
+                                <!-- for described module descriptor support,
+                                     remove when product have fully moved to Atlassian Plugins 3 -->
+                                <pluginArtifact>
+                                    <groupId>com.atlassian.plugins</groupId>
+                                    <artifactId>atlassian-plugins-schema-plugin</artifactId>
+                                    <version>${atlassian-plugins-schema.version}</version>
+                                </pluginArtifact>
+                            </pluginArtifacts>
+                        </product>
+                        <product>
                             <id>jira</id>
                             <version>${jira.version}</version>
                             <log4jProperties>src/test/aps/log4j-jira.properties</log4jProperties>
                             </includes>
                         </testGroup>
                         <testGroup>
+                            <id>refapp_schema</id>
+                            <productIds>
+                                <productId>refapp_schema</productId>
+                            </productIds>
+                            <includes>
+                                <include>it/**</include>
+                            </includes>
+                        </testGroup>
+                        <testGroup>
                             <id>jira</id>
                             <productIds>
                                 <productId>jira</productId>

File test-plugin/src/test/aps/log4j-refapp.properties

 log4j.appender.console = org.apache.log4j.ConsoleAppender
 log4j.appender.console.layout = org.apache.log4j.PatternLayout
 log4j.appender.console.layout.ConversionPattern = - %m%n
+
+log4j.logger.com.atlassian.webhooks = DEBUG, console
+log4j.additivity.com.atlassian.webhooks = false