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          newPlugin.setPluginsVersion(oldPlugin.getPluginsVersion());
46          newPlugin.setDynamicallyLoaded(oldPlugin.isDynamicallyLoaded());
47          
48          // Make sure it's visible to the user
49          newPlugin.setSystemPlugin(false);
50  
51          newPlugin.setPluginInformation(oldPlugin.getPluginInformation());
52  
53          final List<ModuleDescriptor<?>> moduleDescriptors = new ArrayList<ModuleDescriptor<?>>(oldPlugin.getModuleDescriptors());
54  
55          for (final ModuleDescriptor<?> descriptor : moduleDescriptors)
56          {
57              // If we find the module descriptor that is causing the problem, skip it
58              if ((unloadableDescriptor != null) && descriptor.getKey().equals(unloadableDescriptor.getKey()))
59              {
60                  continue;
61              }
62              newPlugin.addModuleDescriptor(descriptor);
63          }
64  
65          // Add the unloadable descriptor to the end (if it exists)
66          if (unloadableDescriptor != null)
67          {
68              newPlugin.addModuleDescriptor(unloadableDescriptor);
69          }
70  
71          return newPlugin;
72      }
73  }