| 1 package com.daffodilwoods.rmi; 2 3 import java.io.*; 4 import java.lang.reflect.*; 5 import java.net.*; 6 import java.rmi.*; 7 import java.rmi.registry.*; 8 9 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 10 import com.daffodilwoods.daffodildb.server.datasystem.mergesystem.*; 11 import com.daffodilwoods.daffodildb.server.serversystem.*; 12 import com.daffodilwoods.database.utility.*; 13 import com.daffodilwoods.rmi.interfaces.*; 14 import com.daffodilwoods.rmi.server.*; 15 import java.rmi.server.*; 16 17 public class DaffodilDBServer { 18 int PORT; 19 String HOST_NAME = "LocalHost"; 20 boolean exit = true; 21 22 public DaffodilDBServer() throws Exception { 23 HOST_NAME = InetAddress.getLocalHost().getHostAddress(); 24 } 25 26 public static void main(String [] a) { 27 try { 28 29 DaffodilDBServer server = new DaffodilDBServer(); 30 if (a.length == 0) 31 a = new String [] {"-start"}; 32 try { 33 server.PORT = Integer.parseInt(System.getProperty("DaffodilDB_Port", "3456").trim()); 34 } catch (Exception e) { 35 server.PORT = 3456; 36 } 37 if (a[0].trim().equalsIgnoreCase("stop") || a[0].trim().equalsIgnoreCase("-stop")) { 38 boolean browser = a.length > 1 && (a[1].trim().equalsIgnoreCase("browser")); 39 server.exit = !browser; 40 server.stopServer(); 41 } 42 if (a[0].trim().equalsIgnoreCase("start") || a[0].trim().equalsIgnoreCase("-start")) { 43 boolean verbose=false, savemode=false; 44 if( P.indexOf(a,"verbose") != -1 || P.indexOf(a,"-verbose") != -1) 45 verbose = true; 46 if( P.indexOf(a,"savemode") != -1 || P.indexOf(a,"-savemode") != -1) 47 savemode = true; 48 for (int i = 0; i < a.length; i++) { 49 String asd = a[i].trim(); 50 int ind = a[i].indexOf("port="); 51 if(ind != -1){ 52 asd = asd.substring(ind+5); 53 try { 54 int asasas = Integer.parseInt(asd); 55 if(asasas < 0 || asasas > 65535){ 56 throw new Exception ("Invalid port Number "+asasas); 57 } 58 server.PORT = asasas; 59 } catch (NumberFormatException e) { 60 throw new Exception ("Invalid port Number "+asd); 61 } 62 } 63 } 64 65 server.startServer(verbose, savemode); 66 69 } else 70 System.out.println("\nPARAMETER IS INCORRECT [start | -start] | [stop | -stop]"); 71 } catch (Exception ex) { 72 ex.printStackTrace(); 73 } 74 } 75 76 public void startServer(boolean verbose, boolean saveMode) { 77 try { 78 Registry r = LocateRegistry.createRegistry(PORT); 79 ServerSystem serverSystem = new ServerSystem(); 80 serverSystem.setVerbose(verbose); 81 serverSystem.setSaveMode(saveMode); 82 RmiServerServerSide rmiServer = new RmiServerServerSide(serverSystem); 83 84 rmiServer.setPortNumber(PORT); 85 Naming.rebind("rmi://" + HOST_NAME + ":" + Integer.toString(PORT) + "/" + "DaffodilDB", rmiServer); 86 System.out.println("\nDaffodilDB Server has been Registered on port :" + PORT); 87 System.out.println("\nPAUSE -> 0, CONTINUE -> 1, SHUTDOWN -> shutdown\n"); 88 BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); 89 90 Runtime rt = Runtime.getRuntime(); 91 Class cc = rt.getClass(); 92 Method mt = null; 93 try { 94 mt = cc.getDeclaredMethod("addShutdownHook", new Class [] {Thread .class}); 95 } catch (SecurityException ex1) { 96 System.err.println(ex1); 97 } catch (NoSuchMethodException ex1) { 98 System.err.println(ex1); 99 } 100 if (mt == null) { 101 return; 102 } 103 Thread tt = new Thread () { 104 public void run() { 105 try { 106 exit = false; 107 stopServer(); 108 } catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } 112 }; 113 114 rmiServer.setThreadObject(tt); 115 mt.invoke(rt, new Object [] {tt}); 116 while (true) { 117 System.out.print(">"); 118 String str = ""; 119 str = rd.readLine(); 120 try { 121 if (str.equalsIgnoreCase("shutdown")) 122 System.exit(0); 123 else if (str.equalsIgnoreCase("0")) { 124 Naming.unbind("rmi://LocalHost:" + System.getProperty("DaffodilDBPort", "3456").trim() + "/DaffodilDB"); 125 System.out.println("Daffodil DB Server is PAUSED .."); 126 } else if (str.equalsIgnoreCase("1")) { 127 Naming.rebind("rmi://" + HOST_NAME + ":" + Integer.toString(PORT) + "/" + "DaffodilDB", rmiServer); 128 System.out.println("Daffodil DB Server is RUNNING .."); 129 } else if (str.equalsIgnoreCase("memory")) { 130 System.out.println(" total Memory " + Runtime.getRuntime().totalMemory()); 131 System.out.println(" Free Memory " + Runtime.getRuntime().freeMemory()); 132 System.out.println(" used Memory " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())); 133 } else if ( str.toLowerCase().startsWith("status") ){ 134 int index = str.toLowerCase().indexOf("status"); 135 String dbName = str.substring(index+6).trim(); 136 _Database db = serverSystem.getMergeDatabase(dbName); 137 ((MergeDatabase)db).getStatus(); 138 } 139 else { 140 System.out.println("No such command [" + str + "], use :- "); 141 System.out.println("\nPAUSE -> 0, CONTINUE -> 1, SHUTDOWN -> shutdown\n"); 142 } 143 } catch (Exception ex) { 144 } 145 } 146 } catch (java.rmi.server.ExportException EE) { 147 System.out.println("Port No.: " + PORT + " is already in use or Server is running"); 148 System.exit(0); 149 } catch (java.rmi.ConnectIOException CE) { 150 System.out.println("Check your Network configuration from Control panel and try again !"); 151 System.exit(0); 152 } catch (Exception ee) { 153 ee.printStackTrace(); 154 } 155 } 156 157 public void stopServer() throws Exception { 158 String port = "3456"; 159 try { 160 System.out.println("\nDaffodilDB Server shut down called..."); 161 port = System.getProperty("DaffodilDBPort", "3456").trim(); 162 String url = "rmi://LocalHost:" + port + "/DaffodilDB"; 163 System.out.println("Server is shutting down. Please Wait ...."); 164 _RmiServer shtDwn = (_RmiServer) Naming.lookup(url); 165 shtDwn.shutDown(exit); 166 Naming.unbind("rmi://LocalHost:" + System.getProperty("DaffodilDBPort", "3456").trim() + "/DaffodilDB"); 167 } catch (java.rmi.ConnectException ce) { 168 System.out.println("\n\nSERVER IS NOT RUNNING AT SPECIFIED PORT : " + port); 169 } catch (Exception e) { 170 } finally { 171 System.out.println("Shut down complete"); 172 if (exit) 173 System.exit(0); 174 } 175 } 176 177 178 public static void startServer(boolean verbose,int portNumber) { 179 try { 180 String HOST_NAME = InetAddress.getLocalHost().getHostAddress(); 181 Registry r = LocateRegistry.createRegistry(portNumber); 182 ServerSystem serverSystem = new ServerSystem(); 183 serverSystem.setVerbose(verbose); 184 RmiServerServerSide rmiServer = new RmiServerServerSide(serverSystem); 185 rmiServer.setPortNumber(portNumber); 186 Naming.rebind("rmi://" + HOST_NAME + ":" + Integer.toString(portNumber) + "/" + "DaffodilDB", rmiServer); 187 System.out.println("\nDaffodilDB Server has been Registered on port :" + portNumber); 188 System.out.println("\nPAUSE -> 0, CONTINUE -> 1, SHUTDOWN -> shutdown\n"); 189 BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); 190 Runtime rt = Runtime.getRuntime(); 191 Class cc = rt.getClass(); 192 Method mt = null; 193 try { 194 mt = cc.getDeclaredMethod("addShutdownHook", new Class [] {Thread .class}); 195 } catch (SecurityException ex1) { 196 System.err.println(ex1); 197 } catch (NoSuchMethodException ex1) { 198 System.err.println(ex1); 199 } 200 if (mt == null) { 201 return; 202 } 203 final int port = portNumber; 204 Thread tt = new Thread () { 205 public void run() { 206 try { 207 try { 208 System.out.println("\nDaffodilDB Server shut down called..."); 209 String url = "rmi://LocalHost:" + port + "/DaffodilDB"; 210 System.out.println("Server is shutting down. Please Wait ...."); 211 _RmiServer shtDwn = (_RmiServer) Naming.lookup(url); 212 shtDwn.shutDown(false); 213 Naming.unbind("rmi://LocalHost:" + Integer.toString(port) + "/DaffodilDB"); 214 } catch (java.rmi.ConnectException ce) { 215 System.out.println("\n\nSERVER IS NOT RUNNING AT SPECIFIED PORT : " + port); 216 } 217 218 } catch (Exception e) { 219 e.printStackTrace(); 220 } 221 } 222 }; 223 224 rmiServer.setThreadObject(tt); 225 mt.invoke(rt, new Object [] {tt}); 226 while (true) { 227 System.out.print(">"); 228 String str = ""; 229 str = rd.readLine(); 230 try { 231 if (str.equalsIgnoreCase("shutdown")) 232 System.exit(0); 233 else if (str.equalsIgnoreCase("0")) { 234 Naming.unbind("rmi://LocalHost:" + System.getProperty("DaffodilDBPort", "3456").trim() + "/DaffodilDB"); 235 System.out.println("Daffodil DB Server is PAUSED .."); 236 } else if (str.equalsIgnoreCase("1")) { 237 Naming.rebind("rmi://" + HOST_NAME + ":" + Integer.toString(portNumber) + "/" + "DaffodilDB", rmiServer); 238 System.out.println("Daffodil DB Server is RUNNING .."); 239 } else if (str.equalsIgnoreCase("memory")) { 240 System.out.println(" total Memory " + Runtime.getRuntime().totalMemory()); 241 System.out.println(" Free Memory " + Runtime.getRuntime().freeMemory()); 242 System.out.println(" used Memory " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())); 243 } else { 244 System.out.println("No such command [" + str + "], use :- "); 245 System.out.println("\nPAUSE -> 0, CONTINUE -> 1, SHUTDOWN -> shutdown\n"); 246 } 247 } catch (Exception ex) { 248 } 249 } 250 } catch (java.rmi.server.ExportException EE) { 251 System.out.println("Port No.: " + portNumber + " is already in use or Server is running"); 252 System.exit(0); 253 } catch (java.rmi.ConnectIOException CE) { 254 System.out.println("Check your Network configuration from Control panel and try again !"); 255 System.exit(0); 256 } catch (Exception ee) { 257 ee.printStackTrace(); 258 } 259 260 } 261 public static void startServerWithoutConsole(boolean verbose,int portNumber) { 262 try { 263 String HOST_NAME = InetAddress.getLocalHost().getHostAddress(); 264 Registry r = LocateRegistry.createRegistry(portNumber); 265 ServerSystem serverSystem = new ServerSystem(); 266 serverSystem.setVerbose(verbose); 267 RmiServerServerSide rmiServer = new RmiServerServerSide(serverSystem); 268 rmiServer.setPortNumber(portNumber); 269 Naming.rebind("rmi://" + HOST_NAME + ":" + Integer.toString(portNumber) + "/" + "DaffodilDB", rmiServer); 270 Runtime rt = Runtime.getRuntime(); 271 Class cc = rt.getClass(); 272 Method mt = null; 273 try { 274 mt = cc.getDeclaredMethod("addShutdownHook", new Class [] {Thread .class}); 275 } catch (SecurityException ex1) { 276 System.err.println(ex1); 277 } catch (NoSuchMethodException ex1) { 278 System.err.println(ex1); 279 } 280 if (mt == null) { 281 return; 282 } 283 final int port = portNumber; 284 Thread tt = new Thread () { 285 public void run() { 286 try { 287 try { 288 System.out.println("\nDaffodilDB Server shut down called..."); 289 String url = "rmi://LocalHost:" + port + "/DaffodilDB"; 290 System.out.println("Server is shutting down. Please Wait ...."); 291 _RmiServer shtDwn = (_RmiServer) Naming.lookup(url); 292 shtDwn.shutDown(false); 293 Naming.unbind("rmi://LocalHost:" + Integer.toString(port) + "/DaffodilDB"); 294 } catch (java.rmi.ConnectException ce) { 295 System.out.println("\n\nSERVER IS NOT RUNNING AT SPECIFIED PORT : " + port); 296 } 297 298 } catch (Exception e) { 299 e.printStackTrace(); 300 } 301 } 302 }; 303 304 rmiServer.setThreadObject(tt); 305 mt.invoke(rt, new Object [] {tt}); 306 307 } catch (java.rmi.server.ExportException EE) { 308 System.out.println("Port No.: " + portNumber + " is already in use or Server is running"); 309 System.exit(0); 310 } catch (java.rmi.ConnectIOException CE) { 311 System.out.println("Check your Network configuration from Control panel and try again !"); 312 System.exit(0); 313 } catch (Exception ee) { 314 ee.printStackTrace(); 315 } 316 } 317 public static void stopServer(int portNumber) throws Exception { 318 boolean exit=true; 319 try { 320 System.out.println("\nDaffodilDB Server shut down called..."); 321 String url = "rmi://LocalHost:" + portNumber + "/DaffodilDB"; 322 System.out.println("Server is shutting down. Please Wait ...."); 323 _RmiServer shtDwn = (_RmiServer) Naming.lookup(url); 324 shtDwn.shutDown(exit); 325 Naming.unbind("rmi://LocalHost:" + Integer.toString(portNumber) + "/DaffodilDB"); 326 } catch (java.rmi.ConnectException ce) { 327 System.out.println("\n\nSERVER IS NOT RUNNING AT SPECIFIED PORT : " + portNumber); 328 } catch (Exception e) { 329 } 330 } 331 } 332 | Popular Tags |