KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.jorm.naming.lib;
19
20 import org.objectweb.jorm.naming.api.PName;
21 import org.objectweb.jorm.naming.api.PBinder;
22 import org.objectweb.jorm.naming.api.PNameCoder;
23 import org.objectweb.jorm.naming.api.FilteredPNamingContext;
24 import org.objectweb.jorm.naming.api.PNameGetterConverter;
25 import org.objectweb.jorm.naming.api.PExceptionNaming;
26 import org.objectweb.jorm.api.PException;
27
28 import java.util.Date JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.math.BigInteger JavaDoc;
31 import java.math.BigDecimal JavaDoc;
32
33 /**
34  * This class is abstract implementation of the FilteredPNamingContext interface.
35  * The aim is to manage a tree of naming contexts. At each node a filter is able
36  * to say if a pname structure matches to the type represented by the
37  * PNamingContext. To use this class it is required
38  * - to override the right match and decodeXXX methods in according to the
39  * PARENT coding type,
40  * - to override the right encodeXXX methods in according to the coding type,
41  * - to implement the encodeString and decodeString method
42  *
43  * Example of overriding of a decodeXXX method:
44  * ============================================
45  * public PName decodeAbstract(Object en) throws PExceptionNaming {
46  * //check coding type asked with the parent coding type
47  * if (parentCodingType != CTCOMPOSITE) {
48  * throw new PExceptionNaming("Decoding a composite name is not supported: " + en);
49  * }
50  *
51  * //obtain a PName with the 'en' parameter
52  * PName pn;
53  * if (en instanceof PName) {
54  * pn = (PName) en
55  * } else if (en instanceof ID1ID2PNG) {
56  * pn = new ID1ID2PName(this,
57  * ((ID1ID2PNG) en).pnGetId1(),
58  * ((ID1ID2PNG) en).pnGetId1());
59  * } else if (en instanceof PNameGetter) {
60  * pn = new ID1ID2PName(this,
61  * ((PNameGetter) en).pnGetString("id1"),
62  * ((PNameGetter) en).pnGetLong("id2") );
63  * } else {
64  * throw new PExceptionNaming("Impossible to decode this parameter: " + en);
65  * }
66  *
67  * // resolve the PName
68  * try {
69  * return resolve(null, pn);
70  * } catch (PException e) {
71  * throw new PExceptionNaming(e, "Impossible to resolve " + en);
72  * }
73  * }
74  *
75  * Example of overriding of a match method:
76  * ========================================
77  * public boolean match(Object en) throws PExceptionNaming {
78  * if (parentCodingType != CTCOMPOSITE) {
79  * throw new PExceptionNaming("Decoding a composite name is not supported: " + en);
80  * }
81  * String id1 = ((FooPNG) en).getId1();
82  * long id2 = ((FooPNG) en).getId2();
83  * return id2 > 0 && "kangaroo".equals(id1)
84  * }
85  *
86  *
87  * Example of overriding of a encodeXXX method:
88  * ============================================
89  * public Object encodeAbstract(PName pn) throws PExceptionNaming {
90  * return pn; //The PName is a PNameGetter
91  * }
92  *
93  *
94  * @author S.Chassande-Barrioz
95  * @author P.Dechamboux
96  */

