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