1 package com.atlassian.plugin.util;
2
3 import com.atlassian.plugin.Application;
4 import com.atlassian.plugin.InstallationMode;
5 import com.atlassian.plugin.ModuleDescriptor;
6 import com.atlassian.plugin.Plugin;
7 import com.atlassian.plugin.descriptors.RequiresRestart;
8 import com.google.common.base.Function;
9 import com.google.common.base.Joiner;
10 import com.google.common.base.Objects;
11 import com.google.common.collect.Iterables;
12 import org.dom4j.Element;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21
22
23
24
25
26 public class PluginUtils
27 {
28 private static final Logger logger = LoggerFactory.getLogger(PluginUtils.class);
29
30 public static final String ATLASSIAN_DEV_MODE = "atlassian.dev.mode";
31
32
33
34
35
36
37
38 public static final String ATLASSIAN_PLUGINS_ENABLE_WAIT = "atlassian.plugins.enable.wait";
39
40
41
42
43
44
45 public static final String DEFAULT_ATLASSIAN_PLUGINS_ENABLE_WAIT_SECONDS = "60";
46
47
48
49
50
51
52
53
54 public static final String WEBRESOURCE_FILE_CACHE_SIZE = new String("atlassian.webresource.file.cache.size");
55
56
57
58
59
60
61
62 public static final String WEBRESOURCE_DISABLE_FILE_CACHE = new String("atlassian.webresource.file.cache.disable");
63
64
65
66
67
68
69
70
71
72 public static boolean doesPluginRequireRestart(final Plugin plugin)
73 {
74
75 if (Boolean.getBoolean(ATLASSIAN_DEV_MODE))
76 {
77 return false;
78 }
79
80 for (final ModuleDescriptor<?> descriptor : plugin.getModuleDescriptors())
81 {
82 if (descriptor.getClass().getAnnotation(RequiresRestart.class) != null)
83 {
84 return true;
85 }
86 }
87 return false;
88 }
89
90
91
92
93
94
95
96
97
98 public static Set<String> getPluginModulesThatRequireRestart(final Plugin plugin)
99 {
100 Set<String> keys = new HashSet<String>();
101 for (final ModuleDescriptor<?> descriptor : plugin.getModuleDescriptors())
102 {
103 if (descriptor.getClass().getAnnotation(RequiresRestart.class) != null)
104 {
105 keys.add(descriptor.getKey());
106 }
107 }
108 return keys;
109 }
110
111
112
113
114
115
116
117
118
119
120 public static boolean doesModuleElementApplyToApplication(Element element, Set<Application> applications, InstallationMode installationMode)
121 {
122 checkNotNull(element);
123 checkNotNull(applications);
124
125 final ModuleRestricts restricts = ModuleRestricts.parse(element);
126 final boolean valid = restricts.isValidFor(applications, installationMode);
127 if (!valid)
128 {
129 logger.debug("Module '{}' with key '{}' is restricted to the following " +
130 "applications {} and therefore does not apply to applications {}",
131 new Object[]{
132 element.getName(),
133 element.attributeValue("key"),
134 restricts,
135 asString(applications)
136 });
137 }
138 return valid;
139 }
140
141 private static String asString(Set<Application> applications)
142 {
143 return "[" + Joiner.on(",").join(Iterables.transform(applications, new Function<Application, String>()
144 {
145 @Override
146 public String apply(Application app)
147 {
148 return Objects.toStringHelper(app.getKey())
149 .add("version", app.getVersion())
150 .add("build", app.getBuildNumber())
151 .toString();
152 }
153 })) + "]";
154 }
155
156
157
158
159
160 public static int getDefaultEnablingWaitPeriod()
161 {
162 return Integer.parseInt(System.getProperty(ATLASSIAN_PLUGINS_ENABLE_WAIT,
163 DEFAULT_ATLASSIAN_PLUGINS_ENABLE_WAIT_SECONDS));
164 }
165 }