1 18 package org.objectweb.speedo.naming.lib; 19 20 import org.objectweb.jorm.api.PClassMapping; 21 import org.objectweb.jorm.api.PException; 22 import org.objectweb.jorm.api.PMapper; 23 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdBinderInfo; 24 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgrImpl; 25 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdPName; 26 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphRefNC; 27 import org.objectweb.jorm.metainfo.api.CompositeName; 28 import org.objectweb.jorm.metainfo.api.CommonClassMapping; 29 import org.objectweb.jorm.metainfo.api.Manager; 30 import org.objectweb.jorm.metainfo.api.MetaObject; 31 import org.objectweb.jorm.metainfo.api.NameDef; 32 import org.objectweb.jorm.metainfo.api.NameRef; 33 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 34 import org.objectweb.jorm.metainfo.api.Reference; 35 import org.objectweb.jorm.metainfo.api.GenClassRef; 36 import org.objectweb.jorm.naming.api.PBinder; 37 import org.objectweb.jorm.naming.api.PName; 38 import org.objectweb.jorm.naming.api.PNameCoder; 39 import org.objectweb.jorm.naming.api.PNamingContext; 40 import org.objectweb.jorm.type.api.PType; 41 import org.objectweb.jorm.type.api.PTypeSpace; 42 import org.objectweb.perseus.cache.api.CacheManager; 43 import org.objectweb.speedo.mapper.api.JormFactory; 44 import org.objectweb.speedo.api.SpeedoException; 45 import org.objectweb.speedo.api.SpeedoProperties; 46 import org.objectweb.speedo.generation.jorm.JormMIMappingBuilder; 47 import org.objectweb.speedo.metadata.SpeedoClass; 48 import org.objectweb.speedo.metadata.SpeedoExtension; 49 import org.objectweb.speedo.metadata.SpeedoIdentity; 50 import org.objectweb.speedo.naming.api.MIBuilderHelper; 51 import org.objectweb.speedo.naming.api.NamingManager; 52 import org.objectweb.speedo.pm.api.ProxyManagerFactory; 53 import org.objectweb.util.monolog.api.Logger; 54 55 import java.util.Collection ; 56 import java.util.Map ; 57 import java.util.Properties ; 58 59 63 public class PolymorphIdNamingManager implements NamingManager { 64 65 69 private final static String POLYMORH_ID_NAME 70 = "org.objectweb.jorm.facility.naming.polymorphid.PolymorphId"; 71 72 private final static String BINDER_CLASS_NAME 73 = POLYMORH_ID_NAME + "BinderInfo"; 74 75 79 private final static String POLYMORH_ID_OID = "objectId"; 80 84 private final static String POLYMORH_ID_CID = "classId"; 85 86 90 protected PolymorphIdMgrImpl cIdManager = null; 91 92 private PMapper mapper; 93 private ProxyManagerFactory pmf; 94 private Logger logger; 95 96 public PolymorphIdMgrImpl getcIdManager() throws PException { 97 if (cIdManager == null) { 98 cIdManager = new PolymorphIdMgrImpl(); 99 cIdManager.setLogger(logger); 100 cIdManager.init(mapper, PClassMapping.CREATE_STRUCTURE_IF_NEEDED); 101 } 102 return cIdManager; 103 } 104 105 public boolean supportPNamingcontext() { 108 return true; 109 } 110 111 public Object encode(PName pn) throws PException { 112 if (pn instanceof PolymorphIdPName) { 113 return pn.getPNameManager().getPType().getJormName() 114 + SEP + pn.encodeString(); 115 } 116 return null; 117 } 118 119 public PName decode(PNameCoder pnc, Object oid, Class clazz, JormFactory jf) throws PException { 120 if (oid instanceof String ) { 121 String stroid = (String ) oid; 122 int idx = stroid.indexOf(SEP); 123 if (pnc != null) { 124 if (pnc instanceof PolymorphIdBinderInfo 125 || pnc instanceof PolymorphRefNC) { 126 if (idx != -1) { 127 return pnc.decodeString(stroid.substring(idx + SEP.length())); 129 } else { 130 return pnc.decodeString(stroid); 132 } 133 134 } else { 135 return null; 137 } 138 } else { 139 if (idx != -1) { 141 String fqcn = stroid.substring(0, idx); 143 ClassLoader cl = getClass().getClassLoader(); 144 if (cl == null) { 145 cl = ClassLoader.getSystemClassLoader(); 146 } 147 try { 148 pnc = jf.getPBinder(fqcn, cl); 149 } catch (Exception e) { 150 } 151 return (pnc instanceof PolymorphIdBinderInfo 152 || pnc instanceof PolymorphRefNC 153 ? pnc.decodeString(stroid.substring(idx + SEP.length())) 154 : null); 155 } else { 156 return null; 158 } 159 } 160 } 161 return null; 162 } 163 164 public void setPMapper(PMapper mapper) { 165 this.mapper = mapper; 166 } 167 168 public void setPmf(ProxyManagerFactory pmf) { 169 170 } 171 172 public void setLogger(Logger logger) { 173 this.logger = logger; 174 } 175 176 public boolean canManage(SpeedoClass sc) { 177 if (sc.identityType == SpeedoIdentity.CONTAINER_ID) { 178 SpeedoExtension se = sc.getExtension( 179 SpeedoProperties.VENDOR_NAME, SpeedoProperties.ID); 180 return se != null && se.value == SpeedoProperties.ID_POLYMORPH_2L; 181 } else { 182 return false; 183 } 184 } 185 186 public boolean canProvidePBinder(Object hints, ClassLoader classLoader) { 187 return BINDER_CLASS_NAME.equals(hints); 188 } 189 190 public boolean canProvidePNamingContext(Object hints, ClassLoader classLoader) { 191 return canProvidePBinder(hints, classLoader); 192 } 193 194 public PBinder getPBinder(String className, 195 String hints, 196 ClassLoader classLoader, 197 byte mappingStructureRule, 198 Map cn2binder, 199 Map cn2pnc) throws PException { 200 return getcIdManager().getPBinder(className); 201 } 202 203 public PNamingContext getPNamingContext(String className, 204 String hints, 205 ClassLoader classLoader, 206 byte mappingStructureRule, 207 Map cn2binder, 208 Map cn2pnc, 209 Manager miManager, 210 PClassMapping pcm) throws PException { 211 return getcIdManager().getRefNC(className); 212 } 213 214 public void fillNameDef(MIBuilderHelper mibh, 215 Manager manager, 216 NameDef nd, 217 SpeedoClass tsc, 218 SpeedoClass ssc, 219 MetaObject mo, 220 Reference ref, 221 CommonClassMapping hcm, 222 JormMIMappingBuilder mb, 223 boolean isIdentifier, 224 boolean isInGenClass, 225 boolean createField, 226 Collection createdMOs) throws SpeedoException, PException { 227 String prefix = mibh.getNameDefFieldPrefix(ref, isIdentifier, isInGenClass); 228 if (!isIdentifier && !isInGenClass && ref instanceof GenClassRef) { 229 prefix = ref.getName() + "_"; 232 } 233 PrimitiveElement oid, cid; 234 String oidname = prefix + "o_id"; 235 String cidname = prefix + "c_id"; 236 if (true || createField) { 237 oid = mibh.createNameDefField(mo, oidname, PTypeSpace.LONG); 238 cid = mibh.createNameDefField(mo, cidname, PTypeSpace.LONG); 239 } else { 240 cid = mibh.getPrimitiveField(mo, cidname); 241 if (cid == null) { 242 throw new SpeedoException( 243 mibh.getErrorMessage(tsc, mo, ref) 244 + " Impossible to get the field '" + cidname + "'"); 245 } 246 oid = mibh.getPrimitiveField(mo, oidname); 247 if (cid == null) { 248 throw new SpeedoException( 249 mibh.getErrorMessage(tsc, mo, ref) 250 + " Impossible to get the field '" + oidname + "'"); 251 } 252 } 253 CompositeName speedoidcn = getPolymorphIdCN(manager); 254 NameRef nr = nd.createNameRef(speedoidcn); 255 nr.addProjection(POLYMORH_ID_OID, oid.getName()); 256 nr.addProjection(POLYMORH_ID_CID, cid.getName()); 257 if (( mb != null) && (true || createField)) { 258 mb.createNameDefMapping(hcm, nd, ssc, isIdentifier, isInGenClass); 259 } 260 } 261 262 private CompositeName getPolymorphIdCN(Manager manager) { 263 CompositeName cn = manager.getCompositeName(POLYMORH_ID_NAME); 264 if (cn == null) { 265 cn = manager.createCompositeName(POLYMORH_ID_NAME); 266 cn.createCompositeNameField(POLYMORH_ID_CID, PTypeSpace.LONG, PType.NOSIZE, PType.NOSIZE); 267 cn.createCompositeNameField(POLYMORH_ID_OID, PTypeSpace.LONG, PType.NOSIZE, PType.NOSIZE); 268 } 269 return cn; 270 } 271 272 public void getJormNamingConfig(NameDef nd, 273 SpeedoClass targetClass, 274 MetaObject sourceMO, 275 String key, 276 Properties result) { 277 result.setProperty(key, BINDER_CLASS_NAME); 278 } 279 280 public String getPNameHints(SpeedoClass sc, NameDef nd) { 281 return "null"; 282 } 283 284 public String getGCPNameHints(SpeedoClass sc, NameDef nd) { 285 return "null"; 286 } 287 288 public NamingManager.NamingField[] getNamingfields(SpeedoClass sc) { 289 return null; 290 } 291 292 public void setCache(CacheManager cache) { 293 } 294 } 295 | Popular Tags |