View Javadoc

1   package com.atlassian.plugin.loaders;
2   
3   import com.atlassian.plugin.*;
4   import com.atlassian.plugin.parsers.DescriptorParser;
5   import com.atlassian.plugin.parsers.XmlDescriptorParserFactory;
6   import com.atlassian.plugin.parsers.DescriptorParserFactory;
7   import com.atlassian.plugin.util.ClassLoaderUtils;
8   import com.atlassian.plugin.impl.StaticPlugin;
9   
10  import java.io.InputStream;
11  import java.util.Collection;
12  import java.util.Collections;
13  
14  /**
15   * Loads a single plugin from the descriptor provided, which can either be an InputStream
16   * or a resource on the classpath. The classes used by the plugin must already be available
17   * on the classpath because this plugin loader does <b>not</b> load any classes.
18   * <p/>
19   * Because the code which is run by these plugins must already be in the classpath (and
20   * is therefore more trusted than an uploaded plugin), if the plugin is marked as a system
21   * plugin in the descriptor file, it will actually be marked as a system plugin at runtime.
22   *
23   * @see PluginLoader
24   * @see ClassPathPluginLoader
25   * @see DescriptorParser#isSystemPlugin()
26   */
27  public class SinglePluginLoader implements PluginLoader
28  {
29      protected Collection<Plugin> plugins;
30      protected String resource;
31      protected InputStream is;
32      private DescriptorParserFactory descriptorParserFactory = new XmlDescriptorParserFactory();
33  
34      public SinglePluginLoader(String resource)
35      {
36          this.resource = resource;
37      }
38  
39      public SinglePluginLoader(InputStream is)
40      {
41          this.is = is;
42      }
43  
44      public Collection<Plugin> loadAllPlugins(ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
45      {
46          if (plugins == null)
47              plugins = Collections.singleton(loadPlugin(moduleDescriptorFactory));
48          return plugins;
49      }
50  
51      public boolean supportsRemoval()
52      {
53          return false;
54      }
55  
56      public boolean supportsAddition()
57      {
58          return false;
59      }
60  
61      public Collection<Plugin> addFoundPlugins(ModuleDescriptorFactory moduleDescriptorFactory)
62      {
63          throw new UnsupportedOperationException("This PluginLoader does not support addition.");
64      }
65  
66      public void removePlugin(Plugin plugin) throws PluginException
67      {
68          throw new PluginException("This PluginLoader does not support removal.");
69      }
70  
71      protected Plugin loadPlugin(ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
72      {
73          InputStream source = getSource();
74          if (source == null)
75              throw new PluginParseException("Invalid resource or inputstream specified to load plugins from.");
76  
77          Plugin plugin;
78          try
79          {
80              DescriptorParser parser = descriptorParserFactory.getInstance(source);
81              plugin = parser.configurePlugin(moduleDescriptorFactory, getNewPlugin());
82              if (parser.isSystemPlugin())
83                  plugin.setSystemPlugin(true);
84          }
85          catch (PluginParseException e)
86          {
87              throw new PluginParseException("Unable to load plugin resource: " + resource + " - " + e.getMessage() ,e);
88          }
89  
90          return plugin;
91      }
92  
93      protected StaticPlugin getNewPlugin()
94      {
95          return new StaticPlugin();
96      }
97  
98      protected InputStream getSource()
99      {
100         if (resource == null)
101             return is;
102 
103         return ClassLoaderUtils.getResourceAsStream(resource, this.getClass());
104     }
105 }