KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > server > socket > ServerCommunicationAgentSocketImpl


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

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 JavaDoc;
38 import java.io.ObjectInputStream JavaDoc;
39 import java.io.ObjectOutputStream JavaDoc;
40 import java.net.Socket JavaDoc;
41 import java.net.SocketException JavaDoc;
42 import java.util.Collection JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.Map JavaDoc;
45
46 public class ServerCommunicationAgentSocketImpl extends Thread JavaDoc implements ServerCommunicationAgent {
47
48     public ServerCommunicationAgentSocketImpl(Socket JavaDoc socket) {
49         try {
50             this.socket = socket;
51             this.socketInfos = socket.toString();
52             this.fromClient = new ObjectInputStream JavaDoc(socket.getInputStream());
53             this.toClient = new ObjectOutputStream JavaDoc(socket.getOutputStream());
54         } catch (IOException JavaDoc ioe) {
55             throw new RemoteException(ioe);
56         }
57     }
58
59     private void initSession() throws IOException JavaDoc {
60         String JavaDoc propertiesPath = (String JavaDoc) 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 JavaDoc ioe) {
75             throw new JalistoException(ioe);
76         }
77         try {
78             while (decode) {
79                 try {
80                     int code = readInt();
81                     decodeMethodCode(code);
82                 } catch (SocketException JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc {
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 JavaDoc readObject() throws IOException JavaDoc {
134         try {
135             Object JavaDoc request = null;
136             while (request == null) {
137                 request = fromClient.readObject();
138             }
139             return request;
140         } catch (ClassNotFoundException JavaDoc cnfe) {
141             throw new JalistoException(cnfe);
142         }
143     }
144
145     private void sendException(Exception JavaDoc e) throws IOException JavaDoc {
146         RemoteException re = new RemoteException(e);
147         toClient.writeObject(re);
148         toClient.flush();
149 // re.printStackTrace();
150
}
151
152     private void sendBack(Object JavaDoc reply) throws IOException JavaDoc {
153         toClient.writeObject(reply);
154         toClient.flush();
155     }
156
157     private void decodeMethodCode(int code) throws IOException JavaDoc {
158         if (code < 6) {
159             switch (code) {
160                 case 1:
161                     try {
162                         session.closeSession();
163                         sendBack("ok");
164 // decode = false;
165
} catch (Exception JavaDoc e) {
166                         sendException(e);
167                     }
168                     break;
169                 case 2:
170                     try {
171                         Collection JavaDoc toRead = (Collection JavaDoc) readObject();
172                         sendBack(session.readObjectsByOids(toRead));
173                     } catch (Exception JavaDoc e) {
174                         sendException(e);
175                     }
176                     break;
177                 case 3:
178                     try {
179                         LogicalOid oid = (LogicalOid) readObject();
180                         Object JavaDoc[] objectToCreate = (Object JavaDoc[]) readObject();
181                         session.makeNewFileOid(oid);
182                         Object JavaDoc result = session.createObject(oid, objectToCreate);
183                         sendBack(result);
184                     } catch (Exception JavaDoc 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 JavaDoc e) {
194                         sendException(e);
195                     }
196                     break;
197                 case 5:
198                     try {
199                         Object JavaDoc oid = readObject();
200                         session.deleteObjectByOid(oid);
201                         sendBack("ok");
202                     } catch (Exception JavaDoc e) {
203                         sendException(e);
204                     }
205                     break;
206
207             }
208         } else if (code < 11) {
209             switch (code) {
210                 case 6:
211                     try {
212                         String JavaDoc fullClassName = (String JavaDoc) readObject();
213                         Object JavaDoc result = session.getExtent(fullClassName);
214                         sendBack(result);
215                     } catch (Exception JavaDoc e) {
216                         sendException(e);
217                     }
218                     break;
219                 case 7:
220                     try {
221                         String JavaDoc fullClassName = (String JavaDoc) readObject();
222                         Object JavaDoc result = session.makeNewFileOid(fullClassName);
223                         sendBack(result);
224                     } catch (Exception JavaDoc e) {
225                         sendException(e);
226                     }
227                     break;
228                 case 8:
229                     try {
230                         session.openSession();
231                         sendBack("ok");
232                     } catch (Exception JavaDoc e) {
233                         sendException(e);
234                     }
235                     break;
236                 case 9:
237                     try {
238                         String JavaDoc fullClassName = (String JavaDoc) readObject();
239                         session.removeClass(fullClassName);
240                         sendBack("ok");
241                     } catch (Exception JavaDoc e) {
242                         sendException(e);
243                     }
244                     break;
245                 case 10:
246                     try {
247                         Object JavaDoc oid = readObject();
248                         Object JavaDoc[] objectToUpdate = (Object JavaDoc[]) readObject();
249                         Object JavaDoc result = session.updateObjectByOid(oid, objectToUpdate);
250                         sendBack(result);
251                     } catch (Exception JavaDoc e) {
252                         sendException(e);
253                     }
254                     break;
255                 case 11:
256                     try {
257                         Object JavaDoc oid = readObject();
258                         Object JavaDoc result = session.refreshObjectByOid(oid);
259                         sendBack(result);
260                     } catch (Exception JavaDoc 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 JavaDoc e) {
272                         sendException(e);
273                     }
274                     break;
275                 case 21:
276                     try {
277                         session.currentTransaction().commit();
278                         sendBack("ok");
279                     } catch (Exception JavaDoc e) {
280                         sendException(e);
281                     }
282                     break;
283                 case 22:
284                     try {
285                         Object JavaDoc oid = readObject();
286                         Boolean JavaDoc withCache = (Boolean JavaDoc) readObject();
287                         Object JavaDoc result = session.readObjectByOid(oid, withCache.booleanValue());
288                         sendBack(result);
289                     } catch (Exception JavaDoc e) {
290                         sendException(e);
291                     }
292                     break;
293                 case 23:
294                     try {
295                         session.currentTransaction().rollback();
296                         sendBack("ok");
297                     } catch (Exception JavaDoc 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 JavaDoc e) {
310                         sendException(e);
311                     }
312                     break;
313                 case 25:
314                     try {
315                         sendBack(session.getSessionId());
316                     } catch (Exception JavaDoc e) {
317                         sendException(e);
318                     }
319                     break;
320                 case 26:
321                     try {
322                         session.reorganize();
323                         sendBack("ok");
324                     } catch (Exception JavaDoc e) {
325                         sendException(e);
326                     }
327                     break;
328                 case 27:
329                     try {
330                         session.eraseStorage();
331                         sendBack("ok");
332                     } catch (Exception JavaDoc e) {
333                         sendException(e);
334                     }
335                     break;
336             }
337         } else if (code < 35) {
338             switch (code) {
339                 case 30:
340                     try {
341                         Object JavaDoc result = ((InternalMetaRepositoryImpl) session.getMetaRepository()).getClassMetas();
342                         sendBack(result);
343                     } catch (Exception JavaDoc e) {
344                         sendException(e);
345                     }
346                     break;
347                 case 31:
348                     try {
349                         Object JavaDoc result = ((InternalMetaRepositoryImpl) session.getMetaRepository()).getClassTable();
350                         sendBack(result);
351                     } catch (Exception JavaDoc e) {
352                         sendException(e);
353                     }
354                     break;
355                 case 32:
356                     try {
357                         String JavaDoc className = (String JavaDoc) readObject();
358                         Object JavaDoc result = ((InternalMetaRepository)session.getMetaRepository()).getClidFromClassName(className);
359                         sendBack(result);
360                     } catch (Exception JavaDoc e) {
361                         sendException(e);
362                     }
363                     break;
364                 case 33:
365                     try {
366                         Map JavaDoc createdObjects = (Map JavaDoc) readObject();
367                         Iterator JavaDoc oids = createdObjects.keySet().iterator();
368                         while (oids.hasNext()) {
369                             LogicalOid oid = (LogicalOid) oids.next();
370                             Object JavaDoc[] objectToCreate = (Object JavaDoc[]) 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 JavaDoc e) {
378                         sendException(e);
379                     }
380                     break;
381                 case 34:
382                     try {
383                         Map JavaDoc updatedObjects = (Map JavaDoc) readObject();
384                         Iterator JavaDoc oids = updatedObjects.keySet().iterator();
385                         while (oids.hasNext()) {
386                             Object JavaDoc oid = oids.next();
387                             Object JavaDoc[] objectToUpdate = (Object JavaDoc[]) updatedObjects.get(oid);
388                             session.updateObjectByOid(oid, objectToUpdate);
389                         }
390                         sendBack("ok");
391                     } catch (Exception JavaDoc 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 JavaDoc oids = ((Collection JavaDoc) readObject()).iterator();
403                         while (oids.hasNext()) {
404                             Object JavaDoc oid = oids.next();
405                             session.deleteObjectByOid(oid);
406                         }
407                         sendBack("ok");
408                     } catch (Exception JavaDoc e) {
409                         sendException(e);
410                     }
411                     break;
412                 case 36:
413                     try {
414                         Object JavaDoc sessionId = readObject();
415                         Object JavaDoc clid = readObject();
416                         Object JavaDoc result = session.getFloidsFromClid(sessionId, clid);
417                         sendBack(result);
418                     } catch (Exception JavaDoc e) {
419                         sendException(e);
420                     }
421                     break;
422                 case 37:
423                     try {
424                         short clid = ((Short JavaDoc) readObject()).shortValue();
425                         int number = ((Integer JavaDoc) readObject()).intValue();
426                         ;
427                         Long JavaDoc result = session.reserveFloids(clid, number);
428                         sendBack(result);
429                     } catch (Exception JavaDoc e) {
430                         sendException(e);
431                     }
432                     break;
433                 case 38:
434                     try {
435                         Iterator JavaDoc oids = ((Collection JavaDoc) readObject()).iterator();
436                         while (oids.hasNext()) {
437                             LogicalOid oid = (LogicalOid) oids.next();
438                             session.makeNewFileOid(oid);
439                         }
440                         sendBack("ok");
441                     } catch (Exception JavaDoc 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 JavaDoc socket;
454     private String JavaDoc socketInfos;
455     private ObjectInputStream JavaDoc fromClient;
456     private ObjectOutputStream JavaDoc toClient;
457
458     private SessionInternal session;
459     private Trace trace;
460 }
461
Popular Tags