1 27 28 package org.objectweb.speedo.generation.generator.fields; 29 30 import org.apache.velocity.app.Velocity; 31 import org.apache.velocity.context.Context; 32 import org.objectweb.asm.Type; 33 import org.objectweb.jorm.api.PException; 34 import org.objectweb.speedo.api.SpeedoException; 35 import org.objectweb.speedo.api.SpeedoProperties; 36 import org.objectweb.speedo.generation.enhancer.Util; 37 import org.objectweb.speedo.generation.generator.api.SpeedoGenerationException; 38 import org.objectweb.speedo.generation.generator.lib.SpeedoGenerator; 39 import org.objectweb.speedo.generation.lib.NamingRules; 40 import org.objectweb.speedo.metadata.SpeedoClass; 41 import org.objectweb.speedo.metadata.SpeedoField; 42 import org.objectweb.util.monolog.wrapper.velocity.VelocityLogger; 43 44 import java.io.FileWriter ; 45 import java.util.ArrayList ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.Map ; 49 50 56 public class FieldsGenerator extends SpeedoGenerator { 57 58 public final static String LOGGER_NAME 59 = SpeedoProperties.LOGGER_NAME + ".generation.generator.fields"; 60 61 public final static String TEMPLATE_NAME = TEMPLATE_DIR + ".fields.Fields"; 62 63 64 67 public boolean init() throws SpeedoException { 68 logger = scp.loggerFactory.getLogger(LOGGER_NAME); 69 return !scp.getXmldescriptor().isEmpty(); 70 } 71 72 75 82 public void generate(SpeedoClass sClass, String fileName) 83 throws SpeedoException { 84 computeTemplate(TEMPLATE_NAME.replace('.', '/') + ".vm"); 85 try { 86 Context ctx = getContext(sClass); 87 FileWriter fw = new FileWriter (fileName); 88 ve.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, 89 new VelocityLogger(logger)); 90 template.merge(ctx, fw); 91 fw.flush(); 92 fw.close(); 93 } catch (Exception e) { 94 throw new SpeedoGenerationException( 95 "Error during the generation of the file " + fileName, e); 96 } 97 } 98 99 100 103 public boolean isNull(Object o) { 104 return o == null; 105 } 106 107 108 111 117 protected Context getContext(SpeedoClass jdoClass) throws SpeedoException { 118 Context ctx = super.getContext(jdoClass); 119 String sc = jdoClass.superClassName; 120 if (sc != null && sc.length() > 0) { 121 ctx.put("superclass", NamingRules.fieldsName(sc)); 122 } else { 123 ctx.put("superclass", "BasicSpeedoAccessor"); 124 } 125 ctx.put("persistentsuperclass", jdoClass.superClassName); 126 127 try { 128 Object nfs = scp.nmf.getNamingManager(jdoClass) 129 .getNamingfields(jdoClass); 130 if (nfs != null) { 131 ctx.put("namingFields", nfs); 132 } 133 } catch (PException e) { 134 throw new SpeedoException( 135 "Impossible to manage the naming of the class '" 136 + jdoClass.getFQName() + "'", e); 137 } 138 139 Map userCaches = computeUserCaches(jdoClass); 141 ctx.put("hasUserCache", Boolean.valueOf(!userCaches.isEmpty())); 142 ctx.put("userCacheNames", new ArrayList (userCaches.keySet())); 143 for (Iterator iter = userCaches.entrySet().iterator(); iter.hasNext();) { 144 Map.Entry me = (Map.Entry ) iter.next(); 145 List fields = (List ) me.getValue(); 146 StringBuffer sb = new StringBuffer (); 147 if (fields.size() > 1) { 148 sb.append("new UserCacheKey(new Object[] {"); 149 } 150 String sep = ""; 151 for (int j = 0; j < fields.size(); j++) { 152 sb.append(sep); 153 sep = ", "; 154 SpeedoField sf = (SpeedoField) fields.get(j); 155 Class clazz; 156 try { 157 clazz = Util.getClass(Type.getType(sf.desc), null); 158 } catch (Exception e1) { 159 throw new SpeedoException("Field '" + sf.name 160 + "' cannot be used in an index: ", e1); 161 } 162 if (clazz.isPrimitive()) { 163 sb.append("new ").append(getJavaLangType(clazz)); 164 sb.append("(").append(sf.name).append(")"); 165 } else { 166 sb.append(sf.name); 167 } 168 } 169 if (fields.size() > 1) { 170 sb.append("})"); 171 } 172 userCaches.put(me.getKey(), sb.toString()); 173 } 174 ctx.put("userCachekeys", userCaches); 175 return ctx; 176 } 177 178 public boolean isParentField(SpeedoClass sc, String fn) { 179 if (sc.superClassName == null 180 || sc.superClassName.length() == 0) 181 return false; 182 SpeedoClass psc = sc.jdoPackage.jdoXMLDescriptor.getSpeedoClass( 183 sc.superClassName, true); 184 return psc != null && psc.jdoField.get(fn) != null; 185 } 186 } 187 | Popular Tags |