1 23 24 package org.objectweb.jorm.facility.naming.polymorphid; 25 26 import org.objectweb.jorm.api.PAccessor; 27 import org.objectweb.jorm.api.PBinding; 28 import org.objectweb.jorm.api.PClassMapping; 29 import org.objectweb.jorm.api.PException; 30 import org.objectweb.jorm.api.PMapper; 31 import org.objectweb.jorm.api.PBindingCtrl; 32 import org.objectweb.jorm.api.PStateGraph; 33 import org.objectweb.jorm.api.PExceptionProtocol; 34 import org.objectweb.jorm.api.PExceptionNoDSI; 35 import org.objectweb.jorm.type.api.PType; 36 import org.objectweb.jorm.facility.naming.generator.LongGen; 37 import org.objectweb.jorm.naming.api.PName; 38 import org.objectweb.jorm.naming.api.PExceptionNaming; 39 import org.objectweb.jorm.naming.api.PExceptionExistingName; 40 import org.objectweb.perseus.cache.api.CacheEntry; 41 import org.objectweb.perseus.cache.api.CacheException; 42 43 51 public class PolymorphIdBinderInfo 52 extends PolymorphIdBinder 53 implements PAccessor, ClassIdAccessor, IdClassAccessor { 54 public static final String CLASSIDN = 55 "org.objectweb.jorm.facility.naming.polymorphid.ClassId"; 56 public static final String IDCLASSN = 57 "org.objectweb.jorm.facility.naming.polymorphid.IdClass"; 58 Long classId; 59 String className; 60 private PolymorphIdMgr mgr; 61 private LongGen objectIdGen; 62 63 68 void init(PMapper m, String cn, PolymorphIdMgr mgr) throws PException { 69 Object conn = mgr.getPMapper().getConnection(); 70 try { 71 init(m, cn, mgr, conn); 72 } catch (PException e) { 73 throw e; 74 } finally { 75 mgr.getPMapper().closeConnection(conn); 76 } 77 } 78 79 84 void init(PMapper m, String cn, PolymorphIdMgr mgr, Object conn) throws PException { 85 this.mgr = mgr; 86 className = cn; 87 PClassMapping pcm = m.lookup(CLASSIDN); 88 PBinding binding = pcm.createPBinding(); 89 objectIdGen = mgr.getLongGenMgr().getLongGen(className); 90 binding.bind(pcm.getPBinder().decodeString(className)); 91 Object connection = conn; 92 if (connection == null) 93 connection = mgr.getPMapper().getConnection(); 94 try { 95 binding.read(connection, this); 96 } catch (PExceptionNoDSI e) { 97 binding.export(connection, className); 98 classId = new Long (mgr.newClassId(connection)); 99 binding.write(connection, this); 100 PClassMapping pcm2 = m.lookup(IDCLASSN); 101 PBinding binding2 = pcm2.createPBinding(); 102 binding2.bind(pcm2.getPBinder().decodeLong(classId.longValue())); 103 if (!binding2.exist(connection)) { 104 binding2.export(connection, classId); 105 binding2.write(connection, this); 106 } 107 binding2.unbind(); 108 } 109 binding.unbind(); 110 } 111 112 125 void init(PMapper m, long id, PolymorphIdMgr mgr, Object conn) 126 throws PException { 127 Object connection = conn; 128 if (connection == null) 129 connection = mgr.getPMapper().getConnection(); 130 try { 131 this.mgr = mgr; 132 PClassMapping pcm = m.lookup(IDCLASSN); 133 PBinding binding = pcm.createPBinding(); 134 binding.bind(pcm.getPBinder().decodeLong(id)); 135 try { 136 binding.read(connection, this); 137 } catch (PExceptionNoDSI e) { 138 throw new PExceptionNaming(e, "The binder " + id + " does not exist"); 139 } finally { 140 binding.unbind(); 141 } 142 objectIdGen = mgr.getLongGenMgr().getLongGen(className); 143 } catch (PException e) { 144 throw e; 145 } finally { 146 mgr.getPMapper().closeConnection(connection); 147 } 148 } 149 150 151 153 public String getClassName() { 154 return className; 155 } 156 157 public void setPType(PType pt) { 158 super.setPType(pt); 159 mgr.binderTypeDef(this); 160 } 161 162 public PName export(Object conn, Object binding) throws PException { 163 PBindingCtrl pb = (PBindingCtrl) binding; 164 byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(), 165 PBinding.ACTION_EXPORT); 166 if (nextstate == PBinding.LIFECYCLE_ERROR) 167 throw new PExceptionProtocol("Unauthorized operation"); 168 PName res = new PolymorphIdPName(classId.longValue(), 170 objectIdGen.genId(conn), this); 171 if (bindingcache != null) { 172 synchronized (bindingcache) { 173 if (bindingcache.lookup(res) != null) { 174 throw new PExceptionExistingName( 175 "[" + getClassName() + "]: an object has been already export with the same identifier"); 176 } 177 try { 178 CacheEntry ce = bindingcache.bind(res, binding); 179 bindingcache.fix(ce); 180 } catch (CacheException e) { 181 throw new PException(e, "[" + getClassName() + "]: problem with cache management"); 182 } 183 } 184 } 185 pb.setPName(res); 186 pb.setStatus(nextstate); 187 return res; 188 } 189 190 public PName export(Object conn, Object binding, Object ctxt) 191 throws PException { 192 throw new PExceptionNaming("Supports only auto-generated names (no need of a user context)."); 193 } 194 195 197 public Object getMemoryInstance() { 198 return this; 199 } 200 201 203 public void paSetClassId(long val) throws PException { 205 classId = new Long (val); 206 } 207 208 public long paGetClassId() throws PException { 209 return classId.longValue(); 210 } 211 212 214 public void paSetClassName(String val) throws PException { 216 className = val; 217 } 218 219 public String paGetClassName() throws PException { 220 return className; 221 } 222 } 223 | Popular Tags |