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.org.iscream.cms.server.componentmanager; |
23 |
|
24 |
//---IMPORTS--- |
25 |
import java.util.*; |
26 |
import java.io.*; |
27 |
import uk.org.iscream.cms.server.util.*; |
28 |
|
29 |
/** |
30 |
* The component manager is the starting point for all |
31 |
* server side components of the iscream system. |
32 |
* It loads its initial system configuration from the |
33 |
* default properties file, it then starts all the iscream |
34 |
* components as specified in the default.properties under |
35 |
* uk.org.iscream.cms.server.ComponentList |
36 |
* |
37 |
* @author $Author: tdb $ |
38 |
* @version $Id: ComponentManager.java,v 1.40 2002/05/18 18:16:01 tdb Exp $ |
39 |
*/ |
40 |
public class ComponentManager { |
41 |
|
42 |
//---FINAL ATTRIBUTES--- |
43 |
|
44 |
/** |
45 |
* The current CVS revision of this class |
46 |
*/ |
47 |
public static final String REVISION = "$Revision: 1.40 $"; |
48 |
|
49 |
/** |
50 |
* The toString() of this class |
51 |
* As it won't be instatiated, this is needed. |
52 |
* Not also that we pass a null as the class name (as we are static) |
53 |
*/ |
54 |
public static final String toString = FormatName.getName("ComponentManager", null, REVISION); |
55 |
|
56 |
/** |
57 |
* The default location of the properties file for the system |
58 |
*/ |
59 |
public static final String DEFAULTPROPERTIES = "./etc/default.properties"; |
60 |
|
61 |
/** |
62 |
* The default time to wait before retrying |
63 |
* component. |
64 |
*/ |
65 |
public static final int DEFAULT_COMPONENT_START_TIMEOUT = 5; |
66 |
|
67 |
//---STATIC METHODS--- |
68 |
|
69 |
/** |
70 |
* The main method which starts the components as |
71 |
* listed in the default.properties file. |
72 |
* |
73 |
* @param args the command line arguments |
74 |
*/ |
75 |
public static void main(String[] args) { |
76 |
System.out.println("-----------------------------------------"); |
77 |
System.out.println("--- i-scream Server Component Manager ---"); |
78 |
System.out.println("--- (c) 2001 The i-scream Project ---"); |
79 |
System.out.println("--- (http://www.i-scream.org.uk) ---"); |
80 |
System.out.println("-----------------------------------------"); |
81 |
System.out.println("--- Starting System ---"); |
82 |
|
83 |
// get the command line args |
84 |
String defaultProperties = DEFAULTPROPERTIES; |
85 |
String filterName = null; |
86 |
String filterManagerName = null; |
87 |
for(int i=0; i < args.length; i++) { |
88 |
if(args[i].equals("-h")) { |
89 |
usage(); |
90 |
} |
91 |
else if(args[i].equals("-f")) { |
92 |
if(++i < args.length) { |
93 |
filterName = args[i]; |
94 |
} |
95 |
else { |
96 |
usage(); |
97 |
} |
98 |
} |
99 |
else if(args[i].equals("-fm")) { |
100 |
if(++i < args.length) { |
101 |
filterManagerName = args[i]; |
102 |
} |
103 |
else { |
104 |
usage(); |
105 |
} |
106 |
} |
107 |
else if(args[i].equals("-l")) { |
108 |
if(++i < args.length) { |
109 |
defaultProperties = args[i]; |
110 |
} |
111 |
else { |
112 |
usage(); |
113 |
} |
114 |
} |
115 |
else { |
116 |
usage(); |
117 |
} |
118 |
} |
119 |
|
120 |
// load the default properties file into the system properties |
121 |
System.out.println(toString + ": initialising - using " + defaultProperties); |
122 |
try { |
123 |
Properties initProperties = new Properties(System.getProperties()); |
124 |
initProperties.load(new FileInputStream(new File(defaultProperties))); |
125 |
System.setProperties(initProperties); |
126 |
} catch (Exception e) { |
127 |
System.err.println(toString + ": ERROR " + e.getMessage()); |
128 |
usage(); |
129 |
} |
130 |
|
131 |
// continue to bring the system up |
132 |
System.out.println(toString + ": coming up"); |
133 |
|
134 |
// start the ORB by initialising the ReferenceManager |
135 |
ReferenceManager refman = ReferenceManager.getInstance(); |
136 |
|
137 |
// now the ORB is running, we need to activate our RootPOA |
138 |
// so that we can start serving requests once servants start up |
139 |
refman.activatePOA(); |
140 |
|
141 |
// get the list of components |
142 |
String componentList = System.getProperty("uk.org.iscream.cms.server.ComponentList"); |
143 |
StringTokenizer st = new StringTokenizer(componentList, ";"); |
144 |
_componentsToStart = new LinkedList(); |
145 |
|
146 |
// this could be done using reflection |
147 |
// but..well..we don't ;-p |
148 |
while (st.hasMoreTokens()){ |
149 |
String componentName = st.nextToken(); |
150 |
Component component = null; |
151 |
|
152 |
|
153 |
// ### This is where the list of supported components is checked! ### |
154 |
if (componentName.equalsIgnoreCase("core")) { |
155 |
component = new uk.org.iscream.cms.server.core.Core(); |
156 |
// note the passing of the FilterManagers's name in its constructor |
157 |
} else if (componentName.equalsIgnoreCase("filtermanager")) { |
158 |
component = new uk.org.iscream.cms.server.filtermanager.FilterManager(filterManagerName); |
159 |
} else if (componentName.equalsIgnoreCase("rootfilter")) { |
160 |
component = new uk.org.iscream.cms.server.rootfilter.RootFilter(); |
161 |
} else if (componentName.equalsIgnoreCase("dbinterface")) { |
162 |
component = new uk.org.iscream.cms.server.dbinterface.DBInterface(); |
163 |
} else if (componentName.equalsIgnoreCase("clientinterface")) { |
164 |
component = new uk.org.iscream.cms.server.clientinterface.ClientInterfaceMain(); |
165 |
// note the passing of the Filter's name in its constructor |
166 |
} else if (componentName.equalsIgnoreCase("filter")) { |
167 |
component = new uk.org.iscream.cms.server.filter.FilterMain(filterName); |
168 |
} else if (componentName.equalsIgnoreCase("client")) { |
169 |
component = new uk.org.iscream.cms.server.client.ClientMain(); |
170 |
} |
171 |
// ### Add new component constructors in the above section! ### |
172 |
|
173 |
// build the list of components to start |
174 |
if (component != null) { |
175 |
_componentsToStart.add(component); |
176 |
} else { |
177 |
System.err.println(toString + ": WARNING unsupported component not started: "+componentName); |
178 |
} |
179 |
} |
180 |
|
181 |
|
182 |
|
183 |
// get the value for timeout |
184 |
|
185 |
String confTimeout = null; |
186 |
try { |
187 |
confTimeout = System.getProperty("uk.org.iscream.cms.server.ComponentTimeout"); |
188 |
confTimeout.trim(); |
189 |
_startTimeout = Integer.parseInt(confTimeout); |
190 |
} catch (NumberFormatException e) { |
191 |
_startTimeout = DEFAULT_COMPONENT_START_TIMEOUT; |
192 |
System.err.println(toString + ": unable to read uk.org.iscream.cms.server.ComponentTimeout value (" + confTimeout + "), using default!"); |
193 |
} |
194 |
System.out.println(toString + ": using component start timeout of " + _startTimeout + " seconds"); |
195 |
|
196 |
// startup the system components |
197 |
startUp(); |
198 |
|
199 |
// block on the ORB...in time, management functionality can be placed here. |
200 |
// if we detect a CORBA communication error, we'll restart all the components. |
201 |
// !! this doesn't appear to work !! |
202 |
while(true) { |
203 |
try { |
204 |
refman.getORB().run(); |
205 |
} catch (org.omg.CORBA.COMM_FAILURE e) { |
206 |
System.out.println(toString + ": WARNING CORBA communications failure - " + e.getMessage()); |
207 |
System.out.println(toString + ": WARNING CORBA connections have been lost - attempting to restart!"); |
208 |
} |
209 |
startUp(); |
210 |
} |
211 |
} |
212 |
|
213 |
/** |
214 |
* Starts the components as obtained from the configuration. |
215 |
* This method calls the start() methods on all the components. |
216 |
* If a component fails to start due to a CORBA communication |
217 |
* problem, then it catches this and tries to start it again |
218 |
* according to uk.org.iscream.cms.server.ComponentTimeout time. |
219 |
* |
220 |
* If the server dies and CORBA connections are lost, this method |
221 |
* is called again. |
222 |
*/ |
223 |
private static void startUp() { |
224 |
// now we try and start the beast up |
225 |
boolean tryAgain = true; |
226 |
Component component = null; |
227 |
|
228 |
// keep trying until we've started all the components. |
229 |
// maybe add support for a limited number of retries |
230 |
while(tryAgain) { |
231 |
Iterator i = _componentsToStart.iterator(); |
232 |
LinkedList failedComponents = new LinkedList(); |
233 |
// go through all the components |
234 |
while(i.hasNext()) { |
235 |
// get a refence to the component |
236 |
component = (Component) i.next(); |
237 |
System.out.println(toString + ": dependency checking component - " + component.toString()); |
238 |
|
239 |
// check it's dependencies |
240 |
boolean depOK = component.depCheck(); |
241 |
if(depOK) { |
242 |
System.out.println(toString + ": starting component - " + component.toString()); |
243 |
// it should be ok to start the component |
244 |
try { |
245 |
// start the component |
246 |
component.start(); |
247 |
} catch (ComponentStartException e) { |
248 |
// if we get this then there was a problem |
249 |
// that we can't recover from |
250 |
System.err.println(toString + ": ERROR starting component - " + component.toString()); |
251 |
System.err.println(toString + ": component reports - " + e.getMessage()); |
252 |
System.exit(1); |
253 |
} |
254 |
} |
255 |
else { |
256 |
// it seems the depedencies failed |
257 |
// so we want to try again after a delay |
258 |
System.err.println(toString + ": WARNING Component reported dependency failure"); |
259 |
System.err.println(toString + ": This could be because it can't communicate with components it needs."); |
260 |
|
261 |
// make a list of the failed components |
262 |
failedComponents.add(component); |
263 |
} |
264 |
} |
265 |
|
266 |
// if we had some failed components that we can retry |
267 |
if (failedComponents.size() > 0) { |
268 |
System.err.println(toString + ": WARNING One or more components failed to start correctly."); |
269 |
System.err.println(toString + ": Will try again in " + _startTimeout + " seconds"); |
270 |
|
271 |
// our list is now the failed list |
272 |
_componentsToStart = failedComponents; |
273 |
|
274 |
// sleep for a given timeout |
275 |
try { |
276 |
Thread.sleep(_startTimeout * 1000); |
277 |
} catch (InterruptedException e) { |
278 |
// we're not bothered |
279 |
} |
280 |
// otherwise we started everything |
281 |
} else { |
282 |
// so we set to exit the loop |
283 |
tryAgain = false; |
284 |
} |
285 |
} |
286 |
|
287 |
System.out.println(toString + ": running"); |
288 |
} |
289 |
|
290 |
/** |
291 |
* A simple method to print the usage of this class. |
292 |
* It never returns, but instead exits to the system |
293 |
* with a value 1, to indicate the system did not start |
294 |
* properly. |
295 |
*/ |
296 |
public static void usage() { |
297 |
System.out.println("USAGE: java uk.org.iscream.cms.server.componentmanager.ComponentManager <option>"); |
298 |
System.out.println(" or: java -jar iscream.jar <option>"); |
299 |
System.out.println("WHERE <option>:"); |
300 |
System.out.println(" -l <filename> - the location of initial system properties"); |
301 |
System.out.println(" the default is ./etc/default.properties"); |
302 |
System.out.println(" -f <name> - the name of the filter (if there is one configured"); |
303 |
System.out.println(" -fm <name> - the name of the filter manager (if there is one configured"); |
304 |
System.out.println(" -h - this help screen"); |
305 |
System.exit(1); |
306 |
} |
307 |
|
308 |
//---CONSTRUCTORS--- |
309 |
|
310 |
//---PUBLIC METHODS--- |
311 |
|
312 |
//---PRIVATE METHODS--- |
313 |
|
314 |
//---ACCESSOR/MUTATOR METHODS--- |
315 |
|
316 |
//---ATTRIBUTES--- |
317 |
|
318 |
//---STATIC ATTRIBUTES--- |
319 |
|
320 |
private static LinkedList _componentsToStart; |
321 |
private static int _startTimeout = 0; |
322 |
|
323 |
} |