Commits

Jon Mort committed c17197f

tweak code for bug report

Comments (0)

Files changed (14)

consumer-dynamic/src/main/java/com/adaptavist/plugin/bug/consumer/MyDynamicDictionary.java

 package com.adaptavist.plugin.bug.consumer;
 
-import com.adaptavist.plugin.bug.service.Dictionary;
-
-public class MyDynamicDictionary implements Dictionary {
+public class MyDynamicDictionary { //implements Dictionary {
     public String getDefinition(String text) {
         return "Dynamic: " + text;
     }

consumer-dynamic/src/main/resources/atlassian-plugin.xml

         <description>${project.description}</description>
         <version>${project.version}</version>
         <vendor name="${project.organization.name}" url="${project.organization.url}"/>
-        <bundle-instructions>
-            <DynamicImport-Package>com.adaptavist.plugin.bug.service</DynamicImport-Package>
-            <Import-Package>!com.adaptavist.plugin.bug.service;*;resolution:=optional</Import-Package>
-        </bundle-instructions>
     </plugin-info>
 
     <dictionary key="myDynamicDictionary" lang="dynamic" class="com.adaptavist.plugin.bug.consumer.MyDynamicDictionary" />
 
+
+    <dictionary key="myDynamicDictionary2" lang="dynamic2" />
+
+
 </atlassian-plugin>

consumer/src/main/java/com/adaptavist/plugin/bug/consumer/ComponentServlet.java

+package com.adaptavist.plugin.bug.consumer;
+
+import com.adaptavist.plugin.bug.service.DictionaryService;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class ComponentServlet extends HttpServlet {
+
+    private final DictionaryComponent dictionaryComponent;
+
+    public ComponentServlet(DictionaryComponent dictionaryComponent) {
+        this.dictionaryComponent = dictionaryComponent;
+    }
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        for (String str : dictionaryComponent.lookup("Consumer")) {
+            resp.getWriter().println(str);
+        }
+        resp.getWriter().close();
+    }
+}

consumer/src/main/java/com/adaptavist/plugin/bug/consumer/DictionaryComponent.java

+package com.adaptavist.plugin.bug.consumer;
+
+import com.adaptavist.plugin.bug.service.DictionaryService;
+
+import java.util.List;
+
+public class DictionaryComponent {
+
+    private final DictionaryService dictionaryService;
+
+    public DictionaryComponent(DictionaryService dictionaryService) {
+        this.dictionaryService = dictionaryService;
+    }
+
+    public List<String> lookup(String str) {
+        return dictionaryService.lookup(str);
+    }
+}

consumer/src/main/java/com/adaptavist/plugin/bug/consumer/ExampleServlet.java

+package com.adaptavist.plugin.bug.consumer;
+
+import com.adaptavist.plugin.bug.service.DictionaryService;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class ExampleServlet extends HttpServlet {
+
+    private final DictionaryService dictionaryService;
+
+    public ExampleServlet(DictionaryService dictionaryService) {
+        this.dictionaryService = dictionaryService;
+    }
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        for (String str : dictionaryService.lookup("Consumer")) {
+            resp.getWriter().println(str);
+        }
+        resp.getWriter().close();
+    }
+}

consumer/src/main/resources/atlassian-plugin.xml

         <description>${project.description}</description>
         <version>${project.version}</version>
         <vendor name="${project.organization.name}" url="${project.organization.url}"/>
+        <bundle-instructions>
+            <Import-Package>com.adaptavist.plugin.bug.service;resolution:=optional,*</Import-Package>
+        </bundle-instructions>
     </plugin-info>
 
-    <dictionary key="myEnglishDictionary" lang="english" class="com.adaptavist.plugin.bug.consumer.MyDictionary" />
+    <dictionary key="myEnglishDictionary" lang="english" />
 
+    <!--<dictionary key="myEnglishDictionary" lang="english" class="com.adaptavist.plugin.bug.consumer.MyDictionary" />-->
+
+    <servlet name="dictionary-servlet" class="com.adaptavist.plugin.bug.consumer.ExampleServlet" key="test">
+        <url-pattern>/consumer/test</url-pattern>
+    </servlet>
+
+    <servlet name="dictionary-servlet" class="com.adaptavist.plugin.bug.consumer.ComponentServlet" key="test2">
+        <url-pattern>/consumer/test2</url-pattern>
+    </servlet>
+    <component-import key="dictionaryService" interface="com.adaptavist.plugin.bug.service.DictionaryService" />
+    <component key="component" class="com.adaptavist.plugin.bug.consumer.DictionaryComponent" />
 </atlassian-plugin>

