1 package net.sf.logdistiller;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.util.*;
18
19 import net.sf.logdistiller.plugins.BasePlugins;
20 import net.sf.logdistiller.util.ExtensionHelper;
21 import net.sf.logdistiller.util.FormatUtil;
22
23
24
25
26
27
28
29
30
31
32
33 public abstract class Plugins
34 {
35
36
37
38
39
40
41 public abstract List<Plugin> definePlugins();
42
43 public static final List<Plugin> ALL;
44
45 public static final Map<String, Plugin> MAP;
46 static
47 {
48 List<Plugin> all = new ArrayList<Plugin>( new BasePlugins().definePlugins() );
49 all.addAll( loadExtensionPlugins() );
50 ALL = Collections.unmodifiableList( all );
51
52 Map<String, Plugin> map = new HashMap<String, Plugin>();
53 for ( Plugin plugin : ALL )
54 {
55 map.put( plugin.getId(), plugin );
56 }
57 MAP = Collections.unmodifiableMap( map );
58 }
59
60
61
62
63 public static List<Plugin> getAllPlugins()
64 {
65 return ALL;
66 }
67
68 public static String listAllPluginIds()
69 {
70 return FormatUtil.join( ", ", MAP.keySet().iterator() );
71 }
72
73 public static Plugin getPlugin( String id )
74 {
75 return MAP.get( id );
76 }
77
78
79
80
81
82
83
84
85 public static LogDistillation.Plugin newInstance( LogDistiller.Plugin pluginConf )
86 {
87 Plugin plugin = getPlugin( pluginConf.getType() );
88 if ( plugin == null )
89 {
90 throw new PluginConfigException( "plugin type '" + pluginConf.getType() + "' unknown, valid value: "
91 + listAllPluginIds() );
92 }
93 return plugin.newInstance( pluginConf );
94 }
95
96
97
98
99
100
101
102
103 private static List<Plugin> loadExtensionPlugins()
104 {
105 List<Plugin> plugins = new ArrayList<Plugin>();
106 for ( String pluginsClass : ExtensionHelper.findExtensions( "plugins" ) )
107 {
108 if ( pluginsClass != null )
109 {
110 plugins.addAll( loadPlugins( pluginsClass ) );
111 }
112 }
113 return plugins;
114 }
115
116 private static List<Plugin> loadPlugins( String pluginsClass )
117 {
118 try
119 {
120 Plugins plugins = (Plugins) Class.forName( pluginsClass ).newInstance();
121 return plugins.definePlugins();
122 }
123 catch ( ClassNotFoundException cnfe )
124 {
125 throw new RuntimeException( "unable to load plugins class " + pluginsClass, cnfe );
126 }
127 catch ( IllegalAccessException iae )
128 {
129 throw new RuntimeException( "unable to access plugins constructor for class " + pluginsClass, iae );
130 }
131 catch ( InstantiationException ie )
132 {
133 throw new RuntimeException( "unable to instanciate plugins class " + pluginsClass, ie );
134 }
135 }
136 }