View Javadoc

1   package com.atlassian.plugin.descriptors;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.atlassian.plugin.ModuleDescriptorFactory;
5   import com.atlassian.plugin.PluginParseException;
6   import com.atlassian.plugin.ModuleDescriptor;
7   import org.dom4j.Element;
8   
9   /**
10   * Utility class to create UnloadableModuleDescriptor instances when there are problems
11   */
12  public final class UnrecognisedModuleDescriptorFactory
13  {
14      /**
15       * Creates a new UnrecognisedModuleDescriptor, for when a problem occurs during the retrieval
16       * of the ModuleDescriptor itself.
17       *
18       * This instance has the same information as the original ModuleDescriptor, but also contains
19       * an error message that reports the error.
20       *
21       * @param plugin the Plugin the ModuleDescriptor belongs to
22       * @param element the XML Element used to construct the ModuleDescriptor
23       * @param e the Throwable
24       * @param moduleDescriptorFactory a ModuleDescriptorFactory used to retrieve ModuleDescriptor instances
25       * @return a new UnloadableModuleDescriptor instance
26       * @throws com.atlassian.plugin.PluginParseException if there was a problem constructing the UnloadableModuleDescriptor
27       */
28      public static UnrecognisedModuleDescriptor createUnrecognisedModuleDescriptor(Plugin plugin, Element element, Throwable e, ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
29      {
30          UnrecognisedModuleDescriptor descriptor = new UnrecognisedModuleDescriptor();
31          descriptor.init(plugin, element);
32  
33          String name = element.getName();
34          Class moduleClass = moduleDescriptorFactory.getModuleDescriptorClass(name);
35          String moduleClassName;
36  
37          if (moduleClass == null)
38              moduleClassName = descriptor.getKey();
39          else
40              moduleClassName = moduleClass.getName();
41  
42          String errorMsg = UnrecognisedModuleDescriptorFactory.constructErrorMessage(plugin, name, moduleClassName, e);
43  
44          descriptor.setErrorText(errorMsg);
45  
46          return descriptor;
47      }
48  
49      /**
50       * Constructs an error message from a module and exception
51       *
52       * @param plugin the Plugin the module belongs to
53       * @param moduleName the name of the module
54       * @param moduleClass the class of the module
55       * @param e the Throwable
56       * @return an appropriate String representing the error
57       */
58      private static String constructErrorMessage(Plugin plugin, String moduleName, String moduleClass, Throwable e)
59      {
60          String errorMsg;
61  
62          if(e.getMessage()==null || "".equals(e.getMessage()))
63          {
64              if (e instanceof PluginParseException)
65                  errorMsg = "There was a problem loading the descriptor for module '" + moduleName + "' in plugin '" + (plugin == null ? "null" : plugin.getName()) + "'.\n ";
66              else if (e instanceof InstantiationException)
67                  errorMsg = "Could not instantiate module descriptor: " + moduleClass + ".<br/>";
68              else if (e instanceof IllegalAccessException)
69                  errorMsg = "Exception instantiating module descriptor: " + moduleClass + ".<br/>";
70              else if (e instanceof ClassNotFoundException)
71                  errorMsg = "Could not find module descriptor class: " + moduleClass + ".<br/>";
72              else if (e instanceof NoClassDefFoundError)
73                  errorMsg = "A required class was missing: " + moduleClass + ". Please check that you have all of the required dependencies.<br/>";
74              else
75                  errorMsg = "There was a problem loading the module descriptor: " + moduleClass + ".<br/>";
76          }
77          else
78          {
79              errorMsg = e.getMessage();
80          }
81          return errorMsg;
82      }
83  }