32 |
|
|
33 |
|
//---CONSTRUCTORS--- |
34 |
|
|
35 |
< |
/** |
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 |
< |
*/ |
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"); |
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 |
< |
*/ |
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 |
< |
// get our ACL from the configuration |
63 |
< |
ACL acl = null; |
64 |
< |
try { |
65 |
< |
String stringACL = ConfigurationProxy.getInstance().getProperty("Filter." + FilterMain.NAME, "Filter.TCPACL"); |
66 |
< |
acl = new ACL(stringACL); |
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(PropertyNotFoundException e) { |
71 |
< |
_logger.write(toString(), Logger.WARNING, "No ACL found for TCPReader: " + e); |
70 |
> |
catch(IOException e){ |
71 |
> |
_logger.write(toString(), Logger.FATAL, "Failed to setup ServerSocket: " + e); |
72 |
> |
return; |
73 |
|
} |
74 |
< |
|
75 |
< |
ServerSocket listenPort=null; |
74 |
> |
|
75 |
> |
// Log what machine/port we're listening on |
76 |
|
try{ |
77 |
< |
// Setup the ServerSocket so that clients can connect |
78 |
< |
// use an ACLServerSocket if we have an ACL |
79 |
< |
if(acl != null) { |
80 |
< |
listenPort = new ACLServerSocket(acl, _port); |
78 |
< |
} |
79 |
< |
else { |
80 |
< |
listenPort = new ServerSocket(_port); |
81 |
< |
} |
77 |
> |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
78 |
> |
+InetAddress.getLocalHost().getHostName() |
79 |
> |
+"/"+InetAddress.getLocalHost().getHostAddress() |
80 |
> |
+" port "+listenPort.getLocalPort()); |
81 |
|
} |
82 |
< |
catch(IOException e){ |
82 |
> |
catch(UnknownHostException e){ |
83 |
> |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
84 |
> |
+"port "+listenPort.getLocalPort()); |
85 |
|
} |
86 |
|
|
87 |
< |
// Log what machine/port we're listening on |
87 |
< |
try{ |
88 |
< |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
89 |
< |
+InetAddress.getLocalHost().getHostName() |
90 |
< |
+"/"+InetAddress.getLocalHost().getHostAddress() |
91 |
< |
+" port "+listenPort.getLocalPort()); |
92 |
< |
} |
93 |
< |
catch(UnknownHostException e){ |
94 |
< |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
95 |
< |
+"port "+listenPort.getLocalPort()); |
96 |
< |
} |
97 |
< |
|
98 |
< |
// We use this boolean so we can break out of the while loop if we want |
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 |
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 |
< |
Socket hostSocket=null; |
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 |
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 |
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 ! |
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 |
128 |
> |
try { |
129 |
> |
// Setup the TCPReaderInit and start it |
130 |
|
TCPReaderInit init = new TCPReaderInit(hostSocket, _queue); |
131 |
< |
// and start it |
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 |
138 |
> |
// Best log the fact that we're stopping |
139 |
|
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
140 |
|
} |
141 |
|
|
179 |
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
180 |
|
|
181 |
|
/** |
182 |
< |
* The port on which the server should listen. |
183 |
< |
*/ |
182 |
> |
* The port on which the server should listen. |
183 |
> |
*/ |
184 |
|
private int _port; |
185 |
|
|
186 |
|
/** |