KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > client > socket > ClientCommunicationAgentSocketImpl


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.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 JavaDoc;
41 import java.io.ObjectInputStream JavaDoc;
42 import java.io.ObjectOutputStream JavaDoc;
43 import java.net.Socket JavaDoc;
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 JavaDoc ioe) {
57             throw new JalistoException(ioe);
58         }
59     }
60
61     private void waitEndMethod() {
62         try {
63             Object JavaDoc 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 JavaDoc ioe) {
74             throw new JalistoException(ioe);
75         } catch (ClassNotFoundException JavaDoc cnfe) {
76             throw new JalistoException(cnfe);
77         }
78     }
79
80     private Object JavaDoc waitResult() {
81         try {
82             Object JavaDoc 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 JavaDoc ioe) {
91             throw new JalistoException(ioe);
92         } catch (ClassNotFoundException JavaDoc cnfe) {
93             throw new JalistoException(cnfe);
94         }
95     }
96
97     private void writeInt(ObjectOutputStream JavaDoc toServer, int i) throws IOException JavaDoc {
98         toServer.writeInt(i);
99     }
100
101     private void writeObject(ObjectOutputStream JavaDoc toServer, Object JavaDoc o) throws IOException JavaDoc {
102         toServer.writeObject(o);
103     }
104
105     private void flush(ObjectOutputStream JavaDoc toServer) throws IOException JavaDoc {
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 JavaDoc ioe) {
115             throw new JalistoException(ioe);
116         }
117     }
118
119     private Object JavaDoc callRemoteMethod(int code, Object JavaDoc 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 JavaDoc ioe) {
131             throw new JalistoException(ioe);
132         }
133     }
134
135     private Object JavaDoc callRemoteMethod(int code, Object JavaDoc o1, Object JavaDoc 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 JavaDoc ioe) {
148             throw new JalistoException(ioe);
149         }
150     }
151
152     /**
153      * ********************************* SESSION **************************************************
154      */

155
156     public void closeSession() {
157         callRemoteMethod(MethodEncoder.closeSession);
158 // disconnect();
159
}
160
161     public boolean contains(Object JavaDoc oid) {
162         throw new UnsupportedOperationException JavaDoc();
163     }
164
165     public Object JavaDoc createObject(Object JavaDoc[] objectToCreate, Class JavaDoc objectClass) {
166         throw new UnsupportedOperationException JavaDoc();
167     }
168
169     public Object JavaDoc createObject(Object JavaDoc[] objectToCreate, String JavaDoc objectClassName) {
170         throw new UnsupportedOperationException JavaDoc();
171     }
172
173     public Object JavaDoc createObject(Object JavaDoc oid, Object JavaDoc[] objectToCreate) {
174         return callRemoteMethod(MethodEncoder.createObjectOid, oid, objectToCreate, true);
175     }
176
177     public Transaction currentTransaction() {
178         throw new UnsupportedOperationException JavaDoc();
179     }
180
181     public void defineClass(ClassDescription classDescription) {
182         callRemoteMethod(MethodEncoder.defineClass, classDescription, false);
183     }
184
185     public void deleteObjectByOid(Object JavaDoc oid) {
186         callRemoteMethod(MethodEncoder.deleteObjectByOid, oid, false);
187     }
188
189     public void eraseStorage() {
190         callRemoteMethod(MethodEncoder.eraseStorage);
191     }
192
193     public String JavaDoc getClassNameFor(Object JavaDoc oid) {
194         throw new UnsupportedOperationException JavaDoc();
195     }
196
197     public Extent getExtent(String JavaDoc fullClassName) {
198         return (Extent) callRemoteMethod(MethodEncoder.createObjectOid, fullClassName, true);
199     }
200
201     public Extent getExtent(Class JavaDoc theClass) {
202         throw new UnsupportedOperationException JavaDoc();
203     }
204
205     public SessionInternal getInternalSession() {
206         throw new UnsupportedOperationException JavaDoc();
207     }
208
209     public MetaRepository getMetaRepository() {
210         throw new UnsupportedOperationException JavaDoc();
211     }
212
213     public QueryManager getQueryManager() {
214         throw new UnsupportedOperationException JavaDoc();
215     }
216
217     public boolean isClassDefined(String JavaDoc fullQualifiedClassName) {
218         throw new UnsupportedOperationException JavaDoc();
219     }
220
221     public boolean isNewBase() {
222         throw new UnsupportedOperationException JavaDoc();
223     }
224
225     public boolean isOpen() {
226         throw new UnsupportedOperationException JavaDoc();
227     }
228
229     public Object JavaDoc makeNewFileOid(Class JavaDoc objectClass) {
230         throw new UnsupportedOperationException JavaDoc();
231     }
232
233     public Object JavaDoc makeNewFileOid(String JavaDoc objectClassName) {
234         return callRemoteMethod(MethodEncoder.makeNewFileOid, objectClassName, true);
235     }
236
237     public void openSession() {
238         callRemoteMethod(MethodEncoder.openSession);
239     }
240
241     public Object JavaDoc[] readObjectByOid(Object JavaDoc oid) {
242         throw new UnsupportedOperationException JavaDoc();
243     }
244
245     public Object JavaDoc[] refreshObjectByOid(Object JavaDoc oid) {
246         return (Object JavaDoc[]) 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 JavaDoc fullClassName) {
254         callRemoteMethod(MethodEncoder.removeClass, fullClassName, false);
255     }
256
257     public void reorganize() {
258         callRemoteMethod(MethodEncoder.reorganize);
259     }
260
261     public Object JavaDoc updateObjectByOid(Object JavaDoc oid, Object JavaDoc[] objectToUpdate) {
262         return callRemoteMethod(MethodEncoder.updateObjectByOid, oid, objectToUpdate, true);
263     }
264
265
266     /**
267      * ********************************** INTERNAL ********************************************
268      */