service-provider/src/main/java/com/adaptavist/plugin/bug/service/BugDemoService.java

-package com.adaptavist.plugin.bug.service;
-
-public interface BugDemoService {
-    String message();
-}

service-provider/src/main/java/com/adaptavist/plugin/bug/service/DictionaryService.java

+package com.adaptavist.plugin.bug.service;
+
+import java.util.List;
+
+public interface DictionaryService {
+    List<String> lookup(String text);
+}

service-provider/src/main/java/com/adaptavist/plugin/bug/service/internal/BugDemoServiceImpl.java

-package com.adaptavist.plugin.bug.service.internal;
-
-import com.adaptavist.plugin.bug.service.BugDemoService;
-
-public class BugDemoServiceImpl implements BugDemoService {
-
-    public String message() {
-        return "Hello Service World";
-    }
-}

service-provider/src/main/java/com/adaptavist/plugin/bug/service/internal/DefaultDictionary.java

+package com.adaptavist.plugin.bug.service.internal;
+
+import com.adaptavist.plugin.bug.service.Dictionary;
+
+public class DefaultDictionary implements Dictionary {
+    private final String language;
+
+    public DefaultDictionary(String language) {
+        this.language = language;
+    }
+
+    public String getDefinition(String text) {
+        return String.format("%s %s", language, text);
+    }
+}

service-provider/src/main/java/com/adaptavist/plugin/bug/service/internal/DictionaryModuleDescriptor.java

     }
 
     public Dictionary getModule() {
-        return moduleFactory.createModule(moduleClassName, this);
+        return new DefaultDictionary(language);
     }
 
     public String getLanguage() {
         return language;
     }
+
 }

service-provider/src/main/java/com/adaptavist/plugin/bug/service/internal/DictionaryServiceImpl.java

+package com.adaptavist.plugin.bug.service.internal;
+
+import com.adaptavist.plugin.bug.service.DictionaryService;
+import com.atlassian.plugin.PluginAccessor;
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class DictionaryServiceImpl implements DictionaryService {
+    private final PluginAccessor pluginAccessor;
+
+    public DictionaryServiceImpl(PluginAccessor pluginAccessor) {
+        this.pluginAccessor = pluginAccessor;
+    }
+
+
+    public List<String> lookup(final String text) {
+        List<DictionaryModuleDescriptor> dictionaryModuleDescriptors =
+                pluginAccessor.getEnabledModuleDescriptorsByClass(DictionaryModuleDescriptor.class);
+        return Lists.transform(dictionaryModuleDescriptors, new Function<DictionaryModuleDescriptor, String>() {
+            public String apply(DictionaryModuleDescriptor from) {
+                try {
+                    return from.getModule().getDefinition(text);
+                } catch (ClassCastException howCanThisBe) {
+                    howCanThisBe.printStackTrace();
+                    return howCanThisBe.getMessage();
+                }
+            }
+        });
+    }
+}

