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.org.iscream.cms.server.filter; |
22 |
|
|
23 |
|
//---IMPORTS--- |
24 |
< |
import uk.ac.ukc.iscream.core.*; |
25 |
< |
import uk.ac.ukc.iscream.filter.*; |
26 |
< |
import org.omg.CORBA.*; |
27 |
< |
import org.omg.CosNaming.*; |
8 |
< |
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 |
|
|
44 |
|
|
45 |
|
//---STATIC METHODS--- |
46 |
|
|
47 |
< |
public static void main(String[] args) { |
48 |
< |
System.setProperty("org.omg.CORBA.ORBClass","jacorb.orb.ORB"); |
49 |
< |
System.setProperty("org.omg.CORBA.ORBSingletonClass","jacorb.orb.ORBSingleton"); |
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 |
> |
//---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 |
< |
// get our name from the command line |
68 |
< |
String ourName = ""; |
69 |
< |
if (args.length == 1) { |
70 |
< |
ourName = args[0]; |
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) + ")"; |
105 |
> |
// need to use the Queue later on |
106 |
> |
Queue queue; |
107 |
|
|
108 |
< |
try { |
109 |
< |
ORB orb = ORB.init(args, null); |
110 |
< |
|
111 |
< |
// something to hold objects |
112 |
< |
org.omg.CORBA.Object objRef = null; |
113 |
< |
|
114 |
< |
// get the Root POA |
115 |
< |
objRef = orb.resolve_initial_references("RootPOA"); |
116 |
< |
POA poa = POAHelper.narrow(objRef); |
117 |
< |
|
118 |
< |
// get a hook to the name service |
119 |
< |
objRef = orb.resolve_initial_references("NameService"); |
120 |
< |
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); |
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 |
< |
// get a ref to the ConfigurationManager, Logger & the FilterManager |
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 |
< |
objRef = ncRef.resolve(ncRef.to_name("iscream.ConfigurationManager")); |
138 |
< |
ConfigurationManager configManager = ConfigurationManagerHelper.narrow(objRef); |
139 |
< |
|
140 |
< |
objRef = ncRef.resolve(ncRef.to_name("iscream.Logger")); |
141 |
< |
Logger logger = LoggerHelper.narrow(objRef); |
142 |
< |
|
143 |
< |
logger.write(toString, Logger.SYSINIT, "coming up"); |
144 |
< |
|
67 |
< |
|
68 |
< |
// **** THIS SECTION WILL NEED CHANGING TO GET RELEVANT CONFIG |
69 |
< |
// **** Please ignore this block of code, it's just "copy/paste" :) |
70 |
< |
|
71 |
< |
// get the config |
72 |
< |
Configuration myConfig = configManager.getConfiguration(ourName); |
73 |
< |
|
74 |
< |
// read some config here |
75 |
< |
int UDPport = 0; |
76 |
< |
int TCPport = 0; |
77 |
< |
String parentFilterName = null; |
78 |
< |
|
79 |
< |
// did we? |
80 |
< |
if (myConfig == null) { |
81 |
< |
System.out.println("Failed: is it there?, can you read it?"); |
82 |
< |
System.exit(1); |
83 |
< |
} else { |
84 |
< |
|
85 |
< |
// get the property |
86 |
< |
try { |
87 |
< |
//QUERY HERE.... ??? |
88 |
< |
UDPport = new Integer(myConfig.getProperty("Filter.UDPlistenPort")).intValue(); |
89 |
< |
TCPport = new Integer(myConfig.getProperty("Filter.TCPlistenPort")).intValue(); |
90 |
< |
parentFilterName = myConfig.getProperty("Filter.parentFilter"); |
91 |
< |
} catch (org.omg.CORBA.MARSHAL e) { |
92 |
< |
System.out.println("Caught org.omg.CORBA.MARSHAL, must be a null we got back"); |
93 |
< |
//System.exit(1); |
94 |
< |
} |
137 |
> |
// startup a monitor on this queue |
138 |
> |
try { |
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 |
< |
logger.write(toString, Logger.SYSINIT, "configured"); |
148 |
< |
|
149 |
< |
// **** END COMMENT |
150 |
< |
|
101 |
< |
// **** INITIAL FILTER MANAGER COMMUNICATIONS HERE |
102 |
< |
|
103 |
< |
// get a root (CHANGE to use FilterManager) |
104 |
< |
objRef = ncRef.resolve(ncRef.to_name("iscream.Filter." + parentFilterName)); |
105 |
< |
Filter parentFilter = FilterHelper.narrow(objRef); |
106 |
< |
|
107 |
< |
// SETUP our Servant |
108 |
< |
|
109 |
< |
// create the FilterServant |
110 |
< |
FilterServant filterServant = new FilterServant(logger, parentFilter, ourName, new Integer(TCPport).toString() , new Integer(UDPport).toString()); |
111 |
< |
|
112 |
< |
// register ourselves |
113 |
< |
|
114 |
< |
// and advertise it to the naming context |
115 |
< |
objRef = poa.servant_to_reference(filterServant); |
116 |
< |
ncRef.bind(ncRef.to_name("iscream.Filter."+ourName), objRef); |
117 |
< |
|
118 |
< |
// **** END COMMENT |
119 |
< |
|
120 |
< |
// END SETUP |
121 |
< |
|
122 |
< |
|
123 |
< |
/************************************************************** |
124 |
< |
Here would be an ideal place to start another thread to do |
125 |
< |
the listening part of the Filter. Ideally it should just be |
126 |
< |
created and then run(). It may be necessary to pass some of |
127 |
< |
the following parameters into the thread by the constructor. |
128 |
< |
|
129 |
< |
Logger logger |
130 |
< |
- a reference to a Logger object |
131 |
< |
FilterManager filterManager |
132 |
< |
- a reference to the system filter manager |
133 |
< |
ConfigurationManager configManager |
134 |
< |
- a reference to the system configuration manager |
135 |
< |
Configuration myConfig |
136 |
< |
- a reference to the configuration object for this |
137 |
< |
filter instance |
138 |
< |
String ourName |
139 |
< |
- our "identifier" name |
140 |
< |
|
141 |
< |
**************************************************************/ |
142 |
< |
|
143 |
< |
logger.write(toString, Logger.SYSINIT, "starting Filter UDP listener"); |
144 |
< |
UDPReader udpReader = new UDPReader(UDPport, parentFilter, logger); |
145 |
< |
udpReader.start(); |
146 |
< |
logger.write(toString, Logger.SYSINIT, "Filter UDP listener started"); |
147 |
< |
|
148 |
< |
logger.write(toString, Logger.SYSINIT, "starting Filter TCP listener"); |
149 |
< |
TCPReader tcpReader = new TCPReader(logger, configManager, TCPport, parentFilter); |
150 |
< |
tcpReader.start(); |
151 |
< |
logger.write(toString, Logger.SYSINIT, "Filter TCP listener started"); |
152 |
< |
|
153 |
< |
// TEST |
154 |
< |
//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>"); |
155 |
< |
|
156 |
< |
// start the POA off |
157 |
< |
poa.the_POAManager().activate(); |
158 |
< |
|
159 |
< |
logger.write(toString, Logger.SYSINIT, "started"); |
160 |
< |
|
161 |
< |
// now we are running, we just need to serve |
162 |
< |
// so we ask the orb to block for us until it has finished |
163 |
< |
orb.run(); |
164 |
< |
|
165 |
< |
} catch (Exception e) { |
166 |
< |
System.err.println("FILTER ERROR: " + e); |
167 |
< |
e.printStackTrace(System.out); |
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 |
+ |
else { |
153 |
+ |
// it's pointless carrying on really... |
154 |
+ |
throw new ComponentStartException("Can't start Filter without any inbound data feeds"); |
155 |
+ |
} |
156 |
+ |
|
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 |
+ |
|
162 |
+ |
// TCP Reader start (for heartbeats) |
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 |
+ |
// 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 |
|
|
184 |
– |
//---CONSTRUCTORS--- |
185 |
– |
|
186 |
– |
//---PUBLIC METHODS--- |
187 |
– |
|
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 |
> |
} |