1 27 28 package org.objectweb.speedo.generation.generator; 29 30 import org.apache.velocity.app.Velocity; 31 import org.apache.velocity.app.VelocityEngine; 32 import org.objectweb.speedo.api.SpeedoException; 33 import org.objectweb.speedo.api.SpeedoProperties; 34 import org.objectweb.speedo.generation.generator.fields.FieldsGenerator; 35 import org.objectweb.speedo.generation.generator.home.HomeGenerator; 36 import org.objectweb.speedo.generation.generator.lib.AbstractVelocityGenerator; 37 import org.objectweb.speedo.generation.generator.objectid.ObjectIdGenerator; 38 import org.objectweb.speedo.generation.generator.proxy.ProxyGenerator; 39 import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent; 40 import org.objectweb.speedo.generation.lib.NamingRules; 41 import org.objectweb.speedo.metadata.SpeedoClass; 42 import org.objectweb.speedo.metadata.SpeedoPackage; 43 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor; 44 import org.objectweb.util.monolog.api.BasicLevel; 45 import org.objectweb.util.monolog.api.Logger; 46 import org.objectweb.util.monolog.wrapper.velocity.VelocityLogger; 47 48 import java.io.File ; 49 import java.util.Collection ; 50 import java.util.Iterator ; 51 52 56 public class SpeedoGenerator extends AbstractGeneratorComponent { 57 58 public final static String LOGGER_NAME 59 = SpeedoProperties.LOGGER_NAME + ".generation.generator"; 60 61 private static final String CLASS_RESOURCE_LOADER_DESCRIPTION_PROP 62 = "class.resource.loader.description"; 63 private static final String CLASS_RESOURCE_LOADER_DESCRIPTION_VALUE 64 = "Velocity Classpath Resource Loader"; 65 private static final String CLASS_RESOURCE_LOADER_CLASS_PROP 66 = "class.resource.loader.class"; 67 private static final String CLASS_RESOURCE_LOADER_CLASS_VALUE 68 = "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"; 69 70 71 74 public boolean init() { 75 logger = scp.loggerFactory.getLogger(LOGGER_NAME); 76 return !scp.getXmldescriptor().isEmpty(); 77 } 78 79 87 public void process() throws SpeedoException { 88 if (scp.getXmldescriptor().isEmpty()) 89 return; 90 char fs = File.separatorChar; 92 94 VelocityLogger vl = new VelocityLogger(logger); 95 VelocityEngine ve = new VelocityEngine(); 96 ve.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, vl); 97 98 AbstractVelocityGenerator[] vgs = new AbstractVelocityGenerator[]{ 99 new FieldsGenerator(), 100 new ProxyGenerator(), 101 new HomeGenerator(), 102 new ObjectIdGenerator()}; 103 Logger[] loggers = new Logger[vgs.length]; 104 String strlibs = ""; 105 boolean first = true; 106 for (int i = 0; i < vgs.length; i++) { 107 vgs[i].setSpeedoCompilerParameter(scp); 108 vgs[i].init(); 109 Collection col = vgs[i].getExternalsTemplate(); 110 if (col != null && col.size() > 0) { 111 for (Iterator it = col.iterator(); it.hasNext();) { 112 strlibs += (first ? "" : ",") + it.next(); 113 first = false; 114 } 115 } 116 vgs[i].setVelocityEngine(ve); 117 loggers[i] = scp.loggerFactory.getLogger( 118 vgs[i].getLogger().getName() + ".velocity"); 119 } 120 ve.setProperty(Velocity.RESOURCE_LOADER, "class"); 121 ve.setProperty(CLASS_RESOURCE_LOADER_DESCRIPTION_PROP, 122 CLASS_RESOURCE_LOADER_DESCRIPTION_VALUE); 123 ve.setProperty(CLASS_RESOURCE_LOADER_CLASS_PROP, 124 CLASS_RESOURCE_LOADER_CLASS_VALUE); 125 ve.setProperty(Velocity.VM_LIBRARY, strlibs); 126 try { 127 ve.init(); 128 } catch (Exception e) { 129 throw new SpeedoException( 130 "Impossible to initialize the VelocityEngine", e); 131 } 132 133 for (Iterator itDesc = scp.getXmldescriptor().values().iterator(); itDesc.hasNext();) { 134 SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next(); 135 for (Iterator itPack = desc.jdoPackage.values().iterator(); itPack.hasNext();) { 136 SpeedoPackage sp = (SpeedoPackage) itPack.next(); 137 String baseDir = scp.output + fs + sp.name.replace('.', fs) + fs; 139 for (Iterator itclass = sp.jdoClass.values().iterator(); itclass.hasNext();) { 140 SpeedoClass sClass = (SpeedoClass) itclass.next(); 141 142 if (sClass.failed) 144 continue; 145 String [] filenames = { 146 baseDir + NamingRules.fieldsName(sClass.name) + ".java", 147 baseDir + NamingRules.proxyName(sClass.name) + ".java", 148 baseDir + NamingRules.homeName(sClass.name) + ".java", 149 baseDir + NamingRules.generatedObjectIdName(sClass.name) + ".java"}; 150 try { 151 vl.setLog(loggers[0]); 152 vgs[0].generate(sClass, filenames[0]); 153 vl.setLog(loggers[1]); 154 vgs[1].generate(sClass, filenames[1]); 155 vl.setLog(loggers[2]); 156 vgs[2].generate(sClass, filenames[2]); 157 if (sClass.generateObjectId()) { 158 vl.setLog(loggers[3]); 159 vgs[3].generate(sClass, filenames[3]); 160 logger.log(BasicLevel.WARN, 161 "No object id class defined for the class '" 162 + sClass.getFQName() 163 + "': use the generated class: '" 164 + NamingRules.generatedObjectIdName(sClass.getFQName()) + "'"); 165 } 166 logger.log(BasicLevel.INFO, "Classes generated for " 167 + sClass.getFQName()); 168 } catch (SpeedoException e) { 169 logger.log(BasicLevel.ERROR, 170 "ERROR during the speedo generation of files " 171 + filenames[0] + ", " + filenames[1] + ", " 172 + filenames[2] + " or " + filenames[3], e); 173 throw e; 174 } 175 } 176 177 } 178 } 179 } 180 } 181 | Popular Tags |