ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/corbaservices/uk/org/iscream/cms/corbaservices/CorbaServices.java
Revision: 1.12
Committed: Fri Feb 21 13:45:47 2003 UTC (21 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.11: +57 -13 lines
Log Message:
Main change is making the use of jacorb.properties transparent to the end
user - something which should have been done with CorbaServices a long time
ago :-) Now a base jacorb.properties is included in the jar files, because
it's searched by the jacorb loading stuff. The only value outside of the
jar file is the naming service location, which can now be found in the
default.properties files.

The server and corbaservices assume that the naming services is running on
localhost on port 8052, which is ok for most normal things. If it's on a
different host the default.properties just needs fixing.

Also added a default.properties style thing to the corbaservices, and fixed
some bugs in the command line argument parsing.

Finally, fixed a bug with the make src target of all the Makefiles.

File Contents

# Content
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.corbaservices;
23
24 //---IMPORTS---
25 import java.lang.reflect.*;
26 import java.util.*;
27 import java.io.*;
28 import java.net.*;
29
30 /**
31 * CORBA Services Manager
32 *
33 * This class provides a simple wrapper to CORBA services
34 * that require a web service to "share" their IOR files.
35 * This provides an all-in-one way of starting up service(s)
36 * and serving their IOR files. No external web servers are
37 * required.
38 *
39 * @author $Author: tdb $
40 * @version $Id: CorbaServices.java,v 1.11 2002/05/21 16:47:11 tdb Exp $
41 */
42 class CorbaServices {
43
44 //---FINAL ATTRIBUTES---
45
46 /**
47 * The current CVS revision of this class
48 */
49 public static final String REVISION = "$Revision: 1.11 $";
50
51 /**
52 * Default path to "ok URL's" list
53 */
54 public static final String DEFAULTOKURLS = "./etc/okUrls.conf";
55
56 /**
57 * Default path to services list
58 */
59 public static final String DEFAULTSERVICESCONF = "./etc/services.conf";
60
61 /**
62 * Default path to web directory. Note there is *no* trailing slash.
63 */
64 public static final String DEFAULTWEBDIR = "./web";
65
66 /**
67 * Default port number
68 */
69 public static final int DEFAULTPORT = 8052;
70
71 /**
72 * The default location of the properties file for the system
73 */
74 public static final String DEFAULTPROPERTIES = "./etc/default.properties";
75
76 //---STATIC METHODS---
77
78 /**
79 * This main method starts the whole system up. A lot
80 * of the functionality is provided in this main class.
81 */
82 public static void main(String args[]) {
83
84 System.out.println("-----------------------------------------");
85 System.out.println("--- i-scream CORBA Services Manager ---");
86 System.out.println("--- (c) 2001 The i-scream Project ---");
87 System.out.println("--- (http://www.i-scream.org.uk) ---");
88 System.out.println("-----------------------------------------");
89 System.out.println("--- Starting System ---");
90
91 // Read command line arguments
92 String okUrls = DEFAULTOKURLS;
93 String servicesConf = DEFAULTSERVICESCONF;
94 String webDir = DEFAULTWEBDIR;
95 int port = DEFAULTPORT;
96 String defaultProperties = DEFAULTPROPERTIES;
97 for(int i=0; i < args.length; i++) {
98 if(args[i].equals("-h")) {
99 usage();
100 }
101 else if(args[i].equals("-u")) {
102 if(++i < args.length) {
103 okUrls = args[i];
104 }
105 else {
106 usage();
107 }
108 }
109 else if(args[i].equals("-s")) {
110 if(++i < args.length) {
111 servicesConf = args[i];
112 }
113 else {
114 usage();
115 }
116 }
117 else if(args[i].equals("-w")) {
118 if(++i < args.length) {
119 webDir = args[i];
120 }
121 else {
122 usage();
123 }
124 }
125 else if(args[i].equals("-p")) {
126 if(++i < args.length) {
127 port = Integer.parseInt(args[i]);
128 }
129 else {
130 usage();
131 }
132 }
133 else if(args[i].equals("-l")) {
134 if(++i < args.length) {
135 defaultProperties = args[i];
136 }
137 else {
138 usage();
139 }
140 }
141 else {
142 usage();
143 }
144 }
145
146 // load the default properties file into the system properties
147 System.out.println("initialising: reading " + defaultProperties);
148 try {
149 Properties initProperties = new Properties(System.getProperties());
150 initProperties.load(new FileInputStream(new File(defaultProperties))
151 );
152 System.setProperties(initProperties);
153 } catch (Exception e) {
154 System.err.println("ERROR: " + e.getMessage());
155 usage();
156 }
157
158 // Read in the services configuration
159 System.out.println("--- Reading Configuration ---");
160
161 LinkedList serviceList = getServicesList(servicesConf);
162 String[] svcList = new String[serviceList.size()];
163 LinkedList[] svcListParams = new LinkedList[serviceList.size()];
164 Iterator i = serviceList.iterator();
165 int j=0;
166 while(i.hasNext()) {
167 StringTokenizer st = new StringTokenizer((String)i.next());
168 svcList[j] = (String) st.nextToken();
169 LinkedList l = new LinkedList();
170 while (st.hasMoreTokens()) {
171 l.add(st.nextToken());
172 }
173 svcListParams[j] = l;
174 j++;
175 }
176
177 // Start the services one by one
178 System.out.println("--- Starting CORBA Services ---");
179
180 try {
181
182 // get hooks on the system loaders
183 Runtime myRuntime = Runtime.getRuntime();
184 ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
185
186 // start each service one by one
187 for (int k=0; k < svcList.length; k++) {
188
189 // Load the class
190 Class myClass = myClassLoader.loadClass(svcList[k]);
191 // Sort out the command line parameters
192 String[] passedArgs = new String[svcListParams[k].size()];
193 // keep a String list for printing out later :)
194 String arglist = "";
195 i = svcListParams[k].iterator();
196 int p = 0;
197 while(i.hasNext()) {
198 passedArgs[p] = (String) i.next();
199 arglist += " " + passedArgs[p];
200 p++;
201 }
202
203 // create an array of classes that are listed in the main method header
204 // so we can get a hook to the main method
205 Class[] passedArgsClass = {passedArgs.getClass()};
206 // get a hook on the main method
207 Method myMethod = myClass.getMethod("main",passedArgsClass);
208
209 // Now we know where it is, we need to create a thread for it, passing in
210 // a list of objects that represent its args.
211 Object[] passedArgsObject = {passedArgs};
212 ServiceThread aSvc = new ServiceThread(myMethod, passedArgsObject);
213
214 // Finally we start it going
215 aSvc.start();
216 System.out.println(svcList[k]+arglist);
217 }
218
219 } catch (Exception e) {
220 System.out.println(e);
221 e.printStackTrace();
222 }
223
224 System.out.println("--- Starting Mini WebServer ---");
225
226 // print out some system details
227 String host;
228 try {
229 host = InetAddress.getLocalHost().getHostName();
230 } catch(UnknownHostException e) {
231 host = "<unknown>";
232 }
233 System.out.println("Host:\t\t"+host);
234 System.out.println("Port:\t\t"+port);
235 System.out.println("URL List File:\t"+okUrls);
236 System.out.println("Web Directory:\t"+webDir);
237
238 // start the mini-webserver
239 MiniWebServer mws = new MiniWebServer(port, okUrls, webDir);
240 mws.start();
241
242 System.out.println("--- i-scream CORBA Services Ready ---");
243 }
244
245 /**
246 * Print out usage information.
247 */
248 private static void usage() {
249 System.out.println("USAGE: java uk.org.iscream.cms.corbaservices.CorbaServices <option>");
250 System.out.println(" or: java -jar iscream-corbaservices.jar <option>");
251 System.out.println("WHERE <option>:");
252 System.out.println(" -u <filename> - the location of okUrls config file");
253 System.out.println(" the default is ./etc/okUrls.conf");
254 System.out.println(" -s <filename> - the location of services config file");
255 System.out.println(" the default is ./etc/services.conf");
256 System.out.println(" -w <dir> - the location of the web directory");
257 System.out.println(" the default is ./web (no trailing /)");
258 System.out.println(" -p <port> - this port to bind the webserver to");
259 System.out.println(" the default is 8052");
260 System.out.println(" -l <filename> - the location of initial system properties");
261 System.out.println(" the default is ./etc/default.properties");
262 System.out.println(" -h - this help screen");
263 System.exit(1);
264 }
265
266 /**
267 * Read the services into a LinkedList. This is later
268 * parsed to get classes and parameters.
269 *
270 * @param filename the services configuration file
271 * @return a LinkedList of services
272 */
273 private static LinkedList getServicesList(String filename) {
274 File file = new File(filename);
275 LinkedList svcList = new LinkedList();
276 if(file.exists() && file.canRead()) {
277 // if we can read the file, do so into svcList
278 try {
279 BufferedReader reader = new BufferedReader(new FileReader(file));
280 while(reader.ready()) {
281 String line = reader.readLine();
282 // ignore comments
283 if(!line.startsWith("#")) {
284 svcList.add(line);
285 }
286 }
287 } catch(IOException e) {
288 System.out.println(e);
289 e.printStackTrace();
290 }
291 }
292 return svcList;
293 }
294
295 //---CONSTRUCTORS---
296
297 //---PUBLIC METHODS---
298
299 //---PRIVATE METHODS---
300
301 //---ACCESSOR/MUTATOR METHODS---
302
303 //---ATTRIBUTES---
304
305 //---STATIC ATTRIBUTES---
306
307 //---INNER CLASSES---
308
309 /**
310 * Inner class to provide a thread for running a Service.
311 */
312 private static class ServiceThread extends Thread {
313
314 /**
315 * The constructor takes the name of the method we're gonna run
316 * and the arguments we're gonna pass into it.
317 *
318 * @param mainMethod a method that we'll treat as main
319 * @param args an array of arguments for mainMethod
320 */
321 ServiceThread(Method mainMethod, Object[] args) {
322 _mainMethod = mainMethod;
323 _args = args;
324 }
325
326 /**
327 * Invokes our main method, with the arguments we
328 * have been given.
329 */
330 public void run() {
331 try {
332 _mainMethod.invoke(null, _args);
333 } catch (Exception e){
334 System.out.println(e);
335 e.printStackTrace();
336 }
337 }
338
339 /**
340 * The method we'll treat as main
341 */
342 Method _mainMethod = null;
343
344 /**
345 * The array of arguments
346 */
347 Object[] _args = null;
348 }
349
350 }