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/FilterMain.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/filter/FilterMain.java (file contents):
Revision 1.13 by ajm, Thu Nov 30 02:18:51 2000 UTC vs.
Revision 1.35 by tdb, Sat May 18 18:16:01 2002 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * Copyright (C) 2000-2002 i-scream
4 + *
5 + * This program is free software; you can redistribute it and/or
6 + * modify it under the terms of the GNU General Public License
7 + * as published by the Free Software Foundation; either version 2
8 + * of the License, or (at your option) any later version.
9 + *
10 + * This program is distributed in the hope that it will be useful,
11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 + * GNU General Public License for more details.
14 + *
15 + * You should have received a copy of the GNU General Public License
16 + * along with this program; if not, write to the Free Software
17 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 + */
19 +
20   //---PACKAGE DECLARATION---
21 < package uk.ac.ukc.iscream.filter;
21 > package uk.org.iscream.cms.server.filter;
22  
23   //---IMPORTS---
24 < import uk.ac.ukc.iscream.refman.*;
25 < import uk.ac.ukc.iscream.core.*;
26 < import uk.ac.ukc.iscream.filter.*;
27 < import org.omg.CORBA.*;
9 < import org.omg.CosNaming.*;
10 < import org.omg.PortableServer.*;
24 > import uk.org.iscream.cms.server.util.*;
25 > import uk.org.iscream.cms.server.core.*;
26 > import uk.org.iscream.cms.server.componentmanager.*;
27 > import uk.org.iscream.cms.server.filter.*;
28  
29   /**
30   * A Filter Startup Class
31 + * A filter is an iscream component.
32   *
33   * @author  $Author$
34   * @version $Id$
35   */
36 < class FilterMain {
36 > public class FilterMain implements Component {
37  
38   //---FINAL ATTRIBUTES---
39  
# Line 26 | Line 44 | class FilterMain {
44      
45   //---STATIC METHODS---
46  
47 <    public static void main(String[] args) {
48 <        // ***************************************
49 <        // VERY TEMPORARY - will find a better way
50 <        System.setProperty("org.omg.CORBA.ORBClass","jacorb.orb.ORB");
51 <        System.setProperty("org.omg.CORBA.ORBSingletonClass","jacorb.orb.ORBSingleton");
52 <        // ***************************************
47 > //---CONSTRUCTORS---
48 >    
49 >    /**
50 >     * Constructs a Filter with the name given
51 >     *
52 >     * @param givenName the name
53 >     */
54 >    public FilterMain(String givenName) {
55 >        NAME = givenName;
56 >    }
57  
58 <        // get our name from the command line
59 <        String ourName = "";
60 <        if (args.length == 1) {
61 <            ourName = args[0];
58 > //---PUBLIC METHODS---
59 >
60 >    /**
61 >     * Starts the Filter component
62 >     */
63 >    public void start() throws ComponentStartException {
64 >        // get references to key objects
65 >        _logger = _refman.getLogger();
66 >        
67 >        _logger.write(toString(), Logger.SYSINIT, "coming up");
68 >        
69 >        ConfigurationProxy cp = ConfigurationProxy.getInstance();
70 >        
71 >        // which input methods do we need to activate?
72 >        // default to activating them
73 >        boolean activateTCPReader = true;
74 >        boolean activateUDPReader = true;
75 >        boolean activateCORBAReader = true;
76 >        
77 >        // check for TCP Reader
78 >        try {
79 >            int tcp = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Filter.ActivateTCPReader"));
80 >            activateTCPReader = (tcp == 1);
81 >        } catch (PropertyNotFoundException e) {
82 >            activateTCPReader = false;
83 >        } catch (NumberFormatException e) {
84 >            activateTCPReader = false;
85          }
86 <        else {
87 <            usage();
86 >        // check for UDP Reader
87 >        try {
88 >            int udp = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Filter.ActivateUDPReader"));
89 >            activateUDPReader = (udp == 1);
90 >        } catch (PropertyNotFoundException e) {
91 >            activateUDPReader = false;
92 >        } catch (NumberFormatException e) {
93 >            activateUDPReader = false;
94          }
95 +        // check for CORBA Reader
96 +        try {
97 +            int corba = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Filter.ActivateCORBAReader"));
98 +            activateCORBAReader = (corba == 1);
99 +        } catch (PropertyNotFoundException e) {
100 +            activateCORBAReader = false;
101 +        } catch (NumberFormatException e) {
102 +            activateCORBAReader = false;
103 +        }
104          
105 <        // can't have a real toString() :)
106 <        String toString = "Filter{" + ourName + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")";
47 <                
48 <        ReferenceManager refman = ReferenceManager.init(null, ourName);
105 >        // need to use the Queue later on
106 >        Queue queue;
107          
108 <        refman.getLogger().write(toString, Logger.SYSINIT, "coming up");
108 >        // there's little point starting a Queue and a FilterThread
109 >        // if nothing is going to be giving us any data
110 >        if(activateTCPReader || activateUDPReader || activateCORBAReader) {
111 >            // see if this Queue needs a size limit
112 >            try {
113 >                int queueSizeLimit = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Queue.SizeLimit"));
114 >                String queueRemoveAlgorithm = cp.getProperty("Filter." + FilterMain.NAME, "Queue.RemoveAlgorithm");
115 >                int algorithm = StringUtils.getStringPos(queueRemoveAlgorithm, Queue.algorithms);
116 >                if(algorithm != -1) {
117 >                    _logger.write(toString(), Logger.DEBUG, "Starting Queue with size limit of "+queueSizeLimit+", using remove algorithm "+queueRemoveAlgorithm);
118 >                    // we have valid values, so lets start it.
119 >                    queue = new Queue(queueSizeLimit, algorithm);
120 >                }
121 >                else {
122 >                    _logger.write(toString(), Logger.WARNING, "Bad Queue Algorithm configuration, not known: "+queueRemoveAlgorithm);
123 >                    // just don't activate a limit
124 >                    queue = new Queue();
125 >                }
126 >                
127 >            } catch (PropertyNotFoundException e) {
128 >                _logger.write(toString(), Logger.DEBUG, "Optional config not set: "+e);
129 >                // just don't activate a limit
130 >                queue = new Queue();
131 >            } catch (NumberFormatException e) {
132 >                _logger.write(toString(), Logger.WARNING, "Bad Queue SizeLimit configuration: "+e);
133 >                // just don't activate a limit
134 >                queue = new Queue();
135 >            }
136              
137 <        // configuration variables we require
53 <        int UDPListenPort = 0;
54 <        int TCPListenPort = 0;
55 <        String parentFilterName = null;
56 <
57 <        Configuration config = refman.getCM().getConfiguration(refman.getName());
58 <        if (config == null) {
59 <            throw new RuntimeException ("CRITICAL:Unable to obtain configuration" +
60 <                                        "         Advise you check the i-scream log for more information.");
61 <        } else {
137 >            // startup a monitor on this queue
138              try {
139 <                UDPListenPort = Integer.parseInt(config.getProperty("Filter.UDPListenPort"));
140 <                TCPListenPort = Integer.parseInt(config.getProperty("Filter.TCPListenPort"));
141 <                parentFilterName = config.getProperty("Filter.parentFilter");
142 <            } catch (org.omg.CORBA.MARSHAL e) {
143 <                refman.getLogger().write(toString, Logger.FATAL, "required config property not present");
144 <                throw new RuntimeException ("CRITICAL:Unable to obtain required configuration property" +
69 <                                            "         Advise you check the i-scream log for more information.");
70 <
139 >                // try to get the interval, if this fails, we won't start up the monitor
140 >                int queueMonitorInterval = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Queue.MonitorInterval"));
141 >                String queueName = NAME + " Filter";
142 >                queue.startMonitor(queueMonitorInterval*1000, queueName);
143 >            } catch (PropertyNotFoundException e) {
144 >                _logger.write(toString(), Logger.WARNING, "failed to find queue monitor config, disabling. " + e);
145              }
146 +            
147 +            // Start a filter thread
148 +            _logger.write(toString(), Logger.SYSINIT, "starting Filter Thread / Queue consumer");
149 +            FilterThread filterThread = new FilterThread(queue);
150 +            filterThread.start();
151          }
152 <        refman.getLogger().write(toString, Logger.SYSINIT, "configured");
153 <        // get parent
154 <        Filter parentFilter = FilterHelper.narrow(refman.getCORBARef("iscream.Filter." + parentFilterName));
152 >        else {
153 >            // it's pointless carrying on really...
154 >            throw new ComponentStartException("Can't start Filter without any inbound data feeds");            
155 >        }
156          
157 <                
157 >        // the corba listener needs these to be set, so lets
158 >        // make them something obviously invalid initially
159 >        int TCPListenPort = -1;
160 >        int UDPListenPort = -1;
161          
79        // FilterServant start (for inbound child filter data)
80        refman.getLogger().write(toString, Logger.DEBUG, "starting Filter Child -> Parent link for upstream parent - " + parentFilterName);
81        FilterServant filterServant = new FilterServant(parentFilter, TCPListenPort, UDPListenPort);
82        refman.bindToOrb(filterServant, "iscream.Filter." + refman.getName());
83
84        // UDL Reader start (for inbound host data)
85        refman.getLogger().write(toString, Logger.SYSINIT, "starting Filter UDP listener");
86        UDPReader udpReader = new UDPReader(UDPListenPort, parentFilter);
87        udpReader.start();
88
162          // TCP Reader start (for heartbeats)
163 <        refman.getLogger().write(toString, Logger.SYSINIT, "starting Filter TCP listener");
164 <        TCPReader tcpReader = new TCPReader(TCPListenPort, parentFilter);
165 <        tcpReader.start();
163 >        if(activateTCPReader) {
164 >            try {
165 >                // get the port number from the configuration
166 >                TCPListenPort = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Filter.TCPListenPort"));
167 >                // start the TCPReader
168 >                _logger.write(toString(), Logger.SYSINIT, "starting Filter TCP listener");
169 >                TCPReader tcpReader = new TCPReader(TCPListenPort, queue);
170 >                tcpReader.start();
171 >            } catch (PropertyNotFoundException e) {
172 >                _logger.write(toString(), Logger.WARNING, "Unable to start TCPReader due to missing configuration: " + e);
173 >            } catch (NumberFormatException e) {
174 >                _logger.write(toString(), Logger.WARNING, "Unable to start TCPReader due to invalid configuration: " + e);
175 >            }
176 >        }
177          
178 <        // start the POA off
179 <        // now we are running, we just need to serve
180 <        // so we ask the orb to block for us until it has finished
181 <        refman.activatePOA();
182 <        refman.getLogger().write(toString, Logger.SYSINIT, "started");
183 <        refman.getORB().run();
178 >        // UDP Reader start (for inbound host data)
179 >        if(activateUDPReader) {
180 >            try {
181 >                // get the port number from the configuration
182 >                UDPListenPort = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Filter.UDPListenPort"));
183 >                // start the UDPReader
184 >                _logger.write(toString(), Logger.SYSINIT, "starting Filter UDP listener");
185 >                UDPReader udpReader = new UDPReader(UDPListenPort, queue);
186 >                udpReader.start();
187 >            } catch (PropertyNotFoundException e) {
188 >                _logger.write(toString(), Logger.WARNING, "Unable to start UDPReader due to missing configuration: " + e);
189 >            } catch (NumberFormatException e) {
190 >                _logger.write(toString(), Logger.WARNING, "Unable to start UDPReader due to invalid configuration: " + e);
191 >            }
192 >        }
193 >        
194 >        // FilterServant start (for inbound child filter data)
195 >        if(activateCORBAReader) {
196 >            // start the FilterServant
197 >            _logger.write(toString(), Logger.SYSINIT, "starting Servant to listen for downstream filters");
198 >            FilterServant filterServant = new FilterServant(queue);
199 >            _refman.bindToOrb(filterServant, "iscream.Filter." + FilterMain.NAME);
200 >        }
201 >        
202 >        // FilterInfoServant start (to provide filter information to the server)
203 >        _logger.write(toString(), Logger.SYSINIT, "starting Servant to provide filter information");
204 >        FilterInfoServant filterInfoServant = new FilterInfoServant(TCPListenPort, UDPListenPort);
205 >        _refman.bindToOrb(filterInfoServant, "iscream.FilterInfo." + FilterMain.NAME);
206 >        
207 >        _logger.write(toString(), Logger.SYSINIT, "started");
208      }
209 <
210 <    /**
211 <     * A simple method to print the usage of this class.
212 <     * It never returns, but instead exits to the system
213 <     * with a value 1, to indicate the system did not start
214 <     * properly.
209 >    
210 >    /**
211 >     * Does a dependency check. Used mainly at startup to
212 >     * see if the required dependencies (components) are up
213 >     * and running.
214 >     *
215 >     * @return a boolean value, true if the depdencies are satisfied
216       */
217 <    public static void usage() {
218 <        System.out.println("USAGE: java FilterMain <name>");
219 <        System.out.println("WHERE <name>:");
220 <        System.out.println("      The unique identifier for the Filter in the system.");
221 <        System.exit(1);
217 >    public boolean depCheck() {
218 >        try {
219 >            org.omg.CORBA.Object obj;
220 >            // first check the ConfigurationManager is alive
221 >            obj = _refman.getCORBARef("iscream.ConfigurationManager");
222 >            // then suss out our parent filter
223 >            ConfigurationProxy cp = ConfigurationProxy.getInstance();
224 >            String parentFilterName = cp.getProperty("Filter." + FilterMain.NAME, "Filter.parentFilter");
225 >            // finally check the parent filter is alive
226 >            obj = _refman.getCORBARef("iscream.Filter." + parentFilterName);
227 >        } catch(ComponentCORBAException e) {
228 >            System.err.println(toString() + ": Dependency Failure: "+e);
229 >            return false;
230 >        } catch(PropertyNotFoundException e) {
231 >            System.err.println(toString() + ": Unable to obtain configuration: "+e);
232 >            return false;
233 >        }
234 >        // dependency check suceeded
235 >        return true;
236      }
237 +    
238 +    /**
239 +     * Overrides the {@link java.lang.Object#toString() Object.toString()}
240 +     * method to provide clean logging (every class should have this).
241 +     *
242 +     * This uses the uk.org.iscream.cms.server.util.NameFormat class
243 +     * to format the toString()
244 +     *
245 +     * @return the name of this class and its CVS revision
246 +     */
247 +    public String toString() {
248 +        return FormatName.getName(
249 +            NAME,
250 +            getClass().getName(),
251 +            REVISION);
252 +    }
253  
115 //---CONSTRUCTORS---
116
117 //---PUBLIC METHODS---
118
254   //---PRIVATE METHODS---
255  
256   //---ACCESSOR/MUTATOR METHODS---
257  
258   //---ATTRIBUTES---
259  
260 +    /**
261 +     * This holds a reference to the
262 +     * system logger that is being used.
263 +     */
264 +    private Logger _logger;
265 +
266 +    /**
267 +     * A reference to the reference manager in use
268 +     */
269 +    private ReferenceManager _refman = ReferenceManager.getInstance();
270 +
271   //---STATIC ATTRIBUTES---
272  
273 < }            
273 >    /**
274 >     * The friendly name for this component, used by
275 >     * all related classes.
276 >     * This is set from the configuration.
277 >     */
278 >    public static String NAME;
279 >
280 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines