KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > generator > fields > FieldsGenerator


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

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 JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Map JavaDoc;
49
50 /**
51  * This class is used to generate accessor classes. This new file will
52  * manage status of persistent classes and values of persistent fields.
53  *
54  * @author S.Chassande-Barrioz
55  */

56 public class FieldsGenerator extends SpeedoGenerator {
57
58     public final static String JavaDoc LOGGER_NAME
59         = SpeedoProperties.LOGGER_NAME + ".generation.generator.fields";
60
61     public final static String JavaDoc TEMPLATE_NAME = TEMPLATE_DIR + ".fields.Fields";
62
63
64     // IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
65
//----------------------------------------------------//
66

67     public boolean init() throws SpeedoException {
68         logger = scp.loggerFactory.getLogger(LOGGER_NAME);
69         return !scp.getXmldescriptor().isEmpty();
70     }
71
72     // IMPLEMENTATION OF THE VelocityGenerator INTERFACE //
73
//---------------------------------------------------//
74

75     /**
76      * This method generates the a file since a SpeedoClass meta object.
77      * @param sClass is the speedo meta object.
78      * @param fileName name of the new file.
79      * @exception org.objectweb.speedo.generation.generator.api.SpeedoGenerationException If there is a problem during writing
80      * the new file.
81      */

82     public void generate(SpeedoClass sClass, String JavaDoc fileName)
83         throws SpeedoException {
84         computeTemplate(TEMPLATE_NAME.replace('.', '/') + ".vm");
85         try {
86             Context ctx = getContext(sClass);
87             FileWriter JavaDoc fw = new FileWriter JavaDoc(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 JavaDoc e) {
94             throw new SpeedoGenerationException(
95                 "Error during the generation of the file " + fileName, e);
96         }
97     }
98
99
100     // HELPER METHODS FOR THE GENERATION //
101
//-----------------------------------//
102

103     public boolean isNull(Object JavaDoc o) {
104         return o == null;
105     }
106
107
108     // PRIVATE METHODS //
109
//-----------------//
110

111     /**
112      * This method initialises the Velocity context.
113      *
114      * @param jdoClass the jdoClass which represents the class to generate.
115      * @return a Velocity context.
116      */

117     protected Context getContext(SpeedoClass jdoClass) throws SpeedoException {
118         Context ctx = super.getContext(jdoClass);
119         String JavaDoc 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 JavaDoc 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         //Compute user caches
140
Map JavaDoc userCaches = computeUserCaches(jdoClass);
141         ctx.put("hasUserCache", Boolean.valueOf(!userCaches.isEmpty()));
142         ctx.put("userCacheNames", new ArrayList JavaDoc(userCaches.keySet()));
143         for (Iterator JavaDoc iter = userCaches.entrySet().iterator(); iter.hasNext();) {
144             Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
145             List JavaDoc fields = (List JavaDoc) me.getValue();
146             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
147             if (fields.size() > 1) {
148                 sb.append("new UserCacheKey(new Object[] {");
149             }
150             String JavaDoc 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 JavaDoc clazz;
156                 try {
157                     clazz = Util.getClass(Type.getType(sf.desc), null);
158                 } catch (Exception JavaDoc 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 JavaDoc 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