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      /**
16       * Creates an UnloadablePlugin instance from a given plugin, when there were problems loading the modules or the plugin itself
17       *
18       * @param oldPlugin the Plugin that is unloadable
19       * @return UnloadablePlugin instance
20       */
21      public static UnloadablePlugin createUnloadablePlugin(final Plugin oldPlugin)
22      {
23          return createUnloadablePlugin(oldPlugin, null);
24      }
25  
26      /**
27       * Creates an UnloadablePlugin instance from a given plugin.
28       *
29       * It also allows a problematic ModuleDescriptor to be passed in, which will replace the existing
30       * descriptor with the same key in the new plugin.
31       *
32       * @param oldPlugin the Plugin that is unloadable
33       * @param unloadableDescriptor the ModuleDescriptor containing the error
34       * @return UnloadablePlugin instance
35       */
36      public static UnloadablePlugin createUnloadablePlugin(final Plugin oldPlugin, final UnloadableModuleDescriptor unloadableDescriptor)
37      {
38          final UnloadablePlugin newPlugin = new UnloadablePlugin();
39  
40          newPlugin.setName(oldPlugin.getName());
41          newPlugin.setKey(oldPlugin.getKey());
42          newPlugin.setI18nNameKey(oldPlugin.getI18nNameKey());
43          newPlugin.setUninstallable(oldPlugin.isUninstallable());
44          newPlugin.setDeletable(oldPlugin.isDeleteable());
45  
46          // Make sure it's visible to the user
47          newPlugin.setSystemPlugin(false);
48  
49          newPlugin.setPluginInformation(oldPlugin.getPluginInformation());
50  
51          final List<ModuleDescriptor<?>> moduleDescriptors = new ArrayList<ModuleDescriptor<?>>(oldPlugin.getModuleDescriptors());
52  
53          for (final ModuleDescriptor<?> descriptor : moduleDescriptors)
54          {
55              // If we find the module descriptor that is causing the problem, skip it
56              if ((unloadableDescriptor != null) && descriptor.getKey().equals(unloadableDescriptor.getKey()))
57              {
58                  continue;
59              }
60              newPlugin.addModuleDescriptor(descriptor);
61          }
62  
63          // Add the unloadable descriptor to the end (if it exists)
64          if (unloadableDescriptor != null)
65          {
66              newPlugin.addModuleDescriptor(unloadableDescriptor);
67          }
68  
69          return newPlugin;
70      }
71  }