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.12 by ajm, Wed Nov 29 21:27:39 2000 UTC vs.
Revision 1.39 by tdb, Mon Feb 24 20:18:49 2003 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * http://www.i-scream.org.uk
4 + * 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   //---PACKAGE DECLARATION---
22 < package uk.ac.ukc.iscream.filter;
22 > package uk.org.iscream.cms.server.filter;
23  
24   //---IMPORTS---
25 < import uk.ac.ukc.iscream.core.*;
26 < import uk.ac.ukc.iscream.filter.*;
27 < import org.omg.CORBA.*;
28 < import org.omg.CosNaming.*;
9 < import org.omg.PortableServer.*;
25 > import uk.org.iscream.cms.util.*;
26 > import uk.org.iscream.cms.server.core.*;
27 > import uk.org.iscream.cms.server.componentmanager.*;
28 > import uk.org.iscream.cms.server.filter.*;
29  
30   /**
31   * A Filter Startup Class
32 + * A filter is an iscream component.
33   *
34   * @author  $Author$
35   * @version $Id$
36   */
37 < class FilterMain {
37 > public class FilterMain implements Component {
38  
39   //---FINAL ATTRIBUTES---
40  
# Line 25 | Line 45 | class FilterMain {
45      
46   //---STATIC METHODS---
47  
48 <    public static void main(String[] args) {
49 <        System.setProperty("org.omg.CORBA.ORBClass","jacorb.orb.ORB");
50 <        System.setProperty("org.omg.CORBA.ORBSingletonClass","jacorb.orb.ORBSingleton");
48 > //---CONSTRUCTORS---
49 >    
50 >    /**
51 >     * Constructs a Filter with the name given
52 >     *
53 >     * @param givenName the name
54 >     */
55 >    public FilterMain(String givenName) {
56 >        NAME = givenName;
57 >    }
58 >
59 > //---PUBLIC METHODS---
60 >
61 >    /**
62 >     * Starts the Filter component
63 >     */
64 >    public void start() throws ComponentStartException {
65 >        // get references to key objects
66 >        _logger = _refman.getLogger();
67          
68 <        // get our name from the command line
69 <        String ourName = "";
70 <        if (args.length == 1) {
71 <            ourName = args[0];
68 >        _logger.write(toString(), Logger.SYSINIT, "coming up");
69 >        
70 >        ConfigurationProxy cp = ConfigurationProxy.getInstance();
71 >        
72 >        // which input methods do we need to activate?
73 >        // default to activating them
74 >        boolean activateUDPReader = true;
75 >        boolean activateCORBAReader = true;
76 >        
77 >        // check for UDP Reader
78 >        try {
79 >            int udp = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Filter.ActivateUDPReader"));
80 >            activateUDPReader = (udp == 1);
81 >        } catch (PropertyNotFoundException e) {
82 >            activateUDPReader = false;
83 >        } catch (NumberFormatException e) {
84 >            activateUDPReader = false;
85          }
86 <        else {
87 <            usage();
86 >        // check for CORBA Reader
87 >        try {
88 >            int corba = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Filter.ActivateCORBAReader"));
89 >            activateCORBAReader = (corba == 1);
90 >        } catch (PropertyNotFoundException e) {
91 >            activateCORBAReader = false;
92 >        } catch (NumberFormatException e) {
93 >            activateCORBAReader = false;
94          }
95          
96 <        // can't have a real toString() :)
97 <        String toString = "Filter{" + ourName + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")";
96 >        // need to use the Queue later on
97 >        Queue queue;
98          
99 <        try {        
100 <            ORB orb = ORB.init(args, null);
101 <            
102 <            // something to hold objects
103 <            org.omg.CORBA.Object objRef = null;    
104 <            
105 <            // get the Root POA
106 <            objRef = orb.resolve_initial_references("RootPOA");
107 <            POA poa = POAHelper.narrow(objRef);
108 <            
109 <            // get a hook to the name service
110 <            objRef = orb.resolve_initial_references("NameService");
111 <            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
99 >        // there's little point starting a Queue and a FilterThread
100 >        // if nothing is going to be giving us any data
101 >        if(activateUDPReader || activateCORBAReader) {
102 >            // see if this Queue needs a size limit
103 >            try {
104 >                int queueSizeLimit = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Queue.SizeLimit"));
105 >                String queueRemoveAlgorithm = cp.getProperty("Filter." + FilterMain.NAME, "Queue.RemoveAlgorithm");
106 >                int algorithm = StringUtils.getStringPos(queueRemoveAlgorithm, Queue.algorithms);
107 >                if(algorithm != -1) {
108 >                    _logger.write(toString(), Logger.DEBUG, "Starting Queue with size limit of "+queueSizeLimit+", using remove algorithm "+queueRemoveAlgorithm);
109 >                    // we have valid values, so lets start it.
110 >                    queue = new Queue(queueSizeLimit, algorithm);
111 >                }
112 >                else {
113 >                    _logger.write(toString(), Logger.WARNING, "Bad Queue Algorithm configuration, not known: "+queueRemoveAlgorithm);
114 >                    // just don't activate a limit
115 >                    queue = new Queue();
116 >                }
117                  
118 <            // get a ref to the ConfigurationManager, Logger & the FilterManager
118 >            } catch (PropertyNotFoundException e) {
119 >                _logger.write(toString(), Logger.DEBUG, "Optional config not set: "+e);
120 >                // just don't activate a limit
121 >                queue = new Queue();
122 >            } catch (NumberFormatException e) {
123 >                _logger.write(toString(), Logger.WARNING, "Bad Queue SizeLimit configuration: "+e);
124 >                // just don't activate a limit
125 >                queue = new Queue();
126 >            }
127              
128 <            objRef = ncRef.resolve(ncRef.to_name("iscream.ConfigurationManager"));
129 <            ConfigurationManager configManager = ConfigurationManagerHelper.narrow(objRef);
130 <            
131 <            objRef = ncRef.resolve(ncRef.to_name("iscream.Logger"));
132 <            Logger logger = LoggerHelper.narrow(objRef);
133 <            
134 <            logger.write(toString, Logger.SYSINIT, "coming up");
135 <            
68 <            
69 <            // **** THIS SECTION WILL NEED CHANGING TO GET RELEVANT CONFIG
70 <            // **** Please ignore this block of code, it's just "copy/paste" :)
71 <            
72 <            // get the config
73 <            Configuration myConfig = configManager.getConfiguration(ourName);
74 <            
75 <            // read some config here
76 <            int UDPport = 0;
77 <            int TCPport = 0;
78 <            String parentFilterName = null;
79 <  
80 <            // did we?
81 <            if (myConfig == null) {
82 <                System.out.println("Failed: is it there?, can you read it?");
83 <                System.exit(1);
84 <            } else {
85 <              
86 <                // get the property
87 <                try {
88 <                    //QUERY HERE.... ???
89 <                    UDPport = new Integer(myConfig.getProperty("Filter.UDPlistenPort")).intValue();
90 <                    TCPport = new Integer(myConfig.getProperty("Filter.TCPlistenPort")).intValue();
91 <                    parentFilterName = myConfig.getProperty("Filter.parentFilter");
92 <                } catch (org.omg.CORBA.MARSHAL e) {
93 <                    System.out.println("Caught org.omg.CORBA.MARSHAL, must be a null we got back");
94 <                    //System.exit(1);
95 <                }
128 >            // startup a monitor on this queue
129 >            try {
130 >                // try to get the interval, if this fails, we won't start up the monitor
131 >                int queueMonitorInterval = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Queue.MonitorInterval"));
132 >                String queueName = NAME + " Filter";
133 >                queue.startMonitor(queueMonitorInterval*1000, queueName);
134 >            } catch (PropertyNotFoundException e) {
135 >                _logger.write(toString(), Logger.WARNING, "failed to find queue monitor config, disabling. " + e);
136              }
137              
138 <            logger.write(toString, Logger.SYSINIT, "configured");
139 <            
140 <            // **** END COMMENT
141 <            
102 <            // **** INITIAL FILTER MANAGER COMMUNICATIONS HERE
103 <            
104 <            // get a root (CHANGE to use FilterManager)
105 <            objRef = ncRef.resolve(ncRef.to_name("iscream.Filter." + parentFilterName));
106 <            Filter parentFilter = FilterHelper.narrow(objRef);
107 <
108 <            // SETUP our Servant
109 <            
110 <            // create the FilterServant
111 <            logger.write(toString, Logger.DEBUG, "firing servant with parent - " + parentFilterName);
112 <            FilterServant filterServant = new FilterServant(logger, parentFilter, ourName, new Integer(TCPport).toString() , new Integer(UDPport).toString());
113 <
114 <            // register ourselves
115 <
116 <            // and advertise it to the naming context
117 <            objRef = poa.servant_to_reference(filterServant);
118 <            ncRef.bind(ncRef.to_name("iscream.Filter."+ourName), objRef);
119 <
120 <            // **** END COMMENT
121 <            
122 <            // END SETUP
123 <            
124 <
125 <            /**************************************************************
126 <              Here would be an ideal place to start another thread to do
127 <              the listening part of the Filter. Ideally it should just be
128 <              created and then run(). It may be necessary to pass some of
129 <              the following parameters into the thread by the constructor.
130 <              
131 <                  Logger logger
132 <                      - a reference to a Logger object
133 <                  FilterManager filterManager
134 <                      - a reference to the system filter manager
135 <                  ConfigurationManager configManager
136 <                      - a reference to the system configuration manager
137 <                  Configuration myConfig
138 <                      - a reference to the configuration object for this
139 <                        filter instance
140 <                  String ourName
141 <                      - our "identifier" name
142 <            
143 <            **************************************************************/
144 <                                    
145 <            logger.write(toString, Logger.SYSINIT, "starting Filter UDP listener");
146 <            UDPReader udpReader = new UDPReader(UDPport, parentFilter, logger);
147 <            udpReader.start();
148 <            logger.write(toString, Logger.SYSINIT, "Filter UDP listener started");
149 <
150 <            logger.write(toString, Logger.SYSINIT, "starting Filter TCP listener");
151 <            TCPReader tcpReader = new TCPReader(logger, configManager, TCPport, parentFilter);
152 <            tcpReader.start();
153 <            logger.write(toString, Logger.SYSINIT, "Filter TCP listener started");
154 <
155 <            // TEST
156 <            //parentFilter.receiveXML("<?xml version=\"1.0\" encoding=\"ISO8859-1\"?><packet><test>This is just a debugging test, we ("+ourName+") are live on UDPport: "+UDPport+"</test></packet>");
157 <            
158 <            // start the POA off
159 <            poa.the_POAManager().activate();
160 <                        
161 <            logger.write(toString, Logger.SYSINIT, "started");
162 <            
163 <            // now we are running, we just need to serve
164 <            // so we ask the orb to block for us until it has finished
165 <            orb.run();
166 <            
167 <        } catch (Exception e) {
168 <            System.err.println("FILTER ERROR: " + e);
169 <            e.printStackTrace(System.out);
138 >            // Start a filter thread
139 >            _logger.write(toString(), Logger.SYSINIT, "starting Filter Thread / Queue consumer");
140 >            FilterThread filterThread = new FilterThread(queue);
141 >            filterThread.start();
142          }
143 +        else {
144 +            // it's pointless carrying on really...
145 +            throw new ComponentStartException("Can't start Filter without any inbound data feeds");            
146 +        }
147 +        
148 +        // the corba listener needs these to be set, so lets
149 +        // make them something obviously invalid initially
150 +        int UDPListenPort = -1;
151 +        
152 +        // UDP Reader start (for inbound host data)
153 +        if(activateUDPReader) {
154 +            try {
155 +                // get the port number from the configuration
156 +                UDPListenPort = Integer.parseInt(cp.getProperty("Filter." + FilterMain.NAME, "Filter.UDPListenPort"));
157 +                // start the UDPReader
158 +                _logger.write(toString(), Logger.SYSINIT, "starting Filter UDP listener");
159 +                UDPReader udpReader = new UDPReader(UDPListenPort, queue);
160 +                udpReader.start();
161 +            } catch (PropertyNotFoundException e) {
162 +                _logger.write(toString(), Logger.WARNING, "Unable to start UDPReader due to missing configuration: " + e);
163 +            } catch (NumberFormatException e) {
164 +                _logger.write(toString(), Logger.WARNING, "Unable to start UDPReader due to invalid configuration: " + e);
165 +            }
166 +        }
167 +        
168 +        // FilterServant start (for inbound child filter data)
169 +        if(activateCORBAReader) {
170 +            // start the FilterServant
171 +            _logger.write(toString(), Logger.SYSINIT, "starting Servant to listen for downstream filters");
172 +            FilterServant filterServant = new FilterServant(queue);
173 +            _refman.bindToOrb(filterServant, "iscream.Filter\\." + FilterMain.NAME);
174 +        }
175 +        
176 +        // FilterInfoServant start (to provide filter information to the server)
177 +        _logger.write(toString(), Logger.SYSINIT, "starting Servant to provide filter information");
178 +        FilterInfoServant filterInfoServant = new FilterInfoServant(UDPListenPort);
179 +        _refman.bindToOrb(filterInfoServant, "iscream.FilterInfo\\." + FilterMain.NAME);
180 +        
181 +        _logger.write(toString(), Logger.SYSINIT, "started");
182      }
183 <
184 <    /**
185 <     * A simple method to print the usage of this class.
186 <     * It never returns, but instead exits to the system
187 <     * with a value 1, to indicate the system did not start
188 <     * properly.
183 >    
184 >    /**
185 >     * Does a dependency check. Used mainly at startup to
186 >     * see if the required dependencies (components) are up
187 >     * and running.
188 >     *
189 >     * @return a boolean value, true if the depdencies are satisfied
190       */
191 <    public static void usage() {
192 <        System.out.println("USAGE: java FilterMain <name>");
193 <        System.out.println("WHERE <name>:");
194 <        System.out.println("      The unique identifier for the Filter in the system.");
195 <        System.exit(1);
191 >    public boolean depCheck() {
192 >        try {
193 >            org.omg.CORBA.Object obj;
194 >            // first check the ConfigurationManager is alive
195 >            obj = _refman.getCORBARef("iscream.ConfigurationManager");
196 >            // then suss out our parent filter
197 >            ConfigurationProxy cp = ConfigurationProxy.getInstance();
198 >            String parentFilterName = cp.getProperty("Filter." + FilterMain.NAME, "Filter.parentFilter");
199 >            // finally check the parent filter is alive
200 >            obj = _refman.getCORBARef("iscream.Filter\\." + parentFilterName);
201 >        } catch(ComponentCORBAException e) {
202 >            System.err.println(toString() + ": Dependency Failure: "+e);
203 >            return false;
204 >        } catch(PropertyNotFoundException e) {
205 >            System.err.println(toString() + ": Unable to obtain configuration: "+e);
206 >            return false;
207 >        }
208 >        // dependency check suceeded
209 >        return true;
210      }
211 +    
212 +    /**
213 +     * Overrides the {@link java.lang.Object#toString() Object.toString()}
214 +     * method to provide clean logging (every class should have this).
215 +     *
216 +     * This uses the uk.org.iscream.cms.util.NameFormat class
217 +     * to format the toString()
218 +     *
219 +     * @return the name of this class and its CVS revision
220 +     */
221 +    public String toString() {
222 +        return FormatName.getName(
223 +            NAME,
224 +            getClass().getName(),
225 +            REVISION);
226 +    }
227  
186 //---CONSTRUCTORS---
187
188 //---PUBLIC METHODS---
189
228   //---PRIVATE METHODS---
229  
230   //---ACCESSOR/MUTATOR METHODS---
231  
232   //---ATTRIBUTES---
233  
234 +    /**
235 +     * This holds a reference to the
236 +     * system logger that is being used.
237 +     */
238 +    private Logger _logger;
239 +
240 +    /**
241 +     * A reference to the reference manager in use
242 +     */
243 +    private ReferenceManager _refman = ReferenceManager.getInstance();
244 +
245   //---STATIC ATTRIBUTES---
246  
247 < }            
247 >    /**
248 >     * The friendly name for this component, used by
249 >     * all related classes.
250 >     * This is set from the configuration.
251 >     */
252 >    public static String NAME;
253 >
254 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines