View Javadoc

1   package com.atlassian.plugin.impl;
2   
3   import static com.atlassian.plugin.util.concurrent.CopyOnWriteMap.newLinkedMap;
4   
5   import com.atlassian.plugin.*;
6   import com.atlassian.plugin.elements.ResourceDescriptor;
7   import com.atlassian.plugin.elements.ResourceLocation;
8   import com.atlassian.plugin.util.VersionStringComparator;
9   
10  import java.util.ArrayList;
11  import java.util.Collection;
12  import java.util.Date;
13  import java.util.List;
14  import java.util.Map;
15  
16  public abstract class AbstractPlugin implements Plugin, Comparable<Plugin>
17  {
18      private final Map<String, ModuleDescriptor<?>> modules = newLinkedMap();
19      private String name;
20      private String i18nNameKey;
21      private String key;
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 final Date dateLoaded = new Date();
29  
30      public String getName()
31      {
32          return name;
33      }
34  
35      public void setName(final String name)
36      {
37          this.name = name;
38      }
39  
40      public String getI18nNameKey()
41      {
42          return i18nNameKey;
43      }
44  
45      public void setI18nNameKey(final String i18nNameKey)
46      {
47          this.i18nNameKey = i18nNameKey;
48      }
49  
50      public String getKey()
51      {
52          return key;
53      }
54  
55      public void setKey(final String aPackage)
56      {
57          key = aPackage;
58      }
59  
60      public void addModuleDescriptor(final ModuleDescriptor<?> moduleDescriptor)
61      {
62          modules.put(moduleDescriptor.getKey(), moduleDescriptor);
63      }
64  
65      protected void removeModuleDescriptor(final String key)
66      {
67          modules.remove(key);
68      }
69  
70      /**
71       * Returns a copy of the module descriptors for this plugin
72       * @return A copy of the internal list
73       */
74      public Collection<ModuleDescriptor<?>> getModuleDescriptors()
75      {
76          return new ArrayList<ModuleDescriptor<?>>(modules.values());
77      }
78  
79      public ModuleDescriptor<?> getModuleDescriptor(final String key)
80      {
81          return modules.get(key);
82      }
83  
84      public <T> List<ModuleDescriptor<T>> getModuleDescriptorsByModuleClass(final Class<T> aClass)
85      {
86          final List<ModuleDescriptor<T>> result = new ArrayList<ModuleDescriptor<T>>();
87          for (final ModuleDescriptor<?> moduleDescriptor : modules.values())
88          {
89              final Class<?> moduleClass = moduleDescriptor.getModuleClass();
90              if (aClass.isAssignableFrom(moduleClass))
91              {
92                  @SuppressWarnings("unchecked")
93                  final ModuleDescriptor<T> typedModuleDescriptor = (ModuleDescriptor<T>) moduleDescriptor;
94                  result.add(typedModuleDescriptor);
95              }
96          }
97          return result;
98      }
99  
100     public PluginState getPluginState()
101     {
102         return (isEnabled() ? PluginState.ENABLED : PluginState.DISABLED);
103     }
104 
105     public boolean isEnabledByDefault()
106     {
107         return enabledByDefault && ((pluginInformation == null) || pluginInformation.satisfiesMinJavaVersion());
108     }
109 
110     public void setEnabledByDefault(final boolean enabledByDefault)
111     {
112         this.enabledByDefault = enabledByDefault;
113     }
114 
115     public int getPluginsVersion()
116     {
117         return pluginsVersion;
118     }
119 
120     public void setPluginsVersion(final int pluginsVersion)
121     {
122         this.pluginsVersion = pluginsVersion;
123     }
124 
125     public PluginInformation getPluginInformation()
126     {
127         return pluginInformation;
128     }
129 
130     public void setPluginInformation(final PluginInformation pluginInformation)
131     {
132         this.pluginInformation = pluginInformation;
133     }
134 
135     public void setResources(final Resourced resources)
136     {
137         this.resources = resources != null ? resources : Resources.EMPTY_RESOURCES;
138     }
139 
140     public List<ResourceDescriptor> getResourceDescriptors()
141     {
142         return resources.getResourceDescriptors();
143     }
144 
145     public List<ResourceDescriptor> getResourceDescriptors(final String type)
146     {
147         return resources.getResourceDescriptors(type);
148     }
149 
150     public ResourceLocation getResourceLocation(final String type, final String name)
151     {
152         return resources.getResourceLocation(type, name);
153     }
154 
155     /**
156      * @deprecated
157      */
158     @Deprecated
159     public ResourceDescriptor getResourceDescriptor(final String type, final String name)
160     {
161         return resources.getResourceDescriptor(type, name);
162     }
163 
164     /**
165      * @return true if the plugin has been enabled
166      */
167     public boolean isEnabled()
168     {
169         return enabled;
170     }
171 
172     /**
173      * Setter for the enabled state of a plugin. If this is set to false then the plugin will not execute.
174      */
175     public void setEnabled(final boolean enabled)
176     {
177         this.enabled = enabled;
178     }
179 
180     public boolean isSystemPlugin()
181     {
182         return system;
183     }
184 
185     public boolean containsSystemModule()
186     {
187         for (final ModuleDescriptor<?> moduleDescriptor : modules.values())
188         {
189             if (moduleDescriptor.isSystemModule())
190             {
191                 return true;
192             }
193         }
194         return false;
195     }
196 
197     public void setSystemPlugin(final boolean system)
198     {
199         this.system = system;
200     }
201 
202     public Date getDateLoaded()
203     {
204         return dateLoaded;
205     }
206 
207     /**
208      * Plugins with the same key are compared by version number, using {@link VersionStringComparator}.
209      * If the other plugin has a different key, this method returns <tt>1</tt>.
210      *
211      * @return <tt>-1</tt> if the other plugin is newer, <tt>0</tt> if equal,
212      * <tt>1</tt> if the other plugin is older or has a different plugin key.
213      */
214     public int compareTo(final Plugin otherPlugin)
215     {
216         if (otherPlugin.getKey() == null)
217         {
218             return 1;
219         }
220         if (getKey() == null)
221         {
222             return -1;
223         }
224 
225         // If the compared plugin doesn't have the same key, the current object is greater
226         if (!otherPlugin.getKey().equals(getKey()))
227         {
228             return getKey().compareTo(otherPlugin.getKey());
229         }
230 
231         final String thisVersion = cleanVersionString((getPluginInformation() != null ? getPluginInformation().getVersion() : null));
232         final String otherVersion = cleanVersionString((otherPlugin.getPluginInformation() != null ? otherPlugin.getPluginInformation().getVersion() : null));
233 
234         if (!VersionStringComparator.isValidVersionString(thisVersion))
235         {
236             return -1;
237         }
238         if (!VersionStringComparator.isValidVersionString(otherVersion))
239         {
240             return -1;
241         }
242 
243         return new VersionStringComparator().compare(thisVersion, otherVersion);
244     }
245 
246     private String cleanVersionString(final String version)
247     {
248         if ((version == null) || version.trim().equals(""))
249         {
250             return "0";
251         }
252         return version.replaceAll(" ", "");
253     }
254 
255     @Override
256     public String toString()
257     {
258         final PluginInformation info = getPluginInformation();
259         return getKey() + ":" + (info == null ? "?" : info.getVersion());
260     }
261 }