1 package com.atlassian.plugin.impl;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Date;
6 import java.util.LinkedHashMap;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.concurrent.ConcurrentHashMap;
10
11 import com.atlassian.plugin.*;
12 import com.atlassian.plugin.elements.ResourceDescriptor;
13 import com.atlassian.plugin.elements.ResourceLocation;
14 import com.atlassian.plugin.util.VersionStringComparator;
15
16 public abstract class AbstractPlugin implements Plugin, Comparable<Plugin>
17 {
18 private String name;
19 private String i18nNameKey;
20 private String key;
21 private Map<String,ModuleDescriptor<?>> modules = new LinkedHashMap<String,ModuleDescriptor<?>>();
22 private boolean enabledByDefault = true;
23 private PluginInformation pluginInformation = new PluginInformation();
24 private boolean enabled;
25 private boolean system;
26 private Resourced resources = Resources.EMPTY_RESOURCES;
27 private int pluginsVersion = 1;
28 private Date dateLoaded = new Date();
29
30 public String getName()
31 {
32 return name;
33 }
34
35 public void setName(String name)
36 {
37 this.name = name;
38 }
39
40 public String getI18nNameKey()
41 {
42 return i18nNameKey;
43 }
44
45 public void setI18nNameKey(String i18nNameKey)
46 {
47 this.i18nNameKey = i18nNameKey;
48 }
49
50 public String getKey()
51 {
52 return key;
53 }
54
55 public void setKey(String aPackage)
56 {
57 this.key = aPackage;
58 }
59
60 public void addModuleDescriptor(ModuleDescriptor<?> moduleDescriptor)
61 {
62 modules.put(moduleDescriptor.getKey(), moduleDescriptor);
63 }
64
65 protected void removeModuleDescriptor(String key)
66 {
67 modules.remove(key);
68 }
69
70
71
72
73
74 public Collection<ModuleDescriptor<?>> getModuleDescriptors()
75 {
76 return new ArrayList<ModuleDescriptor<?>>(modules.values());
77 }
78
79 public ModuleDescriptor<?> getModuleDescriptor(String key)
80 {
81 return modules.get(key);
82 }
83
84 public <T> List<ModuleDescriptor<T>> getModuleDescriptorsByModuleClass(Class<T> aClass)
85 {
86 List<ModuleDescriptor<T>> result = new ArrayList<ModuleDescriptor<T>>();
87
88 for (ModuleDescriptor moduleDescriptor : modules.values())
89 {
90 Class moduleClass = moduleDescriptor.getModuleClass();
91 if (aClass.isAssignableFrom(moduleClass))
92 {
93 result.add((ModuleDescriptor<T>) moduleDescriptor);
94 }
95 }
96
97 return result;
98 }
99
100 public boolean isEnabledByDefault()
101 {
102 return enabledByDefault && (pluginInformation == null || pluginInformation.satisfiesMinJavaVersion());
103 }
104
105 public void setEnabledByDefault(boolean enabledByDefault)
106 {
107 this.enabledByDefault = enabledByDefault;
108 }
109
110 public int getPluginsVersion()
111 {
112 return pluginsVersion;
113 }
114
115 public void setPluginsVersion(int pluginsVersion)
116 {
117 this.pluginsVersion = pluginsVersion;
118 }
119
120 public PluginInformation getPluginInformation()
121 {
122 return pluginInformation;
123 }
124
125 public void setPluginInformation(PluginInformation pluginInformation)
126 {
127 this.pluginInformation = pluginInformation;
128 }
129
130 public void setResources(Resourced resources)
131 {
132 this.resources = resources != null ? resources : Resources.EMPTY_RESOURCES;
133 }
134
135 public List getResourceDescriptors()
136 {
137 return resources.getResourceDescriptors();
138 }
139
140 public List getResourceDescriptors(String type)
141 {
142 return resources.getResourceDescriptors(type);
143 }
144
145 public ResourceLocation getResourceLocation(String type, String name)
146 {
147 return resources.getResourceLocation(type, name);
148 }
149
150
151
152
153 public ResourceDescriptor getResourceDescriptor(String type, String name)
154 {
155 return resources.getResourceDescriptor(type, name);
156 }
157
158
159
160
161 public boolean isEnabled()
162 {
163 return enabled;
164 }
165
166
167
168
169 public void setEnabled(boolean enabled)
170 {
171 this.enabled = enabled;
172 }
173
174 public boolean isSystemPlugin()
175 {
176 return system;
177 }
178
179 public boolean containsSystemModule()
180 {
181 for (ModuleDescriptor moduleDescriptor : modules.values())
182 {
183 if(moduleDescriptor.isSystemModule())
184 {
185 return true;
186 }
187 }
188 return false;
189 }
190
191 public void setSystemPlugin(boolean system)
192 {
193 this.system = system;
194 }
195
196 public Date getDateLoaded()
197 {
198 return dateLoaded;
199 }
200
201
202
203
204
205
206
207
208 public int compareTo(Plugin otherPlugin)
209 {
210
211 if (!otherPlugin.getKey().equals(this.getKey())) return getKey().compareTo(otherPlugin.getKey());
212
213 String thisVersion = cleanVersionString(
214 (this.getPluginInformation() != null ? this.getPluginInformation().getVersion() : null));
215 String otherVersion = cleanVersionString(
216 (otherPlugin.getPluginInformation() != null ? otherPlugin.getPluginInformation().getVersion() : null));
217
218 if (!VersionStringComparator.isValidVersionString(thisVersion)) return -1;
219 if (!VersionStringComparator.isValidVersionString(otherVersion)) return -1;
220
221 return new VersionStringComparator().compare(thisVersion, otherVersion);
222 }
223
224 private String cleanVersionString(String version)
225 {
226 if (version == null || version.trim().equals("")) return "0";
227 return version.replaceAll(" ", "");
228 }
229
230 public String toString()
231 {
232 final PluginInformation info = getPluginInformation();
233 return getKey() + ":" + (info == null ? "?" : info.getVersion());
234 }
235 }