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.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 filterd. It harvests UDP traffic, and queues it. |
16 |
* |
17 |
* @author $Author: tdb $ |
18 |
* @version $Id: UDPReader.java,v 1.19 2002/03/19 17:22:12 tdb Exp $ |
19 |
*/ |
20 |
public class UDPReader extends Thread{ |
21 |
|
22 |
//---FINAL ATTRIBUTES--- |
23 |
|
24 |
/** |
25 |
* The current CVS revision of this class |
26 |
*/ |
27 |
public final String REVISION = "$Revision: 1.19 $"; |
28 |
|
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 |
// get our ACL from the configuration |
61 |
ACL acl = null; |
62 |
try { |
63 |
String stringACL = ConfigurationProxy.getInstance().getProperty(FilterMain.NAME, "Filter.UDPACL"); |
64 |
acl = new ACL(stringACL); |
65 |
} |
66 |
catch(PropertyNotFoundException e) { |
67 |
_logger.write(toString(), Logger.WARNING, "No ACL found for UDPReader: " + e); |
68 |
} |
69 |
|
70 |
// setup a Datagram socket |
71 |
DatagramSocket socket = null; |
72 |
try { |
73 |
// use an ACLServerSocket if we have an ACL |
74 |
if(acl != null) { |
75 |
socket = new ACLDatagramSocket(acl, _port); |
76 |
} |
77 |
else { |
78 |
socket = new DatagramSocket(_port); |
79 |
} |
80 |
} |
81 |
catch (BindException e){ |
82 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+" as this port was already in use."); |
83 |
return; |
84 |
} |
85 |
catch (Exception e){ |
86 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+"."); |
87 |
return; |
88 |
} |
89 |
|
90 |
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
91 |
|
92 |
byte[] buf; |
93 |
|
94 |
// read UDP packets and queue them |
95 |
boolean running = true; |
96 |
while (running){ |
97 |
try { |
98 |
|
99 |
// receive request and put it in the Queue |
100 |
buf = new byte[packetSizeLimit]; |
101 |
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
102 |
socket.receive(packet); |
103 |
String xml = new String(packet.getData()); |
104 |
_queue.add(xml); |
105 |
} |
106 |
catch (IOException e) { |
107 |
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
108 |
running = false; |
109 |
} |
110 |
} |
111 |
socket.close(); |
112 |
} |
113 |
|
114 |
/** |
115 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
116 |
* method to provide clean logging (every class should have this). |
117 |
* |
118 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
119 |
* to format the toString() |
120 |
* |
121 |
* @return the name of this class and its CVS revision |
122 |
*/ |
123 |
public String toString() { |
124 |
return FormatName.getName( |
125 |
_name, |
126 |
getClass().getName(), |
127 |
REVISION); |
128 |
} |
129 |
|
130 |
//---PRIVATE METHODS--- |
131 |
|
132 |
//---ACCESSOR/MUTATOR METHODS--- |
133 |
|
134 |
//---ATTRIBUTES--- |
135 |
|
136 |
/** |
137 |
* This is the friendly identifier of the |
138 |
* component this class is running in. |
139 |
* eg, a Filter may be called "filter1", |
140 |
* If this class does not have an owning |
141 |
* component, a name from the configuration |
142 |
* can be placed here. This name could also |
143 |
* be changed to null for utility classes. |
144 |
*/ |
145 |
private String _name = FilterMain.NAME; |
146 |
|
147 |
/** |
148 |
* This holds a reference to the |
149 |
* system logger that is being used. |
150 |
*/ |
151 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
152 |
|
153 |
/** |
154 |
* The port that this reader is using |
155 |
*/ |
156 |
int _port; |
157 |
|
158 |
/** |
159 |
* The Queue object |
160 |
*/ |
161 |
Queue _queue; |
162 |
|
163 |
//---STATIC ATTRIBUTES--- |
164 |
|
165 |
} |