1 |
< |
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.filter; |
3 |
< |
|
4 |
< |
//---IMPORTS--- |
5 |
< |
import java.io.*; |
6 |
< |
import java.util.*; |
7 |
< |
|
8 |
< |
import uk.ac.ukc.iscream.util.*; |
9 |
< |
import uk.ac.ukc.iscream.core.*; |
10 |
< |
|
11 |
< |
/** |
12 |
< |
* the plugin filter manager |
13 |
< |
* This is a singleton class. |
14 |
< |
* |
15 |
< |
* @author $Author$ |
16 |
< |
* @version $Id$ |
17 |
< |
*/ |
18 |
< |
class PluginFilterManager { |
19 |
< |
|
20 |
< |
//---FINAL ATTRIBUTES--- |
21 |
< |
|
22 |
< |
/** |
23 |
< |
* The current CVS revision of this class |
24 |
< |
*/ |
25 |
< |
public final String REVISION = "$Revision$"; |
26 |
< |
|
27 |
< |
//---STATIC METHODS--- |
28 |
< |
|
29 |
< |
// Return a reference to the single class. |
30 |
< |
// Construct it if it does not already exist, otherwise just return the reference. |
31 |
< |
public static PluginFilterManager getInstance() throws NotInitialisedException { |
32 |
< |
if (_instance == null){ |
33 |
< |
return new PluginFilterManager(); |
34 |
< |
} |
35 |
< |
return _instance; |
36 |
< |
} |
37 |
< |
|
38 |
< |
//---CONSTRUCTORS--- |
39 |
< |
|
40 |
< |
// Private Constructor - this part creates the filter pipeline |
41 |
< |
// This is a singleton class, btw. |
42 |
< |
private PluginFilterManager(){ |
43 |
< |
|
44 |
< |
_logger.write(toString(), Logger.SYSINIT, "Initialising"); |
45 |
< |
_logger.write(toString(), Logger.SYSMSG, "Creating filter pipeline for plugin filters ..."); |
46 |
< |
|
47 |
< |
// Get our config |
48 |
< |
ReferenceManager refman = ReferenceManager.getInstance(); |
49 |
< |
Configuration config = refman.getCM().getConfiguration(refman.getName()); |
50 |
< |
String pluginsPackage = config.getProperty("Filter.PluginsPackage"); |
51 |
< |
String pluginsList = config.getProperty("Filter.Plugins"); |
52 |
< |
|
53 |
< |
StringTokenizer st = new StringTokenizer(pluginsList, ";"); |
54 |
< |
|
55 |
< |
while(st.hasMoreTokens()) { |
56 |
< |
String className = pluginsPackage + "." + st.nextToken() + _suffix; |
57 |
< |
_logger.write(toString(), Logger.DEBUG, "Attempting to create plugin: "+className); |
58 |
< |
|
59 |
< |
// Create an instance of the specified PluginFilter to include |
60 |
< |
// within the filterPipe. Add it to the filterPipeline |
61 |
< |
try { |
62 |
< |
PluginFilter pf = (PluginFilter)ClassLoader.getSystemClassLoader().loadClass(className).newInstance(); |
63 |
< |
_filterPipeline.add(pf); |
64 |
< |
_logger.write(toString(), Logger.DEBUG, "Added filter: "+className+" ("+pf.getDescription()+")"); |
65 |
< |
} |
66 |
< |
catch (InstantiationException e){ |
67 |
< |
_logger.write(toString(), Logger.ERROR, "Failed to instantiate "+className+" to the plugin filter pipeline."); |
68 |
< |
_logger.write(toString(), Logger.ERROR, e.getMessage()); |
69 |
< |
} |
70 |
< |
catch (Exception e){ |
71 |
< |
_logger.write(toString(), Logger.ERROR, "Failed to add "+className+" to the plugin filter pipeline."); |
72 |
< |
_logger.write(toString(), Logger.ERROR, e.toString()); |
73 |
< |
} |
74 |
< |
} |
75 |
< |
_logger.write(toString(), Logger.SYSMSG, "The filter pipeline has been set up with "+_filterPipeline.size()+" plugin filters."); |
76 |
< |
|
77 |
< |
} |
78 |
< |
|
79 |
< |
//---PUBLIC METHODS--- |
80 |
< |
|
81 |
< |
// apply all of the filters in the pipeline to the packet. |
82 |
< |
// return true if they all accept the packet. |
83 |
< |
// return false if any single filter rejects the packet. |
84 |
< |
// return true if there are no filters inthe pipeline. |
85 |
< |
public boolean runFilters(XMLPacket packet){ |
86 |
< |
|
87 |
< |
// for each filter in the pipeline... |
88 |
< |
ListIterator pluginFilters = _filterPipeline.listIterator(0); |
89 |
< |
while (pluginFilters.hasNext()){ |
90 |
< |
PluginFilter filter = (PluginFilter)pluginFilters.next(); |
91 |
< |
if (!filter.runFilter(packet)){ |
92 |
< |
return false; |
93 |
< |
} |
94 |
< |
} |
95 |
< |
|
96 |
< |
return true; |
97 |
< |
} |
98 |
< |
|
99 |
< |
/** |
100 |
< |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
101 |
< |
* method to provide clean logging (every class should have this). |
102 |
< |
* |
103 |
< |
* @return the name of this class and its CVS revision |
104 |
< |
*/ |
105 |
< |
public String toString() { |
106 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
107 |
< |
} |
108 |
< |
|
109 |
< |
//---PRIVATE METHODS--- |
110 |
< |
|
111 |
< |
//---ACCESSOR/MUTATOR METHODS--- |
112 |
< |
|
113 |
< |
//---ATTRIBUTES--- |
114 |
< |
|
115 |
< |
// file name suffix for plugin filter classes: |
116 |
< |
private final String _suffix = "__Plugin"; |
117 |
< |
|
118 |
< |
// LinkedList for holding the PluginFilter objects (the pipeline). |
119 |
< |
private LinkedList _filterPipeline = new LinkedList(); |
120 |
< |
|
121 |
< |
// A reference to the single instance of this class |
122 |
< |
private static PluginFilterManager _instance; |
123 |
< |
|
124 |
< |
/** |
125 |
< |
* Reference to a Logger |
126 |
< |
*/ |
127 |
< |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
128 |
< |
|
129 |
< |
//---STATIC ATTRIBUTES--- |
130 |
< |
|
131 |
< |
} |
1 |
> |
//---PACKAGE DECLARATION--- |
2 |
> |
package uk.ac.ukc.iscream.filter; |
3 |
> |
|
4 |
> |
//---IMPORTS--- |
5 |
> |
import java.io.*; |
6 |
> |
import java.util.*; |
7 |
> |
|
8 |
> |
import uk.ac.ukc.iscream.util.*; |
9 |
> |
import uk.ac.ukc.iscream.componentmanager.*; |
10 |
> |
import uk.ac.ukc.iscream.core.*; |
11 |
> |
|
12 |
> |
/** |
13 |
> |
* This class setups up and manages Plugins in a Filter. A list |
14 |
> |
* of plugins to use is specified in the configuration, and these |
15 |
> |
* are all loaded upon first use of this class. |
16 |
> |
* This is a singleton class. |
17 |
> |
* |
18 |
> |
* @author $Author$ |
19 |
> |
* @version $Id$ |
20 |
> |
*/ |
21 |
> |
class PluginFilterManager { |
22 |
> |
|
23 |
> |
//---FINAL ATTRIBUTES--- |
24 |
> |
|
25 |
> |
/** |
26 |
> |
* The current CVS revision of this class |
27 |
> |
*/ |
28 |
> |
public final String REVISION = "$Revision$"; |
29 |
> |
|
30 |
> |
//---STATIC METHODS--- |
31 |
> |
|
32 |
> |
/** |
33 |
> |
* Return a reference to the single class. |
34 |
> |
* Construct it if it does not already exist, otherwise just return the reference. |
35 |
> |
*/ |
36 |
> |
public static PluginFilterManager getInstance() { |
37 |
> |
if (_instance == null){ |
38 |
> |
_instance = new PluginFilterManager(); |
39 |
> |
} |
40 |
> |
return _instance; |
41 |
> |
} |
42 |
> |
|
43 |
> |
//---CONSTRUCTORS--- |
44 |
> |
|
45 |
> |
/** |
46 |
> |
* Private Constructor - this part creates the filter pipeline |
47 |
> |
* This is a singleton class, btw. |
48 |
> |
*/ |
49 |
> |
private PluginFilterManager(){ |
50 |
> |
|
51 |
> |
_logger.write(toString(), Logger.SYSINIT, "Initialising"); |
52 |
> |
_logger.write(toString(), Logger.SYSMSG, "Creating filter pipeline for plugin filters ..."); |
53 |
> |
|
54 |
> |
// get the configuration for this plug-in setup |
55 |
> |
Configuration config = _refman.getCM().getConfiguration(FilterMain.NAME); |
56 |
> |
String pluginsPackage = config.getProperty("Filter.PluginsPackage"); |
57 |
> |
String pluginsList = config.getProperty("Filter.Plugins"); |
58 |
> |
|
59 |
> |
StringTokenizer st = new StringTokenizer(pluginsList, ";"); |
60 |
> |
|
61 |
> |
while(st.hasMoreTokens()) { |
62 |
> |
String className = pluginsPackage + "." + st.nextToken() + _suffix; |
63 |
> |
_logger.write(toString(), Logger.DEBUG, "Attempting to create plugin: "+className); |
64 |
> |
|
65 |
> |
// Create an instance of the specified PluginFilter to include |
66 |
> |
// within the filterPipe. Add it to the filterPipeline |
67 |
> |
try { |
68 |
> |
PluginFilter pf = (PluginFilter)ClassLoader.getSystemClassLoader().loadClass(className).newInstance(); |
69 |
> |
_filterPipeline.add(pf); |
70 |
> |
_logger.write(toString(), Logger.DEBUG, "Added filter: "+className+" ("+pf.getDescription()+")"); |
71 |
> |
} |
72 |
> |
catch (InstantiationException e){ |
73 |
> |
_logger.write(toString(), Logger.ERROR, "Failed to instantiate "+className+" to the plugin filter pipeline."); |
74 |
> |
_logger.write(toString(), Logger.ERROR, e.getMessage()); |
75 |
> |
} |
76 |
> |
catch (Exception e){ |
77 |
> |
_logger.write(toString(), Logger.ERROR, "Failed to add "+className+" to the plugin filter pipeline."); |
78 |
> |
_logger.write(toString(), Logger.ERROR, e.toString()); |
79 |
> |
} |
80 |
> |
} |
81 |
> |
_logger.write(toString(), Logger.SYSMSG, "The filter pipeline has been set up with "+_filterPipeline.size()+" plugin filters."); |
82 |
> |
|
83 |
> |
} |
84 |
> |
|
85 |
> |
//---PUBLIC METHODS--- |
86 |
> |
|
87 |
> |
/** |
88 |
> |
* apply all of the filters in the pipeline to the packet. |
89 |
> |
* return true if they all accept the packet. |
90 |
> |
* return false if any single filter rejects the packet. |
91 |
> |
* return true if there are no filters inthe pipeline. |
92 |
> |
* |
93 |
> |
* @param packet an XMLPacket to be tested |
94 |
> |
* @return whether the packet can be passed on |
95 |
> |
*/ |
96 |
> |
public boolean runFilters(XMLPacket packet){ |
97 |
> |
|
98 |
> |
// for each filter in the pipeline... |
99 |
> |
ListIterator pluginFilters = _filterPipeline.listIterator(0); |
100 |
> |
while (pluginFilters.hasNext()){ |
101 |
> |
PluginFilter filter = (PluginFilter)pluginFilters.next(); |
102 |
> |
if (!filter.runFilter(packet)){ |
103 |
> |
return false; |
104 |
> |
} |
105 |
> |
} |
106 |
> |
|
107 |
> |
return true; |
108 |
> |
} |
109 |
> |
|
110 |
> |
/** |
111 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
112 |
> |
* method to provide clean logging (every class should have this). |
113 |
> |
* |
114 |
> |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
115 |
> |
* to format the toString() |
116 |
> |
* |
117 |
> |
* @return the name of this class and its CVS revision |
118 |
> |
*/ |
119 |
> |
public String toString() { |
120 |
> |
return FormatName.getName( |
121 |
> |
_name, |
122 |
> |
getClass().getName(), |
123 |
> |
REVISION); |
124 |
> |
} |
125 |
> |
|
126 |
> |
//---PRIVATE METHODS--- |
127 |
> |
|
128 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
129 |
> |
|
130 |
> |
//---ATTRIBUTES--- |
131 |
> |
|
132 |
> |
/** |
133 |
> |
* file name suffix for plugin filter classes: |
134 |
> |
*/ |
135 |
> |
private final String _suffix = "__Plugin"; |
136 |
> |
|
137 |
> |
/** |
138 |
> |
* LinkedList for holding the PluginFilter objects (the pipeline). |
139 |
> |
*/ |
140 |
> |
private LinkedList _filterPipeline = new LinkedList(); |
141 |
> |
|
142 |
> |
/** |
143 |
> |
* A reference to the single instance of this class |
144 |
> |
*/ |
145 |
> |
private static PluginFilterManager _instance; |
146 |
> |
|
147 |
> |
/** |
148 |
> |
* This is the friendly identifier of the |
149 |
> |
* component this class is running in. |
150 |
> |
* eg, a Filter may be called "filter1", |
151 |
> |
* If this class does not have an owning |
152 |
> |
* component, a name from the configuration |
153 |
> |
* can be placed here. This name could also |
154 |
> |
* be changed to null for utility classes. |
155 |
> |
*/ |
156 |
> |
private String _name = FilterMain.NAME; |
157 |
> |
|
158 |
> |
/** |
159 |
> |
* This holds a reference to the |
160 |
> |
* system logger that is being used. |
161 |
> |
*/ |
162 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
163 |
> |
|
164 |
> |
/** |
165 |
> |
* A reference to the reference manager in use |
166 |
> |
*/ |
167 |
> |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
168 |
> |
|
169 |
> |
//---STATIC ATTRIBUTES--- |
170 |
> |
|
171 |
> |
} |