KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > naming > lib > BasicPolymorphicPNamingContext


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
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  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  */

25
26 package org.objectweb.jorm.naming.lib;
27
28 import java.math.BigDecimal JavaDoc;
29 import java.math.BigInteger JavaDoc;
30 import java.util.Date JavaDoc;
31
32 import org.objectweb.jorm.api.PBinding;
33 import org.objectweb.jorm.api.PClassMapping;
34 import org.objectweb.jorm.api.PException;
35 import org.objectweb.jorm.genclass.lib.GenClassMapping;
36 import org.objectweb.jorm.naming.api.PBinder;
37 import org.objectweb.jorm.naming.api.PExceptionNaming;
38 import org.objectweb.jorm.naming.api.PName;
39 import org.objectweb.jorm.naming.api.PNameManager;
40 import org.objectweb.jorm.naming.api.PolymorphicPName;
41 import org.objectweb.jorm.naming.api.PolymorphicPNamingContext;
42 import org.objectweb.jorm.type.api.PType;
43 import org.objectweb.medor.tuple.api.Tuple;
44 import org.objectweb.medor.tuple.api.TupleCollection;
45 import org.objectweb.perseus.cache.api.CacheEntry;
46 import org.objectweb.perseus.cache.api.CacheManager;
47 import org.objectweb.perseus.persistence.api.ConnectionHolder;
48 import org.objectweb.perseus.persistence.api.WorkingSet;
49
50 /**
51  * This class is used to encode/decode asymetric PNames.
52  * It is linked to a delegated Binder to which all the encode/decode calls are delegated.
53  * It is linked to the cache to perform lookup on resolve(PName) and decodeXX().
54  * It is liked to a PClassMapping to delegate the resolve call in case the PName is not in the cache.
55  * @author Y.Bersihand
56  */

