View Javadoc

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