KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > fos > genclass > FosGenClassMapping


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.mapper.fos.genclass;
25
26 import org.objectweb.jorm.api.PAccessor;
27 import org.objectweb.jorm.api.PBinding;
28 import org.objectweb.jorm.api.PBindingCtrl;
29 import org.objectweb.jorm.api.PClassMapping;
30 import org.objectweb.jorm.api.PException;
31 import org.objectweb.jorm.api.PExceptionIO;
32 import org.objectweb.jorm.api.PExceptionProtocol;
33 import org.objectweb.jorm.api.PGenClassAccessor;
34 import org.objectweb.jorm.api.PIndexedElem;
35 import org.objectweb.jorm.api.PMappingCallback;
36 import org.objectweb.jorm.api.PMappingStructuresManager;
37 import org.objectweb.jorm.api.PNameIterator;
38 import org.objectweb.jorm.api.PStateGraph;
39 import org.objectweb.jorm.genclass.api.FieldDesc;
40 import org.objectweb.jorm.genclass.lib.GenClassMapping;
41 import org.objectweb.jorm.lib.PBindingImpl;
42 import org.objectweb.jorm.metainfo.api.MetaObject;
43 import org.objectweb.jorm.mapper.fos.lib.FosPMappingStructuresManager;
44 import org.objectweb.jorm.naming.api.PName;
45 import org.objectweb.jorm.type.api.PType;
46 import org.objectweb.medor.tuple.api.TupleCollection;
47 import org.objectweb.perseus.connector.ra.fos.FosConnection;
48 import org.objectweb.perseus.fos.api.FosAccess;
49 import org.objectweb.perseus.fos.api.FosException;
50 import org.objectweb.perseus.fos.api.FosStructure;
51
52 import java.io.File JavaDoc;
53 import java.io.ObjectInputStream JavaDoc;
54 import java.io.ObjectOutputStream JavaDoc;
55 import java.io.Serializable JavaDoc;
56 import java.math.BigDecimal JavaDoc;
57 import java.util.Date JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import java.util.Iterator JavaDoc;
60
61 public class FosGenClassMapping
62         extends GenClassMapping
63         implements FosGenClassProp, FosStructure {
64     protected String JavaDoc className = null;
65     protected String JavaDoc fieldName = null;
66     protected String JavaDoc genClassLevel = null;
67     protected String JavaDoc dirName = null;
68
69     private void initDirName() {
70         if (dirName == null) {
71             dirName = className + File.separator + fieldName + File.separator
72                     + genClassLevel;
73         }
74     }
75
76     // IMPLEMENTATION OF METHODS FROM THE PClassMapping INTERFACE
77

78     /**
79      * It intialises the class mapping. It especially assigns a mapper as well
80      * as a meta-object describing either a class or a generic class.
81      * @param mapper The mapper to be assigned to this class mapping.
82      * @param metaclass The meta-object describing the associated class. It may
83      * be null.
84      */

85     public void init(PMappingCallback mapper, MetaObject metaclass) throws PException {
86         super.init(mapper, metaclass);
87         initDirName();
88         ((FosPMappingStructuresManager) mapper.getPMappingStructuresManager())
89                 .addDirName(genClassPType.getJormName(), dirName);
90     }
91
92     /**
93      * It creates a new PBinding.
94      * @return The new PBinding with lifecycle state set to
95      * LIFECYCLE_NOTBOUND.
96      */

97     public PBinding createPBinding() throws PException {
98         return new PBindingImpl(this);
99     }
100
101
102     /**
103      * It returns an iterator over all pname of persitent objects availlable in
104      * this class.
105      * @param conn is the connection which permits to access to the support
106      * @return the iterator over PName objects
107      * @exception PExceptionProtocol A problem occured during the request
108      * that is not related to I/O.
109      * @exception PExceptionIO A problem occured during the request.
110      */

111     public PNameIterator getPNameIterator(Object JavaDoc conn,
112                                           boolean withSubType,
113                                           boolean prefetching,
114                                           Object JavaDoc txctx) throws PException {
115         throw new PException("Not supported yet");
116     }
117
118     /**
119      * It specifies if the given kind of PMapper is supported by this
120      * PClassMapping.
121      * @param mappername The name defining the PMapper kind.
122      * @return true if this kind of mapper is supported.
123      */

124     public boolean isConform(String JavaDoc mappername) {
125         return mappername.equals("fos");
126     }
127
128     public boolean exist(PBinding pb, Object JavaDoc conn) throws PException {
129         byte status = pb.getStatus();
130         byte nextState =
131                 PStateGraph.nextStatePBinding(status, PBinding.ACTION_EXIST);
132         if (nextState == PBinding.LIFECYCLE_ERROR)
133             throw new PExceptionProtocol("Unauthorized operation");
134         status = nextState;
135         FosConnection connection;
136         if (conn == null)
137             throw new PExceptionProtocol("Cannot check the existence without a connection");
138         else if (conn instanceof FosConnection)
139             connection = (FosConnection) conn;
140         else
141             throw new PExceptionProtocol("Cannot check the existence with this kind of connection");
142         try {
143             return connection.exist(dirName, pb.getPName().encodeString());
144         } catch (FosException fe) {
145             throw new PExceptionIO(fe, "Problem while testing the existence of the DSI.");
146         }
147     }
148
149     public void read(PBinding pb, Object JavaDoc conn, PAccessor pa) throws PException {
150         byte status = pb.getStatus();
151         byte nextState =
152                 PStateGraph.nextStatePBinding(status, PBinding.ACTION_READ);
153         if (nextState == PBinding.LIFECYCLE_ERROR)
154             throw new PExceptionProtocol("Unauthorized operation");
155         FosConnection connection;
156         if (conn == null)
157             throw new PExceptionProtocol("Cannot read without a connection");
158         else if (conn instanceof FosConnection)
159             connection = (FosConnection) conn;
160         else
161             throw new PExceptionProtocol("Cannot read with this kind of connection");
162         try {
163             connection.read(dirName, pb.getPName().encodeString(), this, pa);
164         } catch (FosException fe) {
165             throw new PExceptionIO(fe, "Problem while reading the DSI.");
166         }
167         ((PBindingCtrl) pb).setStatus(nextState);
168     }
169
170     public void write(PBinding pb, Object JavaDoc conn, PAccessor pa) throws PException {
171         byte status = pb.getStatus();
172         byte nextState =
173                 PStateGraph.nextStatePBinding(status, PBinding.ACTION_WRITE);
174         if (nextState == PBinding.LIFECYCLE_ERROR)
175             throw new PExceptionProtocol("Unauthorized operation");
176         FosConnection connection;
177         if (conn == null)
178             throw new PExceptionProtocol("Cannot write without a connection");
179         else if (conn instanceof FosConnection)
180             connection = (FosConnection) conn;
181         else
182             throw new PExceptionProtocol("Cannot write with this kind of connection");
183         try {
184             if (status == PBinding.LIFECYCLE_DELTOWRITE) {
185                 connection.delete(dirName, pb.getPName().encodeString());
186             } else if (status == PBinding.LIFECYCLE_NEWTOWRITE) {
187                 connection.write(dirName, pb.getPName().encodeString(), this, pa);
188             } else if (status == PBinding.LIFECYCLE_ACTIVEFORIO) {
189                 connection.write(dirName, pb.getPName().encodeString(), this, pa);
190             }
191         } catch (FosException fe) {
192             throw new PExceptionIO(fe, "Problem while writing the DSI.");
193         }
194         ((PBindingCtrl) pb).setStatus(nextState);
195     }
196
197     /**
198      * Returns null in case of genclass.
199      */

200     public PClassMapping[] getSubPCMs() throws PException {
201         return null;
202     }
203     
204     // IMPLEMENTATION OF THE METHODS FROM THE FosGenClassProp INTERFACE
205

206     public void setClassName(String JavaDoc cn) {
207         className = cn;
208     }
209
210     public void setFieldName(String JavaDoc fn) {
211         fieldName = fn;
212     }
213
214     public void setGenClassLevel(String JavaDoc level) {
215         genClassLevel = level;
216     }
217
218     // IMPLEMENTATION OF METHODS FROM THE FosStructure INTERFACE
219

220     /**
221      * Reads the content of a persistent object from the file represented by
222      * the given ObjectInputStream.
223      * @param ois The ObjectInputStream representing the file from which to
224      * read.
225      * @param conn The connection used to access the File Object Store.
226      */

227     public void readFile(ObjectInputStream JavaDoc ois, FosAccess conn, Object JavaDoc ctxt)
228             throws Exception JavaDoc {
229         PGenClassAccessor paccessorGenClass = null;
230         if (ctxt == null)
231             throw new PExceptionProtocol("Accessor must not be null");
232         else if (ctxt instanceof PGenClassAccessor)
233             paccessorGenClass = (PGenClassAccessor) ctxt;
234         else
235             throw new PExceptionProtocol("Wrong type of accessor");
236         //Read the number of elements
237
int nbelement = ois.readInt();
238         paccessorGenClass.paSetNbElem(nbelement);
239         for (int j = 0; j < nbelement; j++) {
240             PIndexedElem pie = paccessorGenClass.createPIndexedElem();
241             // Read the index fields
242
FieldDesc[] indexes = getIndexFields();
243             for (int i = 0; i < indexes.length; i++) {
244                 switch (indexes[i].type.getTypeCode()) {
245                 case PType.TYPECODE_BYTE:
246                     pie.pieSetByteIndexField(indexes[i].name, ois.readByte());
247                     break;
248                 case PType.TYPECODE_CHAR:
249                     pie.pieSetCharIndexField(indexes[i].name, ois.readChar());
250                     break;
251                 case PType.TYPECODE_INT:
252                     pie.pieSetIntIndexField(indexes[i].name, ois.readInt());
253                     break;
254                 case PType.TYPECODE_LONG:
255                     pie.pieSetLongIndexField(indexes[i].name, ois.readLong());
256                     break;
257                 case PType.TYPECODE_SHORT:
258                     pie.pieSetShortIndexField(indexes[i].name, ois.readShort());
259                     break;
260                 case PType.TYPECODE_DATE:
261                 case PType.TYPECODE_OBJBYTE:
262                 case PType.TYPECODE_OBJCHAR:
263                 case PType.TYPECODE_OBJINT:
264                 case PType.TYPECODE_OBJLONG:
265                 case PType.TYPECODE_OBJSHORT:
266                 case PType.TYPECODE_STRING:
267                     pie.pieSetStringIndexField(indexes[i].name,
268                                                (String JavaDoc) ois.readObject());
269                     break;
270                 default:
271                     throw new PExceptionProtocol("Unauthorized index type: ("
272                                                  + indexes[i].name + ", "
273                                                  + indexes[i].type.getJormName());
274                 }
275             }
276             // Write the element fields
277
FieldDesc[] elements = getElemFields();
278             switch (elements[0].type.getTypeCode()) {
279             case PType.TYPECODE_BOOLEAN:
280                 pie.pieSetBooleanElem(ois.readBoolean());
281                 break;
282             case PType.TYPECODE_OBJBOOLEAN:
283                 pie.pieSetObooleanElem((Boolean JavaDoc) ois.readObject());
284                 break;
285             case PType.TYPECODE_BYTE:
286                 pie.pieSetByteElem(ois.readByte());
287                 break;
288             case PType.TYPECODE_OBJBYTE:
289                 pie.pieSetObyteElem((Byte JavaDoc) ois.readObject());
290                 break;
291             case PType.TYPECODE_CHAR:
292                 pie.pieSetCharElem(ois.readChar());
293                 break;
294             case PType.TYPECODE_OBJCHAR:
295                 pie.pieSetOcharElem((Character JavaDoc) ois.readObject());
296                 break;
297             case PType.TYPECODE_SHORT:
298                 pie.pieSetBooleanElem(ois.readBoolean());
299                 break;
300             case PType.TYPECODE_OBJSHORT:
301                 pie.pieSetOshortElem((Short JavaDoc) ois.readObject());
302                 break;
303             case PType.TYPECODE_INT:
304                 pie.pieSetIntElem(ois.readInt());
305                 break;
306             case PType.TYPECODE_OBJINT:
307                 pie.pieSetOintElem((Integer JavaDoc) ois.readObject());
308                 break;
309             case PType.TYPECODE_LONG:
310                 pie.pieSetLongElem(ois.readLong());
311                 break;
312             case PType.TYPECODE_OBJLONG:
313                 pie.pieSetOlongElem((Long JavaDoc) ois.readObject());
314                 break;
315             case PType.TYPECODE_FLOAT:
316                 pie.pieSetFloatElem(ois.readFloat());
317                 break;
318             case PType.TYPECODE_OBJFLOAT:
319                 pie.pieSetOfloatElem((Float JavaDoc) ois.readObject());
320                 break;
321             case PType.TYPECODE_DOUBLE:
322                 pie.pieSetDoubleElem(ois.readDouble());
323                 break;
324             case PType.TYPECODE_OBJDOUBLE:
325                 pie.pieSetOdoubleElem((Double JavaDoc) ois.readObject());
326                 break;
327             case PType.TYPECODE_DATE:
328                 pie.pieSetDateElem((Date JavaDoc) ois.readObject());
329                 break;
330             case PType.TYPECODE_CHARARRAY:
331                 pie.pieSetCharArrayElem((char[]) ois.readObject());
332                 break;
333             case PType.TYPECODE_BYTEARRAY:
334                 pie.pieSetByteArrayElem((byte[]) ois.readObject());
335                 break;
336             case PType.TYPECODE_SERIALIZED:
337                 pie.pieSetSerializedElem((Serializable JavaDoc) ois.readObject());
338                 break;
339             case PType.TYPECODE_BIGDECIMAL:
340                 pie.pieSetBigDecimalElem((BigDecimal JavaDoc) ois.readObject());
341                 break;
342             case PType.TYPECODE_STRING:
343                 pie.pieSetStringElem((String JavaDoc) ois.readObject());
344                 break;
345             case PType.TYPECODE_REFERENCE:
346                 pie.pieSetRefElem(getPNameCoder()
347                         .decodeString((String JavaDoc) ois.readObject()));
348                 break;
349             default:
350                 throw new PExceptionProtocol("Unauthorized element type: ("
351                                              + elements[0].name + ", "
352                                              + elements[0].type.getJormName());
353             }
354             paccessorGenClass.paAdd(pie, conn);
355         }
356     }
357
358     /**
359      * Writes the content of a persistent object to the file represented by
360      * the given ObjectOutputStream.
361      * @param oos The ObjectOutputStream representing the file to which to
362      * write.
363      * @param conn The connection used to access the File Object Store.
364      */

365     public void writeFile(ObjectOutputStream JavaDoc oos, FosAccess conn, Object JavaDoc ctxt)
366             throws Exception JavaDoc {
367         PGenClassAccessor paccessorGenClass = null;
368         if (ctxt == null)
369             throw new PExceptionProtocol("Accessor must not be null");
370         else if (ctxt instanceof PGenClassAccessor)
371             paccessorGenClass = (PGenClassAccessor) ctxt;
372         else
373             throw new PExceptionProtocol("Wrong type of accessor");
374         //Write the number of element
375
oos.writeInt(paccessorGenClass.paGetNbElem());
376         Iterator pies = paccessorGenClass.paIterator();
377         // For each element of the genclass
378
while (pies.hasNext()) {
379             PIndexedElem pie = (PIndexedElem) pies.next();
380             if (pie.getElemStatus() == PIndexedElem.ELEM_DELETED)
381                 continue;
382             // Write the index fields
383
FieldDesc[] indexes = getIndexFields();
384             for (int i = 0; i < indexes.length; i++) {
385                 switch (indexes[i].type.getTypeCode()) {
386                 case PType.TYPECODE_BYTE:
387                     oos.writeByte(pie.pieGetByteIndexField(indexes[i].name));
388                     break;
389                 case PType.TYPECODE_CHAR:
390                     oos.writeChar(pie.pieGetCharIndexField(indexes[i].name));
391                     break;
392                 case PType.TYPECODE_INT:
393                     oos.writeInt(pie.pieGetIntIndexField(indexes[i].name));
394                     break;
395                 case PType.TYPECODE_LONG:
396                     oos.writeLong(pie.pieGetLongIndexField(indexes[i].name));
397                     break;
398                 case PType.TYPECODE_SHORT:
399                     oos.writeShort(pie.pieGetShortIndexField(indexes[i].name));
400                     break;
401                 case PType.TYPECODE_DATE:
402                 case PType.TYPECODE_OBJBYTE:
403                 case PType.TYPECODE_OBJCHAR:
404                 case PType.TYPECODE_OBJINT:
405                 case PType.TYPECODE_OBJLONG:
406                 case PType.TYPECODE_OBJSHORT:
407                 case PType.TYPECODE_STRING:
408                     oos.writeObject(pie.pieGetStringIndexField(indexes[i].name));
409                     break;
410                 default:
411                     throw new PExceptionProtocol("Unauthorized index type: ("
412                                                  + indexes[i].name + ", "
413                                                  + indexes[i].type.getJormName() + ")");
414                 }
415             }
416             // Write the element fields
417
FieldDesc[] elements = getElemFields();
418             switch (elements[0].type.getTypeCode()) {
419             case PType.TYPECODE_BOOLEAN:
420                 oos.writeBoolean(pie.pieGetBooleanElem());
421                 break;
422             case PType.TYPECODE_OBJBOOLEAN:
423                 oos.writeObject(pie.pieGetObooleanElem());
424                 break;
425             case PType.TYPECODE_BYTE:
426                 oos.writeByte(pie.pieGetByteElem());
427                 break;
428             case PType.TYPECODE_OBJBYTE:
429                 oos.writeObject(pie.pieGetObyteElem());
430                 break;
431             case PType.TYPECODE_CHAR:
432                 oos.writeChar(pie.pieGetCharElem());
433                 break;
434             case PType.TYPECODE_OBJCHAR:
435                 oos.writeObject(pie.pieGetOcharElem());
436                 break;
437             case PType.TYPECODE_SHORT:
438                 oos.writeShort(pie.pieGetShortElem());
439                 break;
440             case PType.TYPECODE_OBJSHORT:
441                 oos.writeObject(pie.pieGetOshortElem());
442                 break;
443             case PType.TYPECODE_INT:
444                 oos.writeInt(pie.pieGetIntElem());
445                 break;
446             case PType.TYPECODE_OBJINT:
447                 oos.writeObject(pie.pieGetOintElem());
448                 break;
449             case PType.TYPECODE_LONG:
450                 oos.writeLong(pie.pieGetLongElem());
451                 break;
452             case PType.TYPECODE_OBJLONG:
453                 oos.writeObject(pie.pieGetOlongElem());
454                 break;
455             case PType.TYPECODE_FLOAT:
456                 oos.writeFloat(pie.pieGetFloatElem());
457                 break;
458             case PType.TYPECODE_OBJFLOAT:
459                 oos.writeObject(pie.pieGetOfloatElem());
460                 break;
461             case PType.TYPECODE_DOUBLE:
462                 oos.writeDouble(pie.pieGetDoubleElem());
463                 break;
464             case PType.TYPECODE_OBJDOUBLE:
465                 oos.writeObject(pie.pieGetOdoubleElem());
466                 break;
467             case PType.TYPECODE_DATE:
468                 oos.writeObject(pie.pieGetDateElem());
469                 break;
470             case PType.TYPECODE_CHARARRAY:
471                 oos.writeObject(pie.pieGetCharArrayElem());
472                 break;
473             case PType.TYPECODE_BYTEARRAY:
474                 oos.writeObject(pie.pieGetByteArrayElem());
475                 break;
476             case PType.TYPECODE_SERIALIZED:
477                 oos.writeObject(pie.pieGetSerializedElem());
478                 break;
479             case PType.TYPECODE_BIGDECIMAL:
480                 oos.writeObject(pie.pieGetBigDecimalElem());
481                 break;
482             case PType.TYPECODE_STRING:
483                 oos.writeObject(pie.pieGetStringElem());
484                 break;
485             case PType.TYPECODE_REFERENCE:
486                 PName pn = pie.pieGetRefElem();
487                 oos.writeObject(pn.encodeString());
488                 break;
489             default:
490                 throw new PExceptionProtocol("Unauthorized element type: ("
491                                              + elements[0].name + ", "
492                                              + elements[0].type.getJormName() + ")");
493             }
494         }
495     }
496
497     /* (non-Javadoc)
498      * @see org.objectweb.jorm.api.PClassMapping#getAssociationTable()
499      */

500     public HashMap JavaDoc getAssociationTable() {
501         // TODO Auto-generated method stub
502
return null;
503     }
504
505     /* (non-Javadoc)
506      * @see org.objectweb.jorm.api.PClassMapping#addAssociation(org.objectweb.jorm.api.PClassMapping, int[])
507      */

508     public void addAssociation(PClassMapping targetClass, int[] indexes) {
509         // TODO Auto-generated method stub
510

511     }
512
513     /* (non-Javadoc)
514      * @see org.objectweb.jorm.api.PClassMapping#getIndexesTable(org.objectweb.jorm.api.PClassMapping)
515      */

516     public int[] getIndexesTable(PClassMapping targetClass) {
517         // TODO Auto-generated method stub
518
return null;
519     }
520
521     /* (non-Javadoc)
522      * @see org.objectweb.jorm.api.PClassMapping#resolve(org.objectweb.jorm.naming.api.PName)
523      */

524     public PName resolve(Object JavaDoc conn, PName pname) throws PException {
525         // TODO Auto-generated method stub
526
return null;
527     }
528
529     /* (non-Javadoc)
530      * @see org.objectweb.jorm.api.PClassMapping#match(org.objectweb.jorm.naming.api.PName)
531      */

532     public boolean match(Object JavaDoc obj, boolean b) throws PException{
533         // TODO Auto-generated method stub
534
return false;
535     }
536
537     /* (non-Javadoc)
538      * @see org.objectweb.jorm.api.PClassMapping#getDecodedPName(org.objectweb.medor.tuple.api.TupleCollection, org.objectweb.jorm.naming.api.PName)
539      */

540     public PName getDecodedPName(TupleCollection tc, PName pname, boolean b) throws PException {
541         // TODO Auto-generated method stub
542
return null;
543     }
544
545     /* (non-Javadoc)
546      * @see org.objectweb.jorm.api.PClassMapping#init(org.objectweb.jorm.api.PMappingStructuresManager)
547      */

548     public void init(PMappingStructuresManager pmsm) throws PException {
549         // TODO Auto-generated method stub
550

551     }
552
553     /* (non-Javadoc)
554      * @see org.objectweb.jorm.api.PClassMapping#classDefined(org.objectweb.jorm.api.PMappingStructuresManager)
555      */

556     public void classDefined(PMappingStructuresManager pmsm) throws PException {
557         // TODO Auto-generated method stub
558

559     }
560 }
561
Popular Tags