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 |
> |
/* |
2 |
> |
* i-scream central monitoring system |
3 |
> |
* http://www.i-scream.org.uk |
4 |
> |
* Copyright (C) 2000-2002 i-scream |
5 |
> |
* |
6 |
> |
* This program is free software; you can redistribute it and/or |
7 |
> |
* modify it under the terms of the GNU General Public License |
8 |
> |
* as published by the Free Software Foundation; either version 2 |
9 |
> |
* of the License, or (at your option) any later version. |
10 |
> |
* |
11 |
> |
* This program is distributed in the hope that it will be useful, |
12 |
> |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
> |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
> |
* GNU General Public License for more details. |
15 |
> |
* |
16 |
> |
* You should have received a copy of the GNU General Public License |
17 |
> |
* along with this program; if not, write to the Free Software |
18 |
> |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
> |
*/ |
20 |
> |
|
21 |
> |
//---PACKAGE DECLARATION--- |
22 |
> |
package uk.org.iscream.cms.server.filter; |
23 |
> |
|
24 |
> |
//---IMPORTS--- |
25 |
> |
import java.io.*; |
26 |
> |
import java.util.*; |
27 |
> |
|
28 |
> |
import uk.org.iscream.cms.server.util.*; |
29 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
30 |
> |
import uk.org.iscream.cms.server.core.*; |
31 |
> |
|
32 |
> |
/** |
33 |
> |
* This class setups up and manages Plugins in a Filter. A list |
34 |
> |
* of plugins to use is specified in the configuration, and these |
35 |
> |
* are all loaded upon first use of this class. |
36 |
> |
* This is a singleton class. |
37 |
> |
* |
38 |
> |
* @author $Author$ |
39 |
> |
* @version $Id$ |
40 |
> |
*/ |
41 |
> |
class PluginFilterManager { |
42 |
> |
|
43 |
> |
//---FINAL ATTRIBUTES--- |
44 |
> |
|
45 |
> |
/** |
46 |
> |
* The current CVS revision of this class |
47 |
> |
*/ |
48 |
> |
public final String REVISION = "$Revision$"; |
49 |
> |
|
50 |
> |
//---STATIC METHODS--- |
51 |
> |
|
52 |
> |
/** |
53 |
> |
* Return a reference to the single class. |
54 |
> |
* Construct it if it does not already exist, otherwise just return the reference. |
55 |
> |
*/ |
56 |
> |
public static PluginFilterManager getInstance() { |
57 |
> |
if (_instance == null){ |
58 |
> |
_instance = new PluginFilterManager(); |
59 |
> |
} |
60 |
> |
return _instance; |
61 |
> |
} |
62 |
> |
|
63 |
> |
//---CONSTRUCTORS--- |
64 |
> |
|
65 |
> |
/** |
66 |
> |
* Private Constructor - this part creates the filter pipeline |
67 |
> |
* This is a singleton class, btw. |
68 |
> |
*/ |
69 |
> |
private PluginFilterManager(){ |
70 |
> |
|
71 |
> |
_logger.write(toString(), Logger.SYSINIT, "Initialising"); |
72 |
> |
_logger.write(toString(), Logger.SYSMSG, "Creating filter pipeline for plugin filters ..."); |
73 |
> |
|
74 |
> |
String pluginsPackage, pluginsList; |
75 |
> |
|
76 |
> |
try { |
77 |
> |
// get the configuration for this plug-in setup |
78 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
79 |
> |
pluginsPackage = cp.getProperty("Filter." + FilterMain.NAME, "Filter.PluginsPackage"); |
80 |
> |
pluginsList = cp.getProperty("Filter." + FilterMain.NAME, "Filter.Plugins"); |
81 |
> |
} catch (PropertyNotFoundException e) { |
82 |
> |
_logger.write(toString(), Logger.WARNING, "Unable to locate property: "+e); |
83 |
> |
// setting this to "" will avoid building a plugin list, |
84 |
> |
// which is what we want when a property doesn't exist |
85 |
> |
pluginsList = ""; |
86 |
> |
// and set this to "", because the compiler is a git |
87 |
> |
pluginsPackage = ""; |
88 |
> |
} |
89 |
> |
|
90 |
> |
StringTokenizer st = new StringTokenizer(pluginsList, ";"); |
91 |
> |
|
92 |
> |
while(st.hasMoreTokens()) { |
93 |
> |
String className = pluginsPackage + "." + st.nextToken() + _suffix; |
94 |
> |
_logger.write(toString(), Logger.DEBUG, "Attempting to create plugin: "+className); |
95 |
> |
|
96 |
> |
// Create an instance of the specified PluginFilter to include |
97 |
> |
// within the filterPipe. Add it to the filterPipeline |
98 |
> |
try { |
99 |
> |
PluginFilter pf = (PluginFilter)ClassLoader.getSystemClassLoader().loadClass(className).newInstance(); |
100 |
> |
_filterPipeline.add(pf); |
101 |
> |
_logger.write(toString(), Logger.DEBUG, "Added filter: "+className+" ("+pf.getDescription()+")"); |
102 |
> |
} |
103 |
> |
catch (InstantiationException e){ |
104 |
> |
_logger.write(toString(), Logger.ERROR, "Failed to instantiate "+className+" to the plugin filter pipeline."); |
105 |
> |
_logger.write(toString(), Logger.ERROR, e.getMessage()); |
106 |
> |
} |
107 |
> |
catch (Exception e){ |
108 |
> |
_logger.write(toString(), Logger.ERROR, "Failed to add "+className+" to the plugin filter pipeline."); |
109 |
> |
_logger.write(toString(), Logger.ERROR, e.toString()); |
110 |
> |
} |
111 |
> |
} |
112 |
> |
_logger.write(toString(), Logger.SYSMSG, "The filter pipeline has been set up with "+_filterPipeline.size()+" plugin filters."); |
113 |
> |
|
114 |
> |
} |
115 |
> |
|
116 |
> |
//---PUBLIC METHODS--- |
117 |
> |
|
118 |
> |
/** |
119 |
> |
* apply all of the filters in the pipeline to the packet. |
120 |
> |
* return true if they all accept the packet. |
121 |
> |
* return false if any single filter rejects the packet. |
122 |
> |
* return true if there are no filters inthe pipeline. |
123 |
> |
* |
124 |
> |
* @param packet an XMLPacket to be tested |
125 |
> |
* @return whether the packet can be passed on |
126 |
> |
*/ |
127 |
> |
public boolean runFilters(XMLPacket packet){ |
128 |
> |
|
129 |
> |
// for each filter in the pipeline... |
130 |
> |
ListIterator pluginFilters = _filterPipeline.listIterator(0); |
131 |
> |
while (pluginFilters.hasNext()){ |
132 |
> |
PluginFilter filter = (PluginFilter)pluginFilters.next(); |
133 |
> |
if (!filter.runFilter(packet)){ |
134 |
> |
return false; |
135 |
> |
} |
136 |
> |
} |
137 |
> |
|
138 |
> |
return true; |
139 |
> |
} |
140 |
> |
|
141 |
> |
/** |
142 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
143 |
> |
* method to provide clean logging (every class should have this). |
144 |
> |
* |
145 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
146 |
> |
* to format the toString() |
147 |
> |
* |
148 |
> |
* @return the name of this class and its CVS revision |
149 |
> |
*/ |
150 |
> |
public String toString() { |
151 |
> |
return FormatName.getName( |
152 |
> |
_name, |
153 |
> |
getClass().getName(), |
154 |
> |
REVISION); |
155 |
> |
} |
156 |
> |
|
157 |
> |
//---PRIVATE METHODS--- |
158 |
> |
|
159 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
160 |
> |
|
161 |
> |
//---ATTRIBUTES--- |
162 |
> |
|
163 |
> |
/** |
164 |
> |
* file name suffix for plugin filter classes: |
165 |
> |
*/ |
166 |
> |
private final String _suffix = "__Plugin"; |
167 |
> |
|
168 |
> |
/** |
169 |
> |
* LinkedList for holding the PluginFilter objects (the pipeline). |
170 |
> |
*/ |
171 |
> |
private LinkedList _filterPipeline = new LinkedList(); |
172 |
> |
|
173 |
> |
/** |
174 |
> |
* A reference to the single instance of this class |
175 |
> |
*/ |
176 |
> |
private static PluginFilterManager _instance; |
177 |
> |
|
178 |
> |
/** |
179 |
> |
* This is the friendly identifier of the |
180 |
> |
* component this class is running in. |
181 |
> |
* eg, a Filter may be called "filter1", |
182 |
> |
* If this class does not have an owning |
183 |
> |
* component, a name from the configuration |
184 |
> |
* can be placed here. This name could also |
185 |
> |
* be changed to null for utility classes. |
186 |
> |
*/ |
187 |
> |
private String _name = FilterMain.NAME; |
188 |
> |
|
189 |
> |
/** |
190 |
> |
* This holds a reference to the |
191 |
> |
* system logger that is being used. |
192 |
> |
*/ |
193 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
194 |
> |
|
195 |
> |
/** |
196 |
> |
* A reference to the reference manager in use |
197 |
> |
*/ |
198 |
> |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
199 |
> |
|
200 |
> |
//---STATIC ATTRIBUTES--- |
201 |
> |
|
202 |
> |
} |