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