KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > generator > lib > JormGenerator


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
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  * Contact: jorm-team@objectweb.org
21  *
22  */

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 JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.Map JavaDoc;
52
53 /**
54  * This class start the generations of the jorm objects and user object. The
55  * jorm objects are the mapping, the binding and the accessor. It is needed to
56  * specify a MOPFactory before start the generation.The generate method
57  * starts the generation. To generate user objects it is necessary to specify
58  * their GeneratorFactory by the addGeneratorFactory method.
59  */

60 public class JormGenerator implements Generator, Loggable {
61     /**
62      * Velocity constant.
63      */

64     private static final String JavaDoc CLASS_RESOURCE_LOADER_DESCRIPTION_PROP
65             = "class.resource.loader.description";
66
67     /**
68      * Velocity constant.
69      */

70     private static final String JavaDoc CLASS_RESOURCE_LOADER_DESCRIPTION_VALUE
71             = "Velocity Classpath Resource Loader";
72
73     /**
74      * Velocity constant.
75      */

76     private static final String JavaDoc CLASS_RESOURCE_LOADER_CLASS_PROP
77             = "class.resource.loader.class";
78
79     /**
80      * Velocity constant.
81      */

82     private static final String JavaDoc 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     /**
89      * The mopFactories which specialize jorm for different supports
90      */

91     private HashMap JavaDoc mopFactories = new HashMap JavaDoc();
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     /**
110      * Initializes the generator. Should be called after setCompilerParameter
111      * and setCompilerConfigurator.
112      * @throws PException
113      */

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         // Connect the velocity log system to monolog
122
velocityEngine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new VelocityLogger(
123                 (loggerFactory == null
124                  ? logger
125                  : loggerFactory.getLogger(logger.getName() + ".velocity"))
126         ));
127         StringBuffer JavaDoc strlibs = new StringBuffer JavaDoc();
128         String JavaDoc libs[][][];
129         libs = setupClassGenerators(compilerConfigurator, compilerParameter);
130         String JavaDoc 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 JavaDoc e) {
157             throw new PExceptionCompiler(e, "Problem while initializing velocity");
158         }
159     }
160
161     private String JavaDoc[][][] setupClassGenerators(JormCompilerConfigurator jcc, JormCompilerParameter cp) throws PException {
162         logger.log(BasicLevel.DEBUG, "setupClassGenerators");
163         Map JavaDoc generators = new HashMap JavaDoc();
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 JavaDoc[][][] setupCompositeNameGenerators() {
199         logger.log(BasicLevel.DEBUG, "setupCompositeNameGenerators");
200         Map JavaDoc generators = new HashMap JavaDoc();
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 JavaDoc[][][] setupGenerators(Map JavaDoc generators) {
217         int size = generators.size();
218         String JavaDoc[][][] templateLibs = new String JavaDoc[size][][];
219         Iterator JavaDoc it = generators.entrySet().iterator();
220         int i = 0;
221         while(it.hasNext()) {
222             Map.Entry JavaDoc me = (Map.Entry JavaDoc) 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 JavaDoc filename, JormCompilerParameter jcp) {
239         logger.log(BasicLevel.DEBUG, "overloadCompilerParameter");
240         JormCompilerParameter res;
241         File JavaDoc fn = new File JavaDoc(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 JavaDoc 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     // IMPLEMENTATION OF METHODS FROM THE Generator INTERFACE
261

262     /**
263      * Assigns the compiler parameter to this generator.
264      * @param jcp the compiler parameter.
265      */

266     public void setCompilerParameter(JormCompilerParameter jcp) {
267         compilerParameter = jcp;
268     }
269
270     /**
271      * Assigns the compiler configurator to this generator.
272      * @param jcc the compiler configurator.
273      */

274     public void setCompilerConfigurator(JormCompilerConfigurator jcc) {
275         compilerConfigurator = jcc;
276     }
277
278     /**
279      * Assigns the meta information manager to this generator.
280      * @param mim the meta information manager to use.
281      */

282     public void setMetaInfoManager(Manager mim) {
283         metaInfoManager = mim;
284     }
285
286     /**
287      * Assigns a PathExplorer object for locating files that have to be parsed.
288      *
289      * @param pathexpl the PathExplorer to be used for file location
290      */

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     /**
300      * Adds a mapping generator which generates a specific mapping part.
301      * @param mappername The name of the mapper.
302      * @param mappinggenerator The Mapping Verifier object.
303      */

304     public void addMappingGenerator(String JavaDoc mappername, MOPFactory mappinggenerator) {
305         mopFactories.put(mappername, mappinggenerator);
306     }
307
308     /**
309      * This method generates the jorm objects by delegation to the
310      * AccessorGenerator, the BindingGenerator and the MappingGenerator. Its
311      * second task is to launch the user objects generator.
312      * @param mo This meta object is a Class.
313      * @param holder The target holder which allows to create files.
314      * @param cp The compiler parameter.
315      * @exception PException if an error occurs during the generation.
316      */

317     public void generate(Class JavaDoc 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         // generate the accessor
351
accessorGenerator.generate(mo, holder, ocp);
352         if (logger.isLoggable(BasicLevel.DEBUG)) {
353             logger.log(BasicLevel.DEBUG, mo.getName() + "Accessor generated");
354         }
355         //generate the serializable data
356
serializableGenerator.generate(mo, holder, ocp);
357         if (logger.isLoggable(BasicLevel.DEBUG)) {
358             logger.log(BasicLevel.DEBUG, mo.getName() + "SerializableData generated");
359         }
360     }
361
362     /**
363      * This method generates the jorm objects by delegation to the
364      * PNameGenerator, BinderGenerator and CompositeNameGenerator.
365      * @param mo This meta object is a CompositeName.
366      * @param holder The target holder which allows to create files.
367      * @param cp The compiler parameter.
368      * @exception PException if an error occurs during the generation.
369      */

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         // generate the pnamegetter
377
pNameGetterGenerator.generate(mo, holder, cp);
378         if (logger.isLoggable(BasicLevel.DEBUG)) {
379             logger.log(BasicLevel.DEBUG, "XXXpnamegetter generated");
380         }
381         // generate the pname
382
pNameGenerator.generate(mo, holder, cp);
383         if (logger.isLoggable(BasicLevel.DEBUG)) {
384             logger.log(BasicLevel.DEBUG, "XXXpname generated");
385         }
386         // generate the binder
387
binderGenerator.generate(mo, holder, cp);
388         if (logger.isLoggable(BasicLevel.DEBUG)) {
389             logger.log(BasicLevel.DEBUG, "XXXbinder generated");
390         }
391     }
392
393     // IMPLEMENTATION OF METHODS FROM THE Loggable INTERFACE
394

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