Add Registration functionality and tidy up
[WeStealzYourDataz.git] / src / uk / ac / ntu / n0521366 / wsyd / Launcher.java
diff --git a/src/uk/ac/ntu/n0521366/wsyd/Launcher.java b/src/uk/ac/ntu/n0521366/wsyd/Launcher.java
new file mode 100644 (file)
index 0000000..03b1293
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * The MIT License
+ *
+ * Copyright 2015 Eddie Berrisford-Lynch <dev@fun2be.me>.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package uk.ac.ntu.n0521366.wsyd;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import uk.ac.ntu.n0521366.wsyd.server.ServerSocial;
+import uk.ac.ntu.n0521366.wsyd.management.ServerManagement;
+import uk.ac.ntu.n0521366.wsyd.client.ClientGUI;
+
+/**
+ *
+ * @author Eddie Berrisford-Lynch <dev@fun2be.me>
+ */
+public class Launcher {
+    
+    static class App implements Runnable {
+
+        private final Class<?> _classRef;
+        private final String[] _args;
+
+        public App(Class<?> classRef, String[] args) {
+            _classRef = classRef;
+            _args = args;
+        }
+
+        @Override
+        public void run() {
+            try {
+                ClassLoader cl = _classRef.getClassLoader();
+                try {
+                    Object o = cl.loadClass(_classRef.getName()).newInstance();
+                    Class<?> newClass = o.getClass();
+                    Method m = newClass.getMethod("main", String[].class);
+                    m.invoke(null, (Object) this._args);
+                } catch (ClassNotFoundException ex) {
+                    Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
+                } catch (InstantiationException ex) {
+                    Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
+                }
+            } catch (NoSuchMethodException ex) {
+                Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
+            } catch (SecurityException ex) {
+                Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
+            } catch (IllegalAccessException ex) {
+                Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
+            } catch (IllegalArgumentException ex) {
+                Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
+            } catch (InvocationTargetException ex) {
+                Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
+            }
+
+        }
+    }
+    static ArrayList<App> apps;
+
+    public static void main(String[] args) {
+        
+        apps = new ArrayList<>();
+        
+        apps.add(new App(    ServerSocial.class, args));
+        apps.add(new App(ServerManagement.class, args));
+        for (int qty = 1; qty <= 1; qty++) {
+            apps.add(new App(ClientGUI.class, args));
+        }
+        
+        for (App app: apps) {
+            System.out.println("Starting app " + app._classRef.getTypeName());
+            new Thread(app).start();
+        }
+    }
+    
+}