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.42
Committed: Wed Feb 5 16:43:46 2003 UTC (21 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.41: +3 -3 lines
Log Message:
Changed the server to use the external util package. Quite a minor change,
but does affect a lot of files.

File Contents

# User Rev Content
1 tdb 1.40 /*
2     * i-scream central monitoring system
3 tdb 1.41 * http://www.i-scream.org.uk
4 tdb 1.40 * 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 ajm 1.1 //---PACKAGE DECLARATION---
22 tdb 1.38 package uk.org.iscream.cms.server.componentmanager;
23 ajm 1.1
24     //---IMPORTS---
25     import java.util.*;
26     import java.io.*;
27 tdb 1.42 import uk.org.iscream.cms.util.*;
28 ajm 1.1
29     /**
30 ajm 1.2 * 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 tdb 1.38 * uk.org.iscream.cms.server.ComponentList
36 ajm 1.1 *
37 tdb 1.39 * @author $Author: tdb $
38 tdb 1.42 * @version $Id: ComponentManager.java,v 1.41 2002/05/21 16:47:17 tdb Exp $
39 ajm 1.1 */
40     public class ComponentManager {
41    
42     //---FINAL ATTRIBUTES---
43    
44     /**
45     * The current CVS revision of this class
46     */
47 tdb 1.42 public static final String REVISION = "$Revision: 1.41 $";
48 ajm 1.1
49     /**
50     * The toString() of this class
51     * As it won't be instatiated, this is needed.
52 ajm 1.2 * Not also that we pass a null as the class name (as we are static)
53 ajm 1.1 */
54 ajm 1.2 public static final String toString = FormatName.getName("ComponentManager", null, REVISION);
55 ajm 1.1
56     /**
57     * The default location of the properties file for the system
58     */
59 ajm 1.2 public static final String DEFAULTPROPERTIES = "./etc/default.properties";
60 ajm 1.1
61 ajm 1.22 /**
62     * The default time to wait before retrying
63     * component.
64     */
65     public static final int DEFAULT_COMPONENT_START_TIMEOUT = 5;
66    
67 ajm 1.1 //---STATIC METHODS---
68    
69     /**
70 ajm 1.2 * The main method which starts the components as
71     * listed in the default.properties file.
72 ajm 1.1 *
73     * @param args the command line arguments
74     */
75     public static void main(String[] args) {
76 ajm 1.36 System.out.println("-----------------------------------------");
77 tdb 1.11 System.out.println("--- i-scream Server Component Manager ---");
78 tdb 1.37 System.out.println("--- (c) 2001 The i-scream Project ---");
79 ajm 1.36 System.out.println("--- (http://www.i-scream.org.uk) ---");
80     System.out.println("-----------------------------------------");
81 ajm 1.2 System.out.println("--- Starting System ---");
82    
83 ajm 1.1 // get the command line args
84     String defaultProperties = DEFAULTPROPERTIES;
85     String filterName = null;
86 tdb 1.39 String filterManagerName = null;
87 tdb 1.11 for(int i=0; i < args.length; i++) {
88 tdb 1.12 if(args[i].equals("-h")) {
89 ajm 1.1 usage();
90 tdb 1.11 }
91 tdb 1.12 else if(args[i].equals("-f")) {
92 tdb 1.39 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 tdb 1.11 }
107 tdb 1.12 else if(args[i].equals("-l")) {
108 tdb 1.39 if(++i < args.length) {
109     defaultProperties = args[i];
110     }
111     else {
112     usage();
113     }
114 tdb 1.11 }
115     else {
116     usage();
117     }
118 ajm 1.1 }
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 ajm 1.2 ReferenceManager refman = ReferenceManager.getInstance();
136 ajm 1.1
137 ajm 1.2 // now the ORB is running, we need to activate our RootPOA
138     // so that we can start serving requests once servants start up
139 ajm 1.1 refman.activatePOA();
140    
141 ajm 1.2 // get the list of components
142 tdb 1.38 String componentList = System.getProperty("uk.org.iscream.cms.server.ComponentList");
143 ajm 1.1 StringTokenizer st = new StringTokenizer(componentList, ";");
144 tdb 1.33 _componentsToStart = new LinkedList();
145 ajm 1.13
146 ajm 1.1 // 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 ajm 1.13
152 ajm 1.2
153     // ### This is where the list of supported components is checked! ###
154     if (componentName.equalsIgnoreCase("core")) {
155 tdb 1.38 component = new uk.org.iscream.cms.server.core.Core();
156 tdb 1.39 // note the passing of the FilterManagers's name in its constructor
157 ajm 1.3 } else if (componentName.equalsIgnoreCase("filtermanager")) {
158 tdb 1.39 component = new uk.org.iscream.cms.server.filtermanager.FilterManager(filterManagerName);
159 ajm 1.4 } else if (componentName.equalsIgnoreCase("rootfilter")) {
160 tdb 1.38 component = new uk.org.iscream.cms.server.rootfilter.RootFilter();
161 ajm 1.5 } else if (componentName.equalsIgnoreCase("dbinterface")) {
162 tdb 1.38 component = new uk.org.iscream.cms.server.dbinterface.DBInterface();
163 ajm 1.6 } else if (componentName.equalsIgnoreCase("clientinterface")) {
164 tdb 1.38 component = new uk.org.iscream.cms.server.clientinterface.ClientInterfaceMain();
165 ajm 1.7 // note the passing of the Filter's name in its constructor
166     } else if (componentName.equalsIgnoreCase("filter")) {
167 tdb 1.38 component = new uk.org.iscream.cms.server.filter.FilterMain(filterName);
168 tdb 1.30 } else if (componentName.equalsIgnoreCase("client")) {
169 tdb 1.38 component = new uk.org.iscream.cms.server.client.ClientMain();
170 ajm 1.1 }
171 ajm 1.2 // ### Add new component constructors in the above section! ###
172 ajm 1.3
173 ajm 1.22 // build the list of components to start
174 ajm 1.1 if (component != null) {
175 ajm 1.32 _componentsToStart.add(component);
176 ajm 1.13 } else {
177 tdb 1.33 System.err.println(toString + ": WARNING unsupported component not started: "+componentName);
178 ajm 1.13 }
179     }
180    
181 ajm 1.32
182 ajm 1.22
183     // get the value for timeout
184 ajm 1.32
185 ajm 1.28 String confTimeout = null;
186 ajm 1.22 try {
187 tdb 1.38 confTimeout = System.getProperty("uk.org.iscream.cms.server.ComponentTimeout");
188 ajm 1.29 confTimeout.trim();
189 ajm 1.32 _startTimeout = Integer.parseInt(confTimeout);
190 ajm 1.22 } catch (NumberFormatException e) {
191 ajm 1.32 _startTimeout = DEFAULT_COMPONENT_START_TIMEOUT;
192 tdb 1.38 System.err.println(toString + ": unable to read uk.org.iscream.cms.server.ComponentTimeout value (" + confTimeout + "), using default!");
193 ajm 1.22 }
194 ajm 1.32 System.out.println(toString + ": using component start timeout of " + _startTimeout + " seconds");
195 ajm 1.22
196 ajm 1.31 // 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 tdb 1.33 // !! this doesn't appear to work !!
202 ajm 1.31 while(true) {
203     try {
204     refman.getORB().run();
205 ajm 1.32 } catch (org.omg.CORBA.COMM_FAILURE e) {
206 ajm 1.31 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 tdb 1.38 * according to uk.org.iscream.cms.server.ComponentTimeout time.
219 ajm 1.31 *
220     * If the server dies and CORBA connections are lost, this method
221     * is called again.
222     */
223     private static void startUp() {
224 ajm 1.32 // now we try and start the beast up
225     boolean tryAgain = true;
226     Component component = null;
227    
228 ajm 1.22 // keep trying until we've started all the components.
229     // maybe add support for a limited number of retries
230 ajm 1.13 while(tryAgain) {
231 ajm 1.32 Iterator i = _componentsToStart.iterator();
232 tdb 1.33 LinkedList failedComponents = new LinkedList();
233 ajm 1.22 // go through all the components
234 ajm 1.13 while(i.hasNext()) {
235 tdb 1.33 // get a refence to the component
236     component = (Component) i.next();
237 tdb 1.35 System.out.println(toString + ": dependency checking component - " + component.toString());
238 tdb 1.33
239     // check it's dependencies
240     boolean depOK = component.depCheck();
241     if(depOK) {
242 tdb 1.35 System.out.println(toString + ": starting component - " + component.toString());
243 tdb 1.33 // 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 ajm 1.14 System.err.println(toString + ": This could be because it can't communicate with components it needs.");
260 ajm 1.22
261     // make a list of the failed components
262 ajm 1.20 failedComponents.add(component);
263 ajm 1.1 }
264 ajm 1.13 }
265 ajm 1.22
266     // if we had some failed components that we can retry
267 ajm 1.21 if (failedComponents.size() > 0) {
268 ajm 1.14 System.err.println(toString + ": WARNING One or more components failed to start correctly.");
269 ajm 1.32 System.err.println(toString + ": Will try again in " + _startTimeout + " seconds");
270 ajm 1.22
271     // our list is now the failed list
272 ajm 1.32 _componentsToStart = failedComponents;
273 ajm 1.22
274     // sleep for a given timeout
275 ajm 1.18 try {
276 ajm 1.32 Thread.sleep(_startTimeout * 1000);
277 ajm 1.17 } catch (InterruptedException e) {
278     // we're not bothered
279     }
280 ajm 1.22 // otherwise we started everything
281 ajm 1.18 } else {
282 ajm 1.22 // so we set to exit the loop
283 ajm 1.18 tryAgain = false;
284 ajm 1.1 }
285     }
286 ajm 1.13
287 ajm 1.3 System.out.println(toString + ": running");
288 ajm 1.31 }
289 ajm 1.3
290     /**
291 ajm 1.1 * 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 tdb 1.38 System.out.println("USAGE: java uk.org.iscream.cms.server.componentmanager.ComponentManager <option>");
298 tdb 1.8 System.out.println(" or: java -jar iscream.jar <option>");
299 ajm 1.1 System.out.println("WHERE <option>:");
300     System.out.println(" -l <filename> - the location of initial system properties");
301 ajm 1.2 System.out.println(" the default is ./etc/default.properties");
302 ajm 1.1 System.out.println(" -f <name> - the name of the filter (if there is one configured");
303 tdb 1.39 System.out.println(" -fm <name> - the name of the filter manager (if there is one configured");
304 ajm 1.1 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 ajm 1.32
320 tdb 1.33 private static LinkedList _componentsToStart;
321 ajm 1.32 private static int _startTimeout = 0;
322 ajm 1.1
323     }