97 public abstract class AbstractInheritFilteredPNamingContext
98     extends BasicPNamingContext
99     implements FilteredPNamingContext {
100
101     /**
102      * The list of sub PNameCoderSelector
103      * element type: PNameCoderSelector
104      */

105     private FilteredPNamingContext[] fpncs = new FilteredPNamingContext[0];
106
107     /**
108      * The Binder associated to the current type. A null value means the type is
109      * abstract.
110      */

111     protected PBinder binder;
112
113     /**
114      * The codingType of the current PNamingContext
115      */

116     protected int codingType;
117
118     /**
119      * the null PName
120      */

121     private PName nullPName;
122
123     protected PNameGetterConverter converter;
124
125     public AbstractInheritFilteredPNamingContext() {
126     }
127
128     public AbstractInheritFilteredPNamingContext(int codingType) {
129         this.codingType = codingType;
130     }
131
132     public AbstractInheritFilteredPNamingContext(int codingType, PNameGetterConverter converter) {
133         this.codingType = codingType;
134         this.converter = converter;
135     }
136
137     public PBinder getBinder() {
138         return binder;
139     }
140
141     public void setBinder(PBinder binder) {
142         this.binder = binder;
143     }
144
145     public void setCodingType(int codingType) {
146         this.codingType = codingType;
147     }
148
149     public PNameGetterConverter getConverter() {
150         return converter;
151     }
152
153     public void setConverter(PNameGetterConverter converter) {
154         this.converter = converter;
155     }
156
157     public synchronized void bindSubFPNC(FilteredPNamingContext fpnc) {
158         FilteredPNamingContext[] newfpncs = new FilteredPNamingContext[fpncs.length + 1];
159         System.arraycopy(fpncs, 0, newfpncs, 0, fpncs.length);
160         newfpncs[fpncs.length] = fpnc;
161         fpncs = newfpncs;
162     }
163
164     public synchronized void unbindSubFPNC(FilteredPNamingContext fpnc) {
165         int j = Arrays.binarySearch(fpncs, fpnc);
166         if (j == -1) {
167             //The PNC is not bound
168
return;
169         }
170         FilteredPNamingContext[] newfpncs = new FilteredPNamingContext[fpncs.length - 1];
171         if (j > 0) {
172             //Copy the begin
173
System.arraycopy(fpncs, 0, newfpncs, 0, j);
174         }
175         if (j < newfpncs.length) {
176             //Copy the end
177
System.arraycopy(fpncs, j + 1, newfpncs, j, fpncs.length - j);
178         }
179         fpncs = newfpncs;
180     }
181
182     public boolean codingSupported(int codingtype) {
183         return codingtype == codingType || codingtype == PNameCoder.CTSTRING;
184     }
185
186     public PName getNull() {
187         return nullPName;
188     }
189
190     public void setNullPName(Object JavaDoc o) throws PException {
191         nullPName = (PName) o;
192     }
193
194     public PName resolve(Object JavaDoc conn, PName pn) throws PException {
195         int i = fpncs.length - 1;
196         switch (codingType) {
197         case CTCOMPOSITE: {
198             Object JavaDoc o = pn.encodeAbstract();
199             while(i>=0 && !fpncs[i].match(o)) {
200                 i--;
201             }
202             if (i>=0) {
203                 return fpncs[i].decodeAbstract(o, null);
204             } else if (binder != null) {
205                 return binder.decodeAbstract(o, null);
206             } else {
207                 return pn;
208             }
209         }
210         case CTCHAR:{
211             char o = pn.encodeChar();
212             while(i>=0 && !fpncs[i].match(o)) {
213                 i--;
214             }
215             if (i>=0) {
216                 return fpncs[i].decodeChar(o);
217             } else if (binder != null) {
218                 return binder.decodeChar(o);
219             } else {
220                 return pn;
221             }
222         }
223
224         case CTOCHAR: {
225             Character JavaDoc o = pn.encodeOchar();
226             while(i>=0 && !fpncs[i].match(o)) {
227                 i--;
228             }
229             if (i>=0) {
230                 return fpncs[i].decodeOchar(o);
231             } else if (binder != null) {
232                 return binder.decodeOchar(o);
233             } else {
234                 return pn;
235             }
236         }
237
238         case CTBYTE: {
239             byte o = pn.encodeByte();
240             while(i>=0 && !fpncs[i].match(o)) {
241                 i--;
242             }
243             if (i>=0) {
244                 return fpncs[i].decodeByte(o);
245             } else if (binder != null) {
246                 return binder.decodeByte(o);
247             } else {
248                 return pn;
249             }
250         }
251
252         case CTOBYTE: {
253             Byte JavaDoc o = pn.encodeObyte();
254             while(i>=0 && !fpncs[i].match(o)) {
255                 i--;
256             }
257             if (i>=0) {
258                 return fpncs[i].decodeObyte(o);
259             } else if (binder != null) {
260                 return binder.decodeObyte(o);
261             } else {
262                 return pn;
263             }
264         }
265
266         case CTSHORT: {
267             short o = pn.encodeShort();
268             while(i>=0 && !fpncs[i].match(o)) {
269                 i--;
270             }
271             if (i>=0) {
272                 return fpncs[i].decodeShort(o);
273             } else if (binder != null) {
274                 return binder.decodeShort(o);
275             } else {
276                 return pn;
277             }
278         }
279
280         case CTOSHORT: {
281             Short JavaDoc o = pn.encodeOshort();
282             while(i>=0 && !fpncs[i].match(o)) {
283                 i--;
284             }
285             if (i>=0) {
286                 return fpncs[i].decodeOshort(o);
287             } else if (binder != null) {
288                 return binder.decodeOshort(o);
289             } else {
290                 return pn;
291             }
292         }
293
294         case CTINT: {
295             int o = pn.encodeInt();
296             while(i>=0 && !fpncs[i].match(o)) {
297                 i--;
298             }
299             if (i>=0) {
300                 return fpncs[i].decodeInt(o);
301             } else if (binder != null) {
302                 return binder.decodeInt(o);
303             } else {
304                 return pn;
305             }
306         }
307
308         case CTOINT: {
309             Integer JavaDoc o = pn.encodeOint();
310             while(i>=0 && !fpncs[i].match(o)) {
311                 i--;
312             }
313             if (i>=0) {
314                 return fpncs[i].decodeOint(o);
315             } else if (binder != null) {
316                 return binder.decodeOint(o);
317             } else {
318                 return pn;
319             }
320         }
321
322         case CTLONG: {
323             long o = pn.encodeLong();
324             while(i>=0 && !fpncs[i].match(o)) {
325                 i--;
326             }
327             if (i>=0) {
328                 return fpncs[i].decodeLong(o);
329             } else if (binder != null) {
330                 return binder.decodeLong(o);
331             } else {
332                 return pn;
333             }
334         }
335
336         case CTOLONG: {
337             Long JavaDoc o = pn.encodeOlong();
338             while(i>=0 && !fpncs[i].match(o)) {
339                 i--;
340             }
341             if (i>=0) {
342                 return fpncs[i].decodeOlong(o);
343             } else if (binder != null) {
344                 return binder.decodeOlong(o);
345             } else {
346                 return pn;
347             }
348         }
349
350         case CTSTRING: {
351             String JavaDoc o = pn.encodeString();
352             while(i>=0 && !fpncs[i].match(o)) {
353                 i--;
354             }
355             if (i>=0) {
356                 return fpncs[i].decodeString(o);
357             } else if (binder != null) {
358                 return binder.decodeString(o);
359             } else {
360                 return pn;
361             }
362         }
363
364         case CTBYTEARRAY: {
365             byte[] o = pn.encode();
366             while(i>=0 && !fpncs[i].match(o)) {
367                 i--;
368             }
369             if (i>=0) {
370                 return fpncs[i].decode(o);
371             } else if (binder != null) {
372                 return binder.decode(o);
373             } else {
374                 return pn;
375             }
376         }
377
378         case CTCHARARRAY: {
379             char[] o = pn.encodeCharArray();
380             while(i>=0 && !fpncs[i].match(o)) {
381                 i--;
382             }
383             if (i>=0) {
384                 return fpncs[i].decodeCharArray(o);
385             } else if (binder != null) {
386                 return binder.decodeCharArray(o);
387             } else {
388                 return pn;
389             }
390         }
391
392         case CTDATE: {
393             Date JavaDoc o = pn.encodeDate();
394             while(i>=0 && !fpncs[i].match(o)) {
395                 i--;
396             }
397             if (i>=0) {
398                 return fpncs[i].decodeDate(o);
399             } else if (binder != null) {
400                 return binder.decodeDate(o);
401             } else {
402                 return pn;
403             }
404         }
405
406         case CTBIGDECIMAL: {
407             BigDecimal JavaDoc o = pn.encodeBigDecimal();
408             while(i>=0 && !fpncs[i].match(o)) {
409                 i--;
410             }
411             if (i>=0) {
412                 return fpncs[i].decodeBigDecimal(o);
413             } else if (binder != null) {
414                 return binder.decodeBigDecimal(o);
415             } else {
416                 return pn;
417             }
418         }
419
420         case CTBIGINTEGER: {
421             BigInteger JavaDoc o = pn.encodeBigInteger();
422             while(i>=0 && !fpncs[i].match(o)) {
423                 i--;
424             }
425             if (i>=0) {
426                 return fpncs[i].decodeBigInteger(o);
427             } else if (binder != null) {
428                 return binder.decodeBigInteger(o);
429             } else {
430                 return pn;
431             }
432         }
433
434         default:
435             return getNull();
436         }
437     }
438
439     public PName export(Object JavaDoc conn, Object JavaDoc infoitem) throws PException {
440         return export(conn, infoitem, null);
441     }
442
443     public PName export(Object JavaDoc conn, Object JavaDoc infoitem, Object JavaDoc hints)
444         throws PException {
445         if (infoitem instanceof PName) {
446             return (PName) infoitem;
447         } else {
448             return decodeAbstract(infoitem, hints);
449         }
450     }
451
452     public void unexport(Object JavaDoc conn, PName pn) throws PException {
453         //nothing to do
454
}
455
456     public void unexport(Object JavaDoc conn, PName pn, Object JavaDoc hints) throws PException {
457         //nothing to do
458
}
459
460     public String JavaDoc encodeString(PName pn) throws PExceptionNaming {
461         throw new UnsupportedOperationException JavaDoc("String coding not supported");
462     }
463 }
464
Popular Tags