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
(Generate patch)

Comparing projects/cms/source/corbaservices/uk/org/iscream/cms/corbaservices/CorbaServices.java (file contents):
Revision 1.7 by ajm, Mon Mar 19 03:36:58 2001 UTC vs.
Revision 1.8 by tdb, Sat May 12 21:29:58 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines