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.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: tdb $ |
34 |
* @version $Id: FilterMain.java,v 1.34 2002/03/22 10:43:06 tdb Exp $ |
35 |
*/ |
36 |
public class FilterMain implements Component { |
37 |
|
38 |
//---FINAL ATTRIBUTES--- |
39 |
|
40 |
/** |
41 |
* The current CVS revision of this class |
42 |
*/ |
43 |
public static final String REVISION = "$Revision: 1.34 $"; |
44 |
|
45 |
//---STATIC METHODS--- |
46 |
|
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 |
_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 |
// 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 |
// need to use the Queue later on |
106 |
Queue queue; |
107 |
|
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 |
// 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 |
// 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 |
* 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 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 |
|
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 |
/** |
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 |
} |