1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.componentmanager; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.util.*; |
6 |
import java.io.*; |
7 |
import uk.ac.ukc.iscream.util.*; |
8 |
|
9 |
/** |
10 |
* The component manager is the starting point for all |
11 |
* server side components of the iscream system. |
12 |
* It loads its initial system configuration from the |
13 |
* default properties file, it then starts all the iscream |
14 |
* components as specified in the default.properties under |
15 |
* uk.ac.ukc.iscream.ComponentList |
16 |
* |
17 |
* @author $Author: ajm4 $ |
18 |
* @version $Id: ComponentManager.java,v 1.25 2001/02/23 18:00:29 ajm4 Exp $ |
19 |
*/ |
20 |
public class ComponentManager { |
21 |
|
22 |
//---FINAL ATTRIBUTES--- |
23 |
|
24 |
/** |
25 |
* The current CVS revision of this class |
26 |
*/ |
27 |
public static final String REVISION = "$Revision: 1.25 $"; |
28 |
|
29 |
/** |
30 |
* The toString() of this class |
31 |
* As it won't be instatiated, this is needed. |
32 |
* Not also that we pass a null as the class name (as we are static) |
33 |
*/ |
34 |
public static final String toString = FormatName.getName("ComponentManager", null, REVISION); |
35 |
|
36 |
/** |
37 |
* The default location of the properties file for the system |
38 |
*/ |
39 |
public static final String DEFAULTPROPERTIES = "./etc/default.properties"; |
40 |
|
41 |
/** |
42 |
* The default time to wait before retrying |
43 |
* component. |
44 |
*/ |
45 |
public static final int DEFAULT_COMPONENT_START_TIMEOUT = 5; |
46 |
|
47 |
//---STATIC METHODS--- |
48 |
|
49 |
/** |
50 |
* The main method which starts the components as |
51 |
* listed in the default.properties file. |
52 |
* |
53 |
* @param args the command line arguments |
54 |
*/ |
55 |
public static void main(String[] args) { |
56 |
System.out.println("--- i-scream Server Component Manager ---"); |
57 |
System.out.println("--- Starting System ---"); |
58 |
|
59 |
// get the command line args |
60 |
String defaultProperties = DEFAULTPROPERTIES; |
61 |
String filterName = null; |
62 |
for(int i=0; i < args.length; i++) { |
63 |
if(args[i].equals("-h")) { |
64 |
usage(); |
65 |
} |
66 |
else if(args[i].equals("-f")) { |
67 |
i++; filterName = args[i]; |
68 |
} |
69 |
else if(args[i].equals("-l")) { |
70 |
i++; defaultProperties = args[i]; |
71 |
} |
72 |
else { |
73 |
usage(); |
74 |
} |
75 |
} |
76 |
|
77 |
// load the default properties file into the system properties |
78 |
System.out.println(toString + ": initialising - using " + defaultProperties); |
79 |
try { |
80 |
Properties initProperties = new Properties(System.getProperties()); |
81 |
initProperties.load(new FileInputStream(new File(defaultProperties))); |
82 |
System.setProperties(initProperties); |
83 |
} catch (Exception e) { |
84 |
System.err.println(toString + ": ERROR " + e.getMessage()); |
85 |
usage(); |
86 |
} |
87 |
|
88 |
// continue to bring the system up |
89 |
System.out.println(toString + ": coming up"); |
90 |
|
91 |
// start the ORB by initialising the ReferenceManager |
92 |
ReferenceManager refman = ReferenceManager.getInstance(); |
93 |
|
94 |
// now the ORB is running, we need to activate our RootPOA |
95 |
// so that we can start serving requests once servants start up |
96 |
refman.activatePOA(); |
97 |
|
98 |
// get the list of components |
99 |
String componentList = System.getProperty("uk.ac.ukc.iscream.ComponentList"); |
100 |
StringTokenizer st = new StringTokenizer(componentList, ";"); |
101 |
ArrayList componentsToStart = new ArrayList(); |
102 |
|
103 |
// this could be done using reflection |
104 |
// but..well..we don't ;-p |
105 |
while (st.hasMoreTokens()){ |
106 |
String componentName = st.nextToken(); |
107 |
Component component = null; |
108 |
|
109 |
|
110 |
// ### This is where the list of supported components is checked! ### |
111 |
if (componentName.equalsIgnoreCase("core")) { |
112 |
component = new uk.ac.ukc.iscream.core.Core(); |
113 |
} else if (componentName.equalsIgnoreCase("filtermanager")) { |
114 |
component = new uk.ac.ukc.iscream.filtermanager.FilterManager(); |
115 |
} else if (componentName.equalsIgnoreCase("rootfilter")) { |
116 |
component = new uk.ac.ukc.iscream.rootfilter.RootFilter(); |
117 |
} else if (componentName.equalsIgnoreCase("dbinterface")) { |
118 |
component = new uk.ac.ukc.iscream.dbinterface.DBInterface(); |
119 |
} else if (componentName.equalsIgnoreCase("clientinterface")) { |
120 |
component = new uk.ac.ukc.iscream.clientinterface.ClientInterfaceMain(); |
121 |
// note the passing of the Filter's name in its constructor |
122 |
} else if (componentName.equalsIgnoreCase("filter")) { |
123 |
component = new uk.ac.ukc.iscream.filter.FilterMain(filterName); |
124 |
} |
125 |
// ### Add new component constructors in the above section! ### |
126 |
|
127 |
// build the list of components to start |
128 |
if (component != null) { |
129 |
componentsToStart.add(component); |
130 |
} else { |
131 |
System.err.println(toString + ": WARNING unsupported component not started"); |
132 |
} |
133 |
} |
134 |
|
135 |
// now we try and start the beast up |
136 |
boolean tryAgain = true; |
137 |
Component component = null; |
138 |
|
139 |
// get the value for timeout |
140 |
int startTimeout; |
141 |
String confTimeout; |
142 |
try { |
143 |
confTimeout = System.getProperty("uk.ac.ukc.iscream.ComponentTimeout") |
144 |
startTimeout = Integer.parseInt(confTimeout); |
145 |
} catch (NumberFormatException e) { |
146 |
startTimeout = DEFAULT_COMPONENT_START_TIMEOUT; |
147 |
System.err.println(toString + ": unable to read uk.ac.ukc.iscream.ComponentTimeout value (" + confTimeout + "), using default!"); |
148 |
} |
149 |
System.out.println(toString + ": using component start timeout of " + startTimeout + " seconds"); |
150 |
|
151 |
// keep trying until we've started all the components. |
152 |
// maybe add support for a limited number of retries |
153 |
while(tryAgain) { |
154 |
Iterator i = componentsToStart.iterator(); |
155 |
ArrayList failedComponents = new ArrayList(); |
156 |
// go through all the components |
157 |
while(i.hasNext()) { |
158 |
try { |
159 |
component = (Component) i.next(); |
160 |
System.out.println(toString + ": starting component - " + component.toString()); |
161 |
|
162 |
// start the component |
163 |
component.start(); |
164 |
|
165 |
// if we get this then there was a problem |
166 |
// that we can't recover from |
167 |
} catch (ComponentStartException e) { |
168 |
System.err.println(toString + ": ERROR starting component - " + component.toString()); |
169 |
System.err.println(toString + ": component reports - " + e.getMessage()); |
170 |
System.exit(1); |
171 |
|
172 |
// if we get this exception then we've tried |
173 |
// to talk to something which may not be up yet |
174 |
// so we want to try again in a minute |
175 |
} catch(ComponentCORBAException e2) { |
176 |
System.err.println(toString + ": WARNING Component reported CORBA communications failure"); |
177 |
System.err.println(toString + ": This could be because it can't communicate with components it needs."); |
178 |
System.err.println(toString + ": component reports - " + e2.getMessage()); |
179 |
|
180 |
// make a list of the failed components |
181 |
failedComponents.add(component); |
182 |
} |
183 |
} |
184 |
|
185 |
// if we had some failed components that we can retry |
186 |
if (failedComponents.size() > 0) { |
187 |
System.err.println(toString + ": WARNING One or more components failed to start correctly."); |
188 |
System.err.println(toString + ": Will try again in " + startTimeout + " seconds"); |
189 |
|
190 |
// our list is now the failed list |
191 |
componentsToStart = failedComponents; |
192 |
|
193 |
// sleep for a given timeout |
194 |
try { |
195 |
Thread.sleep(startTimeout * 1000); |
196 |
} catch (InterruptedException e) { |
197 |
// we're not bothered |
198 |
} |
199 |
// otherwise we started everything |
200 |
} else { |
201 |
// so we set to exit the loop |
202 |
tryAgain = false; |
203 |
} |
204 |
} |
205 |
|
206 |
System.out.println(toString + ": running"); |
207 |
|
208 |
// block on the ORB...in time, management functionality can be placed here. |
209 |
refman.getORB().run(); |
210 |
} |
211 |
|
212 |
/** |
213 |
* A simple method to print the usage of this class. |
214 |
* It never returns, but instead exits to the system |
215 |
* with a value 1, to indicate the system did not start |
216 |
* properly. |
217 |
*/ |
218 |
public static void usage() { |
219 |
System.out.println("USAGE: java uk.ac.ukc.iscream.componentmanager.ComponentManager <option>"); |
220 |
System.out.println(" or: java -jar iscream.jar <option>"); |
221 |
System.out.println("WHERE <option>:"); |
222 |
System.out.println(" -l <filename> - the location of initial system properties"); |
223 |
System.out.println(" the default is ./etc/default.properties"); |
224 |
System.out.println(" -f <name> - the name of the filter (if there is one configured"); |
225 |
System.out.println(" -h - this help screen"); |
226 |
System.exit(1); |
227 |
} |
228 |
|
229 |
//---CONSTRUCTORS--- |
230 |
|
231 |
//---PUBLIC METHODS--- |
232 |
|
233 |
//---PRIVATE METHODS--- |
234 |
|
235 |
//---ACCESSOR/MUTATOR METHODS--- |
236 |
|
237 |
//---ATTRIBUTES--- |
238 |
|
239 |
//---STATIC ATTRIBUTES--- |
240 |
|
241 |
} |