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