View Javadoc

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