1 27 package org.objectweb.speedo.wizard.generation; 28 29 import org.objectweb.jorm.api.PException; 30 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator; 31 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 32 import org.objectweb.jorm.compiler.lib.JormCompiler; 33 import org.objectweb.jorm.metainfo.api.Class; 34 import org.objectweb.jorm.metainfo.api.ClassProject; 35 import org.objectweb.jorm.metainfo.api.CompositeName; 36 import org.objectweb.jorm.metainfo.api.GenClassRef; 37 import org.objectweb.jorm.metainfo.api.Manager; 38 import org.objectweb.jorm.metainfo.api.Mapping; 39 import org.objectweb.jorm.metainfo.api.NameDef; 40 import org.objectweb.jorm.metainfo.api.TypedElement; 41 import org.objectweb.jorm.util.io.lib.DirJavaExplorer; 42 import org.objectweb.speedo.api.ExceptionHelper; 43 import org.objectweb.speedo.api.SpeedoException; 44 import org.objectweb.speedo.api.SpeedoProperties; 45 import org.objectweb.speedo.generation.api.SpeedoXMLError; 46 import org.objectweb.speedo.generation.jorm.JormMIBuilder; 47 import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent; 48 import org.objectweb.speedo.metadata.SpeedoClass; 49 import org.objectweb.speedo.metadata.SpeedoField; 50 import org.objectweb.speedo.metadata.SpeedoIdentity; 51 import org.objectweb.speedo.metadata.SpeedoModifier; 52 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor; 53 import org.objectweb.util.monolog.api.BasicLevel; 54 55 import java.io.File ; 56 import java.util.ArrayList ; 57 import java.util.Collection ; 58 import java.util.Iterator ; 59 import java.util.List ; 60 import java.util.Set ; 61 62 public class JormWriter extends AbstractGeneratorComponent { 63 64 public final static String LOGGER_NAME = 65 SpeedoProperties.LOGGER_NAME + "wizard.generation"; 66 67 70 protected JormCompiler jormcompiler; 71 72 73 76 public boolean init() throws SpeedoException { 77 logger = scp.loggerFactory.getLogger(LOGGER_NAME); 78 jormcompiler = new JormCompiler(); 79 JormCompilerParameter jcp = jormcompiler.getCompilerParameter(); 80 JormCompilerConfigurator jcc = jormcompiler.getCompilerConfigurator(); 81 try { 82 logger.log(BasicLevel.DEBUG, "JormConfigurator: configure() -- get jorm.mapper.list"); 83 jcc.configure(); 84 logger.log(BasicLevel.DEBUG, "JormCompilerParameter: loadConfFile()"); 85 jcp.loadConfFile(jcc.getGlobalJormcOptsFile(), jcc.knownMappers()); 86 jcc.setLoggerFactory(scp.loggerFactory); 87 jcp.setProjectName(scp.projectName); 88 jcp.setBindingInheritance("%p%c"); 89 jcp.setOutput(scp.pdoutput); 90 jcp.setBindingAbstract(true); 91 jcp.setDtdLocations(scp.dtdLocations); 92 jcp.setInputFiles(scp.jorm); 93 DirJavaExplorer pe = new DirJavaExplorer(); 94 pe.setLogger(scp.loggerFactory.getLogger("org.objectweb.jorm.io.pathexplorer")); 95 pe.addPath(scp.jormclasspath); 96 jcp.setClasspath(pe); 97 jcp.setGeneratedWithMapperPackage(false); 98 if ((scp.projectName == null) || (scp.mapperName == null)) 99 return true; 100 int idx = scp.mapperName.indexOf('.'); 101 if (idx == -1) { 102 jcc.removeMapper(scp.mapperName); 103 jcc.addSubMapper(scp.mapperName, "generic"); 104 } else { 105 String actualmn = scp.mapperName.substring(0,idx); 106 jcc.removeMapper(actualmn); 107 jcc.addSubMapper(actualmn, scp.mapperName.substring(idx + 1)); 108 } 109 Collection c = jcc.getSubMappers("rdb"); 110 logger.log(BasicLevel.DEBUG, "jormcOpts file:" + jcc.getJormcOptsFile()); 111 logger.log(BasicLevel.DEBUG, "rdb sub mappers: " + c); 112 113 } catch (PException e) { 114 logger.log(BasicLevel.ERROR, 115 "Impossible to configure Jorm", ExceptionHelper.getNested(e)); 116 throw new SpeedoException("Impossible to configure Jorm", e); 117 } 118 return true; 119 } 120 121 124 public void process() throws SpeedoException { 125 126 try { 127 Collection c = jormcompiler.getCompilerParameter().getInputFiles(); 129 if ((c != null) && (logger.isLoggable(BasicLevel.DEBUG))) 130 for (Iterator it = c.iterator(); it.hasNext();) 131 logger.log(BasicLevel.DEBUG, "source file: " + it.next()); 132 ArrayList notFoundMOs = new ArrayList (); 133 for (Iterator itDesc = scp.getXmldescriptor().values().iterator(); itDesc.hasNext();) { 136 SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next(); 137 List scs = desc.getSpeedoClasses(); 138 for (int i=0; i<scs.size(); i++) { 139 SpeedoClass sc = (SpeedoClass) scs.get(i); 140 String fn = sc.getJormFileName(); 141 logger.log(BasicLevel.DEBUG, 142 "looking for the file: " + 143 scp.jormDir + File.separatorChar + fn); if ((c == null) || (!c.contains(fn))) { notFoundMOs.add(sc); 146 } 147 } 148 } 149 150 jormcompiler.setupLogger(); 153 jormcompiler.setupMIManager(); 154 Collection createdMOs = null; 155 if (!notFoundMOs.isEmpty()) { 156 logger.log(BasicLevel.DEBUG, "JormWriter: projectname: <" + scp.projectName + 159 "> mappername: " + scp.mapperName); 160 createdMOs = new JormMIBuilder( 161 jormcompiler.getMIManager(), scp.nmf, logger) 162 .createMI(notFoundMOs, scp.projectName, scp.mapperName); 163 } 164 165 if (c != null) { c = new ArrayList (jormcompiler.parseFiles(c)); 170 } 172 if (createdMOs != null && !createdMOs.isEmpty()) { 175 jormcompiler.generateJormFiles(createdMOs); 176 } 177 if (c != null) { c.addAll(createdMOs); 179 } 180 else { 181 c = createdMOs; 182 } 183 184 if (logger.isLoggable(BasicLevel.DEBUG)) 185 for (Iterator it = c.iterator(); it.hasNext();) { 186 Object o = it.next(); 187 if (o instanceof Class ) { 188 logger.log(BasicLevel.DEBUG, "Class : " 189 + ((Class ) o).getFQName()); 190 } else if (o instanceof CompositeName) { 191 logger.log(BasicLevel.DEBUG, "compositeName : " 192 + ((CompositeName) o).getFQName()); 193 } else 194 logger.log(BasicLevel.DEBUG, "metaObject : " + o); 195 } 196 197 } catch (PException e) { 200 throw new SpeedoException("Error during .pd files generation", e); 201 } 202 } 203 204 209 protected void isCompatible() throws SpeedoException { 210 211 List except = new ArrayList (); 212 213 Manager manager = jormcompiler.getMIManager(); 214 for (Iterator itDesc = scp.getXmldescriptor().values().iterator(); itDesc.hasNext();) { 215 SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next(); 216 List scs = desc.getSpeedoClasses(); 217 for (int i=0; i<scs.size(); i++) { 218 compareClass((SpeedoClass) scs.get(i), manager, except); 219 } 220 } 221 if (!except.isEmpty() && logger.isLoggable(BasicLevel.ERROR)) 222 for (Iterator it = except.iterator(); it.hasNext();) 223 logger.log(BasicLevel.ERROR, it.next()); 224 } 225 226 protected void compareClass(SpeedoClass clas, 227 Manager manager, 228 List except) throws SpeedoException { 229 debug = logger.isLoggable(BasicLevel.DEBUG); 230 if (debug) { 231 logger.log(BasicLevel.DEBUG, 232 "Verify the definition of the class " + clas.getFQName()); 233 } 234 Class classJorm = manager.getClass(clas.getFQName()); 235 if (classJorm == null) { 236 throw new SpeedoException("Jorm description of the class '" 237 + clas.getFQName() + "' is not availlable"); 238 } 239 NameDef pName = null; 240 SpeedoClass sc = clas; 241 while(sc.superClassName != null) { 242 SpeedoClass _sc = sc; 243 sc = sc.jdoPackage.jdoXMLDescriptor.smi.getSpeedoClass(sc.superClassName, sc.jdoPackage); 244 if (sc == null) { 245 throw new SpeedoException("Class " + _sc.getFQName() + " not defined in the jorm meta information"); 246 } 247 } 248 Class pclassJorm = manager.getClass(sc.getFQName()); 249 pName = getClassNameDef(pclassJorm); 250 251 if (classJorm == null) { 252 throw new SpeedoXMLError("Class '" + clas.name + "' not defined in JORM metadata"); 253 } 254 255 clas.jormclass = classJorm; 257 for (Iterator efield = clas.jdoField.values().iterator(); efield.hasNext();) { 258 SpeedoField field = (SpeedoField) efield.next(); 259 String fieldName = field.name; 260 261 if (field.persistenceModifier == SpeedoModifier.none) 263 continue; 264 265 TypedElement tElem = classJorm.getTypedElement(fieldName); 266 if (tElem == null) { 267 throw new SpeedoXMLError("Field '" + fieldName 268 + "' not defined in JORM metadata of the class '" 269 + clas.getFQName() + "'"); 270 } 271 272 if (field.primaryKey) { 274 boolean found = false; 275 for (Iterator it = pName.iterateField(); it.hasNext() && !found;) { 276 found = fieldName.equals(it.next()); 277 } 278 if (!found) { 279 throw new SpeedoXMLError("Field '" + fieldName 280 + "' not defined in Class NameDef's Fields of the class '" 281 + clas.getFQName() + "'"); 282 } 283 } 284 285 if (tElem instanceof GenClassRef && field.jdoTuple == null) 286 throw new SpeedoXMLError("field '" + fieldName + "' should be a tuple in JDO metadata of the class '" + clas.getFQName() + "'"); 287 } 288 289 for (Iterator jormfield = classJorm.getFields().iterator(); jormfield.hasNext();) { 291 TypedElement te = (TypedElement) jormfield.next(); 292 String fieldName = te.getName(); 293 if (!clas.jdoField.containsKey(fieldName) 294 && !isContainerIdField(classJorm, te, clas)) 295 throw new SpeedoXMLError("Field '" + fieldName + "' of the class '" 296 + clas.getFQName() + "' is not defined in the '" 297 + clas.jdoPackage.jdoXMLDescriptor.xmlFile 298 + "' file (found: " + clas.jdoField.keySet() + ")."); 299 } 300 301 if (clas.identityType == SpeedoIdentity.USER_ID) { 303 boolean found = false; 304 for (Iterator it = pName.iterateField(); it.hasNext() && !found;) { 305 String fn = (String ) it.next(); 306 SpeedoField sf = (SpeedoField) clas.jdoField.get(fn); 307 if (sf == null) { 308 logger.log(BasicLevel.WARN, "Field " + fn + " defined in the identifier of " 309 + clas.name 310 + ".pd file but not availlable in the class (or .pd file)."); 311 } else if (!sf.primaryKey) { 312 except.add("Field " + fn + " defined in the " 313 + clas.name 314 + ".pd file but is not marked as a primary key field in the " 315 + clas.jdoPackage.jdoXMLDescriptor.xmlFile 316 + " file."); 317 } 318 } 319 } 320 } 321 322 private boolean isContainerIdField(Class clazz, 323 TypedElement te, 324 SpeedoClass sc) throws SpeedoException { 325 return sc.identityType == SpeedoIdentity.CONTAINER_ID 326 && getClassNameDef(clazz).getNameRef() 327 .getProjection().containsValue(te.getName()); 328 } 329 330 331 private Mapping getMapping(Class clazz) throws SpeedoException { 332 ClassProject cp = clazz.getClassProject(scp.projectName); 333 if (cp == null) { 334 throw new SpeedoException("No classproject found for the class " 335 + clazz.getFQName() + " and the project " + scp.projectName); 336 } 337 int idx = scp.mapperName.indexOf('.'); 338 Mapping m = cp.getMapping(idx == -1 339 ? scp.mapperName 340 : scp.mapperName.substring(0, idx)); 341 if (m == null) { 342 throw new SpeedoException("No mapping found for the class " 343 + clazz.getFQName() + ", the project " + scp.projectName 344 + " and the mapper " + scp.mapperName); 345 } 346 return m; 347 } 348 349 private NameDef getClassNameDef(Class clazz) throws SpeedoException { 350 return (NameDef) getMapping(clazz).getClassMapping() 351 .getIdentifierMapping().getLinkedMO(); 352 } 353 354 private void addMOClass(Class mo, Set mos) { 355 mos.add(mo); 356 for(Iterator it = mo.getSuperClasses().iterator();it.hasNext();) { 357 addMOClass((Class ) it.next(), mos); 358 } 359 } 360 } 361 | Popular Tags |