View Javadoc
1   package com.atlassian.plugin.impl;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.Plugin;
5   import com.atlassian.plugin.descriptors.UnloadableModuleDescriptor;
6   
7   import java.util.ArrayList;
8   import java.util.List;
9   
10  /**
11   * Utility class to create UnloadablePlugin instances.
12   */
13  public final class UnloadablePluginFactory {
14      /**
15       * Creates an UnloadablePlugin instance from a given plugin, when there were problems loading the modules or the plugin itself
16       *
17       * @param oldPlugin the Plugin that is unloadable
18       * @return UnloadablePlugin instance
19       */
20      public static UnloadablePlugin createUnloadablePlugin(final Plugin oldPlugin) {
21          return createUnloadablePlugin(oldPlugin, null);
22      }
23  
24      /**
25       * Creates an UnloadablePlugin instance from a given plugin.
26       *
27       * It also allows a problematic ModuleDescriptor to be passed in, which will replace the existing
28       * descriptor with the same key in the new plugin.
29       *
30       * @param oldPlugin            the Plugin that is unloadable
31       * @param unloadableDescriptor the ModuleDescriptor containing the error
32       * @return UnloadablePlugin instance
33       */
34      public static UnloadablePlugin createUnloadablePlugin(final Plugin oldPlugin, final UnloadableModuleDescriptor unloadableDescriptor) {
35          final UnloadablePlugin newPlugin = new UnloadablePlugin();
36  
37          newPlugin.setName(oldPlugin.getName());
38          newPlugin.setKey(oldPlugin.getKey());
39          newPlugin.setI18nNameKey(oldPlugin.getI18nNameKey());
40          newPlugin.setUninstallable(oldPlugin.isUninstallable());
41          newPlugin.setDeletable(oldPlugin.isDeleteable());
42          newPlugin.setPluginsVersion(oldPlugin.getPluginsVersion());
43          newPlugin.setDynamicallyLoaded(oldPlugin.isDynamicallyLoaded());
44  
45          // Make sure it's visible to the user
46          newPlugin.setSystemPlugin(false);
47  
48          newPlugin.setPluginInformation(oldPlugin.getPluginInformation());
49  
50          final List<ModuleDescriptor<?>> moduleDescriptors = new ArrayList<ModuleDescriptor<?>>(oldPlugin.getModuleDescriptors());
51  
52          for (final ModuleDescriptor<?> descriptor : moduleDescriptors) {
53              // If we find the module descriptor that is causing the problem, skip it
54              if ((unloadableDescriptor != null) && descriptor.getKey().equals(unloadableDescriptor.getKey())) {
55                  continue;
56              }
57              newPlugin.addModuleDescriptor(descriptor);
58          }
59  
60          // Add the unloadable descriptor to the end (if it exists)
61          if (unloadableDescriptor != null) {
62              newPlugin.addModuleDescriptor(unloadableDescriptor);
63          }
64  
65          return newPlugin;
66      }
67  }