Commits

Samuel Le Berrigaud  committed a13d05f

WEBHOOKS-25: Update based on latest version of Atlassian Plugins 3 (m17)

  • Participants
  • Parent commits 4196c27

Comments (0)

Files changed (5)

File plugin/pom.xml

             <!--<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>
             <artifactId>dom4j</artifactId>
             <version>1.4</version>

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 com.atlassian.plugin.schema.descriptor.DescribedModuleDescriptorFactory;
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 {
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
-    private final PluginAccessor pluginAccessor;
     private final ListableModuleDescriptorFactory moduleDescriptorFactory;
     private final BundleContext bundleContext;
+    private final Supplier<Boolean> describedModuleTypeDeclared;
 
     private ServiceRegistration serviceRegistration;
 
-    public WebHookDescriptorFactoryRegistration(ListableModuleDescriptorFactory moduleDescriptorFactory, PluginAccessor pluginAccessor, BundleContext bundleContext)
+    public WebHookDescriptorFactoryRegistration(ListableModuleDescriptorFactory moduleDescriptorFactory, BundleContext bundleContext)
     {
         this.moduleDescriptorFactory = checkNotNull(moduleDescriptorFactory);
-        this.pluginAccessor = checkNotNull(pluginAccessor);
         this.bundleContext = checkNotNull(bundleContext);
+        this.describedModuleTypeDeclared = Suppliers.memoize(new DescribedModuleTypeDeclaredSupplier(bundleContext));
     }
 
     @Override
     public void afterPropertiesSet() throws Exception
     {
-        if (!isSchemaPluginInstalled())
+        if (!isDescribedModuleTypeDeclared())
         {
-            logger.debug("Schema plugin is not installed. Registering {}.", moduleDescriptorFactory.getModuleDescriptorClasses());
+            logger.debug("Described Module Type is not installed. Registering {}.", moduleDescriptorFactory.getModuleDescriptorClasses());
 
             serviceRegistration = bundleContext.registerService(
                     new String[]{ListableModuleDescriptorFactory.class.getName(), ModuleDescriptorFactory.class.getName()},
         }
         else
         {
-            logger.debug("Schema plugin is installed. Not registering {}.", moduleDescriptorFactory.getModuleDescriptorClasses());
+            logger.debug("Described Module Type is installed. Not registering {}.", moduleDescriptorFactory.getModuleDescriptorClasses());
         }
     }
 
         }
     }
 
-    private boolean isSchemaPluginInstalled()
+    private boolean isDescribedModuleTypeDeclared()
     {
-        // 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;
+
+        return describedModuleTypeDeclared.get();
+    }
+
+    private static final class DescribedModuleTypeDeclaredSupplier implements Supplier<Boolean>
+    {
+        private final BundleContext bundleContext;
+
+        private DescribedModuleTypeDeclaredSupplier(BundleContext bundleContext)
+        {
+            this.bundleContext = checkNotNull(bundleContext);
+        }
+
+        @Override
+        public Boolean get()
+        {
+            final ServiceReference[] serviceReferences = getServiceReferences(bundleContext, DescribedModuleDescriptorFactory.class);
+            if (serviceReferences == null)
+            {
+                return false;
+            }
+
+            for (ServiceReference reference : serviceReferences)
+            {
+                final ListableModuleDescriptorFactory factory = getService(bundleContext, reference, ListableModuleDescriptorFactory.class);
+                System.out.println(factory + ":" + factory.getModuleDescriptorClasses());
+                if (factory.hasModuleDescriptor("described-module-type"))
+                {
+                    ungetServiceReferences(bundleContext, serviceReferences);
+                    return true;
+                }
+            }
+
+            ungetServiceReferences(bundleContext, serviceReferences);
+            return false;
+        }
+    }
+
+    private static <T> T getService(BundleContext bundleContext, ServiceReference reference, Class<T> type)
+    {
+        return type.cast(bundleContext.getService(reference));
+    }
+
+    private static ServiceReference[] getServiceReferences(BundleContext bundleContext, Class<?> type)
+    {
+        try
+        {
+            return bundleContext.getAllServiceReferences(type.getName(), null);
+        }
+        catch (InvalidSyntaxException e)
+        {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    private static ServiceReference[] ungetServiceReferences(BundleContext bundleContext, ServiceReference[] srs)
+    {
+        for (ServiceReference sr : srs)
+        {
+            bundleContext.ungetService(sr);
+        }
+        return srs;
     }
 }

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

                 </beans:constructor-arg>
                 <beans:constructor-arg index="1" value="described-module-type"/>
                 <beans:constructor-arg index="2"
-                                       value="com.atlassian.plugin.schema.plugin.DescribedModuleTypeModuleDescriptor"/>
+                                       value="com.atlassian.plugin.schema.impl.DescribedModuleTypeModuleDescriptor"/>
             </beans:bean>
         </beans:constructor-arg>
-        <beans:constructor-arg index="1" ref="pluginAccessor"/>
-        <beans:constructor-arg index="2" ref="bundleContext"/>
+        <beans:constructor-arg index="1" ref="bundleContext"/>
     </beans:bean>
 </beans:beans>
     <properties>
         <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.version>3.0.0-SNAPSHOT</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-api.version>2.4</atlassian-util-concurrent-api.version>
         <guava.version>10.0.1</guava.version>
         <spring.version>2.5.6</spring.version>
-        <refapp.version>2.18.0-m2</refapp.version>
         <jira.version>5.2.5</jira.version>
         <confluence.version>4.2.13</confluence.version>
         <sdk.version>4.0</sdk.version>

File test-plugin/pom.xml

                     <products>
                         <product>
                             <id>refapp</id>
-                            <version>${refapp.version}</version>
+                            <version>2.19.0</version>
                             <log4jProperties>src/test/aps/log4j-refapp.properties</log4jProperties>
                         </product>
                         <product>
                             <id>refapp</id>
-                            <instanceId>refapp_schema</instanceId>
-                            <version>${refapp.version}</version>
+                            <instanceId>refapp_3</instanceId>
+                            <version>3.0.0-m1</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>
                             </includes>
                         </testGroup>
                         <testGroup>
-                            <id>refapp_schema</id>
+                            <id>refapp_3</id>
                             <productIds>
-                                <productId>refapp_schema</productId>
+                                <productId>refapp_3</productId>
                             </productIds>
                             <includes>
                                 <include>it/**</include>