1 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 ; 39 40 43 public class PolymorphRefNC extends BasicPNamingContext 44 implements PNamingContext { 45 private HashMap validBinders = new HashMap (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 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 en, Object context) 74 throws PExceptionNaming, UnsupportedOperationException { 75 long cid, oid; 76 if (en instanceof PolymorphIdPNG) { 77 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 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 (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 (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 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 (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 encodeAbstract(PName pn) throws PExceptionNaming, UnsupportedOperationException { 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 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 conn, Object 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 conn, Object infoitem, Object 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 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 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 fn, PType ft) { 189 return true; 190 } 191 192 public boolean supportStaticComposite() { 193 return true; 194 } 195 196 public void unexport(Object conn, PName pn) throws PException { 197 } 198 199 public void unexport(Object conn, PName pn, Object hints) throws PException { 200 } 201 } 202 | Popular Tags |