ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/filtermanager/HostListener.java
Revision: 1.16
Committed: Sun Aug 1 10:41:05 2004 UTC (19 years, 9 months ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.15: +3 -3 lines
Log Message:
Catch a lot of old URL's and update them. Also remove a couple of old files
that aren't used.

File Contents

# User Rev Content
1 tdb 1.13 /*
2     * i-scream central monitoring system
3 tdb 1.16 * http://www.i-scream.org
4 tdb 1.13 * 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 tdb 1.1 //---PACKAGE DECLARATION---
22 tdb 1.9 package uk.org.iscream.cms.server.filtermanager;
23 tdb 1.1
24     //---IMPORTS---
25 tdb 1.9 import uk.org.iscream.cms.server.core.*;
26     import uk.org.iscream.cms.server.componentmanager.*;
27     import uk.org.iscream.cms.server.filter.*;
28 tdb 1.15 import uk.org.iscream.cms.util.*;
29 tdb 1.3 import java.net.*;
30     import java.io.*;
31 tdb 1.1
32     /**
33     * A socket listener to listen for new hosts registering with the system.
34     * When a host makes a connection, the connecton is past to an instance
35     * of the HostInit class, which handles further communication.
36     *
37 tdb 1.10 * @author $Author: tdb $
38 tdb 1.16 * @version $Id: HostListener.java,v 1.15 2003/02/05 16:43:47 tdb Exp $
39 tdb 1.1 */
40     class HostListener extends Thread {
41    
42     //---FINAL ATTRIBUTES---
43    
44     /**
45     * The current CVS revision of this class
46     */
47 tdb 1.16 public final String REVISION = "$Revision: 1.15 $";
48 tdb 1.1
49     //---STATIC METHODS---
50    
51     //---CONSTRUCTORS---
52    
53     /**
54     * Constructs a new listener
55     *
56 tdb 1.3 * @param listenPort The port that the server will listen on.
57 tdb 1.1 */
58 tdb 1.7 public HostListener() {
59 tdb 1.6 // set the Thread name
60     setName("filtermanager.HostListener");
61    
62 tdb 1.1 _logger.write(toString(), Logger.SYSINIT, "started");
63     }
64    
65     //---PUBLIC METHODS---
66    
67     /**
68     * The run() method is the main loop for this thread, and we
69     * will remain in here until such a point as something goes
70     * wrong with the listening. After initially setting up the
71     * ServerSocket we go round a while loop receiving connections
72     * and then passing them off to other processes to deal with.
73     */
74     public void run(){
75 tdb 1.12 // setup an empty ACL defaulting to ALLOW
76     ACL acl = new ACL(ACL.ALLOW);
77 tdb 1.10
78 tdb 1.3 ServerSocket listenSocket=null;
79 tdb 1.1 // We use this boolean so we can break out of the while loop if we want
80     boolean run = true;
81     try{
82 tdb 1.7 // Work out the port we want
83 tdb 1.11 int listenPort = Integer.parseInt(ConfigurationProxy.getInstance().getProperty("FilterManager." + FilterManager.NAME, "FilterManager.listenPort"));
84 tdb 1.1 // Setup the ServerSocket so that clients can connect
85 tdb 1.12 listenSocket = new ACLServerSocket(acl, listenPort);
86 tdb 1.7 _logger.write(toString(), Logger.SYSMSG, "Server listening on "
87 tdb 1.1 +InetAddress.getLocalHost().getHostName()
88     +"/"+InetAddress.getLocalHost().getHostAddress()
89 tdb 1.3 +" port "+listenSocket.getLocalPort());
90 tdb 1.7
91     }
92     catch(UnknownHostException e){
93 tdb 1.1 _logger.write(toString(), Logger.SYSMSG, "Server listening on UnknownHost "
94 tdb 1.3 +"port "+listenSocket.getLocalPort());
95 tdb 1.1 }
96 tdb 1.7 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 tdb 1.1 // Loop round constantly until we decide to stop
110 tdb 1.12 ConfigurationProxy cp = ConfigurationProxy.getInstance();
111     String stringACL = "";
112     String newStringACL = "";
113 tdb 1.1 while(run){
114 tdb 1.12 // 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 tdb 1.1 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 tdb 1.3 hostSocket = listenSocket.accept();
139 tdb 1.1 _logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString());
140     }
141     catch(IOException e){
142     // Something went wrong with the ServerSocket, so we'll stop listening
143     run=false;
144     }
145     // If we've stopped on the line above we won't want to try this !
146     if(run){
147     try {
148     // Setup the HostInit so it can carry on communications with the host
149 tdb 1.3 HostInit init = new HostInit(hostSocket);
150 tdb 1.1 // and start it
151     init.start();
152     } catch (IOException e) {
153     _logger.write(toString(), Logger.ERROR, e.toString());
154     }
155     }
156     }
157     // Best log the fact that we're stopping
158     _logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending");
159     }
160    
161     /**
162     * Overrides the {@link java.lang.Object#toString() Object.toString()}
163     * method to provide clean logging (every class should have this).
164     *
165 tdb 1.15 * This uses the uk.org.iscream.cms.util.NameFormat class
166 ajm 1.4 * to format the toString()
167     *
168 tdb 1.1 * @return the name of this class and its CVS revision
169     */
170     public String toString() {
171 ajm 1.4 return FormatName.getName(
172     _name,
173     this.getClass().getName(),
174     REVISION);
175 tdb 1.1 }
176    
177     //---PRIVATE METHODS---
178    
179     //---ACCESSOR/MUTATOR METHODS---
180    
181     //---ATTRIBUTES---
182    
183     /**
184 ajm 1.4 * This holds a reference to the
185     * system logger that is being used.
186     */
187     private Logger _logger = ReferenceManager.getInstance().getLogger();
188    
189     /**
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 tdb 1.1 */
198 ajm 1.4 private String _name = FilterManager.NAME;
199 tdb 1.1
200     //---STATIC ATTRIBUTES---
201    
202     }