1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* Copyright (C) 2000-2002 i-scream |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
//---PACKAGE DECLARATION--- |
21 |
package uk.org.iscream.cms.server.filter; |
22 |
|
23 |
//---IMPORTS--- |
24 |
import uk.org.iscream.cms.server.core.*; |
25 |
import uk.org.iscream.cms.server.componentmanager.*; |
26 |
import uk.org.iscream.cms.server.filter.*; |
27 |
import java.net.Socket; |
28 |
import java.net.ServerSocket; |
29 |
import java.io.OutputStream; |
30 |
import java.io.IOException; |
31 |
import java.net.InetAddress; |
32 |
import java.net.UnknownHostException; |
33 |
import uk.org.iscream.cms.server.util.*; |
34 |
|
35 |
/** |
36 |
* Reads TCP Heartbeats from the host applications. |
37 |
* |
38 |
* @author $Author: tdb $ |
39 |
* @version $Id: TCPReader.java,v 1.16 2002/03/21 13:01:21 tdb Exp $ |
40 |
*/ |
41 |
class TCPReader extends Thread { |
42 |
|
43 |
//---FINAL ATTRIBUTES--- |
44 |
|
45 |
/** |
46 |
* The current CVS revision of this class |
47 |
*/ |
48 |
public final String REVISION = "$Revision: 1.16 $"; |
49 |
|
50 |
//---STATIC METHODS--- |
51 |
|
52 |
//---CONSTRUCTORS--- |
53 |
|
54 |
/** |
55 |
* Constructs a new TCPReader |
56 |
* |
57 |
* @param queue A reference to our Queue |
58 |
* @param port The port that the TCPReader will listen on |
59 |
*/ |
60 |
public TCPReader(int port, Queue queue) { |
61 |
// set the Thread name |
62 |
setName("filter.TCPReader"); |
63 |
|
64 |
_port = port; |
65 |
_queue = queue; |
66 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
67 |
} |
68 |
|
69 |
|
70 |
|
71 |
//---PUBLIC METHODS--- |
72 |
|
73 |
/** |
74 |
* The run() method is the main loop for this thread, and we |
75 |
* will remain in here until such a point as something goes |
76 |
* wrong with the listening. After initially setting up the |
77 |
* ServerSocket we go round a while loop receiving connections |
78 |
* and then passing them off to other processes to deal with. |
79 |
*/ |
80 |
public void run(){ |
81 |
// setup an empty ACL defaulting to ALLOW |
82 |
ACL acl = new ACL(ACL.ALLOW); |
83 |
|
84 |
// Setup the ServerSocket so that clients can connect |
85 |
ServerSocket listenPort=null; |
86 |
try{ |
87 |
listenPort = new ACLServerSocket(acl, _port); |
88 |
} |
89 |
catch(IOException e){ |
90 |
_logger.write(toString(), Logger.FATAL, "Failed to setup ServerSocket: " + e); |
91 |
return; |
92 |
} |
93 |
|
94 |
// Log what machine/port we're listening on |
95 |
try{ |
96 |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
97 |
+InetAddress.getLocalHost().getHostName() |
98 |
+"/"+InetAddress.getLocalHost().getHostAddress() |
99 |
+" port "+listenPort.getLocalPort()); |
100 |
} |
101 |
catch(UnknownHostException e){ |
102 |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
103 |
+"port "+listenPort.getLocalPort()); |
104 |
} |
105 |
|
106 |
// We use this boolean so we can break out of the while loop if we want |
107 |
boolean run = true; |
108 |
|
109 |
// Loop round constantly until we decide to stop |
110 |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
111 |
String stringACL = ""; |
112 |
String newStringACL = ""; |
113 |
while(run){ |
114 |
// get hold of the ACL in the configuration |
115 |
try { |
116 |
newStringACL = cp.getProperty("Filter." + FilterMain.NAME, "Filter.TCPACL"); |
117 |
} |
118 |
catch(PropertyNotFoundException e) { |
119 |
// if we can't find it, we'll just use a null ACL |
120 |
newStringACL = ""; |
121 |
_logger.write(toString(), Logger.WARNING, "No ACL found for TCPReader, using empty ACL instead: " + e); |
122 |
} |
123 |
// check to see if the ACL has changed |
124 |
if(!newStringACL.equals(stringACL)) { |
125 |
_logger.write(toString(), Logger.SYSMSG, "Reloading Access Control List"); |
126 |
// clear the ACL |
127 |
acl.clear(); |
128 |
// set the default to something sane |
129 |
acl.setDefaultMode(ACL.ALLOW); |
130 |
// add the new ACL (this may change the default) |
131 |
acl.add(newStringACL); |
132 |
stringACL = newStringACL; |
133 |
} |
134 |
Socket hostSocket=null; |
135 |
try{ |
136 |
_logger.write(toString(), Logger.DEBUG, "Waiting for Connection"); |
137 |
// This will block until a host connects - at which point we get a Socket |
138 |
hostSocket = listenPort.accept(); |
139 |
_logger.write(toString(), Logger.DEBUG, "Connection accepted from: " + hostSocket.toString()); |
140 |
} |
141 |
catch(IOException e){ |
142 |
// Something went wrong with the ServerSocket, so we'll stop listening |
143 |
run=false; |
144 |
} |
145 |
// If we've stopped on the line above we won't want to try this ! |
146 |
if(run){ |
147 |
try { |
148 |
// Setup the TCPReaderInit and start it |
149 |
TCPReaderInit init = new TCPReaderInit(hostSocket, _queue); |
150 |
// and start it |
151 |
init.start(); |
152 |
} catch (IOException e) { |
153 |
_logger.write(toString(), Logger.ERROR, e.toString()); |
154 |
} |
155 |
} |
156 |
} |
157 |
// Best log the fact that we're stopping |
158 |
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
159 |
} |
160 |
|
161 |
/** |
162 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
163 |
* method to provide clean logging (every class should have this). |
164 |
* |
165 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
166 |
* to format the toString() |
167 |
* |
168 |
* @return the name of this class and its CVS revision |
169 |
*/ |
170 |
public String toString() { |
171 |
return FormatName.getName( |
172 |
_name, |
173 |
getClass().getName(), |
174 |
REVISION); |
175 |
} |
176 |
|
177 |
//---PRIVATE METHODS--- |
178 |
|
179 |
//---ACCESSOR/MUTATOR METHODS--- |
180 |
|
181 |
//---ATTRIBUTES--- |
182 |
|
183 |
/** |
184 |
* This is the friendly identifier of the |
185 |
* component this class is running in. |
186 |
* eg, a Filter may be called "filter1", |
187 |
* If this class does not have an owning |
188 |
* component, a name from the configuration |
189 |
* can be placed here. This name could also |
190 |
* be changed to null for utility classes. |
191 |
*/ |
192 |
private String _name = FilterMain.NAME; |
193 |
|
194 |
/** |
195 |
* This holds a reference to the |
196 |
* system logger that is being used. |
197 |
*/ |
198 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
199 |
|
200 |
/** |
201 |
* The port on which the server should listen. |
202 |
*/ |
203 |
private int _port; |
204 |
|
205 |
/** |
206 |
* A reference to our Queue |
207 |
*/ |
208 |
private Queue _queue; |
209 |
|
210 |
//---STATIC ATTRIBUTES--- |
211 |
|
212 |
} |