1 23 24 package org.objectweb.jorm.facility.naming.polymorphid; 25 26 import org.objectweb.jorm.api.*; 27 import org.objectweb.jorm.facility.naming.generator.LongGen; 28 import org.objectweb.jorm.facility.naming.generator.LongGenMgr; 29 import org.objectweb.jorm.facility.naming.generator.LongGenMgrRegistry; 30 import org.objectweb.jorm.facility.naming.basidir.BasidBinder; 31 import org.objectweb.jorm.naming.api.PBinder; 32 import org.objectweb.jorm.naming.api.PNamingContext; 33 import org.objectweb.jorm.naming.api.PBinder; 34 35 import org.objectweb.util.monolog.api.Logger; 36 import org.objectweb.util.monolog.api.BasicLevel; 37 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 41 44 public class PolymorphIdMgrImpl implements PolymorphIdMgr { 45 private static final String POLYMORPHIDBINDERCLASS 46 = "org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdBinderInfo"; 47 private final static String LONGGENMGRCLASS 49 = "org.objectweb.jorm.facility.naming.generator.LongGenIncrMapping"; 50 private HashMap cn2binders = new HashMap (); 51 private HashMap id2binders = new HashMap (); 52 private HashMap refNC = new HashMap (); 53 private LongGenMgr lgManager = null; 54 private LongGen binderLG; 55 56 protected Logger logger = null; 57 58 public void setLogger(Logger l) { 59 logger = l; 60 } 61 62 64 67 public void binderTypeDef(PolymorphIdBinderInfo b) { 68 if (Debug.ON && logger != null) { 69 if (b.getPType() == null) 70 logger.log(BasicLevel.DEBUG, b.getPType().getJormName()); 71 else 72 logger.log(BasicLevel.DEBUG, "Null type of the binder: " + b); 73 } 74 Iterator it = refNC.values().iterator(); 75 while (it.hasNext()) { 76 PolymorphRefNC p = (PolymorphRefNC) it.next(); 77 if (p.getPType() == null) 78 continue; 79 if (Debug.ON && logger != null) 80 logger.log(BasicLevel.DEBUG, "compare to " + p.getPType().getJormName()); 81 if (b.getPType().isa(p.getPType())) { 82 p.addValidBinder(b); 83 if (Debug.ON && logger != null) logger.log(BasicLevel.DEBUG, "added"); 84 } 85 } 86 } 87 88 91 public LongGenMgr getLongGenMgr() { 92 return lgManager; 93 } 94 95 100 public PBinder getPBinder(String cn) throws PException { 101 return getPBinder(cn, null); 102 } 103 104 109 public PBinder getPBinder(String cn, Object conn) throws PException { 110 PolymorphIdBinderInfo res = (PolymorphIdBinderInfo) cn2binders.get(cn); 111 if (res == null) { 112 try { 113 res = (PolymorphIdBinderInfo) Class.forName(POLYMORPHIDBINDERCLASS) 114 .newInstance(); 115 } catch (Exception e) { 116 throw new PException(e, "Cannot allocate the Binder (probably a ClassLoader problem)."); 117 } 118 if (conn == null) 119 res.init(lgManager.getPMapper(), cn, this); 120 else 121 res.init(lgManager.getPMapper(), cn, this, conn); 122 cn2binders.put(res.className, res); 123 id2binders.put(res.classId, res); 124 } 125 return res; 126 } 127 128 133 public PBinder getPBinder(long id) throws PException { 134 return getPBinder(id, null); 135 } 136 137 143 public PBinder getPBinder(long id, Object conn) throws PException { 144 PolymorphIdBinderInfo res = (PolymorphIdBinderInfo) id2binders.get(new Long (id)); 145 if (res == null) { 146 try { 147 res = (PolymorphIdBinderInfo) Class.forName(POLYMORPHIDBINDERCLASS) 148 .newInstance(); 149 } catch (Exception e) { 150 throw new PException(e, 151 "Cannot allocate the Binder (probably a ClassLoader problem)."); 152 } 153 res.init(lgManager.getPMapper(), id, this, conn); 154 cn2binders.put(res.className, res); 155 id2binders.put(res.classId, res); 156 } 157 return res; 158 } 159 160 163 public PMapper getPMapper() { 164 return lgManager.getPMapper(); 165 } 166 167 174 public PNamingContext getRefNC(String cn) { 175 PolymorphRefNC res = (PolymorphRefNC) refNC.get(cn); 176 if (res == null) { 177 res = new PolymorphRefNC(); 178 res.init(this); 179 refNC.put(cn, res); 180 } 181 return res; 182 } 183 184 189 public void init(PMapper pm, byte clact) throws PException { 190 synchronized (LongGenMgrRegistry.class) { 191 lgManager = LongGenMgrRegistry.getLongGenMgr(pm); 192 if (lgManager == null) { 193 String cn = pm.cn2mn(LONGGENMGRCLASS); 194 try { 195 lgManager = (LongGenMgr) Class.forName(cn).newInstance(); 196 } catch (Exception e) { 197 throw new PException(e, "Cannot create LongGenMgr (probably a ClassLoader problem): " + cn); 198 } 199 lgManager.init(pm, clact); 200 LongGenMgrRegistry.registerLonGenMgr(lgManager); 201 } 202 } 203 binderLG = lgManager.getLongGen(this.getClass().getPackage().getName() + 204 "." + this.getClass().getName()); 205 String cn = "??"; 206 try { 207 PBinder binder = new BasidBinder(PNamingContext.CTSTRING); 208 cn = pm.cn2mn(PolymorphIdBinderInfo.CLASSIDN) + PMapper.PCLASSMAPPINGAPPENDER; 209 PClassMapping pcm = (PClassMapping) Class.forName(cn).newInstance(); 210 pcm.setPBinder(binder); 211 binder.setPClassMapping(pcm); 212 pm.map(pcm); 213 PMapCluster cl = pm.getPMapCluster(pcm.getClassName()); 214 switch (clact) { 215 case PClassMapping.CREATE_STRUCTURE_IF_NEEDED: 216 cl.createMappingStructures(false); 217 break; 218 case PClassMapping.CLEANUP_REMOVEALL: 219 cl.deleteMappingStructures(); 220 cl.createMappingStructures(false); 221 break; 222 case PClassMapping.CLEANUP_REMOVEDATA: 223 cl.deleteData(); 224 break; 225 case PClassMapping.CLEANUP_DONOTHING: 226 break; 227 } 228 229 binder = new BasidBinder(PNamingContext.CTLONG); 230 cn = pm.cn2mn(PolymorphIdBinderInfo.IDCLASSN) + PMapper.PCLASSMAPPINGAPPENDER; 231 pcm = (PClassMapping) Class.forName(cn).newInstance(); 232 pcm.setPBinder(binder); 233 binder.setPClassMapping(pcm); 234 pm.map(pcm); 235 cl = pm.getPMapCluster(pcm.getClassName()); 236 switch (clact) { 237 case PClassMapping.CREATE_STRUCTURE_IF_NEEDED: 238 cl.createMappingStructures(false); 239 break; 240 case PClassMapping.CLEANUP_REMOVEALL: 241 cl.deleteMappingStructures(); 242 cl.createMappingStructures(false); 243 break; 244 case PClassMapping.CLEANUP_REMOVEDATA: 245 cl.deleteData(); 246 break; 247 case PClassMapping.CLEANUP_DONOTHING: 248 break; 249 } 250 } catch (InstantiationException e) { 251 throw new PException(e, 252 "Cannot create PClassMapping for [" + cn + "]."); 253 } catch (IllegalAccessException e) { 254 throw new PException(e, 255 "Cannot access PClassMapping class [" + cn + "]."); 256 } catch (ClassNotFoundException e) { 257 throw new PException(e, 258 "Cannot load PClassMapping class [" + cn + "]."); 259 } catch (PException e) { 260 throw e; 261 } 262 } 263 264 267 public long newClassId() throws PException { 268 return binderLG.genId(); 269 } 270 271 public long newClassId(Object conn) throws PException { 272 return binderLG.genId(conn); 273 } 274 275 278 public void ncTypeDef(PolymorphRefNC p) { 279 if (Debug.ON && logger != null) { 280 if (p.getPType() == null) 281 logger.log(BasicLevel.DEBUG, p.getPType().getJormName()); 282 else 283 logger.log(BasicLevel.DEBUG, "Null type of the pnc: " + p); 284 } 285 Iterator it = cn2binders.values().iterator(); 286 while (it.hasNext()) { 287 PolymorphIdBinderInfo b = (PolymorphIdBinderInfo) it.next(); 288 if (b.getPType() == null) 289 continue; 290 if (Debug.ON && logger != null) 291 logger.log(BasicLevel.DEBUG, "compare to " + b.getPType().getJormName()); 292 if (b.getPType().isa(p.getPType())) { 293 p.addValidBinder(b); 294 if (Debug.ON && logger != null) logger.log(BasicLevel.DEBUG, "added"); 295 } 296 } 297 } 298 } 299 | Popular Tags |