1 |
tdb |
1.8 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.ac.ukc.iscream.filter; |
3 |
|
|
|
4 |
|
|
//---IMPORTS--- |
5 |
pjm2 |
1.1 |
import java.io.*; |
6 |
|
|
import java.net.*; |
7 |
|
|
import java.util.*; |
8 |
pjm2 |
1.6 |
import uk.ac.ukc.iscream.core.*; |
9 |
tdb |
1.17 |
import uk.ac.ukc.iscream.componentmanager.*; |
10 |
tdb |
1.2 |
import uk.ac.ukc.iscream.filter.*; |
11 |
ajm |
1.11 |
import uk.ac.ukc.iscream.util.*; |
12 |
pjm2 |
1.1 |
|
13 |
tdb |
1.8 |
/** |
14 |
ajm |
1.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 |
tdb |
1.8 |
* |
18 |
tdb |
1.15 |
* Now grabs data from a single queue, rather than |
19 |
|
|
* waiting to be contacted. |
20 |
|
|
* |
21 |
tdb |
1.17 |
* @author $Author: pjm2 $ |
22 |
|
|
* @version $Id: FilterThread.java,v 1.16 2001/01/15 10:09:49 pjm2 Exp $ |
23 |
tdb |
1.8 |
*/ |
24 |
tdb |
1.2 |
public class FilterThread extends Thread{ |
25 |
pjm2 |
1.1 |
|
26 |
tdb |
1.8 |
//---FINAL ATTRIBUTES--- |
27 |
|
|
|
28 |
|
|
/** |
29 |
|
|
* The current CVS revision of this class |
30 |
|
|
*/ |
31 |
tdb |
1.17 |
public final String REVISION = "$Revision: 1.16 $"; |
32 |
tdb |
1.8 |
|
33 |
|
|
//---STATIC METHODS--- |
34 |
|
|
|
35 |
|
|
//---CONSTRUCTORS--- |
36 |
tdb |
1.15 |
|
37 |
ajm |
1.14 |
/** |
38 |
tdb |
1.15 |
* Class constructor. |
39 |
ajm |
1.14 |
*/ |
40 |
tdb |
1.15 |
public FilterThread(Queue queue, Filter parent){ |
41 |
tdb |
1.10 |
_parent = parent; |
42 |
tdb |
1.15 |
_queue = queue; |
43 |
ajm |
1.14 |
_logger.write(toString(), Logger.DEBUG, "created"); |
44 |
pjm2 |
1.1 |
} |
45 |
tdb |
1.8 |
|
46 |
|
|
//---PUBLIC METHODS--- |
47 |
|
|
|
48 |
ajm |
1.14 |
/** |
49 |
|
|
* Runs the thread |
50 |
|
|
*/ |
51 |
pjm2 |
1.1 |
public void run(){ |
52 |
tdb |
1.15 |
// get a queue for ourselves |
53 |
|
|
int n = _queue.getQueue(); |
54 |
pjm2 |
1.16 |
// keep these out here, saves recreating the object |
55 |
tdb |
1.15 |
String xml = null; |
56 |
|
|
while(true) { |
57 |
|
|
// get a String of xml |
58 |
|
|
try { |
59 |
|
|
xml = (String) _queue.get(n); |
60 |
|
|
} |
61 |
|
|
catch (InvalidQueueException e) { |
62 |
|
|
_logger.write(toString(), Logger.ERROR, "Queue error: "+e); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
// Get a string without any null characters in it. |
66 |
|
|
// -- maybe String.trim() would be better here ? |
67 |
|
|
if (xml.indexOf(0) != -1) { |
68 |
|
|
xml = xml.substring(0, xml.indexOf(0)); |
69 |
|
|
} |
70 |
|
|
else { |
71 |
|
|
xml = xml.substring(0, xml.length()); |
72 |
|
|
} |
73 |
pjm2 |
1.16 |
|
74 |
|
|
|
75 |
|
|
// Bundle the XML all on one line (saves space and simplifies |
76 |
|
|
// the protocol between clientinterface and client. |
77 |
|
|
StringTokenizer tokenizer = new StringTokenizer(new String(xml), "\n"); |
78 |
|
|
xml = ""; |
79 |
|
|
while (tokenizer.hasMoreTokens()) { |
80 |
|
|
xml += tokenizer.nextToken(); |
81 |
|
|
} |
82 |
|
|
|
83 |
tdb |
1.15 |
|
84 |
|
|
// Use XMLPacketMaker to make an XMLPacket object. |
85 |
|
|
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
86 |
|
|
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
87 |
|
|
|
88 |
|
|
if(packet != null && PluginFilterManager.getInstance().runFilters(packet)) { |
89 |
|
|
// packet is not null |
90 |
|
|
// packet was not dropped by a plugin |
91 |
|
|
// ... best pass it on ! |
92 |
|
|
_parent.receiveXML(xml); |
93 |
|
|
} |
94 |
|
|
else { |
95 |
|
|
// either we had a null, or a plugin dropped it |
96 |
|
|
_logger.write(toString(), Logger.DEBUG, "An XML packet was sucessfully filtered from the system."); |
97 |
|
|
} |
98 |
pjm2 |
1.1 |
} |
99 |
|
|
} |
100 |
tdb |
1.8 |
|
101 |
|
|
/** |
102 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
103 |
|
|
* method to provide clean logging (every class should have this). |
104 |
|
|
* |
105 |
ajm |
1.14 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
106 |
|
|
* to format the toString() |
107 |
|
|
* |
108 |
tdb |
1.8 |
* @return the name of this class and its CVS revision |
109 |
|
|
*/ |
110 |
|
|
public String toString() { |
111 |
ajm |
1.14 |
return FormatName.getName( |
112 |
|
|
_name, |
113 |
|
|
getClass().getName(), |
114 |
|
|
REVISION); |
115 |
tdb |
1.8 |
} |
116 |
|
|
|
117 |
|
|
//---PRIVATE METHODS--- |
118 |
|
|
|
119 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
120 |
|
|
|
121 |
|
|
//---ATTRIBUTES--- |
122 |
|
|
|
123 |
ajm |
1.14 |
/** |
124 |
|
|
* Our parent filter |
125 |
|
|
*/ |
126 |
tdb |
1.10 |
Filter _parent; |
127 |
ajm |
1.14 |
|
128 |
|
|
/** |
129 |
tdb |
1.15 |
* The Queue object |
130 |
ajm |
1.14 |
*/ |
131 |
tdb |
1.15 |
Queue _queue; |
132 |
ajm |
1.14 |
|
133 |
|
|
/** |
134 |
|
|
* This is the friendly identifier of the |
135 |
|
|
* component this class is running in. |
136 |
|
|
* eg, a Filter may be called "filter1", |
137 |
|
|
* If this class does not have an owning |
138 |
|
|
* component, a name from the configuration |
139 |
|
|
* can be placed here. This name could also |
140 |
|
|
* be changed to null for utility classes. |
141 |
|
|
*/ |
142 |
|
|
private String _name = FilterMain.NAME; |
143 |
|
|
|
144 |
|
|
/** |
145 |
|
|
* This holds a reference to the |
146 |
|
|
* system logger that is being used. |
147 |
|
|
*/ |
148 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
149 |
tdb |
1.8 |
|
150 |
|
|
//---STATIC ATTRIBUTES--- |
151 |
|
|
|
152 |
pjm2 |
1.1 |
} |