ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/componentmanager/ComponentManager.java
Revision: 1.40
Committed: Sat May 18 18:16:01 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.39: +21 -2 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

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