269
270     public void begin() {
271         callRemoteMethod(MethodEncoder.begin);
272     }
273
274     public void checkValidity(String JavaDoc message, boolean mustBeActive) {
275         throw new UnsupportedOperationException JavaDoc();
276     }
277
278     public boolean isRemoteSession() {
279         throw new UnsupportedOperationException JavaDoc();
280     }
281
282     public void clearObjectCache() {
283         throw new UnsupportedOperationException JavaDoc();
284     }
285
286     public Object JavaDoc makeNewFileOid(LogicalOid floid) {
287         throw new UnsupportedOperationException JavaDoc();
288     }
289
290     public void commit() {
291         callRemoteMethod(MethodEncoder.commit);
292     }
293
294     public Collection getAllClassNames() {
295         throw new UnsupportedOperationException JavaDoc();
296     }
297
298     public LogicalSystemPageAccess getFileAccess() {
299         throw new UnsupportedOperationException JavaDoc();
300     }
301
302     public LockManager getLockManager() {
303         throw new UnsupportedOperationException JavaDoc();
304     }
305
306     public OidTable getOidTable() {
307         throw new UnsupportedOperationException JavaDoc();
308     }
309
310     public JalistoProperties getProperties() {
311         try {
312             writeInt(toServer, MethodEncoder.getProperties);
313             flush(toServer);
314             return (JalistoProperties) waitResult();
315         } catch (IOException JavaDoc ioe) {
316             throw new JalistoException(ioe);
317         }
318     }
319
320     public Object JavaDoc getSessionId() {
321         try {
322             writeInt(toServer, MethodEncoder.getSessionId);
323             flush(toServer);
324             return waitResult();
325         } catch (IOException JavaDoc ioe) {
326             throw new JalistoException(ioe);
327         }
328     }
329
330     public Object JavaDoc[] readObjectByOid(Object JavaDoc oid, boolean withCache) {
331         return (Object JavaDoc[]) callRemoteMethod(MethodEncoder.readObjectByOid, oid,
332                                            new Boolean JavaDoc(withCache), true);
333     }
334
335     public void rollback() {
336         callRemoteMethod(MethodEncoder.rollback);
337     }
338
339     public void setOptimistic() {
340         throw new UnsupportedOperationException JavaDoc();
341     }
342
343     public void setPessimistic() {
344         throw new UnsupportedOperationException JavaDoc();
345     }
346
347     /**
348      * *********************************** OTHER ************************************
349      */

350
351     public Map getClassMetas() {
352         try {
353             writeInt(toServer, MethodEncoder.getClassMetas);
354             flush(toServer);
355             return (Map) waitResult();
356         } catch (IOException JavaDoc 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 JavaDoc ioe) {
367             throw new JalistoException(ioe);
368         }
369     }
370
371     public Object JavaDoc getClidFromClassName(String JavaDoc 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 JavaDoc sessionId, Object JavaDoc clid) {
392         return (Collection) callRemoteMethod(MethodEncoder.getFloidsFromClid, sessionId, clid, true);
393     }
394
395     public Long JavaDoc reserveFloids(short clid, int number) {
396         return (Long JavaDoc) callRemoteMethod(MethodEncoder.reserveFloids,
397                                        new Short JavaDoc(clid), new Integer JavaDoc(number), true);
398     }
399
400     /**
401      * ********************************** SOCKET ******************************************
402      */

403
404     public void connect() throws IOException JavaDoc {
405         trace.println(Trace.REMOTE, "try connecting on {0} {1}", serverHost, new Integer JavaDoc(serverPort));
406         socket = new Socket JavaDoc(serverHost, serverPort);
407         toServer = new ObjectOutputStream JavaDoc(socket.getOutputStream());
408         try {
409             Thread.sleep(100);
410         } catch (InterruptedException JavaDoc e) {
411         }
412         fromServer = new ObjectInputStream JavaDoc(socket.getInputStream());
413 // toServer.close();
414
}
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 JavaDoc ioe) {
427             throw new JalistoException(ioe);
428         }
429     }
430
431
432     private String JavaDoc serverHost;
433     private int serverPort;
434
435     private Socket JavaDoc socket;
436     private ObjectInputStream JavaDoc fromServer;
437     private ObjectOutputStream JavaDoc toServer;
438
439     private Trace trace;
440 }
441
Popular Tags