1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.9 |
package uk.org.iscream.cms.server.filtermanager; |
3 |
tdb |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
tdb |
1.9 |
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 |
tdb |
1.3 |
import java.net.*; |
10 |
|
|
import java.io.*; |
11 |
tdb |
1.1 |
|
12 |
|
|
/** |
13 |
|
|
* A socket listener to listen for new hosts registering with the system. |
14 |
|
|
* When a host makes a connection, the connecton is past to an instance |
15 |
|
|
* of the HostInit class, which handles further communication. |
16 |
|
|
* |
17 |
tdb |
1.10 |
* @author $Author: tdb $ |
18 |
tdb |
1.11 |
* @version $Id: HostListener.java,v 1.10 2002/03/20 12:56:37 tdb Exp $ |
19 |
tdb |
1.1 |
*/ |
20 |
|
|
class HostListener extends Thread { |
21 |
|
|
|
22 |
|
|
//---FINAL ATTRIBUTES--- |
23 |
|
|
|
24 |
|
|
/** |
25 |
|
|
* The current CVS revision of this class |
26 |
|
|
*/ |
27 |
tdb |
1.11 |
public final String REVISION = "$Revision: 1.10 $"; |
28 |
tdb |
1.1 |
|
29 |
|
|
//---STATIC METHODS--- |
30 |
|
|
|
31 |
|
|
//---CONSTRUCTORS--- |
32 |
|
|
|
33 |
|
|
/** |
34 |
|
|
* Constructs a new listener |
35 |
|
|
* |
36 |
tdb |
1.3 |
* @param listenPort The port that the server will listen on. |
37 |
tdb |
1.1 |
*/ |
38 |
tdb |
1.7 |
public HostListener() { |
39 |
tdb |
1.6 |
// set the Thread name |
40 |
|
|
setName("filtermanager.HostListener"); |
41 |
|
|
|
42 |
tdb |
1.1 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
//---PUBLIC METHODS--- |
46 |
|
|
|
47 |
|
|
/** |
48 |
|
|
* The run() method is the main loop for this thread, and we |
49 |
|
|
* will remain in here until such a point as something goes |
50 |
|
|
* wrong with the listening. After initially setting up the |
51 |
|
|
* ServerSocket we go round a while loop receiving connections |
52 |
|
|
* and then passing them off to other processes to deal with. |
53 |
|
|
*/ |
54 |
|
|
public void run(){ |
55 |
tdb |
1.10 |
// get our ACL from the configuration |
56 |
|
|
ACL acl = null; |
57 |
|
|
try { |
58 |
tdb |
1.11 |
String stringACL = ConfigurationProxy.getInstance().getProperty("FilterManager." + FilterManager.NAME, "FilterManager.ACL"); |
59 |
tdb |
1.10 |
acl = new ACL(stringACL); |
60 |
|
|
} |
61 |
|
|
catch(PropertyNotFoundException e) { |
62 |
|
|
_logger.write(toString(), Logger.WARNING, "No ACL found for FilterManager: " + e); |
63 |
|
|
} |
64 |
|
|
|
65 |
tdb |
1.3 |
ServerSocket listenSocket=null; |
66 |
tdb |
1.1 |
// We use this boolean so we can break out of the while loop if we want |
67 |
|
|
boolean run = true; |
68 |
|
|
try{ |
69 |
tdb |
1.7 |
// Work out the port we want |
70 |
tdb |
1.11 |
int listenPort = Integer.parseInt(ConfigurationProxy.getInstance().getProperty("FilterManager." + FilterManager.NAME, "FilterManager.listenPort")); |
71 |
tdb |
1.1 |
// Setup the ServerSocket so that clients can connect |
72 |
tdb |
1.10 |
// use an ACLServerSocket if we have an ACL |
73 |
|
|
if(acl != null) { |
74 |
|
|
listenSocket = new ACLServerSocket(acl, listenPort); |
75 |
|
|
} |
76 |
|
|
else { |
77 |
|
|
listenSocket = new ServerSocket(listenPort); |
78 |
|
|
} |
79 |
tdb |
1.7 |
_logger.write(toString(), Logger.SYSMSG, "Server listening on " |
80 |
tdb |
1.1 |
+InetAddress.getLocalHost().getHostName() |
81 |
|
|
+"/"+InetAddress.getLocalHost().getHostAddress() |
82 |
tdb |
1.3 |
+" port "+listenSocket.getLocalPort()); |
83 |
tdb |
1.7 |
|
84 |
|
|
} |
85 |
|
|
catch(UnknownHostException e){ |
86 |
tdb |
1.1 |
_logger.write(toString(), Logger.SYSMSG, "Server listening on UnknownHost " |
87 |
tdb |
1.3 |
+"port "+listenSocket.getLocalPort()); |
88 |
tdb |
1.1 |
} |
89 |
tdb |
1.7 |
catch(IOException e){ |
90 |
|
|
_logger.write(toString(), Logger.FATAL, "IO Error, can't start FilterManager :"+e); |
91 |
|
|
run = false; |
92 |
|
|
} |
93 |
|
|
catch(PropertyNotFoundException e){ |
94 |
|
|
_logger.write(toString(), Logger.FATAL, "Fatal Error, can't find config :"+e); |
95 |
|
|
run = false; |
96 |
|
|
} |
97 |
|
|
catch(NumberFormatException e){ |
98 |
|
|
_logger.write(toString(), Logger.FATAL, "Invalid port configuration found :"+e); |
99 |
|
|
run = false; |
100 |
|
|
} |
101 |
|
|
|
102 |
tdb |
1.1 |
// Loop round constantly until we decide to stop |
103 |
|
|
while(run){ |
104 |
|
|
Socket hostSocket=null; |
105 |
|
|
try{ |
106 |
|
|
_logger.write(toString(), Logger.SYSMSG, "Waiting for Connection"); |
107 |
|
|
// This will block until a host connects - at which point we get a Socket |
108 |
tdb |
1.3 |
hostSocket = listenSocket.accept(); |
109 |
tdb |
1.1 |
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
110 |
|
|
} |
111 |
|
|
catch(IOException e){ |
112 |
|
|
// Something went wrong with the ServerSocket, so we'll stop listening |
113 |
|
|
run=false; |
114 |
|
|
} |
115 |
|
|
// If we've stopped on the line above we won't want to try this ! |
116 |
|
|
if(run){ |
117 |
|
|
try { |
118 |
|
|
// Setup the HostInit so it can carry on communications with the host |
119 |
tdb |
1.3 |
HostInit init = new HostInit(hostSocket); |
120 |
tdb |
1.1 |
// and start it |
121 |
|
|
init.start(); |
122 |
|
|
} catch (IOException e) { |
123 |
|
|
_logger.write(toString(), Logger.ERROR, e.toString()); |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
} |
127 |
|
|
// Best log the fact that we're stopping |
128 |
|
|
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
/** |
132 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
133 |
|
|
* method to provide clean logging (every class should have this). |
134 |
|
|
* |
135 |
tdb |
1.9 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
136 |
ajm |
1.4 |
* to format the toString() |
137 |
|
|
* |
138 |
tdb |
1.1 |
* @return the name of this class and its CVS revision |
139 |
|
|
*/ |
140 |
|
|
public String toString() { |
141 |
ajm |
1.4 |
return FormatName.getName( |
142 |
|
|
_name, |
143 |
|
|
this.getClass().getName(), |
144 |
|
|
REVISION); |
145 |
tdb |
1.1 |
} |
146 |
|
|
|
147 |
|
|
//---PRIVATE METHODS--- |
148 |
|
|
|
149 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
150 |
|
|
|
151 |
|
|
//---ATTRIBUTES--- |
152 |
|
|
|
153 |
|
|
/** |
154 |
ajm |
1.4 |
* This holds a reference to the |
155 |
|
|
* system logger that is being used. |
156 |
|
|
*/ |
157 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
158 |
|
|
|
159 |
|
|
/** |
160 |
|
|
* This is the friendly identifier of the |
161 |
|
|
* component this class is running in. |
162 |
|
|
* eg, a Filter may be called "filter1", |
163 |
|
|
* If this class does not have an owning |
164 |
|
|
* component, a name from the configuration |
165 |
|
|
* can be placed here. This name could also |
166 |
|
|
* be changed to null for utility classes. |
167 |
tdb |
1.1 |
*/ |
168 |
ajm |
1.4 |
private String _name = FilterManager.NAME; |
169 |
tdb |
1.1 |
|
170 |
|
|
//---STATIC ATTRIBUTES--- |
171 |
|
|
|
172 |
|
|
} |