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.filtermanager; |
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 uk.org.iscream.cms.server.util.*; |
29 |
import java.net.*; |
30 |
import java.io.*; |
31 |
|
32 |
/** |
33 |
* A socket listener to listen for new hosts registering with the system. |
34 |
* When a host makes a connection, the connecton is past to an instance |
35 |
* of the HostInit class, which handles further communication. |
36 |
* |
37 |
* @author $Author: tdb $ |
38 |
* @version $Id: HostListener.java,v 1.13 2002/05/18 18:16:02 tdb Exp $ |
39 |
*/ |
40 |
class HostListener extends Thread { |
41 |
|
42 |
//---FINAL ATTRIBUTES--- |
43 |
|
44 |
/** |
45 |
* The current CVS revision of this class |
46 |
*/ |
47 |
public final String REVISION = "$Revision: 1.13 $"; |
48 |
|
49 |
//---STATIC METHODS--- |
50 |
|
51 |
//---CONSTRUCTORS--- |
52 |
|
53 |
/** |
54 |
* Constructs a new listener |
55 |
* |
56 |
* @param listenPort The port that the server will listen on. |
57 |
*/ |
58 |
public HostListener() { |
59 |
// set the Thread name |
60 |
setName("filtermanager.HostListener"); |
61 |
|
62 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
63 |
} |
64 |
|
65 |
//---PUBLIC METHODS--- |
66 |
|
67 |
/** |
68 |
* The run() method is the main loop for this thread, and we |
69 |
* will remain in here until such a point as something goes |
70 |
* wrong with the listening. After initially setting up the |
71 |
* ServerSocket we go round a while loop receiving connections |
72 |
* and then passing them off to other processes to deal with. |
73 |
*/ |
74 |
public void run(){ |
75 |
// setup an empty ACL defaulting to ALLOW |
76 |
ACL acl = new ACL(ACL.ALLOW); |
77 |
|
78 |
ServerSocket listenSocket=null; |
79 |
// We use this boolean so we can break out of the while loop if we want |
80 |
boolean run = true; |
81 |
try{ |
82 |
// Work out the port we want |
83 |
int listenPort = Integer.parseInt(ConfigurationProxy.getInstance().getProperty("FilterManager." + FilterManager.NAME, "FilterManager.listenPort")); |
84 |
// Setup the ServerSocket so that clients can connect |
85 |
listenSocket = new ACLServerSocket(acl, listenPort); |
86 |
_logger.write(toString(), Logger.SYSMSG, "Server listening on " |
87 |
+InetAddress.getLocalHost().getHostName() |
88 |
+"/"+InetAddress.getLocalHost().getHostAddress() |
89 |
+" port "+listenSocket.getLocalPort()); |
90 |
|
91 |
} |
92 |
catch(UnknownHostException e){ |
93 |
_logger.write(toString(), Logger.SYSMSG, "Server listening on UnknownHost " |
94 |
+"port "+listenSocket.getLocalPort()); |
95 |
} |
96 |
catch(IOException e){ |
97 |
_logger.write(toString(), Logger.FATAL, "IO Error, can't start FilterManager :"+e); |
98 |
run = false; |
99 |
} |
100 |
catch(PropertyNotFoundException e){ |
101 |
_logger.write(toString(), Logger.FATAL, "Fatal Error, can't find config :"+e); |
102 |
run = false; |
103 |
} |
104 |
catch(NumberFormatException e){ |
105 |
_logger.write(toString(), Logger.FATAL, "Invalid port configuration found :"+e); |
106 |
run = false; |
107 |
} |
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("FilterManager." + FilterManager.NAME, "FilterManager.ACL"); |
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 FilterManager, 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.SYSMSG, "Waiting for Connection"); |
137 |
// This will block until a host connects - at which point we get a Socket |
138 |
hostSocket = listenSocket.accept(); |
139 |
_logger.write(toString(), Logger.SYSMSG, "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 HostInit so it can carry on communications with the host |
149 |
HostInit init = new HostInit(hostSocket); |
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 |
this.getClass().getName(), |
174 |
REVISION); |
175 |
} |
176 |
|
177 |
//---PRIVATE METHODS--- |
178 |
|
179 |
//---ACCESSOR/MUTATOR METHODS--- |
180 |
|
181 |
//---ATTRIBUTES--- |
182 |
|
183 |
/** |
184 |
* This holds a reference to the |
185 |
* system logger that is being used. |
186 |
*/ |
187 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
188 |
|
189 |
/** |
190 |
* This is the friendly identifier of the |
191 |
* component this class is running in. |
192 |
* eg, a Filter may be called "filter1", |
193 |
* If this class does not have an owning |
194 |
* component, a name from the configuration |
195 |
* can be placed here. This name could also |
196 |
* be changed to null for utility classes. |
197 |
*/ |
198 |
private String _name = FilterManager.NAME; |
199 |
|
200 |
//---STATIC ATTRIBUTES--- |
201 |
|
202 |
} |