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
16
17
18
19
20
21
22
23
24
25
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 }