1 18 package org.objectweb.speedo.naming.lib; 19 20 import org.objectweb.asm.Type; 21 import org.objectweb.jorm.api.PException; 22 import org.objectweb.jorm.facility.naming.basidir.BasidBinder; 23 import org.objectweb.jorm.facility.naming.basidir.BasidName; 24 import org.objectweb.jorm.metainfo.api.Class; 25 import org.objectweb.jorm.metainfo.api.GenClassRef; 26 import org.objectweb.jorm.metainfo.api.CommonClassMapping; 27 import org.objectweb.jorm.metainfo.api.Manager; 28 import org.objectweb.jorm.metainfo.api.MetaObject; 29 import org.objectweb.jorm.metainfo.api.NameDef; 30 import org.objectweb.jorm.metainfo.api.Reference; 31 import org.objectweb.jorm.metainfo.api.TypedElement; 32 import org.objectweb.jorm.naming.api.PBinder; 33 import org.objectweb.jorm.naming.api.PExceptionNaming; 34 import org.objectweb.jorm.naming.api.PName; 35 import org.objectweb.jorm.naming.api.PNameCoder; 36 import org.objectweb.jorm.naming.lib.CTHelper; 37 import org.objectweb.jorm.type.api.PType; 38 import org.objectweb.speedo.api.SpeedoException; 39 import org.objectweb.speedo.api.SpeedoProperties; 40 import org.objectweb.speedo.generation.jorm.JormMIMappingBuilder; 41 import org.objectweb.speedo.generation.lib.NamingRules; 42 import org.objectweb.speedo.mapper.api.JormFactory; 43 import org.objectweb.speedo.metadata.SpeedoClass; 44 import org.objectweb.speedo.metadata.SpeedoExtension; 45 import org.objectweb.speedo.metadata.SpeedoField; 46 import org.objectweb.speedo.metadata.SpeedoIdentity; 47 import org.objectweb.speedo.naming.api.MIBuilderHelper; 48 import org.objectweb.speedo.naming.api.NamingManager; 49 import org.objectweb.util.monolog.api.BasicLevel; 50 51 import java.util.ArrayList ; 52 import java.util.Collection ; 53 import java.util.Date ; 54 import java.util.Iterator ; 55 import java.util.Map ; 56 import java.util.Properties ; 57 58 63 public class UserIdSingleNamingManager 64 extends NamingManagerHelper 65 implements NamingManager { 66 67 private final static String SINGLE_USER_ID = "suid"; 68 69 private int getCodingType(String fn, MetaObject mo) { 70 TypedElement te = null; 71 if (mo instanceof Class ) { 72 te = ((Class ) mo).getTypedElement(fn); 73 } else if (mo instanceof GenClassRef) { 74 te = ((GenClassRef) mo).getHiddenField(fn); 75 } else { 76 return -1; 77 } 78 return CTHelper.ptc2ct(te.getType().getTypeCode()); 79 } 80 81 protected String getName() { 82 return SINGLE_USER_ID; 83 } 84 87 public Object encode(PName pn) throws PException { 88 if (pn instanceof BasidName) { 89 return pn.getPNameManager().getPType().getJormName() 90 + SEP + pn.encodeString(); 91 } 92 return null; 93 } 94 95 public PName decode(PNameCoder pnc, 96 Object oid, 97 java.lang.Class clazz, 98 JormFactory jf) throws PException { 99 if (oid instanceof String ) { 100 String stroid = (String ) oid; 101 int idx = stroid.indexOf(SEP); 102 if (pnc != null) { 103 if (pnc.codingSupported(PNameCoder.CTCOMPOSITE)) { 104 return null; 106 } 107 if (idx != -1) { 108 return pnc.decodeString(stroid.substring(idx + SEP.length())); 110 } else { 111 return pnc.decodeString(stroid); 113 } 114 } 115 if (idx == -1) { 117 return null; 119 } 120 String fqcn = stroid.substring(0, idx); 122 ClassLoader cl = jf.getClass().getClassLoader(); 123 if (cl == null) { 124 cl = ClassLoader.getSystemClassLoader(); 125 } 126 try { 127 pnc = jf.getPNamingContext(fqcn, cl); 128 } catch (Exception e) { 129 return null; 130 } 131 if (pnc == null 132 || pnc.codingSupported(PNameCoder.CTCOMPOSITE)) { 133 return null; 134 } 135 return pnc.decodeString(stroid.substring(idx + SEP.length())); 136 } else if (pnc instanceof BasidBinder) { 137 switch (((BasidBinder) pnc).getCodingType()) { 138 case PNameCoder.CTCHAR: 139 return pnc.decodeChar(((Character ) oid).charValue()); 140 case PNameCoder.CTBYTE: 141 return pnc.decodeByte(((Byte ) oid).byteValue()); 142 case PNameCoder.CTSHORT: 143 return pnc.decodeShort(((Short ) oid).shortValue()); 144 case PNameCoder.CTINT: 145 return pnc.decodeInt(((Integer ) oid).intValue()); 146 case PNameCoder.CTLONG: 147 return pnc.decodeLong(((Long ) oid).longValue()); 148 case PNameCoder.CTOCHAR: 149 return pnc.decodeOchar((Character ) oid); 150 case PNameCoder.CTOBYTE: 151 return pnc.decodeObyte((Byte ) oid); 152 case PNameCoder.CTOSHORT: 153 return pnc.decodeOshort((Short ) oid); 154 case PNameCoder.CTOINT: 155 return pnc.decodeOint((Integer ) oid); 156 case PNameCoder.CTOLONG: 157 return pnc.decodeOlong((Long ) oid); 158 case PNameCoder.CTSTRING: 159 return pnc.decodeString((String ) oid); 160 case PNameCoder.CTDATE: 161 return pnc.decodeDate((Date ) oid); 162 case PNameCoder.CTCHARARRAY: 163 return pnc.decodeCharArray((char[]) oid); 164 case PNameCoder.CTBYTEARRAY: 165 return pnc.decode((byte[]) oid); 166 default: 167 throw new PExceptionNaming("Unmanaged coding type: " 168 + ((BasidBinder) pnc).getCodingType()); 169 } 170 } else { 171 return null; 173 } 174 } 175 176 public boolean canManage(SpeedoClass sc) { 177 if (sc.identityType == SpeedoIdentity.USER_ID && sc.datastoreSequence == null) { 178 ArrayList idFields = new ArrayList (); 179 Iterator fieldsIt = sc.jdoField.values().iterator(); 180 while (fieldsIt.hasNext()) { 181 SpeedoField sp = (SpeedoField) fieldsIt.next(); 182 if (sp.primaryKey) { 183 idFields.add(sp); 184 } 185 } 186 String objectidClass = sc.objectidClass; 187 if (sc.generateObjectId()) { 188 objectidClass = NamingRules.generatedObjectIdName(sc.getFQName()); 189 } 190 return objectidClass == null || objectidClass.length() == 0; 191 } 192 return false; 193 } 194 195 public boolean canProvidePBinder(Object hints, ClassLoader classLoader) { 196 if(!super.canProvidePBinder(hints, classLoader)) { 197 return false; 198 } 199 String ctStr = getBinderClassNameFromHints(hints, SINGLE_USER_ID); 200 if (ctStr == null) { 201 return false; 202 } 203 try { 204 Short.parseShort(ctStr); 205 return true; 206 } catch (NumberFormatException e) { 207 return false; 208 } 209 } 210 211 public boolean canProvidePNamingContext(Object hints, ClassLoader classLoader) { 212 return getPNCClassNameFromHints(hints, SINGLE_USER_ID) != null; 213 } 214 215 public PBinder getPBinder(String className, String hints, ClassLoader cl, byte mappingStructureRule, Map cn2binder, Map cn2pnc) { 216 String ctStr = getBinderClassNameFromHints(hints, SINGLE_USER_ID); 217 return new BasidBinder(Short.parseShort(ctStr)); 218 } 219 220 223 public void getJormNamingConfig(NameDef nd, 224 SpeedoClass targetClass, 225 MetaObject sourceMO, 226 String key, 227 Properties result) throws SpeedoException { 228 StringBuffer sb = new StringBuffer (); 229 sb.append(SINGLE_USER_ID); 230 sb.append(HINTS_SEP); 231 int ct = getCodingType(nd.getFieldName(), sourceMO); 232 sb.append(ct); 233 sb.append(HINTS_SEP); 234 String className = targetClass.getFQName(); 235 SpeedoClass ancestor = targetClass.getAncestor(); 236 if (sourceMO instanceof GenClassRef || (ancestor == null && targetClass.jormclass.getSubClasses().isEmpty())) { 239 sb.append(ct); 240 } else { 241 if (ancestor == null) { 242 ancestor = targetClass; 243 } 244 try { 245 if (targetClass.jormclass.getSubClasses().isEmpty()) { 246 sb.append(ct); 247 } else if (targetClass.jormclass.detectFilterElementNotInPK(targetClass.jormclass.getInheritanceFilter(nd), nd)) { 248 sb.append(POLYMORPHIC_PNC); 252 } else { 253 sb.append(NamingRules.kfpncName(ancestor.getFQName())); 255 } 256 } catch (Exception e) { 257 logger.log(BasicLevel.ERROR, "Error while retrieving the inheritance filter of the namedef:" + nd.toString()); 258 throw new SpeedoException("Error while retrieving the inheritance filter of the namedef:" + nd.toString(), e); 259 } 260 className = ancestor.getFQName(); 261 } 262 sb.append(HINTS_SEP); 263 sb.append(className); 264 result.setProperty(key, sb.toString()); 265 } 266 267 public String getPNameHints(SpeedoClass sc, NameDef nd) { 268 String fn = nd.getFieldName(); 270 int tc = sc.jormclass.getTypedElement(fn).getType().getTypeCode(); 271 fn = "jdoReferenceState." + fn; 272 switch (tc) { 273 case PType.TYPECODE_CHAR: 274 return "new Character(" + fn + ")"; 275 case PType.TYPECODE_BYTE: 276 return "new Byte(" + fn + ")"; 277 case PType.TYPECODE_SHORT: 278 return "new Short(" + fn + ")"; 279 case PType.TYPECODE_INT: 280 return "new Integer(" + fn + ")"; 281 case PType.TYPECODE_LONG: 282 return "new Long(" + fn + ")"; 283 default: 284 return fn; 285 } 286 } 287 288 public String getGCPNameHints(SpeedoClass sc, NameDef nd) { 289 return "proxy.getPName()"; 290 } 291 292 public void fillNameDef(MIBuilderHelper mibh, 293 Manager manager, 294 NameDef nd, 295 SpeedoClass tsc, 296 SpeedoClass ssc, 297 MetaObject mo, 298 Reference ref, 299 CommonClassMapping hcm, 300 JormMIMappingBuilder mb, 301 boolean isIdentifier, 302 boolean isInGenClass, 303 boolean createField, 304 Collection createdMOs) throws SpeedoException, PException { 305 ArrayList idFields = new ArrayList (); 306 Iterator fieldsIt = tsc.jdoField.values().iterator(); 307 while (fieldsIt.hasNext()) { 308 SpeedoField sp = (SpeedoField) fieldsIt.next(); 309 if (sp.primaryKey) { 310 idFields.add(sp); 311 } 312 } 313 if (idFields.size() == 0) { 314 throw new SpeedoException( 315 mibh.getErrorMessage(tsc, mo, ref) 316 + "no primary key field and no object id defined"); 317 } 318 if (idFields.size() > 1) { 319 String fn = "["; 320 String sep = ""; 321 for (int i = 0; i < idFields.size(); i++) { 322 fn += sep + ((SpeedoField) idFields.get(i)).name; 323 sep = ","; 324 } 325 fn += "]"; 326 throw new SpeedoException( 327 mibh.getErrorMessage(tsc, mo, ref) 328 + "there are several primary key fields " 329 + fn 330 + " and no an object id class."); 331 } 332 SpeedoField sf = (SpeedoField) idFields.get(0); 333 String clafn = mibh.getNameDefFieldPrefix( 334 ref, isIdentifier, isInGenClass) + sf.name; 335 if (createField) { 336 int size = PType.NOSIZE; 337 SpeedoExtension se = sf.getExtension(SpeedoProperties.VENDOR_NAME,SpeedoProperties.SIZE); 338 if (se != null) { 339 try { 340 size = Integer.parseInt(se.value); 341 } catch (NumberFormatException e) { 342 } 345 } 346 int scale = PType.NOSIZE; 347 se = sf.getExtension(SpeedoProperties.VENDOR_NAME,SpeedoProperties.SCALE); 348 if (se != null) { 349 try { 350 scale = Integer.parseInt(se.value); 351 } catch (NumberFormatException e) { 352 } 355 } 356 mibh.createNameDefField(mo, clafn, 357 mibh.getPrimitivePType(Type.getType(sf.desc)), size, scale); 358 } 359 nd.setFieldName(clafn); 360 if (createField && (mb != null)) { 361 mb.createNameDefMapping(hcm, nd, ssc, isIdentifier, isInGenClass); 362 } 363 } 364 } 365 | Popular Tags |