KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > generator > SpeedoGenerator


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;
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 JavaDoc;
49 import java.util.Collection JavaDoc;
50 import java.util.Iterator JavaDoc;
51
52 /**
53  * This class generates files "proxy", "manager" and "fields" for persistence capable classes.
54  * @author S. Chassande-Barrioz
55  */

56 public class SpeedoGenerator extends AbstractGeneratorComponent {
57
58     public final static String JavaDoc LOGGER_NAME
59             = SpeedoProperties.LOGGER_NAME + ".generation.generator";
60
61     private static final String JavaDoc CLASS_RESOURCE_LOADER_DESCRIPTION_PROP
62             = "class.resource.loader.description";
63     private static final String JavaDoc CLASS_RESOURCE_LOADER_DESCRIPTION_VALUE
64             = "Velocity Classpath Resource Loader";
65     private static final String JavaDoc CLASS_RESOURCE_LOADER_CLASS_PROP
66             = "class.resource.loader.class";
67     private static final String JavaDoc CLASS_RESOURCE_LOADER_CLASS_VALUE
68             = "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader";
69
70
71     // IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
72
//----------------------------------------------------//
73

74     public boolean init() {
75         logger = scp.loggerFactory.getLogger(LOGGER_NAME);
76         return !scp.getXmldescriptor().isEmpty();
77     }
78
79     /**
80      * Generation all files <className>, and <className>Fields.
81      * If a previous task on a class has failed, the methods doesn't create
82      * files for this class.
83      *
84      * @exception SpeedoException if there is a problem during writing
85      * the new files and the boolean failsonerror is set on true.
86      */

87     public void process() throws SpeedoException {
88         if (scp.getXmldescriptor().isEmpty())
89             return;
90         //separator for file names
91
char fs = File.separatorChar;
92         //for each package
93

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 JavaDoc 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 JavaDoc col = vgs[i].getExternalsTemplate();
110             if (col != null && col.size() > 0) {
111                 for (Iterator JavaDoc 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 JavaDoc e) {
129             throw new SpeedoException(
130                     "Impossible to initialize the VelocityEngine", e);
131         }
132
133         for (Iterator JavaDoc itDesc = scp.getXmldescriptor().values().iterator(); itDesc.hasNext();) {
134             SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next();
135             for (Iterator JavaDoc itPack = desc.jdoPackage.values().iterator(); itPack.hasNext();) {
136                 SpeedoPackage sp = (SpeedoPackage) itPack.next();
137                 //base directory for new files
138
String JavaDoc baseDir = scp.output + fs + sp.name.replace('.', fs) + fs;
139                 for (Iterator JavaDoc itclass = sp.jdoClass.values().iterator(); itclass.hasNext();) {
140                     SpeedoClass sClass = (SpeedoClass) itclass.next();
141
142                     //asm has not failed on this class
143
if (sClass.failed)
144                         continue;
145                     String JavaDoc[] 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