1 24 package org.objectweb.jalisto.se.impl.server.socket; 25 26 import org.objectweb.jalisto.se.api.ClassDescription; 27 import org.objectweb.jalisto.se.api.internal.SessionInternal; 28 import org.objectweb.jalisto.se.api.internal.InternalMetaRepository; 29 import org.objectweb.jalisto.se.api.remote.ServerCommunicationAgent; 30 import org.objectweb.jalisto.se.exception.JalistoException; 31 import org.objectweb.jalisto.se.exception.remote.RemoteException; 32 import org.objectweb.jalisto.se.JalistoFactory; 33 import org.objectweb.jalisto.se.impl.LogicalOid; 34 import org.objectweb.jalisto.se.impl.meta.InternalMetaRepositoryImpl; 35 import org.objectweb.jalisto.se.impl.trace.Trace; 36 37 import java.io.IOException ; 38 import java.io.ObjectInputStream ; 39 import java.io.ObjectOutputStream ; 40 import java.net.Socket ; 41 import java.net.SocketException ; 42 import java.util.Collection ; 43 import java.util.Iterator ; 44 import java.util.Map ; 45 46 public class ServerCommunicationAgentSocketImpl extends Thread implements ServerCommunicationAgent { 47 48 public ServerCommunicationAgentSocketImpl(Socket socket) { 49 try { 50 this.socket = socket; 51 this.socketInfos = socket.toString(); 52 this.fromClient = new ObjectInputStream (socket.getInputStream()); 53 this.toClient = new ObjectOutputStream (socket.getOutputStream()); 54 } catch (IOException ioe) { 55 throw new RemoteException(ioe); 56 } 57 } 58 59 private void initSession() throws IOException { 60 String propertiesPath = (String ) readObject(); 61 session = (SessionInternal) JalistoFactory.getSession(propertiesPath); 62 trace = JalistoFactory.getInternalFactory().getTracer(session.getProperties()); 63 trace.println(Trace.REMOTE, "get session with properties path = {0}", propertiesPath); 64 } 65 66 public SessionInternal getSession() { 67 return session; 68 } 69 70 public void run() { 71 try { 72 initSession(); 73 trace.println(Trace.REMOTE, "Run ServerCommunicationAgentSocketImpl on " + socketInfos); 74 } catch (IOException ioe) { 75 throw new JalistoException(ioe); 76 } 77 try { 78 while (decode) { 79 try { 80 int code = readInt(); 81 decodeMethodCode(code); 82 } catch (SocketException se) { 83 if (se.getMessage().equals("Connection reset")) { 84 trace.println(Trace.REMOTE, "{0} closed", socketInfos); 85 return; 86 } else { 87 throw new JalistoException(se); 88 } 89 } 90 } 91 } catch (IOException ioe) { 92 ioe.printStackTrace(); 93 } finally { 94 closeConnection(); 95 } 96 } 97 98 private void closeConnection() { 99 trace.println(Trace.REMOTE, "cleaning connection"); 100 try { 101 if (fromClient != null) { 102 fromClient.close(); 103 } 104 if (toClient != null) { 105 toClient.close(); 106 } 107 if (socket != null) { 108 trace.println(Trace.REMOTE, "---> closing {0}", socket.toString()); 109 socket.close(); 110 } 111 } catch (Exception e) { 112 } 113 if (session != null) { 114 if (session.currentTransaction().isActive()) { 115 session.currentTransaction().rollback(); 116 } 117 if (session.isOpen()) { 118 session.closeSession(); 119 } 120 } 121 } 122 123 private int readInt() throws IOException { 124 while (true) { 125 int request = fromClient.read(); 126 if (request > 0) { 127 trace.println(Trace.REMOTE, "read int = " + request); 128 return request; 129 } 130 } 131 } 132 133 private Object readObject() throws IOException { 134 try { 135 Object request = null; 136 while (request == null) { 137 request = fromClient.readObject(); 138 } 139 return request; 140 } catch (ClassNotFoundException cnfe) { 141 throw new JalistoException(cnfe); 142 } 143 } 144 145 private void sendException(Exception e) throws IOException { 146 RemoteException re = new RemoteException(e); 147 toClient.writeObject(re); 148 toClient.flush(); 149 } 151 152 private void sendBack(Object reply) throws IOException { 153 toClient.writeObject(reply); 154 toClient.flush(); 155 } 156 157 private void decodeMethodCode(int code) throws IOException { 158 if (code < 6) { 159 switch (code) { 160 case 1: 161 try { 162 session.closeSession(); 163 sendBack("ok"); 164 } catch (Exception e) { 166 sendException(e); 167 } 168 break; 169 case 2: 170 try { 171 Collection toRead = (Collection ) readObject(); 172 sendBack(session.readObjectsByOids(toRead)); 173 } catch (Exception e) { 174 sendException(e); 175 } 176 break; 177 case 3: 178 try { 179 LogicalOid oid = (LogicalOid) readObject(); 180 Object [] objectToCreate = (Object []) readObject(); 181 session.makeNewFileOid(oid); 182 Object result = session.createObject(oid, objectToCreate); 183 sendBack(result); 184 } catch (Exception e) { 185 sendException(e); 186 } 187 break; 188 case 4: 189 try { 190 ClassDescription meta = (ClassDescription) readObject(); 191 session.defineClass(meta); 192 sendBack("ok"); 193 } catch (Exception e) { 194 sendException(e); 195 } 196 break; 197 case 5: 198 try { 199 Object oid = readObject(); 200 session.deleteObjectByOid(oid); 201 sendBack("ok"); 202 } catch (Exception e) { 203 sendException(e); 204 } 205 break; 206 207 } 208 } else if (code < 11) { 209 switch (code) { 210 case 6: 211 try { 212 String fullClassName = (String ) readObject(); 213 Object result = session.getExtent(fullClassName); 214 sendBack(result); 215 } catch (Exception e) { 216 sendException(e); 217 } 218 break; 219 case 7: 220 try { 221 String fullClassName = (String ) readObject(); 222 Object result = session.makeNewFileOid(fullClassName); 223 sendBack(result); 224 } catch (Exception e) { 225 sendException(e); 226 } 227 break; 228 case 8: 229 try { 230 session.openSession(); 231 sendBack("ok"); 232 } catch (Exception e) { 233 sendException(e); 234 } 235 break; 236 case 9: 237 try { 238 String fullClassName = (String ) readObject(); 239 session.removeClass(fullClassName); 240 sendBack("ok"); 241 } catch (Exception e) { 242 sendException(e); 243 } 244 break; 245 case 10: 246 try { 247 Object oid = readObject(); 248 Object [] objectToUpdate = (Object []) readObject(); 249 Object result = session.updateObjectByOid(oid, objectToUpdate); 250 sendBack(result); 251 } catch (Exception e) { 252 sendException(e); 253 } 254 break; 255 case 11: 256 try { 257 Object oid = readObject(); 258 Object result = session.refreshObjectByOid(oid); 259 sendBack(result); 260 } catch (Exception e) { 261 sendException(e); 262 } 263 break; 264 } 265 } else if (code < 24) { 266 switch (code) { 267 case 20: 268 try { 269 session.currentTransaction().begin(); 270 sendBack("ok"); 271 } catch (Exception e) { 272 sendException(e); 273 } 274 break; 275 case 21: 276 try { 277 session.currentTransaction().commit(); 278 sendBack("ok"); 279 } catch (Exception e) { 280 sendException(e); 281 } 282 break; 283 case 22: 284 try { 285 Object oid = readObject(); 286 Boolean withCache = (Boolean ) readObject(); 287 Object result = session.readObjectByOid(oid, withCache.booleanValue()); 288 sendBack(result); 289 } catch (Exception e) { 290 sendException(e); 291 } 292 break; 293 case 23: 294 try { 295 session.currentTransaction().rollback(); 296 sendBack("ok"); 297 } catch (Exception e) { 298 sendException(e); 299 } 300 break; 301 default : 302 throw new JalistoException("unkown code : " + code); 303 } 304 } else if (code < 28) { 305 switch (code) { 306 case 24: 307 try { 308 sendBack(session.getProperties()); 309 } catch (Exception e) { 310 sendException(e); 311 } 312 break; 313 case 25: 314 try { 315 sendBack(session.getSessionId()); 316 } catch (Exception e) { 317 sendException(e); 318 } 319 break; 320 case 26: 321 try { 322 session.reorganize(); 323 sendBack("ok"); 324 } catch (Exception e) { 325 sendException(e); 326 } 327 break; 328 case 27: 329 try { 330 session.eraseStorage(); 331 sendBack("ok"); 332 } catch (Exception e) { 333 sendException(e); 334 } 335 break; 336 } 337 } else if (code < 35) { 338 switch (code) { 339 case 30: 340 try { 341 Object result = ((InternalMetaRepositoryImpl) session.getMetaRepository()).getClassMetas(); 342 sendBack(result); 343 } catch (Exception e) { 344 sendException(e); 345 } 346 break; 347 case 31: 348 try { 349 Object result = ((InternalMetaRepositoryImpl) session.getMetaRepository()).getClassTable(); 350 sendBack(result); 351 } catch (Exception e) { 352 sendException(e); 353 } 354 break; 355 case 32: 356 try { 357 String className = (String ) readObject(); 358 Object result = ((InternalMetaRepository)session.getMetaRepository()).getClidFromClassName(className); 359 sendBack(result); 360 } catch (Exception e) { 361 sendException(e); 362 } 363 break; 364 case 33: 365 try { 366 Map createdObjects = (Map ) readObject(); 367 Iterator oids = createdObjects.keySet().iterator(); 368 while (oids.hasNext()) { 369 LogicalOid oid = (LogicalOid) oids.next(); 370 Object [] objectToCreate = (Object []) createdObjects.get(oid); 371 if (!session.getOidTable().containsFloid(session.getSessionId(), oid)) { 372 session.makeNewFileOid(oid); 373 } 374 session.createObject(oid, objectToCreate); 375 } 376 sendBack("ok"); 377 } catch (Exception e) { 378 sendException(e); 379 } 380 break; 381 case 34: 382 try { 383 Map updatedObjects = (Map ) readObject(); 384 Iterator oids = updatedObjects.keySet().iterator(); 385 while (oids.hasNext()) { 386 Object oid = oids.next(); 387 Object [] objectToUpdate = (Object []) updatedObjects.get(oid); 388 session.updateObjectByOid(oid, objectToUpdate); 389 } 390 sendBack("ok"); 391 } catch (Exception e) { 392 sendException(e); 393 } 394 break; 395 default : 396 throw new JalistoException("unkown code : " + code); 397 } 398 } else { 399 switch (code) { 400 case 35: 401 try { 402 Iterator oids = ((Collection ) readObject()).iterator(); 403 while (oids.hasNext()) { 404 Object oid = oids.next(); 405 session.deleteObjectByOid(oid); 406 } 407 sendBack("ok"); 408 } catch (Exception e) { 409 sendException(e); 410 } 411 break; 412 case 36: 413 try { 414 Object sessionId = readObject(); 415 Object clid = readObject(); 416 Object result = session.getFloidsFromClid(sessionId, clid); 417 sendBack(result); 418 } catch (Exception e) { 419 sendException(e); 420 } 421 break; 422 case 37: 423 try { 424 short clid = ((Short ) readObject()).shortValue(); 425 int number = ((Integer ) readObject()).intValue(); 426 ; 427 Long result = session.reserveFloids(clid, number); 428 sendBack(result); 429 } catch (Exception e) { 430 sendException(e); 431 } 432 break; 433 case 38: 434 try { 435 Iterator oids = ((Collection ) readObject()).iterator(); 436 while (oids.hasNext()) { 437 LogicalOid oid = (LogicalOid) oids.next(); 438 session.makeNewFileOid(oid); 439 } 440 sendBack("ok"); 441 } catch (Exception e) { 442 sendException(e); 443 } 444 break; 445 default : 446 throw new JalistoException("unkown code : " + code); 447 } 448 } 449 } 450 451 452 private boolean decode = true; 453 private Socket socket; 454 private String socketInfos; 455 private ObjectInputStream fromClient; 456 private ObjectOutputStream toClient; 457 458 private SessionInternal session; 459 private Trace trace; 460 } 461 | Popular Tags |