1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.filtermanager; |
2 |
> |
package uk.org.iscream.cms.server.filtermanager; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.ac.ukc.iscream.core.*; |
6 |
< |
import uk.ac.ukc.iscream.filter.*; |
7 |
< |
import java.net.Socket; |
8 |
< |
import java.net.ServerSocket; |
9 |
< |
import java.io.OutputStream; |
10 |
< |
import java.io.IOException; |
11 |
< |
import java.net.InetAddress; |
12 |
< |
import java.net.UnknownHostException; |
13 |
< |
import org.omg.CORBA.*; |
14 |
< |
import org.omg.CosNaming.*; |
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 uk.org.iscream.cms.server.util.*; |
9 |
> |
import java.net.*; |
10 |
> |
import java.io.*; |
11 |
|
|
12 |
|
/** |
13 |
|
* A socket listener to listen for new hosts registering with the system. |
33 |
|
/** |
34 |
|
* Constructs a new listener |
35 |
|
* |
36 |
< |
* @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. |
36 |
> |
* @param listenPort The port that the server will listen on. |
37 |
|
*/ |
38 |
< |
public HostListener(Logger logger, ConfigurationManager configManager, int port, NamingContextExt ncRef) { |
39 |
< |
_logger = logger; |
40 |
< |
_ncRef = ncRef; |
41 |
< |
_configManager = configManager; |
48 |
< |
_port = port; |
38 |
> |
public HostListener() { |
39 |
> |
// set the Thread name |
40 |
> |
setName("filtermanager.HostListener"); |
41 |
> |
|
42 |
|
_logger.write(toString(), Logger.SYSINIT, "started"); |
43 |
|
} |
44 |
|
|
52 |
– |
|
53 |
– |
|
45 |
|
//---PUBLIC METHODS--- |
46 |
|
|
47 |
|
/** |
52 |
|
* and then passing them off to other processes to deal with. |
53 |
|
*/ |
54 |
|
public void run(){ |
55 |
< |
ServerSocket listenPort=null; |
55 |
> |
// setup an empty ACL defaulting to ALLOW |
56 |
> |
ACL acl = new ACL(ACL.ALLOW); |
57 |
> |
|
58 |
> |
ServerSocket listenSocket=null; |
59 |
|
// We use this boolean so we can break out of the while loop if we want |
60 |
|
boolean run = true; |
61 |
|
try{ |
62 |
+ |
// Work out the port we want |
63 |
+ |
int listenPort = Integer.parseInt(ConfigurationProxy.getInstance().getProperty("FilterManager." + FilterManager.NAME, "FilterManager.listenPort")); |
64 |
|
// Setup the ServerSocket so that clients can connect |
65 |
< |
listenPort = new ServerSocket(_port); |
66 |
< |
} |
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 " |
65 |
> |
listenSocket = new ACLServerSocket(acl, listenPort); |
66 |
> |
_logger.write(toString(), Logger.SYSMSG, "Server listening on " |
67 |
|
+InetAddress.getLocalHost().getHostName() |
68 |
|
+"/"+InetAddress.getLocalHost().getHostAddress() |
69 |
< |
+" port "+listenPort.getLocalPort()); |
70 |
< |
} |
71 |
< |
catch(UnknownHostException e){ |
69 |
> |
+" port "+listenSocket.getLocalPort()); |
70 |
> |
|
71 |
> |
} |
72 |
> |
catch(UnknownHostException e){ |
73 |
|
_logger.write(toString(), Logger.SYSMSG, "Server listening on UnknownHost " |
74 |
< |
+"port "+listenPort.getLocalPort()); |
74 |
> |
+"port "+listenSocket.getLocalPort()); |
75 |
|
} |
76 |
+ |
catch(IOException e){ |
77 |
+ |
_logger.write(toString(), Logger.FATAL, "IO Error, can't start FilterManager :"+e); |
78 |
+ |
run = false; |
79 |
+ |
} |
80 |
+ |
catch(PropertyNotFoundException e){ |
81 |
+ |
_logger.write(toString(), Logger.FATAL, "Fatal Error, can't find config :"+e); |
82 |
+ |
run = false; |
83 |
+ |
} |
84 |
+ |
catch(NumberFormatException e){ |
85 |
+ |
_logger.write(toString(), Logger.FATAL, "Invalid port configuration found :"+e); |
86 |
+ |
run = false; |
87 |
+ |
} |
88 |
+ |
|
89 |
|
// Loop round constantly until we decide to stop |
90 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
91 |
+ |
String stringACL = ""; |
92 |
+ |
String newStringACL = ""; |
93 |
|
while(run){ |
94 |
+ |
// get hold of the ACL in the configuration |
95 |
+ |
try { |
96 |
+ |
newStringACL = cp.getProperty("FilterManager." + FilterManager.NAME, "FilterManager.ACL"); |
97 |
+ |
} |
98 |
+ |
catch(PropertyNotFoundException e) { |
99 |
+ |
// if we can't find it, we'll just use a null ACL |
100 |
+ |
newStringACL = ""; |
101 |
+ |
_logger.write(toString(), Logger.WARNING, "No ACL found for FilterManager, using empty ACL instead: " + e); |
102 |
+ |
} |
103 |
+ |
// check to see if the ACL has changed |
104 |
+ |
if(!newStringACL.equals(stringACL)) { |
105 |
+ |
_logger.write(toString(), Logger.SYSMSG, "Reloading Access Control List"); |
106 |
+ |
// clear the ACL |
107 |
+ |
acl.clear(); |
108 |
+ |
// set the default to something sane |
109 |
+ |
acl.setDefaultMode(ACL.ALLOW); |
110 |
+ |
// add the new ACL (this may change the default) |
111 |
+ |
acl.add(newStringACL); |
112 |
+ |
stringACL = newStringACL; |
113 |
+ |
} |
114 |
|
Socket hostSocket=null; |
115 |
|
try{ |
116 |
|
_logger.write(toString(), Logger.SYSMSG, "Waiting for Connection"); |
117 |
|
// This will block until a host connects - at which point we get a Socket |
118 |
< |
hostSocket = listenPort.accept(); |
118 |
> |
hostSocket = listenSocket.accept(); |
119 |
|
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
120 |
|
} |
121 |
|
catch(IOException e){ |
126 |
|
if(run){ |
127 |
|
try { |
128 |
|
// Setup the HostInit so it can carry on communications with the host |
129 |
< |
HostInit init = new HostInit(hostSocket, _configManager, _logger, _ncRef); |
129 |
> |
HostInit init = new HostInit(hostSocket); |
130 |
|
// and start it |
131 |
|
init.start(); |
132 |
|
} catch (IOException e) { |
142 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
143 |
|
* method to provide clean logging (every class should have this). |
144 |
|
* |
145 |
+ |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
146 |
+ |
* to format the toString() |
147 |
+ |
* |
148 |
|
* @return the name of this class and its CVS revision |
149 |
|
*/ |
150 |
|
public String toString() { |
151 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
151 |
> |
return FormatName.getName( |
152 |
> |
_name, |
153 |
> |
this.getClass().getName(), |
154 |
> |
REVISION); |
155 |
|
} |
156 |
|
|
157 |
|
//---PRIVATE METHODS--- |
161 |
|
//---ATTRIBUTES--- |
162 |
|
|
163 |
|
/** |
164 |
< |
* A reference to the logger the system is using |
164 |
> |
* This holds a reference to the |
165 |
> |
* system logger that is being used. |
166 |
|
*/ |
167 |
< |
Logger _logger; |
167 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
168 |
|
|
169 |
|
/** |
170 |
< |
* A reference to the configurator the system is using |
170 |
> |
* This is the friendly identifier of the |
171 |
> |
* component this class is running in. |
172 |
> |
* eg, a Filter may be called "filter1", |
173 |
> |
* If this class does not have an owning |
174 |
> |
* component, a name from the configuration |
175 |
> |
* can be placed here. This name could also |
176 |
> |
* be changed to null for utility classes. |
177 |
|
*/ |
178 |
< |
ConfigurationManager _configManager; |
138 |
< |
|
139 |
< |
/** |
140 |
< |
* The port on which the server should listen. |
141 |
< |
*/ |
142 |
< |
private int _port; |
143 |
< |
|
144 |
< |
private NamingContextExt _ncRef; |
178 |
> |
private String _name = FilterManager.NAME; |
179 |
|
|
180 |
|
//---STATIC ATTRIBUTES--- |
181 |
|
|