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
7 import java.util.Set;
8
9 import org.dom4j.Element;
10 import org.apache.commons.lang.Validate;
11
12 /**
13 * General plugin utility methods
14 *
15 * @since 2.1
16 */
17 public class PluginUtils
18 {
19 /**
20 * Determines if a plugin requires a restart after being installed at runtime. Looks for the annotation
21 * {@link RequiresRestart} on the plugin's module descriptors.
22 *
23 * @param plugin The plugin that was just installed at runtime, but not yet enabled
24 * @return True if a restart is required
25 * @since 2.1
26 */
27 public static boolean doesPluginRequireRestart(final Plugin plugin)
28 {
29 for (final ModuleDescriptor<?> descriptor : plugin.getModuleDescriptors())
30 {
31 if (descriptor.getClass().getAnnotation(RequiresRestart.class) != null)
32 {
33 return true;
34 }
35 }
36 return false;
37 }
38
39 /**
40 * Determines if a module element applies to the current application by matching the 'application' attribute
41 * to the set of keys. If the application is specified, but isn't in the set, we return false
42 * @param element The module element
43 * @param keys The set of application keys
44 * @return True if it should apply, false otherwise
45 * @since 2.2.0
46 */
47 public static boolean doesModuleElementApplyToApplication(Element element, Set<String> keys)
48 {
49 Validate.notNull(keys);
50 Validate.notNull(element);
51 String key = element.attributeValue("application");
52 return !(key != null && !keys.contains(key));
53 }
54 }