1 |
import java.io.*; |
2 |
import java.util.*; |
3 |
|
4 |
import uk.ac.ukc.iscream.util.*; |
5 |
|
6 |
// the plugin filter manager |
7 |
// This is a singleton class. |
8 |
|
9 |
// pjm2@ukc.ac.uk |
10 |
|
11 |
class PluginFilterManager { |
12 |
|
13 |
// public method to return an instance of a PluginFilterManager, |
14 |
// as this class is a singleton. |
15 |
public static PluginFilterManager init() throws AlreadyInitialisedException { |
16 |
if (_instance != null){ |
17 |
throw new AlreadyInitialisedException("init() has already been called."); |
18 |
} |
19 |
_instance = new PluginFilterManager(); |
20 |
return _instance; |
21 |
} |
22 |
|
23 |
// Return a reference to the single class. |
24 |
public static PluginFilterManager getInstance() throws NotInitialisedException { |
25 |
if (_instance == null){ |
26 |
throw new NotInitialisedException("An instance of PluginFilterManager hasn't been initialised yet!"); |
27 |
} |
28 |
return _instance; |
29 |
} |
30 |
|
31 |
// Private Constructor - this part creates the filter pipeline |
32 |
// This is a singleton class, btw. |
33 |
private PluginFilterManager(){ |
34 |
|
35 |
System.out.println("Creating filter pipeline for plugin filters ..."); |
36 |
|
37 |
// Look in the specified directory for "*__Plugin.class" files. |
38 |
File file = new File(_directory); |
39 |
String[] files = file.list(); |
40 |
|
41 |
// For each "__Plugin.class" file... |
42 |
for (int i = 0; i < files.length; i++){ |
43 |
if (files[i].endsWith(_suffix)){ |
44 |
|
45 |
// Remove the ".class" file extension. |
46 |
files[i] = files[i].substring(0, files[i].length() - 6); |
47 |
|
48 |
// Create an instance of the specified PluginFilter to include |
49 |
// within the filterPipe. Add it to the filterPipeline |
50 |
try { |
51 |
PluginFilter pf = (PluginFilter)ClassLoader.getSystemClassLoader().loadClass(files[i]).newInstance(); |
52 |
_filterPipeline.add(pf); |
53 |
System.out.println("Added filter: "+files[i]); |
54 |
} |
55 |
catch (InstantiationException e){ |
56 |
System.out.println("Failed to add "+files[i]+" to the plugin filter pipeline."); |
57 |
} |
58 |
catch (Exception e){ |
59 |
System.out.println("Failed to add "+files[i]+" to the plugin filter pipeline."); |
60 |
} |
61 |
|
62 |
} |
63 |
else { |
64 |
//System.out.println("Ignoring "+files[i]+" in the plugin folder."); |
65 |
} |
66 |
} |
67 |
|
68 |
System.out.println("The filter pipeline has been set up with "+_filterPipeline.size()+" plugin filters."); |
69 |
|
70 |
} |
71 |
|
72 |
|
73 |
// apply all of the filters in the pipeline to the packet. |
74 |
// return true if they all accept the packet. |
75 |
// return false if any single filter rejects the packet. |
76 |
// return true if there are no filters inthe pipeline. |
77 |
public boolean runFilters(XMLPacket packet){ |
78 |
|
79 |
// for each filter in the pipeline... |
80 |
ListIterator pluginFilters = _filterPipeline.listIterator(0); |
81 |
while (pluginFilters.hasNext()){ |
82 |
PluginFilter filter = (PluginFilter)pluginFilters.next(); |
83 |
if (!filter.runFilter(packet)){ |
84 |
return false; |
85 |
} |
86 |
} |
87 |
|
88 |
return true; |
89 |
} |
90 |
|
91 |
|
92 |
// directory storing the plugins: |
93 |
private final String _directory = "./"; |
94 |
|
95 |
// file name suffix for plugin filter classes: |
96 |
private final String _suffix = "__Plugin.class"; |
97 |
|
98 |
// LinkedList for holding the PluginFilter objects (the pipeline). |
99 |
private LinkedList _filterPipeline = new LinkedList(); |
100 |
|
101 |
// A reference to the single instance of this class |
102 |
private static PluginFilterManager _instance; |
103 |
|
104 |
}- |