View Javadoc

1   package com.atlassian.plugin.util;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.Plugin;
5   import com.atlassian.plugin.descriptors.RequiresRestart;
6   import com.google.common.collect.Sets;
7   import org.apache.commons.lang.Validate;
8   import org.dom4j.Element;
9   
10  import java.util.HashSet;
11  import java.util.Set;
12  
13  /**
14   * General plugin utility methods
15   *
16   * @since 2.1
17   */
18  public class PluginUtils
19  {
20      public static final String ATLASSIAN_DEV_MODE = "atlassian.dev.mode";
21  
22      /**
23       * System property for storing and retrieving the time the plugin system will wait for the enabling of a plugin in
24       * seconds
25       * @since 2.3.6
26       */
27      public static final String ATLASSIAN_PLUGINS_ENABLE_WAIT = "atlassian.plugins.enable.wait";
28  
29      /**
30       * Determines if a plugin requires a restart after being installed at runtime.  Looks for the annotation
31       * {@link RequiresRestart} on the plugin's module descriptors.
32       *
33       * @param plugin The plugin that was just installed at runtime, but not yet enabled
34       * @return True if a restart is required
35       * @since 2.1
36       */
37      public static boolean doesPluginRequireRestart(final Plugin plugin)
38      {
39          //PLUG-451: When in dev mode, plugins should not require a restart.
40          if (Boolean.getBoolean(ATLASSIAN_DEV_MODE))
41          {
42              return false;
43          }
44  
45          for (final ModuleDescriptor<?> descriptor : plugin.getModuleDescriptors())
46          {
47              if (descriptor.getClass().getAnnotation(RequiresRestart.class) != null)
48              {
49                  return true;
50              }
51          }
52          return false;
53      }
54  
55      /**
56       * Gets a list of all the module keys in a plugin that require restart.  Looks for the annotation
57       * {@link RequiresRestart} on the plugin's module descriptors.
58       *
59       * @param plugin The plugin
60       * @return A unique set of module keys
61       * @since 2.5.0
62       */
63      public static Set<String> getPluginModulesThatRequireRestart(final Plugin plugin)
64      {
65          Set<String> keys = new HashSet<String>();
66          for (final ModuleDescriptor<?> descriptor : plugin.getModuleDescriptors())
67          {
68              if (descriptor.getClass().getAnnotation(RequiresRestart.class) != null)
69              {
70                  keys.add(descriptor.getKey());
71              }
72          }
73          return keys;
74      }
75  
76      /**
77       * Determines if a module element applies to the current application by matching the 'application' attribute
78       * to the set of keys.  If the application is specified, but isn't in the set, we return false
79       * @param element The module element
80       * @param keys The set of application keys
81       * @return True if it should apply, false otherwise
82       * @since 2.2.0
83       */
84      public static boolean doesModuleElementApplyToApplication(Element element, Set<String> keys)
85      {
86          Validate.notNull(keys);
87          Validate.notNull(element);
88          String keyList = element.attributeValue("application");
89          if (keyList == null) {
90              return true;
91          }
92          final String[] split = keyList.split("\\s*,[,\\s]*");
93          if (split.length == 0 || (split.length == 1 && split[0].trim().length() == 0)) {
94              return true;
95          }
96          for (final String key : split) {
97              if (keys.contains(key)) {
98                  return true;
99              }
100         }
101         return false;
102     }
103 
104     /**
105      * @return The default enabling waiting period in seconds
106      * @since 2.3.6
107      */
108     public static int getDefaultEnablingWaitPeriod()
109     {
110         return Integer.parseInt(System.getProperty(ATLASSIAN_PLUGINS_ENABLE_WAIT, "60"));
111     }
112 }