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