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.Iterator;
7 import java.util.LinkedHashMap;
8 import java.util.List;
9 import java.util.Map;
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
17 {
18 private String name;
19 private String i18nNameKey;
20 private String key;
21 private Map modules = new LinkedHashMap();
22 private boolean enabledByDefault = true;
23 private PluginInformation 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 public Collection getModuleDescriptors()
66 {
67 return modules.values();
68 }
69
70 public ModuleDescriptor getModuleDescriptor(String key)
71 {
72 return (ModuleDescriptor) modules.get(key);
73 }
74
75 public List getModuleDescriptorsByModuleClass(Class aClass)
76 {
77 List result = new ArrayList();
78
79 for (Iterator iterator = modules.values().iterator(); iterator.hasNext();)
80 {
81 ModuleDescriptor moduleDescriptor = (ModuleDescriptor) iterator.next();
82
83 Class moduleClass = moduleDescriptor.getModuleClass();
84 if (aClass.isAssignableFrom(moduleClass))
85 {
86 result.add(moduleDescriptor);
87 }
88 }
89
90 return result;
91 }
92
93 public boolean isEnabledByDefault()
94 {
95 return enabledByDefault && (pluginInformation == null || pluginInformation.satisfiesMinJavaVersion());
96 }
97
98 public void setEnabledByDefault(boolean enabledByDefault)
99 {
100 this.enabledByDefault = enabledByDefault;
101 }
102
103 public int getPluginsVersion()
104 {
105 return pluginsVersion;
106 }
107
108 public void setPluginsVersion(int pluginsVersion)
109 {
110 this.pluginsVersion = pluginsVersion;
111 }
112
113 public PluginInformation getPluginInformation()
114 {
115 return pluginInformation;
116 }
117
118 public void setPluginInformation(PluginInformation pluginInformation)
119 {
120 this.pluginInformation = pluginInformation;
121 }
122
123 public void setResources(Resourced resources)
124 {
125 this.resources = resources != null ? resources : Resources.EMPTY_RESOURCES;
126 }
127
128 public List getResourceDescriptors()
129 {
130 return resources.getResourceDescriptors();
131 }
132
133 public List getResourceDescriptors(String type)
134 {
135 return resources.getResourceDescriptors(type);
136 }
137
138 public ResourceLocation getResourceLocation(String type, String name)
139 {
140 return resources.getResourceLocation(type, name);
141 }
142
143
144
145
146 public ResourceDescriptor getResourceDescriptor(String type, String name)
147 {
148 return resources.getResourceDescriptor(type, name);
149 }
150
151
152
153
154 public boolean isEnabled()
155 {
156 return enabled;
157 }
158
159
160
161
162 public void setEnabled(boolean enabled)
163 {
164 this.enabled = enabled;
165 }
166
167 public boolean isSystemPlugin()
168 {
169 return system;
170 }
171
172 public boolean containsSystemModule()
173 {
174 for (Iterator iterator = modules.values().iterator(); iterator.hasNext();)
175 {
176 ModuleDescriptor moduleDescriptor = (ModuleDescriptor) iterator.next();
177 if(moduleDescriptor.isSystemModule())
178 {
179 return true;
180 }
181 }
182 return false;
183 }
184
185 public void setSystemPlugin(boolean system)
186 {
187 this.system = system;
188 }
189
190 public Date getDateLoaded()
191 {
192 return dateLoaded;
193 }
194
195
196
197
198
199
200
201
202 public int compareTo(Object other)
203 {
204
205 if (!(other instanceof Plugin)) return 1;
206 Plugin otherPlugin = (Plugin) other;
207
208
209 if (!otherPlugin.getKey().equals(this.getKey())) return 1;
210
211 String thisVersion = cleanVersionString(this.getPluginInformation().getVersion());
212 String otherVersion = cleanVersionString(otherPlugin.getPluginInformation().getVersion());
213
214 if (!VersionStringComparator.isValidVersionString(thisVersion)) return -1;
215 if (!VersionStringComparator.isValidVersionString(otherVersion)) return -1;
216
217 return new VersionStringComparator().compare(thisVersion, otherVersion);
218 }
219
220 private String cleanVersionString(String version)
221 {
222 if (version == null || version.trim().equals("")) return "0";
223 return version.replaceAll(" ", "");
224 }
225
226 }