View Javadoc

1   package com.atlassian.plugin.loaders;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   
6   import java.util.*;
7   import java.net.URL;
8   import java.io.IOException;
9   
10  import com.atlassian.plugin.*;
11  import com.atlassian.plugin.util.ClassLoaderUtils;
12  
13  /**
14   * Loads plugins from the classpath
15   */
16  public class ClassPathPluginLoader implements PluginLoader
17  {
18      private static Log log = LogFactory.getLog(ClassPathPluginLoader.class);
19      List<Plugin> plugins;
20      String fileNameToLoad;
21  
22      public ClassPathPluginLoader()
23      {
24          this(PluginManager.PLUGIN_DESCRIPTOR_FILENAME);
25      }
26  
27      public ClassPathPluginLoader(String fileNameToLoad)
28      {
29          this.fileNameToLoad = fileNameToLoad;
30      }
31  
32      private void loadClassPathPlugins(ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException {
33          URL url = null;
34          final Enumeration pluginDescriptorFiles;
35          plugins = new ArrayList<Plugin>();
36          
37          try
38          {
39              pluginDescriptorFiles = ClassLoaderUtils.getResources(fileNameToLoad, this.getClass());
40          }
41          catch (IOException e)
42          {
43              log.error("Could not load classpath plugins: " + e, e);
44              return;
45          }
46  
47          while (pluginDescriptorFiles.hasMoreElements())
48          {
49              url = (URL) pluginDescriptorFiles.nextElement();
50              try
51              {
52                  SinglePluginLoader loader = new SinglePluginLoader(url.openConnection().getInputStream());
53                  plugins.addAll(loader.loadAllPlugins(moduleDescriptorFactory));
54              }
55              catch (IOException e)
56              {
57                  log.error("IOException parsing inputstream for : " + url, e);
58              }
59              catch (PluginParseException e)
60              {
61                  log.error("Unable to load plugin at url: " + url + ", " + e.getMessage(), e);
62              }
63          }
64      }
65  
66      public Collection<Plugin> loadAllPlugins(ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
67      {
68          if (plugins == null)
69          {
70              loadClassPathPlugins(moduleDescriptorFactory);
71          }
72  
73          return plugins;
74      }
75  
76      public boolean supportsRemoval()
77      {
78          return false;
79      }
80  
81      public boolean supportsAddition()
82      {
83          return false;
84      }
85  
86      public Collection<Plugin> addFoundPlugins(ModuleDescriptorFactory moduleDescriptorFactory)
87      {
88          throw new UnsupportedOperationException("This PluginLoader does not support addition.");
89      }
90  
91      public void removePlugin(Plugin plugin) throws PluginException
92      {
93          throw new PluginException("This PluginLoader does not support removal.");
94      }
95  }