ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/filter/TCPReader.java
Revision: 1.16
Committed: Thu Mar 21 13:01:21 2002 UTC (22 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.15: +67 -55 lines
Log Message:
Made the ACL's dynamic where appropriate. This isn't perfect, the ACL will
only get reloaded after the next connection. This is because the check is
done in the main while loop, and will only get done before a new thread is
spawned to deal with a connection. It will have the desired effect though,
I hope :)

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.12 package uk.org.iscream.cms.server.filter;
3 tdb 1.1
4     //---IMPORTS---
5 tdb 1.12 import uk.org.iscream.cms.server.core.*;
6     import uk.org.iscream.cms.server.componentmanager.*;
7     import uk.org.iscream.cms.server.filter.*;
8 tdb 1.1 import java.net.Socket;
9     import java.net.ServerSocket;
10     import java.io.OutputStream;
11     import java.io.IOException;
12     import java.net.InetAddress;
13     import java.net.UnknownHostException;
14 tdb 1.12 import uk.org.iscream.cms.server.util.*;
15 tdb 1.1
16     /**
17 tdb 1.8 * Reads TCP Heartbeats from the host applications.
18 tdb 1.1 *
19 tdb 1.13 * @author $Author: tdb $
20 tdb 1.16 * @version $Id: TCPReader.java,v 1.15 2002/03/20 13:05:50 tdb Exp $
21 tdb 1.1 */
22     class TCPReader extends Thread {
23    
24     //---FINAL ATTRIBUTES---
25    
26     /**
27     * The current CVS revision of this class
28     */
29 tdb 1.16 public final String REVISION = "$Revision: 1.15 $";
30 tdb 1.1
31     //---STATIC METHODS---
32    
33     //---CONSTRUCTORS---
34    
35 tdb 1.16 /**
36 tdb 1.8 * Constructs a new TCPReader
37 tdb 1.1 *
38 tdb 1.8 * @param queue A reference to our Queue
39 tdb 1.16 * @param port The port that the TCPReader will listen on
40     */
41 tdb 1.6 public TCPReader(int port, Queue queue) {
42 tdb 1.10 // set the Thread name
43     setName("filter.TCPReader");
44    
45 tdb 1.1 _port = port;
46 tdb 1.6 _queue = queue;
47 tdb 1.1 _logger.write(toString(), Logger.SYSINIT, "started");
48     }
49    
50    
51    
52     //---PUBLIC METHODS---
53    
54 tdb 1.16 /**
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 tdb 1.1 public void run(){
62 tdb 1.16 // 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 tdb 1.13 }
70 tdb 1.16 catch(IOException e){
71     _logger.write(toString(), Logger.FATAL, "Failed to setup ServerSocket: " + e);
72     return;
73 tdb 1.13 }
74 tdb 1.16
75     // Log what machine/port we're listening on
76 tdb 1.1 try{
77 tdb 1.16 _logger.write(toString(), Logger.SYSMSG, "TCPReader listening on "
78     +InetAddress.getLocalHost().getHostName()
79     +"/"+InetAddress.getLocalHost().getHostAddress()
80     +" port "+listenPort.getLocalPort());
81 tdb 1.1 }
82 tdb 1.16 catch(UnknownHostException e){
83     _logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost "
84     +"port "+listenPort.getLocalPort());
85 tdb 1.1 }
86 tdb 1.13
87 tdb 1.16 // We use this boolean so we can break out of the while loop if we want
88 tdb 1.13 boolean run = true;
89 tdb 1.16
90     // Loop round constantly until we decide to stop
91     ConfigurationProxy cp = ConfigurationProxy.getInstance();
92     String stringACL = "";
93     String newStringACL = "";
94 tdb 1.1 while(run){
95 tdb 1.16 // 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 tdb 1.1 try{
117 tdb 1.9 _logger.write(toString(), Logger.DEBUG, "Waiting for Connection");
118 tdb 1.16 // This will block until a host connects - at which point we get a Socket
119 tdb 1.1 hostSocket = listenPort.accept();
120 tdb 1.9 _logger.write(toString(), Logger.DEBUG, "Connection accepted from: " + hostSocket.toString());
121 tdb 1.1 }
122     catch(IOException e){
123 tdb 1.16 // Something went wrong with the ServerSocket, so we'll stop listening
124 tdb 1.1 run=false;
125     }
126 tdb 1.16 // If we've stopped on the line above we won't want to try this !
127 tdb 1.1 if(run){
128 tdb 1.16 try {
129     // Setup the TCPReaderInit and start it
130 tdb 1.6 TCPReaderInit init = new TCPReaderInit(hostSocket, _queue);
131 tdb 1.16 // and start it
132 tdb 1.1 init.start();
133     } catch (IOException e) {
134     _logger.write(toString(), Logger.ERROR, e.toString());
135     }
136     }
137     }
138 tdb 1.16 // Best log the fact that we're stopping
139 tdb 1.1 _logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending");
140     }
141    
142     /**
143     * Overrides the {@link java.lang.Object#toString() Object.toString()}
144     * method to provide clean logging (every class should have this).
145     *
146 tdb 1.12 * This uses the uk.org.iscream.cms.server.util.NameFormat class
147 ajm 1.5 * to format the toString()
148     *
149 tdb 1.1 * @return the name of this class and its CVS revision
150     */
151     public String toString() {
152 ajm 1.5 return FormatName.getName(
153     _name,
154     getClass().getName(),
155     REVISION);
156 tdb 1.1 }
157    
158     //---PRIVATE METHODS---
159    
160     //---ACCESSOR/MUTATOR METHODS---
161    
162     //---ATTRIBUTES---
163    
164     /**
165 ajm 1.5 * This is the friendly identifier of the
166     * component this class is running in.
167     * eg, a Filter may be called "filter1",
168     * If this class does not have an owning
169     * component, a name from the configuration
170     * can be placed here. This name could also
171     * be changed to null for utility classes.
172     */
173     private String _name = FilterMain.NAME;
174    
175     /**
176     * This holds a reference to the
177     * system logger that is being used.
178 tdb 1.1 */
179 ajm 1.5 private Logger _logger = ReferenceManager.getInstance().getLogger();
180 tdb 1.1
181     /**
182 tdb 1.16 * The port on which the server should listen.
183     */
184 tdb 1.1 private int _port;
185    
186 tdb 1.8 /**
187     * A reference to our Queue
188     */
189 tdb 1.6 private Queue _queue;
190 tdb 1.1
191     //---STATIC ATTRIBUTES---
192    
193     }