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