1 |
+ |
//---PACKAGE DECLARATION--- |
2 |
+ |
package uk.org.iscream.cms.server.filter; |
3 |
+ |
|
4 |
+ |
//---IMPORTS--- |
5 |
|
import java.io.*; |
6 |
|
import java.net.*; |
7 |
|
import java.util.*; |
8 |
< |
import uk.ac.ukc.iscream.filter.*; |
8 |
> |
import uk.org.iscream.cms.server.core.*; |
9 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
10 |
> |
import uk.org.iscream.cms.server.filter.*; |
11 |
> |
import uk.org.iscream.cms.server.util.*; |
12 |
|
|
13 |
+ |
/** |
14 |
+ |
* Handle an incoming packet as a separate thread. |
15 |
+ |
* Passes the data through various plugins, then |
16 |
+ |
* passes it on to the parent filter. |
17 |
+ |
* |
18 |
+ |
* Now grabs data from a single queue, rather than |
19 |
+ |
* waiting to be contacted. |
20 |
+ |
* |
21 |
+ |
* @author $Author$ |
22 |
+ |
* @version $Id$ |
23 |
+ |
*/ |
24 |
|
public class FilterThread extends Thread{ |
25 |
|
|
26 |
< |
public FilterThread(Filter parent){ |
27 |
< |
this.parent = parent; |
28 |
< |
// no-args constructor. |
29 |
< |
} |
26 |
> |
//---FINAL ATTRIBUTES--- |
27 |
> |
|
28 |
> |
/** |
29 |
> |
* The current CVS revision of this class |
30 |
> |
*/ |
31 |
> |
public final String REVISION = "$Revision$"; |
32 |
|
|
33 |
< |
public void run(DatagramPacket packet){ |
34 |
< |
rawPacket = packet.getData(); |
35 |
< |
start(); |
33 |
> |
//---STATIC METHODS--- |
34 |
> |
|
35 |
> |
//---CONSTRUCTORS--- |
36 |
> |
|
37 |
> |
/** |
38 |
> |
* Constructs an instance of a FilterThread |
39 |
> |
* |
40 |
> |
* @param queue the Queue this filter is using |
41 |
> |
*/ |
42 |
> |
public FilterThread(Queue queue){ |
43 |
> |
// set the Thread name |
44 |
> |
setName("filter.FilterThread"); |
45 |
> |
|
46 |
> |
_queue = queue; |
47 |
> |
_logger.write(toString(), Logger.SYSINIT, "created"); |
48 |
|
} |
49 |
< |
|
50 |
< |
public void run(String xml){ |
51 |
< |
rawPacket = xml.getBytes(); |
52 |
< |
start(); |
53 |
< |
} |
54 |
< |
|
49 |
> |
|
50 |
> |
//---PUBLIC METHODS--- |
51 |
> |
|
52 |
> |
/** |
53 |
> |
* Runs the thread, getting data from the Queue and |
54 |
> |
* sending it on to the parent filter. |
55 |
> |
*/ |
56 |
|
public void run(){ |
57 |
< |
|
58 |
< |
// Get a string without any null characters in it. |
59 |
< |
String xml = new String(rawPacket); |
60 |
< |
if(xml.indexOf(0)!=-1) { |
61 |
< |
xml = xml.substring(0, xml.indexOf(0)); |
57 |
> |
// setup the XMLPacketMaker |
58 |
> |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(); |
59 |
> |
// get a queue for ourselves |
60 |
> |
int n = _queue.getQueue(); |
61 |
> |
// keep these out here, saves recreating the object |
62 |
> |
String xml = null; |
63 |
> |
String parentFilterName = ""; |
64 |
> |
Filter parent = null; |
65 |
> |
while(true) { |
66 |
> |
// get a String of xml |
67 |
> |
try { |
68 |
> |
xml = (String) _queue.get(n); |
69 |
> |
} |
70 |
> |
catch (InvalidQueueException e) { |
71 |
> |
_logger.write(toString(), Logger.ERROR, "Queue error: "+e); |
72 |
> |
} |
73 |
> |
|
74 |
> |
// Get a string without any null characters in it. |
75 |
> |
// -- maybe String.trim() would be better here ? |
76 |
> |
if (xml.indexOf(0) != -1) { |
77 |
> |
xml = xml.substring(0, xml.indexOf(0)); |
78 |
> |
} |
79 |
> |
else { |
80 |
> |
xml = xml.substring(0, xml.length()); |
81 |
> |
} |
82 |
> |
|
83 |
> |
// Bundle the XML all on one line (saves space and simplifies |
84 |
> |
// the protocol between clientinterface and client). |
85 |
> |
StringTokenizer tokenizer = new StringTokenizer(new String(xml), "\n"); |
86 |
> |
xml = ""; |
87 |
> |
while (tokenizer.hasMoreTokens()) { |
88 |
> |
xml += tokenizer.nextToken(); |
89 |
> |
} |
90 |
> |
|
91 |
> |
// Use XMLPacketMaker to make an XMLPacket object. |
92 |
> |
XMLPacket packet = null; |
93 |
> |
try { |
94 |
> |
packet = xmlPacketMaker.createXMLPacket(xml); |
95 |
> |
} catch(InvalidXMLException e) { |
96 |
> |
_logger.write(toString(), Logger.ERROR, "Invalid XML: "+e); |
97 |
> |
// skip the rest of this loop iteration |
98 |
> |
continue; |
99 |
> |
} |
100 |
> |
|
101 |
> |
// get parent |
102 |
> |
try { |
103 |
> |
String newParent = ConfigurationProxy.getInstance().getProperty("Filter." + FilterMain.NAME, "Filter.parentFilter"); |
104 |
> |
if(!parentFilterName.equals(newParent)) { |
105 |
> |
parentFilterName = newParent; |
106 |
> |
parent = FilterHelper.narrow(_refman.getCORBARef("iscream.Filter." + parentFilterName)); |
107 |
> |
_logger.write(toString(), Logger.DEBUG, "Parent filter changed to: "+parentFilterName); |
108 |
> |
} |
109 |
> |
} catch (PropertyNotFoundException e) { |
110 |
> |
continue; |
111 |
> |
} |
112 |
> |
|
113 |
> |
// XMLPacket is ok, run filters... |
114 |
> |
if(PluginFilterManager.getInstance().runFilters(packet)) { |
115 |
> |
// and pass it on... |
116 |
> |
parent.receiveXML(xml); |
117 |
> |
} |
118 |
> |
else { |
119 |
> |
// ... or filtered it |
120 |
> |
_logger.write(toString(), Logger.DEBUG, "An XML packet was sucessfully filtered from the system."); |
121 |
> |
} |
122 |
|
} |
123 |
< |
else { |
31 |
< |
xml = xml.substring(0, xml.length()); |
32 |
< |
} |
33 |
< |
System.out.println(xml); |
123 |
> |
} |
124 |
|
|
125 |
< |
// USe my XMLPacketMaker to make an XMLPacket object. |
126 |
< |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
127 |
< |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
128 |
< |
|
129 |
< |
if (packet == null){ |
130 |
< |
System.out.println("UDPReaderThread - A null XMLPacket was returned, I think I'll ignore it!"); |
131 |
< |
return; |
132 |
< |
} |
133 |
< |
System.out.println("UDPReaderThread - An XML Packet was read sucessfully: -"); |
134 |
< |
packet.printAll(); |
135 |
< |
// Now do something with this XMLPacket!!! |
136 |
< |
// .... but what? ;-) |
137 |
< |
parent.receiveXML(xml); |
138 |
< |
|
125 |
> |
/** |
126 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
127 |
> |
* method to provide clean logging (every class should have this). |
128 |
> |
* |
129 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
130 |
> |
* to format the toString() |
131 |
> |
* |
132 |
> |
* @return the name of this class and its CVS revision |
133 |
> |
*/ |
134 |
> |
public String toString() { |
135 |
> |
return FormatName.getName( |
136 |
> |
_name, |
137 |
> |
getClass().getName(), |
138 |
> |
REVISION); |
139 |
|
} |
140 |
+ |
|
141 |
+ |
//---PRIVATE METHODS--- |
142 |
+ |
|
143 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
144 |
+ |
|
145 |
+ |
//---ATTRIBUTES--- |
146 |
|
|
147 |
< |
Filter parent; |
148 |
< |
byte[] rawPacket; |
147 |
> |
/** |
148 |
> |
* The Queue object |
149 |
> |
*/ |
150 |
> |
Queue _queue; |
151 |
> |
|
152 |
> |
/** |
153 |
> |
* This is the friendly identifier of the |
154 |
> |
* component this class is running in. |
155 |
> |
* eg, a Filter may be called "filter1", |
156 |
> |
* If this class does not have an owning |
157 |
> |
* component, a name from the configuration |
158 |
> |
* can be placed here. This name could also |
159 |
> |
* be changed to null for utility classes. |
160 |
> |
*/ |
161 |
> |
private String _name = FilterMain.NAME; |
162 |
> |
|
163 |
> |
/** |
164 |
> |
* This holds a reference to the |
165 |
> |
* system logger that is being used. |
166 |
> |
*/ |
167 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
168 |
> |
|
169 |
> |
/** |
170 |
> |
* A reference to the reference manager in use |
171 |
> |
*/ |
172 |
> |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
173 |
> |
|
174 |
> |
//---STATIC ATTRIBUTES--- |
175 |
> |
|
176 |
|
} |