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.metainfo.api.CompositeName; 23 import org.objectweb.jorm.metainfo.api.GenClassRef; 24 import org.objectweb.jorm.metainfo.api.CommonClassMapping; 25 import org.objectweb.jorm.metainfo.api.Manager; 26 import org.objectweb.jorm.metainfo.api.MetaObject; 27 import org.objectweb.jorm.metainfo.api.NameDef; 28 import org.objectweb.jorm.metainfo.api.NameRef; 29 import org.objectweb.jorm.metainfo.api.Reference; 30 import org.objectweb.jorm.naming.api.PName; 31 import org.objectweb.jorm.naming.api.PNameCoder; 32 import org.objectweb.jorm.type.api.PType; 33 import org.objectweb.medor.filter.lib.ExpressionPrinter; 34 import org.objectweb.speedo.api.SpeedoException; 35 import org.objectweb.speedo.api.SpeedoProperties; 36 import org.objectweb.speedo.generation.jorm.JormMIMappingBuilder; 37 import org.objectweb.speedo.generation.lib.NamingRules; 38 import org.objectweb.speedo.mapper.api.JormFactory; 39 import org.objectweb.speedo.metadata.SpeedoClass; 40 import org.objectweb.speedo.metadata.SpeedoExtension; 41 import org.objectweb.speedo.metadata.SpeedoField; 42 import org.objectweb.speedo.metadata.SpeedoIdentity; 43 import org.objectweb.speedo.naming.api.MIBuilderHelper; 44 import org.objectweb.speedo.naming.api.NamingManager; 45 import org.objectweb.speedo.naming.api.UserId; 46 import org.objectweb.speedo.naming.api.UserIdFactory; 47 import org.objectweb.util.monolog.api.BasicLevel; 48 49 import java.util.ArrayList ; 50 import java.util.Collection ; 51 import java.util.Iterator ; 52 import java.util.Properties ; 53 54 import javax.jdo.JDOUserException; 55 56 60 public class UserIdCompositeNamingManager 61 extends NamingManagerHelper 62 implements NamingManager { 63 64 private final static String COMPOSITE_USER_ID = "cuid"; 65 66 protected String getName() { 67 return COMPOSITE_USER_ID; 68 } 69 70 public boolean canManage(SpeedoClass sc) { 73 if (sc.identityType == SpeedoIdentity.USER_ID && sc.datastoreSequence == null) { 74 ArrayList idFields = new ArrayList (); 75 Iterator fieldsIt = sc.jdoField.values().iterator(); 76 while (fieldsIt.hasNext()) { 77 SpeedoField sp = (SpeedoField) fieldsIt.next(); 78 if (sp.primaryKey) { 79 idFields.add(sp); 80 } 81 } 82 String objectidClass = sc.objectidClass; 83 if (sc.generateObjectId()) { 84 objectidClass = NamingRules.generatedObjectIdName(sc.getFQName()); 85 } 86 return objectidClass != null && objectidClass.length() > 0; 87 } 88 return false; 89 } 90 91 public Object encode(PName pn) throws PException { 92 if (pn instanceof UserIdFactory) { 93 return ((UserIdFactory) pn).getNewUserId(); 94 } 95 return null; 96 } 97 98 public PName decode(PNameCoder pnc, 99 Object oid, 100 Class clazz, 101 JormFactory jf) throws PException { 102 if (oid instanceof UserId) { 103 if (pnc != null) { 105 return pnc.decodeAbstract(oid, null); 107 } else if (((UserId) oid).jdoGetPersistentClassName() != null) { 108 ClassLoader cl = oid.getClass().getClassLoader(); 110 if (cl == null) { 111 cl = jf.getClass().getClassLoader(); 112 } 113 if (cl == null) { 114 cl = ClassLoader.getSystemClassLoader(); 115 } 116 return jf.getPNamingContext( 117 ((UserId) oid).jdoGetPersistentClassName(), cl) 118 .decodeAbstract(oid, null); 119 } 120 } else if (pnc != null 121 && (oid instanceof String ) 122 && (pnc.getNull() instanceof UserIdFactory)) { 123 Class idclass = ((UserIdFactory) pnc.getNull()) 125 .getNewUserId().getClass(); 126 Object o = null; 127 try { 128 o = idclass.getConstructor(new Class []{String .class}) 129 .newInstance(new Object []{(String ) oid}); 130 } catch (Exception e) { 131 throw new JDOUserException("No constructor with a String " + 132 "parameter available on the identifier class: " 133 + idclass.getName()); 134 } 135 return decode(pnc, o, clazz, jf); 137 } 138 139 return null; 140 } 141 142 public boolean canProvidePBinder(Object hints, ClassLoader classLoader) { 143 if(!super.canProvidePBinder(hints, classLoader)) { 144 return false; 145 } 146 String cn = getBinderClassNameFromHints(hints, COMPOSITE_USER_ID); 147 if (cn == null) { 148 return false; 149 } 150 try { 151 classLoader.loadClass(cn); 152 return true; 153 } catch (ClassNotFoundException e) { 154 return false; 155 } 156 } 157 158 public boolean canProvidePNamingContext(Object hints, ClassLoader classLoader) { 159 if(!super.canProvidePNamingContext(hints, classLoader)) { 160 return false; 161 } 162 String cn = getPNCClassNameFromHints(hints, COMPOSITE_USER_ID); 163 if (cn == null) { 164 return false; 165 } 166 try { 167 classLoader.loadClass(getPNCClassNameFromHints(hints, COMPOSITE_USER_ID)); 168 return true; 169 } catch (ClassNotFoundException e) { 170 return false; 171 } 172 } 173 174 public void fillNameDef(MIBuilderHelper mibh, 175 Manager manager, 176 NameDef nd, 177 SpeedoClass tsc, 178 SpeedoClass ssc, 179 MetaObject mo, 180 Reference ref, 181 CommonClassMapping hcm, 182 JormMIMappingBuilder mb, 183 boolean isIdentifier, 184 boolean isInGenClass, 185 boolean createField, 186 Collection createdMOs) throws SpeedoException, PException { 187 boolean debug = logger.isLoggable(BasicLevel.DEBUG); 188 if (debug) { 189 logger.log(BasicLevel.DEBUG, "CompositeUserId: Filling the name def: " 190 + "\n\tsource class=" + (ssc == null ? null : ssc.getFQName()) 191 + "\n\ttarget class=" + (tsc == null ? null : tsc.getFQName()) 192 + "\n\tmo=" + mo 193 + "\n\treference=" + ref 194 + "\n\tisIdentifier=" + isIdentifier 195 + "\n\tisInGenClass=" + isInGenClass 196 + "\n\tcreateField=" + createField); 197 } 198 SpeedoClass clazzWithIdField = tsc; 199 while(clazzWithIdField.superClassName != null) { 200 clazzWithIdField = clazzWithIdField.getSpeedoClassFromContext( 201 clazzWithIdField.superClassName); 202 } 203 ArrayList idFields = new ArrayList (); 204 Iterator fieldsIt = clazzWithIdField.jdoField.values().iterator(); 205 while (fieldsIt.hasNext()) { 206 SpeedoField sf = (SpeedoField) fieldsIt.next(); 207 if (sf.primaryKey) { 208 idFields.add(sf); 209 } 210 } 211 String objectidClass = clazzWithIdField.objectidClass; 212 if (objectidClass == null && tsc.generateObjectId()) { 213 objectidClass = NamingRules.generatedObjectIdName(clazzWithIdField.getFQName()); 214 } 215 CompositeName cn = manager.getCompositeName(objectidClass); 217 boolean cnnotdefined = (cn == null); 218 if (cnnotdefined) { 219 if (debug) { 220 logger.log(BasicLevel.DEBUG, "\tCreate the composite name " + objectidClass); 221 } 222 cn = manager.createCompositeName(objectidClass); 223 createdMOs.add(cn); 224 } 225 NameRef nr = nd.createNameRef(cn); 226 for (int i = 0; i < idFields.size(); i++) { 227 SpeedoField sf = (SpeedoField) idFields.get(i); 228 int size = PType.NOSIZE; 229 SpeedoExtension se = sf.getExtension(SpeedoProperties.VENDOR_NAME, SpeedoProperties.SIZE); 230 if (se != null) { 231 try { 232 size = Integer.parseInt(se.value); 233 } catch (NumberFormatException e) { 234 } 237 } 238 int scale = PType.NOSIZE; 239 se = sf.getExtension(SpeedoProperties.VENDOR_NAME, SpeedoProperties.SCALE); 240 if (se != null) { 241 try { 242 scale = Integer.parseInt(se.value); 243 } catch (NumberFormatException e) { 244 } 247 } 248 if (cnnotdefined) { 249 cn.createCompositeNameField(sf.name, 250 mibh.getPrimitivePType(Type.getType(sf.desc)), 251 size, scale); 252 } 253 String fn = mibh.getNameDefFieldPrefix( 254 ref, isIdentifier, isInGenClass) + sf.name; 255 if (debug) { 256 logger.log(BasicLevel.DEBUG, "\tField name " + fn); 257 } 258 if (createField) { 259 fn = mibh.createNameDefField(mo, fn, 260 mibh.getPrimitivePType(Type.getType(sf.desc)), size, scale).getName(); 261 } 262 nr.addProjection(sf.name, fn); 263 } 264 if (createField && (mb != null)) { 265 mb.createNameDefMapping(hcm, nd, ssc, isIdentifier, isInGenClass); 266 } 267 } 268 269 public String getPNameHints(SpeedoClass sc, NameDef nd) { 270 return "this.jdoReferenceState"; 271 } 272 273 public String getGCPNameHints(SpeedoClass sc, NameDef nd) { 274 return "proxy.getPName()"; 275 } 276 277 286 public void getJormNamingConfig(NameDef nd, 287 SpeedoClass targetClass, 288 MetaObject sourceMO, 289 String key, 290 Properties result) throws SpeedoException { 291 StringBuffer sb = new StringBuffer (); 292 sb.append(COMPOSITE_USER_ID); 293 sb.append(HINTS_SEP); 294 String binderClassName = NamingRules.binderName( 295 nd.getNameRef().getCompositeName().getFQName()); 296 sb.append(binderClassName); 297 sb.append(HINTS_SEP); 298 String className = targetClass.getFQName(); 299 SpeedoClass ancestor = targetClass.getAncestor(); 300 if (sourceMO instanceof GenClassRef || (ancestor == null && targetClass.jormclass.getSubClasses().isEmpty())) { 303 sb.append(binderClassName); 304 } else { 305 if (ancestor == null) { 306 ancestor = targetClass; 307 } 308 try { 309 logger.log(BasicLevel.DEBUG, "Class " + targetClass.jormclass.getFQName() + "\n" 310 + " NamedDef " + nd.toString() + "\n" 311 + " Filter " + ExpressionPrinter.e2str(targetClass.jormclass.getInheritanceFilter(nd)) ); 312 if (targetClass.jormclass.getSubClasses().isEmpty()) { 313 sb.append(binderClassName); 314 } else if (targetClass.jormclass.detectFilterElementNotInPK(targetClass.jormclass.getInheritanceFilter(nd), nd)) { 315 logger.log(BasicLevel.DEBUG, "Composite PName : assign a polymorphic PNC for the class" + targetClass.jormclass.getFQName() + "."); 319 sb.append(POLYMORPHIC_PNC); 320 } else { 321 logger.log(BasicLevel.DEBUG, "Composite PName : assign a KFPNC for the class" + targetClass.jormclass.getFQName() + "."); 323 sb.append(NamingRules.kfpncName(ancestor.getFQName())); 324 } 325 } catch (Exception e) { 326 logger.log(BasicLevel.ERROR, "Error while retrieving the inheritance filter of the namedef:" + nd.toString()); 327 throw new SpeedoException("Error while retrieving the inheritance filter of the namedef:" + nd.toString(), e); 328 } 329 className = ancestor.getFQName(); 330 } 331 sb.append(HINTS_SEP); 332 sb.append(className); 333 result.setProperty(key, sb.toString()); 334 } 335 336 } 337 | Popular Tags |