1 23 24 package org.objectweb.jorm.generator.lib; 25 26 import org.apache.velocity.app.Velocity; 27 import org.apache.velocity.app.VelocityEngine; 28 import org.objectweb.jorm.api.PException; 29 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator; 30 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 31 import org.objectweb.jorm.compiler.api.PExceptionCompiler; 32 import org.objectweb.jorm.generator.api.Generator; 33 import org.objectweb.jorm.generator.api.MOPFactory; 34 import org.objectweb.jorm.metainfo.api.Class; 35 import org.objectweb.jorm.metainfo.api.CompositeName; 36 import org.objectweb.jorm.metainfo.api.Manager; 37 import org.objectweb.jorm.util.api.Loggable; 38 import org.objectweb.jorm.util.io.api.PathExplorer; 39 import org.objectweb.jorm.util.io.api.TargetHolder; 40 import org.objectweb.jorm.util.io.lib.DirJavaExplorer; 41 import org.objectweb.medor.expression.api.ExpressionException; 42 import org.objectweb.util.monolog.api.BasicLevel; 43 import org.objectweb.util.monolog.api.Logger; 44 import org.objectweb.util.monolog.api.LoggerFactory; 45 import org.objectweb.util.monolog.wrapper.velocity.VelocityLogger; 46 47 import java.io.File ; 48 import java.io.InputStream ; 49 import java.util.HashMap ; 50 import java.util.Iterator ; 51 import java.util.Map ; 52 53 60 public class JormGenerator implements Generator, Loggable { 61 64 private static final String CLASS_RESOURCE_LOADER_DESCRIPTION_PROP 65 = "class.resource.loader.description"; 66 67 70 private static final String CLASS_RESOURCE_LOADER_DESCRIPTION_VALUE 71 = "Velocity Classpath Resource Loader"; 72 73 76 private static final String CLASS_RESOURCE_LOADER_CLASS_PROP 77 = "class.resource.loader.class"; 78 79 82 private static final String CLASS_RESOURCE_LOADER_CLASS_VALUE 83 = "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"; 84 85 private Manager metaInfoManager = null; 86 private Logger logger = null; 87 private LoggerFactory loggerFactory = null; 88 91 private HashMap mopFactories = new HashMap (); 92 private PathExplorer pathExplorer = null; 93 94 private AccessorGenerator accessorGenerator = null; 95 private BindingGenerator bindingGenerator = null; 96 private MappingGenerator mappingGenerator = null; 97 private AbstractMappingGenerator abstractMappingGenerator = null; 98 private SerializableGenerator serializableGenerator = null; 99 private PNameGetterGenerator pNameGetterGenerator = null; 100 private PNameGenerator pNameGenerator = null; 101 private BinderGenerator binderGenerator = null; 102 private FPNCGenerator fpncGenerator = null; 103 private KFPNCGenerator kfpncGenerator = null; 104 private VelocityEngine velocityEngine = null; 105 106 private JormCompilerParameter compilerParameter = null; 107 private JormCompilerConfigurator compilerConfigurator = null; 108 109 114 public void init() throws PException { 115 velocityEngine = new VelocityEngine(); 116 velocityEngine.setProperty(Velocity.RESOURCE_LOADER, "class"); 117 velocityEngine.setProperty(CLASS_RESOURCE_LOADER_DESCRIPTION_PROP, 118 CLASS_RESOURCE_LOADER_DESCRIPTION_VALUE); 119 velocityEngine.setProperty(CLASS_RESOURCE_LOADER_CLASS_PROP, 120 CLASS_RESOURCE_LOADER_CLASS_VALUE); 121 velocityEngine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new VelocityLogger( 123 (loggerFactory == null 124 ? logger 125 : loggerFactory.getLogger(logger.getName() + ".velocity")) 126 )); 127 StringBuffer strlibs = new StringBuffer (); 128 String libs[][][]; 129 libs = setupClassGenerators(compilerConfigurator, compilerParameter); 130 String sep = ""; 131 for (int i = 0; i < libs.length; i++) { 132 for (int j = 0; j < libs[i].length; j++) { 133 for (int k = 0; k < libs[i][j].length; k++) { 134 strlibs.append(sep); 135 sep = ","; 136 strlibs.append(libs[i][j][k]); 137 logger.log(BasicLevel.DEBUG, "VM Library: " + libs[i][j][k]); 138 } 139 } 140 } 141 libs = setupCompositeNameGenerators(); 142 for (int i = 0; i < libs.length; i++) { 143 for (int j = 0; j < libs[i].length; j++) { 144 for (int k = 0; k < libs[i][j].length; k++) { 145 strlibs.append(sep); 146 sep = ","; 147 strlibs.append(libs[i][j][k]); 148 logger.log(BasicLevel.DEBUG, "VM Library: " + libs[i][j][k]); 149 } 150 } 151 } 152 velocityEngine.setProperty(Velocity.VM_LIBRARY, strlibs.toString()); 153 try { 154 logger.log(BasicLevel.DEBUG, "initializes the velocity engine"); 155 velocityEngine.init(); 156 } catch (Exception e) { 157 throw new PExceptionCompiler(e, "Problem while initializing velocity"); 158 } 159 } 160 161 private String [][][] setupClassGenerators(JormCompilerConfigurator jcc, JormCompilerParameter cp) throws PException { 162 logger.log(BasicLevel.DEBUG, "setupClassGenerators"); 163 Map generators = new HashMap (); 164 if (accessorGenerator == null) { 165 accessorGenerator = new AccessorGenerator(); 166 generators.put("accessor", accessorGenerator); 167 } 168 if (bindingGenerator == null) { 169 bindingGenerator = new BindingGenerator(); 170 generators.put("binding", bindingGenerator); 171 } 172 if (mappingGenerator == null) { 173 mappingGenerator = new MappingGenerator(jcc, cp); 174 generators.put("mapping", mappingGenerator); 175 } 176 if (serializableGenerator == null) { 177 serializableGenerator = new SerializableGenerator(); 178 generators.put("serializable", serializableGenerator); 179 } 180 181 if (fpncGenerator == null) { 182 fpncGenerator = new FPNCGenerator(); 183 generators.put("fpnc", fpncGenerator); 184 } 185 186 if (kfpncGenerator == null) { 187 kfpncGenerator = new KFPNCGenerator(); 188 generators.put("kfpnc", kfpncGenerator); 189 } 190 191 if (abstractMappingGenerator == null) { 192 abstractMappingGenerator = new AbstractMappingGenerator(jcc, cp); 193 generators.put("abstractmapping", abstractMappingGenerator); 194 } 195 return setupGenerators(generators); 196 } 197 198 private String [][][] setupCompositeNameGenerators() { 199 logger.log(BasicLevel.DEBUG, "setupCompositeNameGenerators"); 200 Map generators = new HashMap (); 201 if (pNameGetterGenerator == null) { 202 pNameGetterGenerator = new PNameGetterGenerator(); 203 generators.put("pnamegetter", pNameGetterGenerator); 204 } 205 if (pNameGenerator == null) { 206 pNameGenerator = new PNameGenerator(); 207 generators.put("pname", pNameGenerator); 208 } 209 if (binderGenerator == null) { 210 binderGenerator = new BinderGenerator(); 211 generators.put("binder", binderGenerator); 212 } 213 return setupGenerators(generators); 214 } 215 216 private String [][][] setupGenerators(Map generators) { 217 int size = generators.size(); 218 String [][][] templateLibs = new String [size][][]; 219 Iterator it = generators.entrySet().iterator(); 220 int i = 0; 221 while(it.hasNext()) { 222 Map.Entry me = (Map.Entry ) it.next(); 223 CommonGenerator gen = (CommonGenerator) me.getValue(); 224 gen.setVelocityEngine(velocityEngine); 225 if (loggerFactory != null) { 226 gen.setLogger( 227 loggerFactory.getLogger(logger.getName() + "." + me.getKey())); 228 gen.setLoggerFactory(loggerFactory); 229 } else { 230 gen.setLogger(logger); 231 } 232 templateLibs[i] = gen.getTemplateLibraries(); 233 i++; 234 } 235 return templateLibs; 236 } 237 238 private JormCompilerParameter overloadCompilerParameter(String filename, JormCompilerParameter jcp) { 239 logger.log(BasicLevel.DEBUG, "overloadCompilerParameter"); 240 JormCompilerParameter res; 241 File fn = new File (filename); 242 try { 243 logger.log(BasicLevel.DEBUG, "look for parameter file: " + fn.getParent() + File.separator + "jormcOpts.properties"); 244 res = (JormCompilerParameter) jcp.clone(); 245 InputStream is = pathExplorer.getInputStream(fn.getParent() + File.separator + "jormcOpts.properties"); 246 if (is == null) { 247 logger.log(BasicLevel.DEBUG, "no overloading"); 248 res = jcp; 249 } else { 250 res.loadConfFile(is, compilerConfigurator.knownMappers()); 251 logger.log(BasicLevel.DEBUG, "parameter overloaded"); 252 } 253 } catch (PException e) { 254 logger.log(BasicLevel.DEBUG, "no overloading"); 255 res = jcp; 256 } 257 return res; 258 } 259 260 262 266 public void setCompilerParameter(JormCompilerParameter jcp) { 267 compilerParameter = jcp; 268 } 269 270 274 public void setCompilerConfigurator(JormCompilerConfigurator jcc) { 275 compilerConfigurator = jcc; 276 } 277 278 282 public void setMetaInfoManager(Manager mim) { 283 metaInfoManager = mim; 284 } 285 286 291 public void setPathExplorer(PathExplorer pathexpl) { 292 pathExplorer = pathexpl; 293 if (pathExplorer == null) { 294 pathExplorer = new DirJavaExplorer(); 295 } 296 logger.log(BasicLevel.DEBUG, "jorm classpath=" + pathExplorer.getClassPath()); 297 } 298 299 304 public void addMappingGenerator(String mappername, MOPFactory mappinggenerator) { 305 mopFactories.put(mappername, mappinggenerator); 306 } 307 308 317 public void generate(Class mo, TargetHolder holder, 318 JormCompilerParameter cp, JormCompilerConfigurator jcc) 319 throws PException { 320 JormCompilerParameter ocp = overloadCompilerParameter(mo.getFileName(), cp); 321 if (logger.isLoggable(BasicLevel.DEBUG)) { 322 logger.log(BasicLevel.DEBUG, "class generation: " + mo.getFQName() 323 + "\nfilename=[" + mo.getFileName() + "]" 324 + "\nClassMappingInheritance = " + ocp.getClassMappingInheritance() 325 + "\nBindingInheritance = " + ocp.getBindingInheritance() 326 + "\nProject Name = " + ocp.getProjectName()); 327 } 328 bindingGenerator.generate(mo, holder, ocp, jcc); 329 if (logger.isLoggable(BasicLevel.DEBUG)) { 330 logger.log(BasicLevel.DEBUG, mo.getName() + "Bindings generated"); 331 } 332 mappingGenerator.generate(mo, holder, ocp, jcc); 333 if (logger.isLoggable(BasicLevel.DEBUG)) { 334 logger.log(BasicLevel.DEBUG, mo.getName() + "Mappings generated"); 335 } 336 fpncGenerator.generate(mo, holder, ocp); 337 if (logger.isLoggable(BasicLevel.DEBUG)) { 338 logger.log(BasicLevel.DEBUG, mo.getName() + "FPNCs generated"); 339 } 340 try { 341 if (mo.generateKFPNC()) { 342 kfpncGenerator.generate(mo, holder, ocp); 343 if (logger.isLoggable(BasicLevel.DEBUG)) { 344 logger.log(BasicLevel.DEBUG, mo.getName() + "KFPNCs generated"); 345 } 346 } 347 } catch (ExpressionException e) { 348 throw new PException("Problem with call to generateKFNC() method." + e.getMessage()); 349 } 350 accessorGenerator.generate(mo, holder, ocp); 352 if (logger.isLoggable(BasicLevel.DEBUG)) { 353 logger.log(BasicLevel.DEBUG, mo.getName() + "Accessor generated"); 354 } 355 serializableGenerator.generate(mo, holder, ocp); 357 if (logger.isLoggable(BasicLevel.DEBUG)) { 358 logger.log(BasicLevel.DEBUG, mo.getName() + "SerializableData generated"); 359 } 360 } 361 362 370 public void generate(CompositeName mo, 371 TargetHolder holder, 372 JormCompilerParameter cp) throws PException { 373 if (logger.isLoggable(BasicLevel.DEBUG)) { 374 logger.log(BasicLevel.DEBUG, "composite name generation: " + mo.getFQName()); 375 } 376 pNameGetterGenerator.generate(mo, holder, cp); 378 if (logger.isLoggable(BasicLevel.DEBUG)) { 379 logger.log(BasicLevel.DEBUG, "XXXpnamegetter generated"); 380 } 381 pNameGenerator.generate(mo, holder, cp); 383 if (logger.isLoggable(BasicLevel.DEBUG)) { 384 logger.log(BasicLevel.DEBUG, "XXXpname generated"); 385 } 386 binderGenerator.generate(mo, holder, cp); 388 if (logger.isLoggable(BasicLevel.DEBUG)) { 389 logger.log(BasicLevel.DEBUG, "XXXbinder generated"); 390 } 391 } 392 393 395 public void setLogger(Logger l) { 396 logger = l; 397 } 398 399 public Logger getLogger() { 400 return logger; 401 } 402 403 public LoggerFactory getLoggerFactory() { 404 return loggerFactory; 405 } 406 407 public void setLoggerFactory(LoggerFactory lf) { 408 loggerFactory = lf; 409 } 410 } 411 | Popular Tags |