1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org.uk |
4 |
+ |
* 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 |
|
//---PACKAGE DECLARATION--- |
22 |
|
package uk.org.iscream.cms.server.filter; |
23 |
|
|
52 |
|
|
53 |
|
//---CONSTRUCTORS--- |
54 |
|
|
55 |
< |
/** |
55 |
> |
/** |
56 |
|
* Constructs a new TCPReader |
57 |
|
* |
58 |
|
* @param queue A reference to our Queue |
59 |
< |
* @param port The port that the TCPReader will listen on |
60 |
< |
*/ |
59 |
> |
* @param port The port that the TCPReader will listen on |
60 |
> |
*/ |
61 |
|
public TCPReader(int port, Queue queue) { |
62 |
|
// set the Thread name |
63 |
|
setName("filter.TCPReader"); |
71 |
|
|
72 |
|
//---PUBLIC METHODS--- |
73 |
|
|
74 |
< |
/** |
75 |
< |
* The run() method is the main loop for this thread, and we |
76 |
< |
* will remain in here until such a point as something goes |
77 |
< |
* wrong with the listening. After initially setting up the |
78 |
< |
* ServerSocket we go round a while loop receiving connections |
79 |
< |
* and then passing them off to other processes to deal with. |
80 |
< |
*/ |
74 |
> |
/** |
75 |
> |
* The run() method is the main loop for this thread, and we |
76 |
> |
* will remain in here until such a point as something goes |
77 |
> |
* wrong with the listening. After initially setting up the |
78 |
> |
* ServerSocket we go round a while loop receiving connections |
79 |
> |
* and then passing them off to other processes to deal with. |
80 |
> |
*/ |
81 |
|
public void run(){ |
82 |
< |
// get our ACL from the configuration |
83 |
< |
ACL tcpACL = null; |
84 |
< |
try { |
85 |
< |
String stringACL = ConfigurationProxy.getInstance().getProperty(FilterMain.NAME, "Filter.TCPACL"); |
86 |
< |
tcpACL = new ACL(stringACL); |
82 |
> |
// setup an empty ACL defaulting to ALLOW |
83 |
> |
ACL acl = new ACL(ACL.ALLOW); |
84 |
> |
|
85 |
> |
// Setup the ServerSocket so that clients can connect |
86 |
> |
ServerSocket listenPort=null; |
87 |
> |
try{ |
88 |
> |
listenPort = new ACLServerSocket(acl, _port); |
89 |
|
} |
90 |
< |
catch(PropertyNotFoundException e) { |
91 |
< |
_logger.write(toString(), Logger.WARNING, "No ACL found for TCPReader: " + e); |
90 |
> |
catch(IOException e){ |
91 |
> |
_logger.write(toString(), Logger.FATAL, "Failed to setup ServerSocket: " + e); |
92 |
> |
return; |
93 |
|
} |
94 |
< |
|
95 |
< |
ServerSocket listenPort=null; |
94 |
> |
|
95 |
> |
// Log what machine/port we're listening on |
96 |
|
try{ |
97 |
< |
// Setup the ServerSocket so that clients can connect |
98 |
< |
// use an ACLServerSocket if we have an ACL |
99 |
< |
if(tcpACL != null) { |
100 |
< |
listenPort = new ACLServerSocket(tcpACL, _port); |
78 |
< |
} |
79 |
< |
else { |
80 |
< |
listenPort = new ServerSocket(_port); |
81 |
< |
} |
97 |
> |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
98 |
> |
+InetAddress.getLocalHost().getHostName() |
99 |
> |
+"/"+InetAddress.getLocalHost().getHostAddress() |
100 |
> |
+" port "+listenPort.getLocalPort()); |
101 |
|
} |
102 |
< |
catch(IOException e){ |
102 |
> |
catch(UnknownHostException e){ |
103 |
> |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
104 |
> |
+"port "+listenPort.getLocalPort()); |
105 |
|
} |
106 |
|
|
107 |
< |
// Log what machine/port we're listening on |
87 |
< |
try{ |
88 |
< |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
89 |
< |
+InetAddress.getLocalHost().getHostName() |
90 |
< |
+"/"+InetAddress.getLocalHost().getHostAddress() |
91 |
< |
+" port "+listenPort.getLocalPort()); |
92 |
< |
} |
93 |
< |
catch(UnknownHostException e){ |
94 |
< |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
95 |
< |
+"port "+listenPort.getLocalPort()); |
96 |
< |
} |
97 |
< |
|
98 |
< |
// We use this boolean so we can break out of the while loop if we want |
107 |
> |
// We use this boolean so we can break out of the while loop if we want |
108 |
|
boolean run = true; |
109 |
< |
|
110 |
< |
// Loop round constantly until we decide to stop |
109 |
> |
|
110 |
> |
// Loop round constantly until we decide to stop |
111 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
112 |
> |
String stringACL = ""; |
113 |
> |
String newStringACL = ""; |
114 |
|
while(run){ |
115 |
< |
Socket hostSocket=null; |
115 |
> |
// get hold of the ACL in the configuration |
116 |
> |
try { |
117 |
> |
newStringACL = cp.getProperty("Filter." + FilterMain.NAME, "Filter.TCPACL"); |
118 |
> |
} |
119 |
> |
catch(PropertyNotFoundException e) { |
120 |
> |
// if we can't find it, we'll just use a null ACL |
121 |
> |
newStringACL = ""; |
122 |
> |
_logger.write(toString(), Logger.WARNING, "No ACL found for TCPReader, using empty ACL instead: " + e); |
123 |
> |
} |
124 |
> |
// check to see if the ACL has changed |
125 |
> |
if(!newStringACL.equals(stringACL)) { |
126 |
> |
_logger.write(toString(), Logger.SYSMSG, "Reloading Access Control List"); |
127 |
> |
// clear the ACL |
128 |
> |
acl.clear(); |
129 |
> |
// set the default to something sane |
130 |
> |
acl.setDefaultMode(ACL.ALLOW); |
131 |
> |
// add the new ACL (this may change the default) |
132 |
> |
acl.add(newStringACL); |
133 |
> |
stringACL = newStringACL; |
134 |
> |
} |
135 |
> |
Socket hostSocket=null; |
136 |
|
try{ |
137 |
|
_logger.write(toString(), Logger.DEBUG, "Waiting for Connection"); |
138 |
< |
// This will block until a host connects - at which point we get a Socket |
138 |
> |
// This will block until a host connects - at which point we get a Socket |
139 |
|
hostSocket = listenPort.accept(); |
140 |
|
_logger.write(toString(), Logger.DEBUG, "Connection accepted from: " + hostSocket.toString()); |
141 |
|
} |
142 |
|
catch(IOException e){ |
143 |
< |
// Something went wrong with the ServerSocket, so we'll stop listening |
143 |
> |
// Something went wrong with the ServerSocket, so we'll stop listening |
144 |
|
run=false; |
145 |
|
} |
146 |
< |
// If we've stopped on the line above we won't want to try this ! |
146 |
> |
// If we've stopped on the line above we won't want to try this ! |
147 |
|
if(run){ |
148 |
< |
try { |
149 |
< |
// Setup the TCPReaderInit and start it |
148 |
> |
try { |
149 |
> |
// Setup the TCPReaderInit and start it |
150 |
|
TCPReaderInit init = new TCPReaderInit(hostSocket, _queue); |
151 |
< |
// and start it |
151 |
> |
// and start it |
152 |
|
init.start(); |
153 |
|
} catch (IOException e) { |
154 |
|
_logger.write(toString(), Logger.ERROR, e.toString()); |
155 |
|
} |
156 |
|
} |
157 |
|
} |
158 |
< |
// Best log the fact that we're stopping |
158 |
> |
// Best log the fact that we're stopping |
159 |
|
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
160 |
|
} |
161 |
|
|
199 |
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
200 |
|
|
201 |
|
/** |
202 |
< |
* The port on which the server should listen. |
203 |
< |
*/ |
202 |
> |
* The port on which the server should listen. |
203 |
> |
*/ |
204 |
|
private int _port; |
205 |
|
|
206 |
|
/** |