1 |
tdb |
1.11 |
/* |
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 |
ajm |
1.3 |
//---PACKAGE DECLARATION--- |
21 |
tdb |
1.9 |
package uk.org.iscream.cms.server.filter; |
22 |
ajm |
1.3 |
|
23 |
|
|
//---IMPORTS--- |
24 |
|
|
import java.io.*; |
25 |
|
|
import java.util.*; |
26 |
|
|
|
27 |
tdb |
1.9 |
import uk.org.iscream.cms.server.util.*; |
28 |
|
|
import uk.org.iscream.cms.server.componentmanager.*; |
29 |
|
|
import uk.org.iscream.cms.server.core.*; |
30 |
ajm |
1.3 |
|
31 |
|
|
/** |
32 |
tdb |
1.6 |
* 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 |
ajm |
1.3 |
* This is a singleton class. |
36 |
|
|
* |
37 |
tdb |
1.10 |
* @author $Author: tdb $ |
38 |
tdb |
1.11 |
* @version $Id: PluginFilterManager.java,v 1.10 2002/03/20 13:05:49 tdb Exp $ |
39 |
ajm |
1.3 |
*/ |
40 |
|
|
class PluginFilterManager { |
41 |
|
|
|
42 |
|
|
//---FINAL ATTRIBUTES--- |
43 |
|
|
|
44 |
|
|
/** |
45 |
|
|
* The current CVS revision of this class |
46 |
|
|
*/ |
47 |
tdb |
1.11 |
public final String REVISION = "$Revision: 1.10 $"; |
48 |
ajm |
1.3 |
|
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 |
tdb |
1.7 |
String pluginsPackage, pluginsList; |
74 |
|
|
|
75 |
|
|
try { |
76 |
|
|
// get the configuration for this plug-in setup |
77 |
|
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
78 |
tdb |
1.10 |
pluginsPackage = cp.getProperty("Filter." + FilterMain.NAME, "Filter.PluginsPackage"); |
79 |
|
|
pluginsList = cp.getProperty("Filter." + FilterMain.NAME, "Filter.Plugins"); |
80 |
tdb |
1.7 |
} 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 |
ajm |
1.3 |
|
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 |
tdb |
1.5 |
* |
123 |
|
|
* @param packet an XMLPacket to be tested |
124 |
|
|
* @return whether the packet can be passed on |
125 |
ajm |
1.3 |
*/ |
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 |
tdb |
1.9 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
145 |
ajm |
1.3 |
* 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 |
|
|
} |