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