1 |
tdb |
1.6 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.18 |
package uk.org.iscream.cms.server.filter; |
3 |
tdb |
1.6 |
|
4 |
|
|
//---IMPORTS--- |
5 |
pjm2 |
1.1 |
import java.io.*; |
6 |
|
|
import java.net.*; |
7 |
|
|
import java.util.*; |
8 |
tdb |
1.18 |
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 |
pjm2 |
1.1 |
|
13 |
tdb |
1.6 |
/** |
14 |
|
|
* This class contains the main method to be run by |
15 |
tdb |
1.14 |
* the filterd. It harvests UDP traffic, and queues it. |
16 |
tdb |
1.6 |
* |
17 |
tdb |
1.19 |
* @author $Author: tdb $ |
18 |
tdb |
1.22 |
* @version $Id: UDPReader.java,v 1.21 2002/03/20 13:05:50 tdb Exp $ |
19 |
tdb |
1.6 |
*/ |
20 |
pjm2 |
1.1 |
public class UDPReader extends Thread{ |
21 |
|
|
|
22 |
tdb |
1.6 |
//---FINAL ATTRIBUTES--- |
23 |
|
|
|
24 |
|
|
/** |
25 |
|
|
* The current CVS revision of this class |
26 |
|
|
*/ |
27 |
tdb |
1.22 |
public final String REVISION = "$Revision: 1.21 $"; |
28 |
tdb |
1.15 |
|
29 |
ajm |
1.11 |
/** |
30 |
|
|
* The maximum size of a packet |
31 |
|
|
*/ |
32 |
|
|
private final int packetSizeLimit = 8192; |
33 |
tdb |
1.6 |
|
34 |
|
|
//---STATIC METHODS--- |
35 |
|
|
|
36 |
|
|
//---CONSTRUCTORS--- |
37 |
|
|
|
38 |
ajm |
1.10 |
/** |
39 |
tdb |
1.14 |
* Constructs a new UDPReader. |
40 |
|
|
* |
41 |
|
|
* @param port The port on which we listen for UDP data |
42 |
|
|
* @param queue The queue which we are using |
43 |
ajm |
1.10 |
*/ |
44 |
tdb |
1.12 |
public UDPReader(int port, Queue queue){ |
45 |
tdb |
1.16 |
// set the Thread name |
46 |
|
|
setName("filter.UDPReader"); |
47 |
|
|
|
48 |
tdb |
1.8 |
_port = port; |
49 |
tdb |
1.12 |
_queue = queue; |
50 |
tdb |
1.15 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
51 |
pjm2 |
1.1 |
} |
52 |
|
|
|
53 |
tdb |
1.6 |
//---PUBLIC METHODS--- |
54 |
tdb |
1.14 |
|
55 |
|
|
/** |
56 |
|
|
* The main method in the class. Reads and queues XML sent |
57 |
|
|
* over UDP. |
58 |
|
|
*/ |
59 |
pjm2 |
1.1 |
public void run() { |
60 |
tdb |
1.22 |
// setup an empty ACL defaulting to ALLOW |
61 |
|
|
ACL acl = new ACL(ACL.ALLOW); |
62 |
tdb |
1.14 |
|
63 |
|
|
// setup a Datagram socket |
64 |
pjm2 |
1.1 |
DatagramSocket socket = null; |
65 |
|
|
try { |
66 |
tdb |
1.22 |
socket = new ACLDatagramSocket(acl, _port); |
67 |
pjm2 |
1.1 |
} |
68 |
|
|
catch (BindException e){ |
69 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+" as this port was already in use."); |
70 |
pjm2 |
1.1 |
return; |
71 |
|
|
} |
72 |
|
|
catch (Exception e){ |
73 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+"."); |
74 |
pjm2 |
1.1 |
return; |
75 |
|
|
} |
76 |
|
|
|
77 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
78 |
tdb |
1.15 |
|
79 |
pjm2 |
1.1 |
byte[] buf; |
80 |
|
|
|
81 |
tdb |
1.22 |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
82 |
|
|
String stringACL = ""; |
83 |
|
|
String newStringACL = ""; |
84 |
|
|
|
85 |
tdb |
1.14 |
// read UDP packets and queue them |
86 |
pjm2 |
1.1 |
boolean running = true; |
87 |
|
|
while (running){ |
88 |
tdb |
1.22 |
// get hold of the ACL in the configuration |
89 |
|
|
try { |
90 |
|
|
newStringACL = cp.getProperty("Filter." + FilterMain.NAME, "Filter.UDPACL"); |
91 |
|
|
} |
92 |
|
|
catch(PropertyNotFoundException e) { |
93 |
|
|
// if we can't find it, we'll just use a null ACL |
94 |
|
|
newStringACL = ""; |
95 |
|
|
_logger.write(toString(), Logger.WARNING, "No ACL found for UDPReader, using empty ACL instead: " + e); |
96 |
|
|
} |
97 |
|
|
// check to see if the ACL has changed |
98 |
|
|
if(!newStringACL.equals(stringACL)) { |
99 |
|
|
_logger.write(toString(), Logger.SYSMSG, "Reloading Access Control List"); |
100 |
|
|
// clear the ACL |
101 |
|
|
acl.clear(); |
102 |
|
|
// set the default to something sane |
103 |
|
|
acl.setDefaultMode(ACL.ALLOW); |
104 |
|
|
// add the new ACL (this may change the default) |
105 |
|
|
acl.add(newStringACL); |
106 |
|
|
stringACL = newStringACL; |
107 |
|
|
} |
108 |
pjm2 |
1.1 |
try { |
109 |
tdb |
1.15 |
|
110 |
|
|
// receive request and put it in the Queue |
111 |
pjm2 |
1.1 |
buf = new byte[packetSizeLimit]; |
112 |
|
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
113 |
|
|
socket.receive(packet); |
114 |
tdb |
1.12 |
String xml = new String(packet.getData()); |
115 |
|
|
_queue.add(xml); |
116 |
pjm2 |
1.1 |
} |
117 |
|
|
catch (IOException e) { |
118 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
119 |
tdb |
1.14 |
running = false; |
120 |
pjm2 |
1.1 |
} |
121 |
|
|
} |
122 |
|
|
socket.close(); |
123 |
|
|
} |
124 |
tdb |
1.15 |
|
125 |
tdb |
1.6 |
/** |
126 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
127 |
|
|
* method to provide clean logging (every class should have this). |
128 |
|
|
* |
129 |
tdb |
1.18 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
130 |
ajm |
1.11 |
* to format the toString() |
131 |
|
|
* |
132 |
tdb |
1.6 |
* @return the name of this class and its CVS revision |
133 |
|
|
*/ |
134 |
|
|
public String toString() { |
135 |
ajm |
1.11 |
return FormatName.getName( |
136 |
|
|
_name, |
137 |
|
|
getClass().getName(), |
138 |
|
|
REVISION); |
139 |
tdb |
1.6 |
} |
140 |
|
|
|
141 |
|
|
//---PRIVATE METHODS--- |
142 |
|
|
|
143 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
144 |
|
|
|
145 |
|
|
//---ATTRIBUTES--- |
146 |
|
|
|
147 |
ajm |
1.11 |
/** |
148 |
|
|
* This is the friendly identifier of the |
149 |
|
|
* component this class is running in. |
150 |
|
|
* eg, a Filter may be called "filter1", |
151 |
|
|
* If this class does not have an owning |
152 |
|
|
* component, a name from the configuration |
153 |
|
|
* can be placed here. This name could also |
154 |
|
|
* be changed to null for utility classes. |
155 |
|
|
*/ |
156 |
|
|
private String _name = FilterMain.NAME; |
157 |
tdb |
1.15 |
|
158 |
ajm |
1.11 |
/** |
159 |
|
|
* This holds a reference to the |
160 |
|
|
* system logger that is being used. |
161 |
|
|
*/ |
162 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
163 |
|
|
|
164 |
|
|
/** |
165 |
|
|
* The port that this reader is using |
166 |
|
|
*/ |
167 |
tdb |
1.8 |
int _port; |
168 |
ajm |
1.11 |
|
169 |
|
|
/** |
170 |
tdb |
1.12 |
* The Queue object |
171 |
ajm |
1.11 |
*/ |
172 |
tdb |
1.15 |
Queue _queue; |
173 |
tdb |
1.6 |
|
174 |
|
|
//---STATIC ATTRIBUTES--- |
175 |
|
|
|
176 |
pjm2 |
1.1 |
} |