1 |
+ |
//---PACKAGE DECLARATION--- |
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.filter.*; |
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 |
< |
// This class contains the main method to be run by |
14 |
< |
// the filter children. It harvests UDP traffic. |
15 |
< |
// |
16 |
< |
// |
13 |
> |
/** |
14 |
> |
* This class contains the main method to be run by |
15 |
> |
* the filterd. It harvests UDP traffic, and queues it. |
16 |
> |
* |
17 |
> |
* @author $Author$ |
18 |
> |
* @version $Id$ |
19 |
> |
*/ |
20 |
|
public class UDPReader extends Thread{ |
21 |
|
|
22 |
< |
// It is normal to use this constructor in preference |
23 |
< |
// to any other in this class. |
24 |
< |
public UDPReader(int port, Filter parent, Logger logger){ |
25 |
< |
this.logger = logger; |
26 |
< |
this.port = port; |
27 |
< |
this.parent = parent; |
19 |
< |
} |
22 |
> |
//---FINAL ATTRIBUTES--- |
23 |
> |
|
24 |
> |
/** |
25 |
> |
* The current CVS revision of this class |
26 |
> |
*/ |
27 |
> |
public final String REVISION = "$Revision$"; |
28 |
|
|
29 |
< |
public UDPReader(Filter parent, Logger logger){ |
30 |
< |
this(4589, parent, logger); |
29 |
> |
/** |
30 |
> |
* The maximum size of a packet |
31 |
> |
*/ |
32 |
> |
private final int packetSizeLimit = 8192; |
33 |
> |
|
34 |
> |
//---STATIC METHODS--- |
35 |
> |
|
36 |
> |
//---CONSTRUCTORS--- |
37 |
> |
|
38 |
> |
/** |
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 |
+ |
|
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.SYSMSG, "Could not start the UDPReader thread on port "+port+" as this port was already in use."); |
69 |
> |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+" as this port was already in use."); |
70 |
|
return; |
71 |
|
} |
72 |
|
catch (Exception e){ |
73 |
< |
logger.write(this.toString(), Logger.SYSMSG, "Could not start the UDPReader thread on port "+port+"."); |
73 |
> |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+"."); |
74 |
|
return; |
75 |
|
} |
76 |
|
|
77 |
< |
logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+port); |
78 |
< |
|
77 |
> |
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
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 |
+ |
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]; |
48 |
– |
// receive request |
112 |
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
113 |
|
socket.receive(packet); |
114 |
< |
FilterThread t = new FilterThread(parent); |
115 |
< |
t.run(packet); |
114 |
> |
String xml = new String(packet.getData()); |
115 |
> |
_queue.add(xml); |
116 |
|
} |
117 |
|
catch (IOException e) { |
118 |
< |
logger.write(this.toString(), Logger.SYSMSG, "The UDPReader thread has been shut down as an exception occured: "+e); |
119 |
< |
return; |
118 |
> |
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
119 |
> |
running = false; |
120 |
|
} |
121 |
|
} |
122 |
|
socket.close(); |
123 |
|
} |
124 |
|
|
125 |
< |
|
126 |
< |
Logger logger; |
127 |
< |
int port; |
128 |
< |
Filter parent; |
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.org.iscream.cms.server.util.NameFormat class |
130 |
> |
* to format the toString() |
131 |
> |
* |
132 |
> |
* @return the name of this class and its CVS revision |
133 |
> |
*/ |
134 |
> |
public String toString() { |
135 |
> |
return FormatName.getName( |
136 |
> |
_name, |
137 |
> |
getClass().getName(), |
138 |
> |
REVISION); |
139 |
> |
} |
140 |
|
|
141 |
< |
final int packetSizeLimit = 8192; |
141 |
> |
//---PRIVATE METHODS--- |
142 |
> |
|
143 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
144 |
> |
|
145 |
> |
//---ATTRIBUTES--- |
146 |
> |
|
147 |
> |
/** |
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 |
|
|
158 |
+ |
/** |
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 |
+ |
int _port; |
168 |
+ |
|
169 |
+ |
/** |
170 |
+ |
* The Queue object |
171 |
+ |
*/ |
172 |
+ |
Queue _queue; |
173 |
+ |
|
174 |
+ |
//---STATIC ATTRIBUTES--- |
175 |
+ |
|
176 |
|
} |