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      protected final HostContainer hostContainer;
21  
22      public ClassPrefixModuleFactory(final HostContainer hostContainer) {
23          this.hostContainer = hostContainer;
24      }
25  
26      public <T> T createModule(final String name, final ModuleDescriptor<T> moduleDescriptor) throws PluginParseException {
27          final Class<T> cls = getModuleClass(name, moduleDescriptor);
28  
29          final Plugin plugin = moduleDescriptor.getPlugin();
30          if (plugin instanceof ContainerManagedPlugin) {
31              final ContainerManagedPlugin cmPlugin = (ContainerManagedPlugin) plugin;
32              final ContainerAccessor containerAccessor = checkNotNull(cmPlugin.getContainerAccessor(),
33                      "Plugin container accessor is null. Plugin: %s. Module name: %s.", cmPlugin, name);
34  
35              return containerAccessor.createBean(cls);
36          } else if (cls != null) {
37              return hostContainer.create(cls);
38          }
39          return null;
40      }
41  
42      <T> Class<T> getModuleClass(final String name, final ModuleDescriptor moduleDescriptor) throws ModuleClassNotFoundException {
43          try {
44              return moduleDescriptor.getPlugin().loadClass(name, null);
45          } catch (ClassNotFoundException e) {
46              throw new ModuleClassNotFoundException(name, moduleDescriptor.getPluginKey(), moduleDescriptor.getKey(), e, createErrorMsg(name));
47          }
48      }
49  
50      private String createErrorMsg(final String className) {
51          // TinyURL resolves to --> https://developer.atlassian.com/display/DOCS/BundleException
52          return "Couldn't load the class '" + className + "'. "
53                  + "This could mean that you misspelled the name of the class (double check) or that "
54                  + "you're using a class in your plugin that you haven't provided bundle instructions for. "
55                  + "See https://developer.atlassian.com/x/mQAN for more details on how to fix this.";
56      }
57  
58      public String getPrefix() {
59          return "class";
60      }
61  }