1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.cms.server.filter; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.io.*; |
6 |
import java.util.*; |
7 |
|
8 |
import uk.org.iscream.cms.server.util.*; |
9 |
import uk.org.iscream.cms.server.componentmanager.*; |
10 |
import uk.org.iscream.cms.server.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: tdb1 $ |
19 |
* @version $Id: PluginFilterManager.java,v 1.8 2001/03/14 23:25:29 tdb1 Exp $ |
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: 1.8 $"; |
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 |
String pluginsPackage, pluginsList; |
55 |
|
56 |
try { |
57 |
// get the configuration for this plug-in setup |
58 |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
59 |
pluginsPackage = cp.getProperty(FilterMain.NAME, "Filter.PluginsPackage"); |
60 |
pluginsList = cp.getProperty(FilterMain.NAME, "Filter.Plugins"); |
61 |
} catch (PropertyNotFoundException e) { |
62 |
_logger.write(toString(), Logger.WARNING, "Unable to locate property: "+e); |
63 |
// setting this to "" will avoid building a plugin list, |
64 |
// which is what we want when a property doesn't exist |
65 |
pluginsList = ""; |
66 |
// and set this to "", because the compiler is a git |
67 |
pluginsPackage = ""; |
68 |
} |
69 |
|
70 |
StringTokenizer st = new StringTokenizer(pluginsList, ";"); |
71 |
|
72 |
while(st.hasMoreTokens()) { |
73 |
String className = pluginsPackage + "." + st.nextToken() + _suffix; |
74 |
_logger.write(toString(), Logger.DEBUG, "Attempting to create plugin: "+className); |
75 |
|
76 |
// Create an instance of the specified PluginFilter to include |
77 |
// within the filterPipe. Add it to the filterPipeline |
78 |
try { |
79 |
PluginFilter pf = (PluginFilter)ClassLoader.getSystemClassLoader().loadClass(className).newInstance(); |
80 |
_filterPipeline.add(pf); |
81 |
_logger.write(toString(), Logger.DEBUG, "Added filter: "+className+" ("+pf.getDescription()+")"); |
82 |
} |
83 |
catch (InstantiationException e){ |
84 |
_logger.write(toString(), Logger.ERROR, "Failed to instantiate "+className+" to the plugin filter pipeline."); |
85 |
_logger.write(toString(), Logger.ERROR, e.getMessage()); |
86 |
} |
87 |
catch (Exception e){ |
88 |
_logger.write(toString(), Logger.ERROR, "Failed to add "+className+" to the plugin filter pipeline."); |
89 |
_logger.write(toString(), Logger.ERROR, e.toString()); |
90 |
} |
91 |
} |
92 |
_logger.write(toString(), Logger.SYSMSG, "The filter pipeline has been set up with "+_filterPipeline.size()+" plugin filters."); |
93 |
|
94 |
} |
95 |
|
96 |
//---PUBLIC METHODS--- |
97 |
|
98 |
/** |
99 |
* apply all of the filters in the pipeline to the packet. |
100 |
* return true if they all accept the packet. |
101 |
* return false if any single filter rejects the packet. |
102 |
* return true if there are no filters inthe pipeline. |
103 |
* |
104 |
* @param packet an XMLPacket to be tested |
105 |
* @return whether the packet can be passed on |
106 |
*/ |
107 |
public boolean runFilters(XMLPacket packet){ |
108 |
|
109 |
// for each filter in the pipeline... |
110 |
ListIterator pluginFilters = _filterPipeline.listIterator(0); |
111 |
while (pluginFilters.hasNext()){ |
112 |
PluginFilter filter = (PluginFilter)pluginFilters.next(); |
113 |
if (!filter.runFilter(packet)){ |
114 |
return false; |
115 |
} |
116 |
} |
117 |
|
118 |
return true; |
119 |
} |
120 |
|
121 |
/** |
122 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
123 |
* method to provide clean logging (every class should have this). |
124 |
* |
125 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
126 |
* to format the toString() |
127 |
* |
128 |
* @return the name of this class and its CVS revision |
129 |
*/ |
130 |
public String toString() { |
131 |
return FormatName.getName( |
132 |
_name, |
133 |
getClass().getName(), |
134 |
REVISION); |
135 |
} |
136 |
|
137 |
//---PRIVATE METHODS--- |
138 |
|
139 |
//---ACCESSOR/MUTATOR METHODS--- |
140 |
|
141 |
//---ATTRIBUTES--- |
142 |
|
143 |
/** |
144 |
* file name suffix for plugin filter classes: |
145 |
*/ |
146 |
private final String _suffix = "__Plugin"; |
147 |
|
148 |
/** |
149 |
* LinkedList for holding the PluginFilter objects (the pipeline). |
150 |
*/ |
151 |
private LinkedList _filterPipeline = new LinkedList(); |
152 |
|
153 |
/** |
154 |
* A reference to the single instance of this class |
155 |
*/ |
156 |
private static PluginFilterManager _instance; |
157 |
|
158 |
/** |
159 |
* This is the friendly identifier of the |
160 |
* component this class is running in. |
161 |
* eg, a Filter may be called "filter1", |
162 |
* If this class does not have an owning |
163 |
* component, a name from the configuration |
164 |
* can be placed here. This name could also |
165 |
* be changed to null for utility classes. |
166 |
*/ |
167 |
private String _name = FilterMain.NAME; |
168 |
|
169 |
/** |
170 |
* This holds a reference to the |
171 |
* system logger that is being used. |
172 |
*/ |
173 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
174 |
|
175 |
/** |
176 |
* A reference to the reference manager in use |
177 |
*/ |
178 |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
179 |
|
180 |
//---STATIC ATTRIBUTES--- |
181 |
|
182 |
} |