1 24 package org.objectweb.jalisto.se.impl.client.socket; 25 26 import org.objectweb.jalisto.se.JalistoFactory; 27 import org.objectweb.jalisto.se.api.*; 28 import org.objectweb.jalisto.se.api.internal.LogicalSystemPageAccess; 29 import org.objectweb.jalisto.se.api.internal.OidTable; 30 import org.objectweb.jalisto.se.api.internal.SessionInternal; 31 import org.objectweb.jalisto.se.api.internal.multi.LockManager; 32 import org.objectweb.jalisto.se.api.query.QueryManager; 33 import org.objectweb.jalisto.se.api.remote.ClientCommunicationAgent; 34 import org.objectweb.jalisto.se.exception.JalistoException; 35 import org.objectweb.jalisto.se.exception.remote.RemoteException; 36 import org.objectweb.jalisto.se.impl.LogicalOid; 37 import org.objectweb.jalisto.se.impl.remote.socket.MethodEncoder; 38 import org.objectweb.jalisto.se.impl.trace.Trace; 39 40 import java.io.IOException ; 41 import java.io.ObjectInputStream ; 42 import java.io.ObjectOutputStream ; 43 import java.net.Socket ; 44 import java.util.*; 45 46 public class ClientCommunicationAgentSocketImpl implements ClientCommunicationAgent { 47 48 public ClientCommunicationAgentSocketImpl(JalistoProperties properties) { 49 this.trace = JalistoFactory.getInternalFactory().getTracer(properties); 50 this.serverHost = properties.getHost(); 51 this.serverPort = properties.getPort(); 52 try { 53 connect(); 54 writeObject(toServer, properties.getServerPropertiesPath()); 55 flush(toServer); 56 } catch (IOException ioe) { 57 throw new JalistoException(ioe); 58 } 59 } 60 61 private void waitEndMethod() { 62 try { 63 Object reply = null; 64 while (reply == null) { 65 reply = fromServer.readObject(); 66 } 67 if (!reply.equals("ok")) { 68 if (reply instanceof RemoteException) { 69 throw new RemoteException((RemoteException)reply); 70 } 71 throw new JalistoException(String.valueOf(reply)); 72 } 73 } catch (IOException ioe) { 74 throw new JalistoException(ioe); 75 } catch (ClassNotFoundException cnfe) { 76 throw new JalistoException(cnfe); 77 } 78 } 79 80 private Object waitResult() { 81 try { 82 Object reply = null; 83 while (reply == null) { 84 reply = fromServer.readObject(); 85 } 86 if (reply instanceof RemoteException) { 87 throw new RemoteException((RemoteException)reply); 88 } 89 return reply; 90 } catch (IOException ioe) { 91 throw new JalistoException(ioe); 92 } catch (ClassNotFoundException cnfe) { 93 throw new JalistoException(cnfe); 94 } 95 } 96 97 private void writeInt(ObjectOutputStream toServer, int i) throws IOException { 98 toServer.writeInt(i); 99 } 100 101 private void writeObject(ObjectOutputStream toServer, Object o) throws IOException { 102 toServer.writeObject(o); 103 } 104 105 private void flush(ObjectOutputStream toServer) throws IOException { 106 toServer.flush(); 107 } 108 109 private void callRemoteMethod(int code) { 110 try { 111 writeInt(toServer, code); 112 flush(toServer); 113 waitEndMethod(); 114 } catch (IOException ioe) { 115 throw new JalistoException(ioe); 116 } 117 } 118 119 private Object callRemoteMethod(int code, Object o, boolean withResult) { 120 try { 121 writeInt(toServer, code); 122 writeObject(toServer, o); 123 flush(toServer); 124 if (withResult) { 125 return waitResult(); 126 } else { 127 waitEndMethod(); 128 return null; 129 } 130 } catch (IOException ioe) { 131 throw new JalistoException(ioe); 132 } 133 } 134 135 private Object callRemoteMethod(int code, Object o1, Object o2, boolean withResult) { 136 try { 137 writeInt(toServer, code); 138 writeObject(toServer, o1); 139 writeObject(toServer, o2); 140 flush(toServer); 141 if (withResult) { 142 return waitResult(); 143 } else { 144 waitEndMethod(); 145 return null; 146 } 147 } catch (IOException ioe) { 148 throw new JalistoException(ioe); 149 } 150 } 151 152 155 156 public void closeSession() { 157 callRemoteMethod(MethodEncoder.closeSession); 158 } 160 161 public boolean contains(Object oid) { 162 throw new UnsupportedOperationException (); 163 } 164 165 public Object createObject(Object [] objectToCreate, Class objectClass) { 166 throw new UnsupportedOperationException (); 167 } 168 169 public Object createObject(Object [] objectToCreate, String objectClassName) { 170 throw new UnsupportedOperationException (); 171 } 172 173 public Object createObject(Object oid, Object [] objectToCreate) { 174 return callRemoteMethod(MethodEncoder.createObjectOid, oid, objectToCreate, true); 175 } 176 177 public Transaction currentTransaction() { 178 throw new UnsupportedOperationException (); 179 } 180 181 public void defineClass(ClassDescription classDescription) { 182 callRemoteMethod(MethodEncoder.defineClass, classDescription, false); 183 } 184 185 public void deleteObjectByOid(Object oid) { 186 callRemoteMethod(MethodEncoder.deleteObjectByOid, oid, false); 187 } 188 189 public void eraseStorage() { 190 callRemoteMethod(MethodEncoder.eraseStorage); 191 } 192 193 public String getClassNameFor(Object oid) { 194 throw new UnsupportedOperationException (); 195 } 196 197 public Extent getExtent(String fullClassName) { 198 return (Extent) callRemoteMethod(MethodEncoder.createObjectOid, fullClassName, true); 199 } 200 201 public Extent getExtent(Class theClass) { 202 throw new UnsupportedOperationException (); 203 } 204 205 public SessionInternal getInternalSession() { 206 throw new UnsupportedOperationException (); 207 } 208 209 public MetaRepository getMetaRepository() { 210 throw new UnsupportedOperationException (); 211 } 212 213 public QueryManager getQueryManager() { 214 throw new UnsupportedOperationException (); 215 } 216 217 public boolean isClassDefined(String fullQualifiedClassName) { 218 throw new UnsupportedOperationException (); 219 } 220 221 public boolean isNewBase() { 222 throw new UnsupportedOperationException (); 223 } 224 225 public boolean isOpen() { 226 throw new UnsupportedOperationException (); 227 } 228 229 public Object makeNewFileOid(Class objectClass) { 230 throw new UnsupportedOperationException (); 231 } 232 233 public Object makeNewFileOid(String objectClassName) { 234 return callRemoteMethod(MethodEncoder.makeNewFileOid, objectClassName, true); 235 } 236 237 public void openSession() { 238 callRemoteMethod(MethodEncoder.openSession); 239 } 240 241 public Object [] readObjectByOid(Object oid) { 242 throw new UnsupportedOperationException (); 243 } 244 245 public Object [] refreshObjectByOid(Object oid) { 246 return (Object []) callRemoteMethod(MethodEncoder.refreshObjectByOid, oid, true); 247 } 248 249 public Collection readObjectsByOids(Collection oids) { 250 return (Collection) callRemoteMethod(MethodEncoder.readObjectsByOids, oids, true); 251 } 252 253 public void removeClass(String fullClassName) { 254 callRemoteMethod(MethodEncoder.removeClass, fullClassName, false); 255 } 256 257 public void reorganize() { 258 callRemoteMethod(MethodEncoder.reorganize); 259 } 260 261 public Object updateObjectByOid(Object oid, Object [] objectToUpdate) { 262 return callRemoteMethod(MethodEncoder.updateObjectByOid, oid, objectToUpdate, true); 263 } 264 265 266 269 270 public void begin() { 271 callRemoteMethod(MethodEncoder.begin); 272 } 273 274 public void checkValidity(String message, boolean mustBeActive) { 275 throw new UnsupportedOperationException (); 276 } 277 278 public boolean isRemoteSession() { 279 throw new UnsupportedOperationException (); 280 } 281 282 public void clearObjectCache() { 283 throw new UnsupportedOperationException (); 284 } 285 286 public Object makeNewFileOid(LogicalOid floid) { 287 throw new UnsupportedOperationException (); 288 } 289 290 public void commit() { 291 callRemoteMethod(MethodEncoder.commit); 292 } 293 294 public Collection getAllClassNames() { 295 throw new UnsupportedOperationException (); 296 } 297 298 public LogicalSystemPageAccess getFileAccess() { 299 throw new UnsupportedOperationException (); 300 } 301 302 public LockManager getLockManager() { 303 throw new UnsupportedOperationException (); 304 } 305 306 public OidTable getOidTable() { 307 throw new UnsupportedOperationException (); 308 } 309 310 public JalistoProperties getProperties() { 311 try { 312 writeInt(toServer, MethodEncoder.getProperties); 313 flush(toServer); 314 return (JalistoProperties) waitResult(); 315 } catch (IOException ioe) { 316 throw new JalistoException(ioe); 317 } 318 } 319 320 public Object getSessionId() { 321 try { 322 writeInt(toServer, MethodEncoder.getSessionId); 323 flush(toServer); 324 return waitResult(); 325 } catch (IOException ioe) { 326 throw new JalistoException(ioe); 327 } 328 } 329 330 public Object [] readObjectByOid(Object oid, boolean withCache) { 331 return (Object []) callRemoteMethod(MethodEncoder.readObjectByOid, oid, 332 new Boolean (withCache), true); 333 } 334 335 public void rollback() { 336 callRemoteMethod(MethodEncoder.rollback); 337 } 338 339 public void setOptimistic() { 340 throw new UnsupportedOperationException (); 341 } 342 343 public void setPessimistic() { 344 throw new UnsupportedOperationException (); 345 } 346 347 350 351 public Map getClassMetas() { 352 try { 353 writeInt(toServer, MethodEncoder.getClassMetas); 354 flush(toServer); 355 return (Map) waitResult(); 356 } catch (IOException ioe) { 357 throw new JalistoException(ioe); 358 } 359 } 360 361 public Map getClassTable() { 362 try { 363 writeInt(toServer, MethodEncoder.getClassTable); 364 flush(toServer); 365 return (Map) waitResult(); 366 } catch (IOException ioe) { 367 throw new JalistoException(ioe); 368 } 369 } 370 371 public Object getClidFromClassName(String className) { 372 return callRemoteMethod(MethodEncoder.getClidFromClassName, className, true); 373 } 374 375 public void makeOids(Collection oids) { 376 callRemoteMethod(MethodEncoder.makeOids, new ArrayList(oids), false); 377 } 378 379 public void createObjects(Map mapping) { 380 callRemoteMethod(MethodEncoder.createObjects, new HashMap(mapping), false); 381 } 382 383 public void updateObjects(Map mapping) { 384 callRemoteMethod(MethodEncoder.updateObjects, new HashMap(mapping), false); 385 } 386 387 public void deleteObjects(Collection oids) { 388 callRemoteMethod(MethodEncoder.deleteObjects, new ArrayList(oids), false); 389 } 390 391 public Collection getFloidsFromClid(Object sessionId, Object clid) { 392 return (Collection) callRemoteMethod(MethodEncoder.getFloidsFromClid, sessionId, clid, true); 393 } 394 395 public Long reserveFloids(short clid, int number) { 396 return (Long ) callRemoteMethod(MethodEncoder.reserveFloids, 397 new Short (clid), new Integer (number), true); 398 } 399 400 403 404 public void connect() throws IOException { 405 trace.println(Trace.REMOTE, "try connecting on {0} {1}", serverHost, new Integer (serverPort)); 406 socket = new Socket (serverHost, serverPort); 407 toServer = new ObjectOutputStream (socket.getOutputStream()); 408 try { 409 Thread.sleep(100); 410 } catch (InterruptedException e) { 411 } 412 fromServer = new ObjectInputStream (socket.getInputStream()); 413 } 415 416 public void closeConnection() { 417 disconnect(); 418 } 419 420 public void disconnect() { 421 trace.println(Trace.REMOTE, "disconnect fron server"); 422 try { 423 toServer.close(); 424 fromServer.close(); 425 socket.close(); 426 } catch (IOException ioe) { 427 throw new JalistoException(ioe); 428 } 429 } 430 431 432 private String serverHost; 433 private int serverPort; 434 435 private Socket socket; 436 private ObjectInputStream fromServer; 437 private ObjectOutputStream toServer; 438 439 private Trace trace; 440 } 441 | Popular Tags |