1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.filter; |
2 |
> |
package uk.org.iscream.cms.server.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.*; |
8 |
> |
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 |
|
|
13 |
|
/** |
14 |
|
* This class contains the main method to be run by |
15 |
< |
* the filter children. It harvests UDP traffic. |
15 |
> |
* the filterd. It harvests UDP traffic, and queues it. |
16 |
|
* |
17 |
|
* @author $Author$ |
18 |
|
* @version $Id$ |
25 |
|
* The current CVS revision of this class |
26 |
|
*/ |
27 |
|
public final String REVISION = "$Revision$"; |
28 |
< |
|
28 |
> |
|
29 |
|
/** |
30 |
|
* The maximum size of a packet |
31 |
|
*/ |
36 |
|
//---CONSTRUCTORS--- |
37 |
|
|
38 |
|
/** |
39 |
< |
* It is normal to use this constructor in preference |
40 |
< |
* to any other in this class. |
39 |
> |
* 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 |
|
*/ |
44 |
|
public UDPReader(int port, Queue queue){ |
45 |
+ |
// set the Thread name |
46 |
+ |
setName("filter.UDPReader"); |
47 |
+ |
|
48 |
|
_port = port; |
49 |
|
_queue = queue; |
50 |
+ |
_logger.write(toString(), Logger.SYSINIT, "started"); |
51 |
|
} |
52 |
|
|
53 |
|
//---PUBLIC METHODS--- |
54 |
< |
|
54 |
> |
|
55 |
> |
/** |
56 |
> |
* The main method in the class. Reads and queues XML sent |
57 |
> |
* over UDP. |
58 |
> |
*/ |
59 |
|
public void run() { |
60 |
< |
|
60 |
> |
// setup an empty ACL defaulting to ALLOW |
61 |
> |
ACL acl = new ACL(ACL.ALLOW); |
62 |
> |
|
63 |
> |
// setup a Datagram socket |
64 |
|
DatagramSocket socket = null; |
65 |
|
try { |
66 |
< |
socket = new DatagramSocket(_port); |
66 |
> |
socket = new ACLDatagramSocket(acl, _port); |
67 |
|
} |
68 |
|
catch (BindException e){ |
69 |
|
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+" as this port was already in use."); |
75 |
|
} |
76 |
|
|
77 |
|
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
78 |
< |
|
78 |
> |
|
79 |
|
byte[] buf; |
80 |
|
|
81 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
82 |
+ |
String stringACL = ""; |
83 |
+ |
String newStringACL = ""; |
84 |
+ |
|
85 |
+ |
// read UDP packets and queue them |
86 |
|
boolean running = true; |
87 |
|
while (running){ |
88 |
+ |
// get hold of the ACL in the configuration |
89 |
|
try { |
90 |
< |
|
91 |
< |
// receive request and put it in the Queue |
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 |
> |
try { |
109 |
> |
|
110 |
> |
// receive request and put it in the Queue |
111 |
|
buf = new byte[packetSizeLimit]; |
112 |
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
113 |
|
socket.receive(packet); |
116 |
|
} |
117 |
|
catch (IOException e) { |
118 |
|
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
119 |
< |
return; |
119 |
> |
running = false; |
120 |
|
} |
121 |
|
} |
122 |
|
socket.close(); |
123 |
|
} |
124 |
< |
|
124 |
> |
|
125 |
|
/** |
126 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
127 |
|
* method to provide clean logging (every class should have this). |
128 |
|
* |
129 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
129 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
130 |
|
* to format the toString() |
131 |
|
* |
132 |
|
* @return the name of this class and its CVS revision |
154 |
|
* be changed to null for utility classes. |
155 |
|
*/ |
156 |
|
private String _name = FilterMain.NAME; |
157 |
< |
|
157 |
> |
|
158 |
|
/** |
159 |
|
* This holds a reference to the |
160 |
|
* system logger that is being used. |
169 |
|
/** |
170 |
|
* The Queue object |
171 |
|
*/ |
172 |
< |
Queue _queue; |
172 |
> |
Queue _queue; |
173 |
|
|
174 |
|
//---STATIC ATTRIBUTES--- |
175 |
|
|