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 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 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 removeMissingPlugins()
62      {
63          throw new UnsupportedOperationException("This PluginLoader does not support removal.");
64      }
65  
66      public Collection addFoundPlugins(ModuleDescriptorFactory moduleDescriptorFactory)
67      {
68          throw new UnsupportedOperationException("This PluginLoader does not support addition.");
69      }
70  
71      public void removePlugin(Plugin plugin) throws PluginException
72      {
73          throw new PluginException("This PluginLoader does not support removal.");
74      }
75  
76      protected Plugin loadPlugin(ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
77      {
78          InputStream source = getSource();
79          if (source == null)
80              throw new PluginParseException("Invalid resource or inputstream specified to load plugins from.");
81  
82          Plugin plugin;
83          try
84          {
85              DescriptorParser parser = descriptorParserFactory.getInstance(source);
86              plugin = parser.configurePlugin(moduleDescriptorFactory, getNewPlugin());
87              if (parser.isSystemPlugin())
88                  plugin.setSystemPlugin(true);
89          }
90          catch (PluginParseException e)
91          {
92              throw new PluginParseException("Unable to load plugin resource: " + resource + " - " + e.getMessage() ,e);
93          }
94  
95          return plugin;
96      }
97  
98      protected StaticPlugin getNewPlugin()
99      {
100         return new StaticPlugin();
101     }
102 
103     protected InputStream getSource()
104     {
105         if (resource == null)
106             return is;
107 
108         return ClassLoaderUtils.getResourceAsStream(resource, this.getClass());
109     }
110 }