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.filter.*; |
10 |
import uk.ac.ukc.iscream.util.*; |
11 |
|
12 |
/** |
13 |
* Handle an incoming packet as a separate thread. |
14 |
* Passes the data through various plugins, then |
15 |
* passes it on to the parent filter. |
16 |
* |
17 |
* @author $Author: tdb1 $ |
18 |
* @version $Id: FilterThread.java,v 1.13 2000/12/06 22:57:45 tdb1 Exp $ |
19 |
*/ |
20 |
public class FilterThread extends Thread{ |
21 |
|
22 |
//---FINAL ATTRIBUTES--- |
23 |
|
24 |
/** |
25 |
* The current CVS revision of this class |
26 |
*/ |
27 |
public final String REVISION = "$Revision: 1.13 $"; |
28 |
|
29 |
//---STATIC METHODS--- |
30 |
|
31 |
//---CONSTRUCTORS--- |
32 |
|
33 |
/** |
34 |
* Class constructor. Obtains the byte[] from a DatagramPacket. |
35 |
*/ |
36 |
public FilterThread(DatagramPacket packet, Filter parent){ |
37 |
_parent = parent; |
38 |
_rawPacket = packet.getData(); |
39 |
_logger.write(toString(), Logger.DEBUG, "created"); |
40 |
|
41 |
} |
42 |
|
43 |
/** |
44 |
* Class constructor for passing XML Strings. |
45 |
*/ |
46 |
public FilterThread(String xml, Filter parent){ |
47 |
_parent = parent; |
48 |
_rawPacket = xml.getBytes(); |
49 |
_logger.write(toString(), Logger.DEBUG, "created"); |
50 |
} |
51 |
|
52 |
//---PUBLIC METHODS--- |
53 |
|
54 |
/** |
55 |
* Runs the thread |
56 |
*/ |
57 |
public void run(){ |
58 |
|
59 |
// Get a string without any null characters in it. |
60 |
// -- maybe String.trim() would be better here ? |
61 |
String xml = new String(_rawPacket); |
62 |
if (xml.indexOf(0) != -1) { |
63 |
xml = xml.substring(0, xml.indexOf(0)); |
64 |
} |
65 |
else { |
66 |
xml = xml.substring(0, xml.length()); |
67 |
} |
68 |
|
69 |
// Use my XMLPacketMaker to make an XMLPacket object. |
70 |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
71 |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
72 |
|
73 |
if(packet != null && PluginFilterManager.getInstance().runFilters(packet)) { |
74 |
// packet is not null |
75 |
// packet was not dropped by a plugin |
76 |
// ... best pass it on ! |
77 |
_parent.receiveXML(xml); |
78 |
} |
79 |
else { |
80 |
// either we had a null, or a plugin dropped it |
81 |
_logger.write(toString(), Logger.DEBUG, "An XML packet was sucessfully filtered from the system."); |
82 |
} |
83 |
|
84 |
} |
85 |
|
86 |
/** |
87 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
88 |
* method to provide clean logging (every class should have this). |
89 |
* |
90 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
91 |
* to format the toString() |
92 |
* |
93 |
* @return the name of this class and its CVS revision |
94 |
*/ |
95 |
public String toString() { |
96 |
return FormatName.getName( |
97 |
_name, |
98 |
getClass().getName(), |
99 |
REVISION); |
100 |
} |
101 |
|
102 |
//---PRIVATE METHODS--- |
103 |
|
104 |
//---ACCESSOR/MUTATOR METHODS--- |
105 |
|
106 |
//---ATTRIBUTES--- |
107 |
|
108 |
/** |
109 |
* Our parent filter |
110 |
*/ |
111 |
Filter _parent; |
112 |
|
113 |
/** |
114 |
* The raw packet data |
115 |
*/ |
116 |
byte[] _rawPacket; |
117 |
|
118 |
/** |
119 |
* This is the friendly identifier of the |
120 |
* component this class is running in. |
121 |
* eg, a Filter may be called "filter1", |
122 |
* If this class does not have an owning |
123 |
* component, a name from the configuration |
124 |
* can be placed here. This name could also |
125 |
* be changed to null for utility classes. |
126 |
*/ |
127 |
private String _name = FilterMain.NAME; |
128 |
|
129 |
/** |
130 |
* This holds a reference to the |
131 |
* system logger that is being used. |
132 |
*/ |
133 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
134 |
|
135 |
//---STATIC ATTRIBUTES--- |
136 |
|
137 |
} |