1 20 package org.objectweb.jorm.facility.naming.longid; 21 22 import org.objectweb.jorm.api.PAccessor; 23 import org.objectweb.jorm.api.PBinding; 24 import org.objectweb.jorm.api.PClassMapping; 25 import org.objectweb.jorm.api.PException; 26 import org.objectweb.jorm.api.PExceptionNoDSI; 27 import org.objectweb.jorm.api.PMapper; 28 import org.objectweb.jorm.api.PMapCluster; 29 import org.objectweb.jorm.facility.naming.basidir.BasidBinder; 30 import org.objectweb.jorm.facility.naming.generator.LongGen; 31 import org.objectweb.jorm.facility.naming.generator.LongGenIncrMgr; 32 import org.objectweb.jorm.facility.naming.generator.PLongGen; 33 import org.objectweb.jorm.facility.naming.polymorphid.ClassIdAccessor; 34 import org.objectweb.jorm.facility.naming.polymorphid.IdClassAccessor; 35 import org.objectweb.jorm.naming.api.PBinder; 36 import org.objectweb.jorm.naming.api.PExceptionNaming; 37 import org.objectweb.jorm.naming.api.PNameCoder; 38 import org.objectweb.jorm.naming.api.PNamingContext; 39 40 53 public class LongIdManager { 54 55 58 public final static int DEFAULT_CID_SIZE = 20; 59 60 64 public final static int LONG_SIZE = 64; 65 66 70 public static final String CLASS_ID_NAME = 71 "org.objectweb.jorm.facility.naming.polymorphid.ClassId"; 72 73 77 public static final String ID_CLASS_NAME = 78 "org.objectweb.jorm.facility.naming.polymorphid.IdClass"; 79 80 85 private LongGenIncrMgr idGenMgr; 86 87 90 private LongGen classIdGen; 91 92 95 private PMapper mapper; 96 97 101 private int cidSize = DEFAULT_CID_SIZE; 102 103 107 public LongIdManager() { 108 setCidSize(DEFAULT_CID_SIZE); 109 } 110 111 116 public LongIdManager(PMapper mapper) throws PException { 117 this(mapper, DEFAULT_CID_SIZE); 118 } 119 120 127 public LongIdManager(PMapper mapper, int cidSize) throws PException { 128 setCidSize(cidSize); 129 setPMapper(mapper); 130 } 131 132 public int getCidSize() { 133 return cidSize; 134 } 135 136 public void setCidSize(int cidSize) { 137 this.cidSize = cidSize; 138 } 139 140 public PMapper getMapper() { 141 return mapper; 142 } 143 144 151 public void setPMapper(PMapper mapper) throws PException { 152 if (mapper == null) { 153 throw new PExceptionNaming("No mapper specified"); 154 } 155 this.mapper = mapper; 156 try { 157 idGenMgr = (LongGenIncrMgr) 158 Class.forName(getLongGenMgr()).newInstance(); 159 } catch (Exception e) { 160 throw new PExceptionNaming(e, 161 "Impossible to instanciate the LongGenMgr " 162 + getLongGenMgr()); 163 } 164 idGenMgr.init(mapper, PClassMapping.CREATE_STRUCTURE_IF_NEEDED); 165 classIdGen = idGenMgr.getLongGen(CLASS_ID_NAME, null); 166 167 PBinder binder = new BasidBinder(PNamingContext.CTSTRING); 168 String cn = mapper.cn2mn(CLASS_ID_NAME) + PMapper.PCLASSMAPPINGAPPENDER; 169 PClassMapping pcm; 170 try { 171 pcm = (PClassMapping) Class.forName(cn).newInstance(); 172 } catch (Exception e) { 173 throw new PException(e); 174 } 175 pcm.setPBinder(binder); 176 binder.setPClassMapping(pcm); 177 mapper.map(pcm); 178 PMapCluster cl = mapper.getPMapCluster(pcm.getClassName()); 179 cl.createMappingStructures(false); 180 } 181 182 187 private String getLongGenMgr() { 188 String mn = mapper.getMapperName(); 189 int idx = mn.indexOf('.'); 190 if (idx != -1) { 191 mn = mn.substring(0, idx); 192 } 193 return "org.objectweb.jorm.facility.naming.generator." 194 + mn + ".LongGenIncrMapping"; 195 } 196 197 201 public PBinder newGenClassPBinder() throws PException { 202 return new BasidBinder(PNameCoder.CTLONG, -1); 203 } 204 205 209 public PNamingContext newClassPNamingContext() throws PException { 210 return new LongIdPNC(cidSize); 211 } 212 213 221 public PBinder newClassPBinder(String className, Object conn) throws PException { 222 if (mapper == null) { 223 throw new PException("The LongIdManager has not been configured, a PMapper is required"); 224 } 225 Object connection = conn; 226 if (conn == null) { 227 connection = mapper.getConnection(); 228 } 229 try { 230 LongGen lg = new CompositePLongGen( 231 (PLongGen) idGenMgr.getLongGen(className, connection), 232 getCID(className, connection), 233 cidSize); 234 LongIdBinder lib = new LongIdPBinder(); 235 lib.setACFLid(lg); 236 return lib; 237 } finally { 238 if (conn == null) { 239 mapper.closeConnection(connection); 240 } 241 } 242 } 243 244 245 257 private long getCID(String className, Object conn) throws PException { 258 PClassMapping pcm = mapper.lookup(CLASS_ID_NAME); 259 PBinding binding = pcm.createPBinding(); 260 binding.bind(pcm.getPBinder().decodeString(className)); 261 Object connection = conn; 262 if (conn == null) 263 connection = mapper.getConnection(); 264 CIDAccessor acc = new CIDAccessor(className); 265 try { 266 binding.read(connection, acc); 267 } catch (PExceptionNoDSI e) { 268 binding.export(connection, className); 269 acc.cid = classIdGen.genId(connection); 270 binding.write(connection, acc); 271 } finally { 272 if (conn == null) { 273 mapper.closeConnection(connection); 274 } 275 } 276 binding.unbind(); 277 return acc.cid; 278 } 279 280 285 private class CIDAccessor implements PAccessor, ClassIdAccessor, IdClassAccessor { 286 public long cid; 287 public String className; 288 289 public CIDAccessor(String className) { 290 this.className = className; 291 } 292 293 public CIDAccessor(long cid) { 294 this.cid = cid; 295 } 296 297 public Object getMemoryInstance() { 298 return this; 299 } 300 301 public void paSetClassId(long val) throws PException { 303 cid = val; 304 } 305 306 public long paGetClassId() throws PException { 307 return cid; 308 } 309 310 public void paSetClassName(String val) throws PException { 312 className = val; 313 } 314 315 public String paGetClassName() throws PException { 316 return className; 317 } 318 } 319 } 320 | Popular Tags |