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 |
|
24 |
//---IMPORTS--- |
25 |
import uk.org.iscream.cms.server.core.*; |
26 |
import uk.org.iscream.cms.server.componentmanager.*; |
27 |
import uk.org.iscream.cms.server.filter.*; |
28 |
import java.net.Socket; |
29 |
import java.net.ServerSocket; |
30 |
import java.io.OutputStream; |
31 |
import java.io.IOException; |
32 |
import java.net.InetAddress; |
33 |
import java.net.UnknownHostException; |
34 |
import uk.org.iscream.cms.util.*; |
35 |
|
36 |
/** |
37 |
* Reads TCP Heartbeats from the host applications. |
38 |
* |
39 |
* @author $Author: tdb $ |
40 |
* @version $Id: TCPReader.java,v 1.19 2003/02/05 16:43:47 tdb Exp $ |
41 |
*/ |
42 |
class TCPReader extends Thread { |
43 |
|
44 |
//---FINAL ATTRIBUTES--- |
45 |
|
46 |
/** |
47 |
* The current CVS revision of this class |
48 |
*/ |
49 |
public final String REVISION = "$Revision: 1.19 $"; |
50 |
|
51 |
//---STATIC METHODS--- |
52 |
|
53 |
//---CONSTRUCTORS--- |
54 |
|
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 |
*/ |
61 |
public TCPReader(int port, Queue queue) { |
62 |
// set the Thread name |
63 |
setName("filter.TCPReader"); |
64 |
|
65 |
_port = port; |
66 |
_queue = queue; |
67 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
68 |
} |
69 |
|
70 |
|
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 |
*/ |
81 |
public void run(){ |
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(IOException e){ |
91 |
_logger.write(toString(), Logger.FATAL, "Failed to setup ServerSocket: " + e); |
92 |
return; |
93 |
} |
94 |
|
95 |
// Log what machine/port we're listening on |
96 |
try{ |
97 |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
98 |
+InetAddress.getLocalHost().getHostName() |
99 |
+"/"+InetAddress.getLocalHost().getHostAddress() |
100 |
+" port "+listenPort.getLocalPort()); |
101 |
} |
102 |
catch(UnknownHostException e){ |
103 |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
104 |
+"port "+listenPort.getLocalPort()); |
105 |
} |
106 |
|
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 |
111 |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
112 |
String stringACL = ""; |
113 |
String newStringACL = ""; |
114 |
while(run){ |
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 |
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 |
144 |
run=false; |
145 |
} |
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 |
150 |
TCPReaderInit init = new TCPReaderInit(hostSocket, _queue); |
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 |
159 |
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
160 |
} |
161 |
|
162 |
/** |
163 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
164 |
* method to provide clean logging (every class should have this). |
165 |
* |
166 |
* This uses the uk.org.iscream.cms.util.NameFormat class |
167 |
* to format the toString() |
168 |
* |
169 |
* @return the name of this class and its CVS revision |
170 |
*/ |
171 |
public String toString() { |
172 |
return FormatName.getName( |
173 |
_name, |
174 |
getClass().getName(), |
175 |
REVISION); |
176 |
} |
177 |
|
178 |
//---PRIVATE METHODS--- |
179 |
|
180 |
//---ACCESSOR/MUTATOR METHODS--- |
181 |
|
182 |
//---ATTRIBUTES--- |
183 |
|
184 |
/** |
185 |
* This is the friendly identifier of the |
186 |
* component this class is running in. |
187 |
* eg, a Filter may be called "filter1", |
188 |
* If this class does not have an owning |
189 |
* component, a name from the configuration |
190 |
* can be placed here. This name could also |
191 |
* be changed to null for utility classes. |
192 |
*/ |
193 |
private String _name = FilterMain.NAME; |
194 |
|
195 |
/** |
196 |
* This holds a reference to the |
197 |
* system logger that is being used. |
198 |
*/ |
199 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
200 |
|
201 |
/** |
202 |
* The port on which the server should listen. |
203 |
*/ |
204 |
private int _port; |
205 |
|
206 |
/** |
207 |
* A reference to our Queue |
208 |
*/ |
209 |
private Queue _queue; |
210 |
|
211 |
//---STATIC ATTRIBUTES--- |
212 |
|
213 |
} |