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