View Javadoc

1   package com.atlassian.plugin.module;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.PluginParseException;
5   import com.atlassian.plugin.hostcontainer.HostContainer;
6   
7   /**
8    * The ClassModuleFactory creates a java bean for the given module class by using either the plugins container or the hostcontainer, depending
9    * if the plugin implements {@link com.atlassian.plugin.module.ContainerManagedPlugin}.
10   * The returned bean class should have all constructor dependencies injected. However it is the containers responsibility to inject the dependencies.
11   * <p/>
12   * The ClassModuleFactory expects the fully qualified name of the java class.
13   *
14   * @since 2.5.0
15   */
16  public class ClassPrefixModuleFactory implements PrefixModuleFactory
17  {
18      protected final HostContainer hostContainer;
19  
20      public ClassPrefixModuleFactory(final HostContainer hostContainer)
21      {
22          this.hostContainer = hostContainer;
23      }
24  
25      public <T> T createModule(String name, ModuleDescriptor<T> moduleDescriptor) throws PluginParseException
26      {
27          Class<T> cls = getModuleClass(name, moduleDescriptor);
28  
29          if (moduleDescriptor.getPlugin() instanceof ContainerManagedPlugin)
30          {
31              ContainerManagedPlugin cmPlugin = (ContainerManagedPlugin) moduleDescriptor.getPlugin();
32              return cmPlugin.getContainerAccessor().createBean(cls);
33          }
34          else if (cls != null)
35          {
36              return hostContainer.create(cls);
37          }
38          return null;
39      }
40  
41      Class getModuleClass(final String name, final ModuleDescriptor moduleDescriptor) throws ModuleClassNotFoundException
42      {
43          try
44          {
45              return moduleDescriptor.getPlugin().loadClass(name, null);
46          }
47          catch (ClassNotFoundException e)
48          {
49              throw new ModuleClassNotFoundException(name, moduleDescriptor.getPluginKey(), moduleDescriptor.getKey(), e, createErrorMsg(name));
50          }
51      }
52  
53      private String createErrorMsg(String className)
54      {
55          StringBuilder builder = new StringBuilder();
56          builder.append("Couldn't load the class '").append(className).append("'. ");
57          builder.append("This could mean that you misspelled the name of the class (double check) or that ");
58          builder.append("you're using a class in your plugin that you haven't provided bundle instructions for. ");
59          builder.append("See https://developer.atlassian.com/x/mQAN for more details on how to fix this.");
60  
61          // TinyURL resolves to --> https://developer.atlassian.com/display/DOCS/BundleException
62  
63          return builder.toString();
64      }
65  
66      public String getPrefix()
67      {
68          return "class";
69      }
70  }