Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BuddiPluginFactory |
|
| 9.25;9.25 | ||||
BuddiPluginFactory$1 |
|
| 9.25;9.25 |
1 | /* | |
2 | * Created on Aug 12, 2007 by wyatt | |
3 | */ | |
4 | package org.homeunix.thecave.buddi.plugin; | |
5 | ||
6 | import java.io.File; | |
7 | import java.io.FileFilter; | |
8 | import java.io.IOException; | |
9 | import java.io.InputStream; | |
10 | import java.util.HashMap; | |
11 | import java.util.LinkedList; | |
12 | import java.util.List; | |
13 | import java.util.Map; | |
14 | import java.util.Properties; | |
15 | import java.util.logging.Logger; | |
16 | ||
17 | import org.homeunix.thecave.buddi.Buddi; | |
18 | import org.homeunix.thecave.buddi.Const; | |
19 | import org.homeunix.thecave.buddi.plugin.api.BuddiExportPlugin; | |
20 | import org.homeunix.thecave.buddi.plugin.api.BuddiImportPlugin; | |
21 | import org.homeunix.thecave.buddi.plugin.api.BuddiPanelPlugin; | |
22 | import org.homeunix.thecave.buddi.plugin.api.BuddiPreferencePlugin; | |
23 | import org.homeunix.thecave.buddi.plugin.api.BuddiReportPlugin; | |
24 | import org.homeunix.thecave.buddi.plugin.api.BuddiRunnablePlugin; | |
25 | import org.homeunix.thecave.buddi.plugin.api.BuddiSynchronizePlugin; | |
26 | import org.homeunix.thecave.buddi.plugin.api.BuddiTransactionCellRendererPlugin; | |
27 | ||
28 | import ca.digitalcave.moss.application.plugin.MossPlugin; | |
29 | import ca.digitalcave.moss.application.plugin.factory.PluginFactory; | |
30 | import ca.digitalcave.moss.common.ClassLoaderFunctions; | |
31 | ||
32 | 0 | public class BuddiPluginFactory extends PluginFactory { |
33 | ||
34 | 2986 | private static final Map<Class<? extends MossPlugin>, List<MossPlugin>> pluginMap = new HashMap<Class<? extends MossPlugin>, List<MossPlugin>>(); |
35 | ||
36 | /** | |
37 | * Removes all the plugins from the cache. Next time the plugins are | |
38 | * accessed, we will reload from disk. | |
39 | */ | |
40 | public static void forcePluginRefresh(){ | |
41 | 43 | pluginMap.clear(); |
42 | 43 | } |
43 | ||
44 | /** | |
45 | * Returns all plugins, both in the classpath and in plugin dirs, of the given type. | |
46 | * @param pluginType | |
47 | * @return | |
48 | */ | |
49 | public static List<? extends MossPlugin> getPlugins(Class<? extends MossPlugin> pluginType){ | |
50 | 308850 | if (pluginMap.get(pluginType) == null){ |
51 | 19432 | pluginMap.put(pluginType, new LinkedList<MossPlugin>()); |
52 | ||
53 | //Load built in plugins | |
54 | String[] builtIn; | |
55 | 19432 | if (pluginType.getName().equals(BuddiReportPlugin.class.getName())) |
56 | 2977 | builtIn = Const.BUILT_IN_REPORTS; |
57 | 16455 | else if (pluginType.getName().equals(BuddiImportPlugin.class.getName())) |
58 | 3020 | builtIn = Const.BUILT_IN_IMPORTS; |
59 | 13435 | else if (pluginType.getName().equals(BuddiExportPlugin.class.getName())) |
60 | 3020 | builtIn = Const.BUILT_IN_EXPORTS; |
61 | 10415 | else if (pluginType.getName().equals(BuddiPreferencePlugin.class.getName())) |
62 | 695 | builtIn = Const.BUILT_IN_PREFERENCE_PANELS; |
63 | 9720 | else if (pluginType.getName().equals(BuddiRunnablePlugin.class.getName())) |
64 | 2986 | builtIn = Const.BUILT_IN_RUNNABLES; |
65 | 6734 | else if (pluginType.getName().equals(BuddiSynchronizePlugin.class.getName())) |
66 | 3020 | builtIn = Const.BUILT_IN_SYNCHRONIZES; |
67 | 3714 | else if (pluginType.getName().equals(BuddiTransactionCellRendererPlugin.class.getName())) |
68 | 695 | builtIn = Const.BUILT_IN_TRANSACTION_CELL_RENDERERS; |
69 | 3019 | else if (pluginType.getName().equals(BuddiPanelPlugin.class.getName())) |
70 | 3019 | builtIn = Const.BUILT_IN_PANELS; |
71 | else { | |
72 | 0 | builtIn = new String[0]; |
73 | 0 | Logger.getLogger(BuddiPluginFactory.class.getName()).warning("Unknown plugin type: " + pluginType.getName()); |
74 | } | |
75 | ||
76 | 40572 | for (String className : builtIn){ |
77 | 21140 | MossPlugin plugin = BuddiPluginFactory.getValidPluginFromClasspath(className); |
78 | 21140 | if (pluginType.isInstance(plugin)){ |
79 | 21140 | pluginMap.get(pluginType).add(plugin); |
80 | } | |
81 | } | |
82 | ||
83 | //Load user defined plugins | |
84 | 19432 | File[] plugins = Buddi.getPluginsFolder().listFiles(pluginFilter); |
85 | 19432 | if (plugins != null){ |
86 | 19432 | for (File pluginFile : plugins){ |
87 | 0 | Properties props = new Properties(); |
88 | try { | |
89 | 0 | InputStream is = ClassLoaderFunctions.getResourceAsStreamFromJar(pluginFile, "/" + Const.PLUGIN_PROPERTIES); |
90 | 0 | if (is != null) |
91 | 0 | props.load(is); |
92 | } | |
93 | 0 | catch (IOException ioe){} |
94 | 0 | for (MossPlugin plugin : getMossPluginsFromJar(pluginFile, Buddi.getVersion(), props.getProperty(Const.PLUGIN_PROPERTIES_ROOT))) { |
95 | 0 | if (pluginType.isInstance(plugin)){ |
96 | 0 | pluginMap.get(pluginType).add((MossPlugin) plugin); |
97 | } | |
98 | } | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | 308850 | return pluginMap.get(pluginType); |
104 | } | |
105 | ||
106 | 2986 | public static final FileFilter pluginFilter = new FileFilter(){ |
107 | public boolean accept(File pathname) { | |
108 | 0 | if (pathname.getName().endsWith(Const.PLUGIN_EXTENSION) |
109 | && pathname.isFile()) | |
110 | 0 | return true; |
111 | 0 | return false; |
112 | } | |
113 | }; | |
114 | ||
115 | public static List<File> getPluginFiles(){ | |
116 | 3724 | List<File> pluginFiles = new LinkedList<File>(); |
117 | ||
118 | //Loop through all files fuond in the plugins folder, and see if they contain a valid | |
119 | // plugin. If so, add it to the list. This time (Buddi 3), we focus on the plugin file | |
120 | // itself, rather than each plugin class within it. | |
121 | 3724 | File[] plugins = Buddi.getPluginsFolder().listFiles(pluginFilter); |
122 | 3724 | if (plugins != null){ |
123 | 3724 | for (File pluginFile : plugins){ |
124 | 0 | Properties props = new Properties(); |
125 | try { | |
126 | 0 | InputStream is = ClassLoaderFunctions.getResourceAsStreamFromJar(pluginFile, "/" + Const.PLUGIN_PROPERTIES); |
127 | 0 | if (is != null) |
128 | 0 | props.load(is); |
129 | } | |
130 | 0 | catch (IOException ioe){} |
131 | 0 | for (MossPlugin plugin : getMossPluginsFromJar(pluginFile, Buddi.getVersion(), props.getProperty(Const.PLUGIN_PROPERTIES_ROOT))) { |
132 | 0 | if (plugin instanceof BuddiExportPlugin |
133 | || plugin instanceof BuddiImportPlugin | |
134 | || plugin instanceof BuddiSynchronizePlugin | |
135 | || plugin instanceof BuddiPreferencePlugin | |
136 | || plugin instanceof BuddiReportPlugin | |
137 | || plugin instanceof BuddiRunnablePlugin | |
138 | || plugin instanceof BuddiTransactionCellRendererPlugin | |
139 | || plugin instanceof BuddiPanelPlugin){ | |
140 | 0 | pluginFiles.add(pluginFile); |
141 | 0 | break; |
142 | } | |
143 | } | |
144 | } | |
145 | } | |
146 | ||
147 | 3724 | return pluginFiles; |
148 | } | |
149 | } |