Add Registration functionality and tidy up
[WeStealzYourDataz.git] / src / uk / ac / ntu / n0521366 / wsyd / Launcher.java
1 /*
2  * The MIT License
3  *
4  * Copyright 2015 Eddie Berrisford-Lynch <dev@fun2be.me>.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 package uk.ac.ntu.n0521366.wsyd;
25
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.util.ArrayList;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import uk.ac.ntu.n0521366.wsyd.server.ServerSocial;
32 import uk.ac.ntu.n0521366.wsyd.management.ServerManagement;
33 import uk.ac.ntu.n0521366.wsyd.client.ClientGUI;
34
35 /**
36  *
37  * @author Eddie Berrisford-Lynch <dev@fun2be.me>
38  */
39 public class Launcher {
40     
41     static class App implements Runnable {
42
43         private final Class<?> _classRef;
44         private final String[] _args;
45
46         public App(Class<?> classRef, String[] args) {
47             _classRef = classRef;
48             _args = args;
49         }
50
51         @Override
52         public void run() {
53             try {
54                 ClassLoader cl = _classRef.getClassLoader();
55                 try {
56                     Object o = cl.loadClass(_classRef.getName()).newInstance();
57                     Class<?> newClass = o.getClass();
58                     Method m = newClass.getMethod("main", String[].class);
59                     m.invoke(null, (Object) this._args);
60                 } catch (ClassNotFoundException ex) {
61                     Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
62                 } catch (InstantiationException ex) {
63                     Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
64                 }
65             } catch (NoSuchMethodException ex) {
66                 Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
67             } catch (SecurityException ex) {
68                 Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
69             } catch (IllegalAccessException ex) {
70                 Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
71             } catch (IllegalArgumentException ex) {
72                 Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
73             } catch (InvocationTargetException ex) {
74                 Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
75             }
76
77         }
78     }
79     static ArrayList<App> apps;
80
81     public static void main(String[] args) {
82         
83         apps = new ArrayList<>();
84         
85         apps.add(new App(    ServerSocial.class, args));
86         apps.add(new App(ServerManagement.class, args));
87         for (int qty = 1; qty <= 1; qty++) {
88             apps.add(new App(ClientGUI.class, args));
89         }
90         
91         for (App app: apps) {
92             System.out.println("Starting app " + app._classRef.getTypeName());
93             new Thread(app).start();
94         }
95     }
96     
97 }