1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.filter; |
2 |
> |
package uk.org.iscream.cms.server.filter; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.ac.ukc.iscream.util.*; |
6 |
< |
import uk.ac.ukc.iscream.core.*; |
7 |
< |
import uk.ac.ukc.iscream.filter.*; |
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 |
< |
// *************************************** |
30 |
< |
// VERY TEMPORARY - will find a better way |
31 |
< |
System.setProperty("org.omg.CORBA.ORBClass","jacorb.orb.ORB"); |
32 |
< |
System.setProperty("org.omg.CORBA.ORBSingletonClass","jacorb.orb.ORBSingleton"); |
33 |
< |
// *************************************** |
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 |
< |
// get our name from the command line |
40 |
< |
String ourName = ""; |
41 |
< |
if (args.length == 1) { |
42 |
< |
ourName = args[0]; |
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 |
> |
_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) + ")"; |
44 |
< |
|
45 |
< |
ReferenceManager refman = ReferenceManager.init(null, ourName); |
86 |
> |
// need to use the Queue later on |
87 |
> |
Queue queue; |
88 |
|
|
89 |
< |
refman.getLogger().write(toString, Logger.SYSINIT, "coming up"); |
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 |
> |
} 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 |
< |
// configuration variables we require |
50 |
< |
int UDPListenPort = 0; |
51 |
< |
int TCPListenPort = 0; |
52 |
< |
String parentFilterName = null; |
53 |
< |
|
54 |
< |
Configuration config = refman.getCM().getConfiguration(refman.getName()); |
55 |
< |
if (config == null) { |
56 |
< |
throw new RuntimeException ("CRITICAL:Unable to obtain configuration" + |
57 |
< |
" Advise you check the i-scream log for more information."); |
58 |
< |
} else { |
118 |
> |
// startup a monitor on this queue |
119 |
|
try { |
120 |
< |
UDPListenPort = Integer.parseInt(config.getProperty("Filter.UDPListenPort")); |
121 |
< |
TCPListenPort = Integer.parseInt(config.getProperty("Filter.TCPListenPort")); |
122 |
< |
parentFilterName = config.getProperty("Filter.parentFilter"); |
123 |
< |
} catch (org.omg.CORBA.MARSHAL e) { |
124 |
< |
refman.getLogger().write(toString, Logger.FATAL, "required config property not present"); |
125 |
< |
throw new RuntimeException ("CRITICAL:Unable to obtain required configuration property" + |
66 |
< |
" Advise you check the i-scream log for more information."); |
67 |
< |
|
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 |
+ |
// 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 |
< |
refman.getLogger().write(toString, Logger.SYSINIT, "configured"); |
134 |
< |
// get parent |
135 |
< |
Filter parentFilter = FilterHelper.narrow(refman.getCORBARef("iscream.Filter." + parentFilterName)); |
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 |
< |
|
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 |
|
|
76 |
– |
// FilterServant start (for inbound child filter data) |
77 |
– |
refman.getLogger().write(toString, Logger.DEBUG, "starting Filter Child -> Parent link for upstream parent - " + parentFilterName); |
78 |
– |
FilterServant filterServant = new FilterServant(parentFilter, TCPListenPort, UDPListenPort); |
79 |
– |
refman.bindToOrb(filterServant, "iscream.Filter." + refman.getName()); |
80 |
– |
|
81 |
– |
// UDL Reader start (for inbound host data) |
82 |
– |
refman.getLogger().write(toString, Logger.SYSINIT, "starting Filter UDP listener"); |
83 |
– |
UDPReader udpReader = new UDPReader(UDPListenPort, parentFilter); |
84 |
– |
udpReader.start(); |
85 |
– |
|
143 |
|
// TCP Reader start (for heartbeats) |
144 |
< |
refman.getLogger().write(toString, Logger.SYSINIT, "starting Filter TCP listener"); |
145 |
< |
TCPReader tcpReader = new TCPReader(TCPListenPort, parentFilter); |
146 |
< |
tcpReader.start(); |
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 |
< |
// start the POA off |
160 |
< |
// now we are running, we just need to serve |
161 |
< |
// so we ask the orb to block for us until it has finished |
162 |
< |
refman.activatePOA(); |
163 |
< |
refman.getLogger().write(toString, Logger.SYSINIT, "started"); |
164 |
< |
refman.getORB().run(); |
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(queue); |
180 |
> |
_refman.bindToOrb(filterServant, "iscream.Filter." + FilterMain.NAME); |
181 |
> |
} |
182 |
> |
|
183 |
> |
// FilterInfoServant start (to provide filter information to the server) |
184 |
> |
_logger.write(toString(), Logger.SYSINIT, "starting Servant to provide filter information"); |
185 |
> |
FilterInfoServant filterInfoServant = new FilterInfoServant(TCPListenPort, UDPListenPort); |
186 |
> |
_refman.bindToOrb(filterInfoServant, "iscream.FilterInfo." + FilterMain.NAME); |
187 |
> |
|
188 |
> |
_logger.write(toString(), Logger.SYSINIT, "started"); |
189 |
|
} |
190 |
< |
|
191 |
< |
/** |
192 |
< |
* A simple method to print the usage of this class. |
193 |
< |
* It never returns, but instead exits to the system |
194 |
< |
* with a value 1, to indicate the system did not start |
195 |
< |
* properly. |
190 |
> |
|
191 |
> |
/** |
192 |
> |
* Does a dependency check. Used mainly at startup to |
193 |
> |
* see if the required dependencies (components) are up |
194 |
> |
* and running. |
195 |
> |
* |
196 |
> |
* @return a boolean value, true if the depdencies are satisfied |
197 |
|
*/ |
198 |
< |
public static void usage() { |
199 |
< |
System.out.println("USAGE: java FilterMain <name>"); |
200 |
< |
System.out.println("WHERE <name>:"); |
201 |
< |
System.out.println(" The unique identifier for the Filter in the system."); |
202 |
< |
System.exit(1); |
198 |
> |
public boolean depCheck() { |
199 |
> |
try { |
200 |
> |
org.omg.CORBA.Object obj; |
201 |
> |
// first check the ConfigurationManager is alive |
202 |
> |
obj = _refman.getCORBARef("iscream.ConfigurationManager"); |
203 |
> |
// then suss out our parent filter |
204 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
205 |
> |
String parentFilterName = cp.getProperty("Filter." + FilterMain.NAME, "Filter.parentFilter"); |
206 |
> |
// finally check the parent filter is alive |
207 |
> |
obj = _refman.getCORBARef("iscream.Filter." + parentFilterName); |
208 |
> |
} catch(ComponentCORBAException e) { |
209 |
> |
System.err.println(toString() + ": Dependency Failure: "+e); |
210 |
> |
return false; |
211 |
> |
} catch(PropertyNotFoundException e) { |
212 |
> |
System.err.println(toString() + ": Unable to obtain configuration: "+e); |
213 |
> |
return false; |
214 |
> |
} |
215 |
> |
// dependency check suceeded |
216 |
> |
return true; |
217 |
|
} |
218 |
+ |
|
219 |
+ |
/** |
220 |
+ |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
221 |
+ |
* method to provide clean logging (every class should have this). |
222 |
+ |
* |
223 |
+ |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
224 |
+ |
* to format the toString() |
225 |
+ |
* |
226 |
+ |
* @return the name of this class and its CVS revision |
227 |
+ |
*/ |
228 |
+ |
public String toString() { |
229 |
+ |
return FormatName.getName( |
230 |
+ |
NAME, |
231 |
+ |
getClass().getName(), |
232 |
+ |
REVISION); |
233 |
+ |
} |
234 |
|
|
112 |
– |
//---CONSTRUCTORS--- |
113 |
– |
|
114 |
– |
//---PUBLIC METHODS--- |
115 |
– |
|
235 |
|
//---PRIVATE METHODS--- |
236 |
|
|
237 |
|
//---ACCESSOR/MUTATOR METHODS--- |
238 |
|
|
239 |
|
//---ATTRIBUTES--- |
240 |
|
|
241 |
+ |
/** |
242 |
+ |
* This holds a reference to the |
243 |
+ |
* system logger that is being used. |
244 |
+ |
*/ |
245 |
+ |
private Logger _logger; |
246 |
+ |
|
247 |
+ |
/** |
248 |
+ |
* A reference to the reference manager in use |
249 |
+ |
*/ |
250 |
+ |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
251 |
+ |
|
252 |
|
//---STATIC ATTRIBUTES--- |
253 |
|
|
254 |
< |
} |
254 |
> |
/** |
255 |
> |
* The friendly name for this component, used by |
256 |
> |
* all related classes. |
257 |
> |
* This is set from the configuration. |
258 |
> |
*/ |
259 |
> |
public static String NAME; |
260 |
> |
|
261 |
> |
} |