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