57 public class BasicPolymorphicPNamingContext implements PolymorphicPNamingContext {
58
59     /**
60      * the delegated binder to which all the encode/decode calls are delegated.
61      */

62     private PBinder delegatedBinder;
63     
64     /**
65      * the PClassMapping to delegate the resolve call.
66      */

67     private PClassMapping pcm;
68     
69     /**
70      * the cache to lookup PNames
71      */

72     private CacheManager cache;
73     
74     /**
75      * the delegated PNameManager: the one originally bound to the PName
76      */

77     private PNameManager delegatedPNM;
78     
79     public BasicPolymorphicPNamingContext() {
80         delegatedBinder = null;
81         pcm = null;
82         cache = null;
83         delegatedPNM = null;
84     }
85     
86     public BasicPolymorphicPNamingContext(PBinder delegatedBinder,
87                                             PClassMapping pcm,
88                                             CacheManager cache,
89                                             PNameManager delegatedPNM) {
90         super();
91         this.delegatedBinder = delegatedBinder;
92         this.pcm = pcm;
93         this.cache = cache;
94         this.delegatedPNM = delegatedPNM;
95     }
96         
97     //////////////////////////////////////
98
// AsymetricPNContext INTERFACE //
99
//////////////////////////////////////
100

101     public CacheManager getCache() {
102         return cache;
103     }
104     public void setCache(CacheManager cache) {
105         this.cache = cache;
106     }
107     public PBinder getDelegatedBinder() {
108         return delegatedBinder;
109     }
110     public void setDelegatedBinder(PBinder delegatedBinder) {
111         this.delegatedBinder = delegatedBinder;
112     }
113     public PClassMapping getPcm() {
114         return pcm;
115     }
116     public void setPcm(PClassMapping pcm) {
117         this.pcm = pcm;
118     }
119     
120     /////////////////////////////////
121
// PNContext INTERFACE //
122
/////////////////////////////////
123

124     public PName resolve(Object JavaDoc conn, PName pname) throws PException {
125         //if the pname is linked to a PBinder, returns it
126
if(pname.getPNameManager() instanceof PBinder) {
127             return pname;
128         } else {
129             try {
130                 //checks in the cache
131
PName pnFound = checkCache(pname);
132                 //if the pnFound from the cache is not the same than the parameter
133
//returns the pnFound
134
if (pnFound != pname)
135                     return pnFound;
136                 if (!(conn instanceof ConnectionHolder))
137                     throw new PException("The conn parameter of PolymorphicPNC.resolve(conn, pname) must be an instance of ConnectionHolder");
138                 ConnectionHolder connHolder = (ConnectionHolder) conn;
139                 //get the tx to register the prefetch buffer
140
WorkingSet ws = (WorkingSet) connHolder.getWorkingSet();
141                 //check the prefetch cache
142
TupleCollection tupleCollection = pcm.getPMapper().getPrefetchCache().getPrefetchTupleCollection(pname, pcm, ws);
143                 if (tupleCollection != null && tupleCollection.getMetaData() != null) {
144                     return pcm.getDecodedPName(tupleCollection, pname, false);
145                 }
146                 //else, delegates to the PClassMapping
147
return pcm.resolve(conn, pname);
148             } catch (Exception JavaDoc e) {
149                 throw new PException(e, "Exception while trying to resolve the pname in the BasicAsymetricPNC. " + e.getMessage());
150             }
151         }
152     }
153     
154     public PName export(Object JavaDoc conn, Object JavaDoc infoitem) throws PException {
155         //delegates to the binder
156
return delegatedBinder.export(conn, infoitem);
157     }
158
159     public PName export(Object JavaDoc conn, Object JavaDoc infoitem, Object JavaDoc hints) throws PException {
160         //delegates to the binder
161
return delegatedBinder.export(conn, infoitem, hints);
162     }
163
164     public void unexport(Object JavaDoc conn, PName pn) throws PException {
165         //delegates to the binder
166
delegatedBinder.unexport(conn, pn);
167     }
168
169     public void unexport(Object JavaDoc conn, PName pn, Object JavaDoc hints) throws PException {
170         //delegates to the binder
171
delegatedBinder.unexport(conn, pn, hints);
172     }
173     
174     //////////////////////////////
175
// PNCoder INTERFACE //
176
//////////////////////////////
177

178     public boolean codingSupported(int codingtype) {
179         //delegates to the binder
180
return delegatedBinder.codingSupported(codingtype);
181     }
182
183     public PName decode(byte[] en) throws PExceptionNaming {
184         //delegates to the binder
185
PName pname = delegatedBinder.decode(en);
186         return checkCache(markAsPolymorphic(pname));
187     }
188
189     public PName decodeAbstract(Object JavaDoc en, Object JavaDoc context)
190             throws PExceptionNaming, UnsupportedOperationException JavaDoc {
191         //delegates to the binder
192
PName pname = delegatedBinder.decodeAbstract(en, context);
193         return checkCache(markAsPolymorphic(pname));
194     }
195
196     public PName decodeByte(byte en) throws PExceptionNaming,
197             UnsupportedOperationException JavaDoc {
198         ///delegates to the binder
199
PName pname = delegatedBinder.decodeByte(en);
200         return checkCache(markAsPolymorphic(pname));
201     }
202
203     public PName decodeObyte(Byte JavaDoc en) throws PExceptionNaming,
204             UnsupportedOperationException JavaDoc {
205         //delegates to the binder
206
PName pname = delegatedBinder.decodeObyte(en);
207         return checkCache(markAsPolymorphic(pname));
208     }
209
210     public PName decodeChar(char en) throws PExceptionNaming,
211             UnsupportedOperationException JavaDoc {
212         //delegates to the binder
213
PName pname = delegatedBinder.decodeChar(en);
214         return checkCache(markAsPolymorphic(pname));
215     }
216
217     public PName decodeOchar(Character JavaDoc en) throws PExceptionNaming,
218             UnsupportedOperationException JavaDoc {
219         //delegates to the binder
220
PName pname = delegatedBinder.decodeOchar(en);
221         return checkCache(markAsPolymorphic(pname));
222     }
223
224     public PName decodeInt(int en) throws PExceptionNaming,
225             UnsupportedOperationException JavaDoc {
226         //delegates to the binder
227
PName pname = delegatedBinder.decodeInt(en);
228         return checkCache(markAsPolymorphic(pname));
229     }
230
231     public PName decodeOint(Integer JavaDoc en) throws PExceptionNaming,
232             UnsupportedOperationException JavaDoc {
233         //delegates to the binder
234
PName pname = delegatedBinder.decodeOint(en);
235         return checkCache(markAsPolymorphic(pname));
236     }
237
238     public PName decodeLong(long en) throws PExceptionNaming,
239             UnsupportedOperationException JavaDoc {
240         //delegates to the binder
241
PName pname = delegatedBinder.decodeLong(en);
242         return checkCache(markAsPolymorphic(pname));
243     }
244
245     public PName decodeOlong(Long JavaDoc en) throws PExceptionNaming,
246             UnsupportedOperationException JavaDoc {
247         //delegates to the binder
248
PName pname = delegatedBinder.decodeOlong(en);
249         return checkCache(markAsPolymorphic(pname));
250     }
251
252     public PName decodeShort(short en) throws PExceptionNaming,
253             UnsupportedOperationException JavaDoc {
254         //delegates to the binder
255
PName pname = delegatedBinder.decodeShort(en);
256         return checkCache(markAsPolymorphic(pname));
257     }
258
259     public PName decodeOshort(Short JavaDoc en) throws PExceptionNaming,
260             UnsupportedOperationException JavaDoc {
261         //delegates to the binder
262
PName pname = delegatedBinder.decodeOshort(en);
263         return checkCache(markAsPolymorphic(pname));
264     }
265
266     public PName decodeString(String JavaDoc en) throws PExceptionNaming {
267         //delegates to the binder
268
PName pname = delegatedBinder.decodeString(en);
269         return checkCache(markAsPolymorphic(pname));
270     }
271
272     public PName decodeCharArray(char[] en) throws PExceptionNaming {
273         //delegates to the binder
274
PName pname = delegatedBinder.decodeCharArray(en);
275         return checkCache(markAsPolymorphic(pname));
276     }
277
278     public PName decodeDate(Date JavaDoc en) throws PExceptionNaming {
279         //delegates to the binder
280
PName pname = delegatedBinder.decodeDate(en);
281         return checkCache(markAsPolymorphic(pname));
282     }
283
284     public PName decodeBigInteger(BigInteger JavaDoc en) throws PExceptionNaming {
285         //delegates to the binder
286
PName pname = delegatedBinder.decodeBigInteger(en);
287         return checkCache(markAsPolymorphic(pname));
288     }
289
290     public PName decodeBigDecimal(BigDecimal JavaDoc en) throws PExceptionNaming {
291         //delegates to the binder
292
PName pname = delegatedBinder.decodeBigDecimal(en);
293         return checkCache(markAsPolymorphic(pname));
294     }
295
296     public byte[] encode(PName pn) throws PExceptionNaming {
297         //delegates to the binder
298
return delegatedBinder.encode(pn);
299     }
300
301     public Object JavaDoc encodeAbstract(PName pn) throws PExceptionNaming,
302             UnsupportedOperationException JavaDoc {
303         //delegates to the binder
304
return delegatedBinder.encodeAbstract(pn);
305     }
306
307     public byte encodeByte(PName pn) throws PExceptionNaming,
308             UnsupportedOperationException JavaDoc {
309         //delegates to the binder
310
return delegatedBinder.encodeByte(pn);
311     }
312
313     public Byte JavaDoc encodeObyte(PName pn) throws PExceptionNaming,
314             UnsupportedOperationException JavaDoc {
315         //delegates to the binder
316
return delegatedBinder.encodeObyte(pn);
317     }
318
319     public char encodeChar(PName pn) throws PExceptionNaming,
320             UnsupportedOperationException JavaDoc {
321         //delegates to the binder
322
return delegatedBinder.encodeChar(pn);
323     }
324
325     public Character JavaDoc encodeOchar(PName pn) throws PExceptionNaming,
326             UnsupportedOperationException JavaDoc {
327         //delegates to the binder
328
return delegatedBinder.encodeOchar(pn);
329     }
330
331     public int encodeInt(PName pn) throws PExceptionNaming,
332             UnsupportedOperationException JavaDoc {
333         //delegates to the binder
334
return delegatedBinder.encodeInt(pn);
335     }
336
337     public Integer JavaDoc encodeOint(PName pn) throws PExceptionNaming,
338             UnsupportedOperationException JavaDoc {
339         //delegates to the binder
340
return delegatedBinder.encodeOint(pn);
341     }
342
343     public long encodeLong(PName pn) throws PExceptionNaming,
344             UnsupportedOperationException JavaDoc {
345         //delegates to the binder
346
return delegatedBinder.encodeLong(pn);
347     }
348
349     public Long JavaDoc encodeOlong(PName pn) throws PExceptionNaming,
350             UnsupportedOperationException JavaDoc {
351         //delegates to the binder
352
return delegatedBinder.encodeOlong(pn);
353     }
354
355     public short encodeShort(PName pn) throws PExceptionNaming,
356             UnsupportedOperationException JavaDoc {
357         //delegates to the binder
358
return delegatedBinder.encodeShort(pn);
359     }
360
361     public Short JavaDoc encodeOshort(PName pn) throws PExceptionNaming,
362             UnsupportedOperationException JavaDoc {
363         //delegates to the binder
364
return delegatedBinder.encodeOshort(pn);
365     }
366
367     public String JavaDoc encodeString(PName pn) throws PExceptionNaming {
368         //delegates to the binder
369
return delegatedBinder.encodeString(pn);
370     }
371
372     public char[] encodeCharArray(PName pn) throws PExceptionNaming {
373         //delegates to the binder
374
return delegatedBinder.encodeCharArray(pn);
375     }
376
377     public Date JavaDoc encodeDate(PName pn) throws PExceptionNaming {
378         //delegates to the binder
379
return delegatedBinder.encodeDate(pn);
380     }
381
382     public BigInteger JavaDoc encodeBigInteger(PName pn) throws PExceptionNaming {
383         //delegates to the binder
384
return delegatedBinder.encodeBigInteger(pn);
385     }
386
387     public BigDecimal JavaDoc encodeBigDecimal(PName pn) throws PExceptionNaming {
388         //delegates to the binder
389
return delegatedBinder.encodeBigDecimal(pn);
390     }
391
392     public PName getNull() {
393         //delegates to the binder
394
return delegatedBinder.getNull();
395     }
396
397     public void setNullPName(Object JavaDoc o) throws PException {
398         //delegates to the binder
399
delegatedBinder.setNullPName(o);
400     }
401
402     public boolean supportDynamicComposite() {
403         //delegates to the binder
404
return delegatedBinder.supportDynamicComposite();
405     }
406
407     public boolean supportCompositeField(String JavaDoc fn, PType ft) {
408         //delegates to the binder
409
return delegatedBinder.supportCompositeField(fn,ft);
410     }
411
412     public boolean supportStaticComposite() {
413         //delegates to the binder
414
return delegatedBinder.supportStaticComposite();
415     }
416
417     public PType getPType() {
418         //delegates to the binder
419
PType type = delegatedBinder.getPType();
420         if (type == null) {
421             type = pcm.getPType();
422             delegatedBinder.setPType(type);
423         }
424         return type;
425     }
426
427     public void setPType(PType pt) {
428         //delegates to the binder
429
delegatedBinder.setPType(pt);
430     }
431
432     
433     //////////////////////////////////////
434
// PRIVATE METHODS //
435
//////////////////////////////////////
436

437     /**
438      * If a "valid" pname is already in the cache, returns this valid pname,
439      * else return the pname parameter.
440      * The "valid" pname is :
441      * - present in the cache,
442      * - not an instance of GenClassMapping
443      * - linked to a PClassMapping which is part of the inheritance graph
444      */

445     private PName checkCache(PName pname) throws PExceptionNaming {
446         PolymorphicPName polymorphicPN = (PolymorphicPName) pname;
447         CacheEntry ce = cache.lookup(pname);
448         if (ce != null) {
449             //get the "valid" pname
450
PName pnFound = (PName) ce.getCeIdentifier();
451             PBinding pbinding = (PBinding) ce.getCeObject();
452             //if the cache entry is linked to a gen pcm, it is not the good one
453
PClassMapping pcmFound = pbinding.getPClassMapping();
454             if ( pcmFound instanceof GenClassMapping) {
455                 return pname;
456             } else {
457                 try {
458                     PClassMapping[] subPCMs = pcm.getSubPCMs();
459                     //checks that the pcm of the cache entry is part of the inheritance graph
460
//TODO: check for the parents???
461
if (subPCMs != null) {
462                         for (int i= 0; i<subPCMs.length; i++) {
463                             if (pcmFound == subPCMs[i]) {
464                                 //this pname found in the cache is the good one
465
return pnFound;
466                             }
467                         }
468                     }
469                     //the pname found is linked to a pcm which is not part
470
//of the inheritance graph, so returns the pname
471
return pname;
472                 } catch (Exception JavaDoc e) {
473                     throw new PExceptionNaming(e,
474                             "Exception on decode, trying to get all the sub PClassMappings of "
475                             + pcm.getClassName() + "." + "Exception nested: " + e.getMessage());
476                 }
477             }
478         } else {
479             //if there is no entry in the cache, returns the pname
480
return pname;
481         }
482     }
483     
484     private PolymorphicPName markAsPolymorphic(PName pn) {
485         PolymorphicPName polymorphicPN = (PolymorphicPName) pn;
486         polymorphicPN.setPolymorphic(true);
487         polymorphicPN.setPNameManager((PNameManager)this);
488         return polymorphicPN;
489     }
490 }
491
Popular Tags