service-provider/src/main/java/com/adaptavist/plugin/bug/service/internal/ExampleServlet.java

 
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-        // To get all the enabled modules of this module descriptor
-        List<DictionaryModuleDescriptor> dictionaryModuleDescriptors =
-                pluginAccessor.getEnabledModuleDescriptorsByClass(DictionaryModuleDescriptor.class);
-        // Now we'll use each one to get a map of languages to translations of the word "OSGi"
-        Map<String, String> results = new HashMap<String, String>();
-        for (DictionaryModuleDescriptor dictionaryModuleDescriptor : dictionaryModuleDescriptors) {
-            try {
-            results.put(dictionaryModuleDescriptor.getLanguage(),
-                    dictionaryModuleDescriptor.getModule().getDefinition("OSGi"));
-            } catch (ClassCastException howCanThisBe) {
-                results.put("class Cast Exception", howCanThisBe.getMessage());
+        try {
+            // To get all the enabled modules of this module descriptor
+            List<DictionaryModuleDescriptor> dictionaryModuleDescriptors =
+                    pluginAccessor.getEnabledModuleDescriptorsByClass(DictionaryModuleDescriptor.class);
+            // Now we'll use each one to get a map of languages to translations of the word "OSGi"
+            Map<String, String> results = new HashMap<String, String>();
+            for (DictionaryModuleDescriptor dictionaryModuleDescriptor : dictionaryModuleDescriptors) {
+                try {
+                    results.put(dictionaryModuleDescriptor.getLanguage(),
+                            dictionaryModuleDescriptor.getModule().getDefinition("OSGi"));
+                } catch (ClassCastException howCanThisBe) {
+                    results.put("class Cast Exception", howCanThisBe.getMessage());
+                }
             }
+
+            resp.getWriter().print(dictionaryModuleDescriptors.size());
+            resp.getWriter().println(" Dictionaries found");
+            for (String key : results.keySet()) {
+                resp.getWriter().print(key);
+                resp.getWriter().print(" = ");
+                resp.getWriter().println(results.get(key));
+            }
+
+            resp.getWriter().println("Now using predicate by name and reflection to do the call");
+
+            Collection<Object> modules = pluginAccessor.getModules(new ModuleDescriptorPredicate<Object>() {
+                public boolean matches(ModuleDescriptor<? extends Object> moduleDescriptor) {
+                    return moduleDescriptor.getClass().getName().equals(DictionaryModuleDescriptor.class.getName());
+                }
+            });
+
+            resp.getWriter().print(modules.size());
+            resp.getWriter().println(" Dictionaries found");
+            for (Object module : modules) {
+                resp.getWriter().println(reflectivlyCallGetDefinition(module));
+            }
+
+            if (results.size() != modules.size()) {
+                resp.getWriter().println("A difference in the number of modules. What is going on?");
+            }
+        }catch (NoClassDefFoundError e) {
+            e.printStackTrace();
+            resp.getWriter().print("Err how did this happen: ");
+            resp.getWriter().println(e);
+            resp.getWriter().println("You'll have to uninstall the service and any plugins that use it to continue");
+        } finally {
+            resp.getWriter().close();
         }
 
-        resp.getWriter().print(dictionaryModuleDescriptors.size());
-        resp.getWriter().println(" Dictionaries found");
-        for (String key : results.keySet()) {
-            resp.getWriter().print(key);
-            resp.getWriter().print(" = ");
-            resp.getWriter().println(results.get(key));
-        }
-
-        Collection<Object> modules = pluginAccessor.getModules(new ModuleDescriptorPredicate<Object>() {
-            public boolean matches(ModuleDescriptor<? extends Object> moduleDescriptor) {
-                return moduleDescriptor.getClass().getName().equals(DictionaryModuleDescriptor.class.getName());
-            }
-        });
-
-        resp.getWriter().println("Now using predicate by name and reflection to do the call");
-        resp.getWriter().print(modules.size());
-        resp.getWriter().println(" Dictionaries found");
-        for (Object module : modules) {
-            resp.getWriter().println(reflectivlyCallGetDefinition(module));
-        }
-
-        resp.getWriter().close();
     }
 
     private String reflectivlyCallGetDefinition(Object module) {
         Class clazz = module.getClass();
         try {
             Method getDefinition = clazz.getMethod("getDefinition", String.class);
-            return (String)getDefinition.invoke(module, "Reflection");
+            return (String) getDefinition.invoke(module, "Reflection");
         } catch (Exception e) {
-            throw new RuntimeException(e);
+            e.printStackTrace();
+            return e.getMessage();
         }
     }
 }

service-provider/src/main/resources/atlassian-plugin.xml

     <servlet name="dictionary-servlet" class="com.adaptavist.plugin.bug.service.internal.ExampleServlet" key="test">
         <url-pattern>/test</url-pattern>
     </servlet>
+
+    <component key="dictionaryService" class="com.adaptavist.plugin.bug.service.internal.DictionaryServiceImpl" public="true" >
+        <interface>com.adaptavist.plugin.bug.service.DictionaryService</interface>
+    </component>
 </atlassian-plugin>