1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org |
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.ac.ukc.iscream.filtermanager; |
22 |
> |
package uk.org.iscream.cms.server.filtermanager; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
< |
import uk.ac.ukc.iscream.core.*; |
26 |
< |
import uk.ac.ukc.iscream.filter.*; |
27 |
< |
import java.net.Socket; |
28 |
< |
import java.net.ServerSocket; |
29 |
< |
import java.io.OutputStream; |
30 |
< |
import java.io.IOException; |
11 |
< |
import java.net.InetAddress; |
12 |
< |
import java.net.UnknownHostException; |
13 |
< |
import org.omg.CORBA.*; |
14 |
< |
import org.omg.CosNaming.*; |
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.util.*; |
29 |
> |
import java.net.*; |
30 |
> |
import java.io.*; |
31 |
|
|
32 |
|
/** |
33 |
|
* A socket listener to listen for new hosts registering with the system. |
53 |
|
/** |
54 |
|
* Constructs a new listener |
55 |
|
* |
56 |
< |
* @param logger a reference to the logger we are using |
41 |
< |
* @param configManager a reference to the ConfigurationManager we are using |
42 |
< |
* @param port The port that the server will listen on. |
56 |
> |
* @param listenPort The port that the server will listen on. |
57 |
|
*/ |
58 |
< |
public HostListener(Logger logger, ConfigurationManager configManager, int port, NamingContextExt ncRef) { |
59 |
< |
_logger = logger; |
60 |
< |
_ncRef = ncRef; |
61 |
< |
_configManager = configManager; |
48 |
< |
_port = port; |
58 |
> |
public HostListener() { |
59 |
> |
// set the Thread name |
60 |
> |
setName("filtermanager.HostListener"); |
61 |
> |
|
62 |
|
_logger.write(toString(), Logger.SYSINIT, "started"); |
63 |
|
} |
64 |
|
|
52 |
– |
|
53 |
– |
|
65 |
|
//---PUBLIC METHODS--- |
66 |
|
|
67 |
|
/** |
72 |
|
* and then passing them off to other processes to deal with. |
73 |
|
*/ |
74 |
|
public void run(){ |
75 |
< |
ServerSocket listenPort=null; |
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 |
< |
listenPort = new ServerSocket(_port); |
86 |
< |
} |
71 |
< |
catch(IOException e){ |
72 |
< |
} |
73 |
< |
// Log what machine/port we're listening on |
74 |
< |
try{ |
75 |
< |
_logger.write(toString(), Logger.SYSMSG, "Server listening on " |
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 "+listenPort.getLocalPort()); |
90 |
< |
} |
91 |
< |
catch(UnknownHostException e){ |
89 |
> |
+" port "+listenSocket.getLocalPort()); |
90 |
> |
|
91 |
> |
} |
92 |
> |
catch(UnknownHostException e){ |
93 |
|
_logger.write(toString(), Logger.SYSMSG, "Server listening on UnknownHost " |
94 |
< |
+"port "+listenPort.getLocalPort()); |
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 = listenPort.accept(); |
138 |
> |
hostSocket = listenSocket.accept(); |
139 |
|
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
140 |
|
} |
141 |
|
catch(IOException e){ |
146 |
|
if(run){ |
147 |
|
try { |
148 |
|
// Setup the HostInit so it can carry on communications with the host |
149 |
< |
HostInit init = new HostInit(hostSocket, _configManager, _logger, _ncRef); |
149 |
> |
HostInit init = new HostInit(hostSocket); |
150 |
|
// and start it |
151 |
|
init.start(); |
152 |
|
} catch (IOException e) { |
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.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 this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
171 |
> |
return FormatName.getName( |
172 |
> |
_name, |
173 |
> |
this.getClass().getName(), |
174 |
> |
REVISION); |
175 |
|
} |
176 |
|
|
177 |
|
//---PRIVATE METHODS--- |
181 |
|
//---ATTRIBUTES--- |
182 |
|
|
183 |
|
/** |
184 |
< |
* A reference to the logger the system is using |
184 |
> |
* This holds a reference to the |
185 |
> |
* system logger that is being used. |
186 |
|
*/ |
187 |
< |
Logger _logger; |
187 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
188 |
|
|
189 |
|
/** |
190 |
< |
* A reference to the configurator the system is using |
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 |
< |
ConfigurationManager _configManager; |
138 |
< |
|
139 |
< |
/** |
140 |
< |
* The port on which the server should listen. |
141 |
< |
*/ |
142 |
< |
private int _port; |
143 |
< |
|
144 |
< |
private NamingContextExt _ncRef; |
198 |
> |
private String _name = FilterManager.NAME; |
199 |
|
|
200 |
|
//---STATIC ATTRIBUTES--- |
201 |
|
|