1 package ch.ethz.prose.tools; 27 28 import java.rmi.NoSuchObjectException ; 30 import java.rmi.Remote ; 31 import java.rmi.RemoteException ; 32 import java.rmi.server.UnicastRemoteObject ; 33 import java.net.ServerSocket ; 34 import java.net.Socket ; 35 import java.io.ObjectOutputStream ; 36 import java.io.ObjectInputStream ; 37 38 import ch.ethz.prose.ProseSystem; 39 import ch.ethz.prose.SystemStartupException; 40 import ch.ethz.prose.SystemTeardownException; 41 42 64 public 65 class RemoteProseComponent 66 { 67 68 protected static String ACTIVE_INSTANCE = "activeInstance"; 69 protected static String TEST_INSTANCE = "testInstance"; 70 private static boolean systemUp = false; 71 private static RemoteAspectManager activeInstance = null; 72 private static RemoteAspectManager testInstance = null; 73 private static Remote activeInstanceRef = null; 74 private static Remote testInstanceRef = null; 75 private static ServerSocket listener = null; 76 77 78 79 88 public synchronized static void startup() throws SystemStartupException 89 { 90 if (systemUp) 91 return; 92 systemUp = true; 93 94 System.setSecurityManager(new java.rmi.RMISecurityManager ()); 95 96 101 int portNumber = -1; 102 try 103 { 104 portNumber = Integer.parseInt(System.getProperty("prose.port","UNDEFINED")); 105 } 106 catch (NumberFormatException noPortWasSpecified) 107 { 108 throw new SystemStartupException("To use prose Remotely please specify the 'prose.port' property"); 109 } 110 111 try 112 { 113 activeInstance = new RemoteAspectManagerImpl(true); 115 activeInstanceRef = UnicastRemoteObject.exportObject(activeInstance); 116 117 testInstance = new RemoteAspectManagerImpl(false); 118 testInstanceRef = UnicastRemoteObject.exportObject(testInstance); 119 120 listener = new ServerSocket (portNumber); 121 122 Thread worker = new Thread () 123 { 124 public void run() 125 { 126 while (systemUp) 127 { 128 try 129 { 130 Socket s = listener.accept(); 131 ObjectOutputStream objOut = new ObjectOutputStream (s.getOutputStream()); 132 objOut.writeObject(activeInstanceRef); 133 objOut.writeObject(testInstanceRef); 134 } 135 catch (Exception e) 136 { e.printStackTrace(); } 137 } 138 } 139 }; 140 141 worker.start(); 142 143 } 144 catch (RemoteException e) 145 { 146 e.printStackTrace(); 147 throw new SystemStartupException("Cannot export RemoteAspectManager"); 148 149 } 150 catch (java.io.IOException e) 151 { 152 e.printStackTrace(); 153 throw new SystemStartupException("Cannot start a listener socket on the given port"); 154 } 155 156 157 158 } 159 160 167 public synchronized static void teardown() throws SystemTeardownException 168 { 169 if (!systemUp) 170 return; 171 172 try 173 { 174 UnicastRemoteObject.unexportObject(activeInstance,true); 175 UnicastRemoteObject.unexportObject(testInstance,true); 176 } 177 catch (NoSuchObjectException noChance) 178 { 179 throw new Error ("FIXME FOR PROSE DEVELOPER: why is the object not there?"); 181 } 182 183 systemUp = false; 184 } 185 186 187 protected static RemoteAspectManager[] doGetRemoteAspectManagers(String host, int connectTo) 188 throws java.net.UnknownHostException ,java.io.IOException  189 { 190 RemoteAspectManager[] result = new RemoteAspectManager[2]; 191 Socket s = new Socket (host,connectTo); 192 ObjectInputStream objIn = new ObjectInputStream (s.getInputStream()); 193 try 194 { 195 result[0]= (RemoteAspectManager)objIn.readObject(); 196 result[1]= (RemoteAspectManager)objIn.readObject(); 197 } 198 catch (java.lang.ClassNotFoundException e) 199 { 200 throw new Error (e.toString()); 201 } 202 return result; 203 } 204 205 206 } 207 208 209
| Popular Tags
|