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