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