KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > facility > naming > polymorphid > PolymorphRefNC


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.facility.naming.polymorphid;
25
26 import org.objectweb.jorm.api.PException;
27 import org.objectweb.jorm.naming.api.PExceptionNaming;
28 import org.objectweb.jorm.naming.api.PNCStringCoder;
29 import org.objectweb.jorm.naming.api.PName;
30 import org.objectweb.jorm.naming.api.PNameGetter;
31 import org.objectweb.jorm.naming.api.PNamingContext;
32 import org.objectweb.jorm.naming.api.PExceptionNameCoding;
33 import org.objectweb.jorm.naming.api.PBinder;
34 import org.objectweb.jorm.naming.lib.BasicPNamingContext;
35 import org.objectweb.jorm.naming.lib.BasicStringCoder;
36 import org.objectweb.jorm.type.api.PType;
37
38 import java.util.HashMap JavaDoc;
39
40 /**
41  * @author P. Dechamboux
42  */

43 public class PolymorphRefNC extends BasicPNamingContext
44         implements PNamingContext {
45     private HashMap JavaDoc validBinders = new HashMap JavaDoc(5);
46     private PolymorphIdMgr mgr;
47     PName nullPName = null;
48
49     void init(PolymorphIdMgr mgr) {
50         this.mgr = mgr;
51         nullPName = new PolymorphIdRefN(this,
52                                         (PolymorphIdPName) PolymorphIdPName.nullPName(this));
53
54     }
55
56     void addValidBinder(PolymorphIdBinderInfo b) {
57         if (validBinders.get(b.classId) != null)
58             return;
59         validBinders.put(b.classId, b);
60     }
61
62     // IMPLEMENTATION OF METHODS FROM THE PNamingContext INTERFACE
63

64     public boolean codingSupported(int codingtype) {
65         switch (codingtype) {
66         case PNamingContext.CTCOMPOSITE:
67         case PNamingContext.CTSTRING:
68             return true;
69         }
70         return false;
71     }
72
73     public PName decodeAbstract(Object JavaDoc en, Object JavaDoc context)
74             throws PExceptionNaming, UnsupportedOperationException JavaDoc {
75         long cid, oid;
76         if (en instanceof PolymorphIdPNG) {
77             // there is a specific PNamegetter
78
try {
79                 oid = ((PolymorphIdPNG) en).pnGetObjectId(context);
80                 cid = ((PolymorphIdPNG) en).pnGetClassId(context);
81             } catch (PException e) {
82                 throw new PExceptionNaming(e, "Wrong name structure!!");
83             }
84         } else if (en instanceof PNameGetter) {
85             // we use the generic PNameGetter
86
try {
87                 oid = ((PNameGetter) en).pngetLongField("objectId", context);
88                 cid = ((PNameGetter) en).pngetLongField("classId", context);
89             } catch (PException e) {
90                 throw new PExceptionNaming(e, "Wrong name structure!!");
91             }
92         } else {
93             throw new PExceptionNaming("A PNamegetter is required to decode this object :" + en);
94         }
95         if ((cid == PolymorphIdPName.NULLVALUE) &&
96                 (oid == PolymorphIdPName.NULLVALUE))
97             return nullPName;
98         PBinder b = (PBinder) validBinders.get(new Long JavaDoc(cid));
99         if (b == null) {
100             try {
101                 b = mgr.getPBinder(cid);
102             } catch (PException e) {
103                 throw new PExceptionNaming(e, "Original binder (" + cid
104                                               + ") unavailable in the decoding PolymorphRefNC.");
105             }
106             validBinders.put(new Long JavaDoc(cid), b);
107         }
108         PolymorphIdRefN res = new PolymorphIdRefN(this);
109         res.nameInBinder = new PolymorphIdPName(cid, oid, b);
110         return res;
111     }
112
113     public PName decodeString(String JavaDoc en) throws PExceptionNaming {
114         long cid, oid;
115         try {
116             PNCStringCoder cnsc = new BasicStringCoder(en);
117             oid = cnsc.getLong();
118             cid = cnsc.getLong();
119         } catch (PException e) {
120             throw new PExceptionNaming(e, "Wrong name structure!!");
121         }
122         if ((cid == PolymorphIdPName.NULLVALUE) &&
123                 (oid == PolymorphIdPName.NULLVALUE))
124             return nullPName;
125         PBinder b = (PBinder) validBinders.get(new Long JavaDoc(cid));
126         if (b == null)
127             throw new PExceptionNaming("Original binder unavailable in the decoding PolymorphRefNC.");
128         PolymorphIdRefN res = new PolymorphIdRefN(this);
129         res.nameInBinder = new PolymorphIdPName(cid, oid, b);
130         return res;
131     }
132
133     public Object JavaDoc encodeAbstract(PName pn) throws PExceptionNaming, UnsupportedOperationException JavaDoc {
134         if (!(pn instanceof PolymorphIdRefN))
135             throw new PExceptionNaming(
136                     "This PNamingContext encode only PolymorphIdRefN: " + pn);
137         if (!((pn.getPNameManager()).equals(this)))
138             throw new PExceptionNaming("This pname is not valid in this pnc");
139         return ((PolymorphIdRefN) pn).nameInBinder;
140     }
141
142     public String JavaDoc encodeString(PName pn) throws PExceptionNaming {
143         if (!(pn instanceof PolymorphIdRefN))
144             throw new PExceptionNaming(
145                     "This PNamingContext encode only PolymorphIdRefN: " + pn);
146         if (!((pn.getPNameManager()).equals(this)))
147             throw new PExceptionNaming("This pname is not valid in this pnc");
148         return ((PolymorphIdRefN) pn).nameInBinder.encodeString();
149     }
150
151     public PName export(Object JavaDoc conn, Object JavaDoc infoitem) throws PException {
152         if (!(infoitem instanceof PolymorphIdPName))
153             throw new PExceptionNaming("PolymorphIdRefNC export requires a " +
154                                        "PolymorphIdPName infoitem: " + infoitem);
155         PolymorphIdRefN res = new PolymorphIdRefN(this);
156         res.nameInBinder = (PolymorphIdPName) infoitem;
157         return res;
158     }
159
160     public PName export(Object JavaDoc conn, Object JavaDoc infoitem, Object JavaDoc hints)
161             throws PException {
162         throw new PExceptionNaming("User-info not supported at export time.");
163     }
164
165     public PName getNull() {
166         return nullPName;
167     }
168
169     public void setNullPName(Object JavaDoc o) throws PException {
170         nullPName = (PName) o;
171     }
172
173     public void setPType(PType pt) {
174         super.setPType(pt);
175         mgr.ncTypeDef(this);
176     }
177
178     public PName resolve(Object JavaDoc conn, PName pn) throws PException {
179         if (!(pn instanceof PolymorphIdRefN))
180             throw new PExceptionNaming("Can only resolve PolymorphIdRefN names: " + pn);
181         return ((PolymorphIdRefN) pn).nameInBinder;
182     }
183
184     public boolean supportDynamicComposite() {
185         return true;
186     }
187
188     public boolean supportCompositeField(String JavaDoc fn, PType ft) {
189         return true;
190     }
191
192     public boolean supportStaticComposite() {
193         return true;
194     }
195
196     public void unexport(Object JavaDoc conn, PName pn) throws PException {
197     }
198
199     public void unexport(Object JavaDoc conn, PName pn, Object JavaDoc hints) throws PException {
200     }
201 }
202
Popular Tags