KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > wizard > generation > JormWriter


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 package org.objectweb.speedo.wizard.generation;
28
29 import org.objectweb.jorm.api.PException;
30 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator;
31 import org.objectweb.jorm.compiler.api.JormCompilerParameter;
32 import org.objectweb.jorm.compiler.lib.JormCompiler;
33 import org.objectweb.jorm.metainfo.api.Class;
34 import org.objectweb.jorm.metainfo.api.ClassProject;
35 import org.objectweb.jorm.metainfo.api.CompositeName;
36 import org.objectweb.jorm.metainfo.api.GenClassRef;
37 import org.objectweb.jorm.metainfo.api.Manager;
38 import org.objectweb.jorm.metainfo.api.Mapping;
39 import org.objectweb.jorm.metainfo.api.NameDef;
40 import org.objectweb.jorm.metainfo.api.TypedElement;
41 import org.objectweb.jorm.util.io.lib.DirJavaExplorer;
42 import org.objectweb.speedo.api.ExceptionHelper;
43 import org.objectweb.speedo.api.SpeedoException;
44 import org.objectweb.speedo.api.SpeedoProperties;
45 import org.objectweb.speedo.generation.api.SpeedoXMLError;
46 import org.objectweb.speedo.generation.jorm.JormMIBuilder;
47 import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent;
48 import org.objectweb.speedo.metadata.SpeedoClass;
49 import org.objectweb.speedo.metadata.SpeedoField;
50 import org.objectweb.speedo.metadata.SpeedoIdentity;
51 import org.objectweb.speedo.metadata.SpeedoModifier;
52 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
53 import org.objectweb.util.monolog.api.BasicLevel;
54
55 import java.io.File JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.Collection JavaDoc;
58 import java.util.Iterator JavaDoc;
59 import java.util.List JavaDoc;
60 import java.util.Set JavaDoc;
61
62 public class JormWriter extends AbstractGeneratorComponent {
63
64    public final static String JavaDoc LOGGER_NAME =
65             SpeedoProperties.LOGGER_NAME + "wizard.generation";
66
67     /**
68      * Jorm classes intended for JORM generation
69      */

70     protected JormCompiler jormcompiler;
71
72
73     // IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
74
//----------------------------------------------------//
75

76     public boolean init() throws SpeedoException {
77         logger = scp.loggerFactory.getLogger(LOGGER_NAME);
78         jormcompiler = new JormCompiler();
79         JormCompilerParameter jcp = jormcompiler.getCompilerParameter();
80         JormCompilerConfigurator jcc = jormcompiler.getCompilerConfigurator();
81         try {
82             logger.log(BasicLevel.DEBUG, "JormConfigurator: configure() -- get jorm.mapper.list");
83             jcc.configure();
84             logger.log(BasicLevel.DEBUG, "JormCompilerParameter: loadConfFile()");
85             jcp.loadConfFile(jcc.getGlobalJormcOptsFile(), jcc.knownMappers());
86             jcc.setLoggerFactory(scp.loggerFactory);
87             jcp.setProjectName(scp.projectName);
88             jcp.setBindingInheritance("%p%c");
89             jcp.setOutput(scp.pdoutput);
90             jcp.setBindingAbstract(true);
91             jcp.setDtdLocations(scp.dtdLocations);
92             jcp.setInputFiles(scp.jorm);
93             DirJavaExplorer pe = new DirJavaExplorer();
94             pe.setLogger(scp.loggerFactory.getLogger("org.objectweb.jorm.io.pathexplorer"));
95             pe.addPath(scp.jormclasspath);
96             jcp.setClasspath(pe);
97             jcp.setGeneratedWithMapperPackage(false);
98             if ((scp.projectName == null) || (scp.mapperName == null))
99                 return true;
100             int idx = scp.mapperName.indexOf('.');
101             if (idx == -1) {
102                 jcc.removeMapper(scp.mapperName);
103                 jcc.addSubMapper(scp.mapperName, "generic");
104             } else {
105                 String JavaDoc actualmn = scp.mapperName.substring(0,idx);
106                 jcc.removeMapper(actualmn);
107                 jcc.addSubMapper(actualmn, scp.mapperName.substring(idx + 1));
108             }
109             Collection JavaDoc c = jcc.getSubMappers("rdb");
110             logger.log(BasicLevel.DEBUG, "jormcOpts file:" + jcc.getJormcOptsFile());
111             logger.log(BasicLevel.DEBUG, "rdb sub mappers: " + c);
112
113         } catch (PException e) {
114             logger.log(BasicLevel.ERROR,
115                     "Impossible to configure Jorm", ExceptionHelper.getNested(e));
116             throw new SpeedoException("Impossible to configure Jorm", e);
117         }
118         return true;
119     }
120
121     /**
122      * Performs the JORM objects' generation.
123      */

124     public void process() throws SpeedoException {
125
126         try {
127             //Scans the collection of existing .pd files
128
Collection JavaDoc c = jormcompiler.getCompilerParameter().getInputFiles();
129             if ((c != null) && (logger.isLoggable(BasicLevel.DEBUG)))
130                 for (Iterator JavaDoc it = c.iterator(); it.hasNext();)
131                     logger.log(BasicLevel.DEBUG, "source file: " + it.next());
132             ArrayList JavaDoc notFoundMOs = new ArrayList JavaDoc();
133             // Scans SpeedoClass objects. If there is no matching Jorm specification,
134
// adds the SpeedoClass object into notFoundMOs.
135
for (Iterator JavaDoc itDesc = scp.getXmldescriptor().values().iterator(); itDesc.hasNext();) {
136                 SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next();
137                 List JavaDoc scs = desc.getSpeedoClasses();
138                 for (int i=0; i<scs.size(); i++) {
139                     SpeedoClass sc = (SpeedoClass) scs.get(i);
140                     String JavaDoc fn = sc.getJormFileName();
141                     logger.log(BasicLevel.DEBUG,
142                             "looking for the file: " +
143                             scp.jormDir + File.separatorChar + fn); //TODO
144
if ((c == null) || (!c.contains(fn))) { //TODO
145
notFoundMOs.add(sc);
146                     }
147                 }
148             }
149
150             // if .pd files are not specified by the user then the jorm meta
151
// information is generated
152
jormcompiler.setupLogger();
153             jormcompiler.setupMIManager();
154             Collection JavaDoc createdMOs = null;
155             if (!notFoundMOs.isEmpty()) {
156                 // Turns JDO meta-objects into JORM meta-objects (only those for which
157
// there does not exist a JORM specification).
158
logger.log(BasicLevel.DEBUG, "JormWriter: projectname: <" + scp.projectName +
159                         "> mappername: " + scp.mapperName);
160                 createdMOs = new JormMIBuilder(
161                         jormcompiler.getMIManager(), scp.nmf, logger)
162                         .createMI(notFoundMOs, scp.projectName, scp.mapperName);
163             }
164
165             if (c != null) { //TODO
166
// Parse the .pd files
167
// Builds JORM meta-objects from the collection of .pd files and
168
// compares JDO meta-objects with the matching JORM meta-objects.
169
c = new ArrayList JavaDoc(jormcompiler.parseFiles(c));
170                 //isCompatible();
171
}
172             // Generates JORM files from the JORM meta-objects that have been
173
// built from JDO meta-objects.
174
if (createdMOs != null && !createdMOs.isEmpty()) {
175                 jormcompiler.generateJormFiles(createdMOs);
176             }
177             if (c != null) { // TODO
178
c.addAll(createdMOs);
179             }
180             else {
181                 c = createdMOs;
182             }
183
184             if (logger.isLoggable(BasicLevel.DEBUG))
185                 for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
186                     Object JavaDoc o = it.next();
187                     if (o instanceof Class JavaDoc) {
188                         logger.log(BasicLevel.DEBUG, "Class : "
189                                 + ((Class JavaDoc) o).getFQName());
190                     } else if (o instanceof CompositeName) {
191                         logger.log(BasicLevel.DEBUG, "compositeName : "
192                                 + ((CompositeName) o).getFQName());
193                     } else
194                         logger.log(BasicLevel.DEBUG, "metaObject : " + o);
195                 }
196
197             //Verify the meta information
198
//jormcompiler.verifyMetaInfo(c);
199
} catch (PException e) {
200             throw new SpeedoException("Error during .pd files generation", e);
201         }
202     }
203
204     /**
205      * Verifies that all persistent capable field for jdo is described in JORM.
206      *
207      * @exception org.objectweb.speedo.generation.api.SpeedoXMLError if JORM an JDO MetaData are not coherent
208      */

209     protected void isCompatible() throws SpeedoException {
210
211         List JavaDoc except = new ArrayList JavaDoc();
212
213         Manager manager = jormcompiler.getMIManager();
214         for (Iterator JavaDoc itDesc = scp.getXmldescriptor().values().iterator(); itDesc.hasNext();) {
215             SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next();
216             List JavaDoc scs = desc.getSpeedoClasses();
217             for (int i=0; i<scs.size(); i++) {
218                 compareClass((SpeedoClass) scs.get(i), manager, except);
219             }
220         }
221         if (!except.isEmpty() && logger.isLoggable(BasicLevel.ERROR))
222             for (Iterator JavaDoc it = except.iterator(); it.hasNext();)
223                 logger.log(BasicLevel.ERROR, it.next());
224     }
225
226     protected void compareClass(SpeedoClass clas,
227                                 Manager manager,
228                                 List JavaDoc except) throws SpeedoException {
229         debug = logger.isLoggable(BasicLevel.DEBUG);
230         if (debug) {
231             logger.log(BasicLevel.DEBUG,
232                     "Verify the definition of the class " + clas.getFQName());
233         }
234         Class JavaDoc classJorm = manager.getClass(clas.getFQName());
235         if (classJorm == null) {
236             throw new SpeedoException("Jorm description of the class '"
237                     + clas.getFQName() + "' is not availlable");
238         }
239         NameDef pName = null;
240         SpeedoClass sc = clas;
241         while(sc.superClassName != null) {
242             SpeedoClass _sc = sc;
243             sc = sc.jdoPackage.jdoXMLDescriptor.smi.getSpeedoClass(sc.superClassName, sc.jdoPackage);
244             if (sc == null) {
245                 throw new SpeedoException("Class " + _sc.getFQName() + " not defined in the jorm meta information");
246             }
247         }
248         Class JavaDoc pclassJorm = manager.getClass(sc.getFQName());
249         pName = getClassNameDef(pclassJorm);
250
251         if (classJorm == null) {
252             throw new SpeedoXMLError("Class '" + clas.name + "' not defined in JORM metadata");
253         }
254
255         // Comparison from JDO
256
clas.jormclass = classJorm;
257         for (Iterator JavaDoc efield = clas.jdoField.values().iterator(); efield.hasNext();) {
258             SpeedoField field = (SpeedoField) efield.next();
259             String JavaDoc fieldName = field.name;
260
261             // Fields with this persistent modifier shouldn't stay in JDO Metadata
262
if (field.persistenceModifier == SpeedoModifier.none)
263                 continue;
264
265             TypedElement tElem = classJorm.getTypedElement(fieldName);
266             if (tElem == null) {
267                 throw new SpeedoXMLError("Field '" + fieldName
268                     + "' not defined in JORM metadata of the class '"
269                     + clas.getFQName() + "'");
270             }
271
272             // for application identity key fields have to be compared
273
if (field.primaryKey) {
274                 boolean found = false;
275                 for (Iterator JavaDoc it = pName.iterateField(); it.hasNext() && !found;) {
276                     found = fieldName.equals(it.next());
277                 }
278                 if (!found) {
279                     throw new SpeedoXMLError("Field '" + fieldName
280                         + "' not defined in Class NameDef's Fields of the class '"
281                         + clas.getFQName() + "'");
282                 }
283             }
284
285             if (tElem instanceof GenClassRef && field.jdoTuple == null)
286                 throw new SpeedoXMLError("field '" + fieldName + "' should be a tuple in JDO metadata of the class '" + clas.getFQName() + "'");
287         }
288
289         // Comparison from JORM
290
for (Iterator JavaDoc jormfield = classJorm.getFields().iterator(); jormfield.hasNext();) {
291             TypedElement te = (TypedElement) jormfield.next();
292             String JavaDoc fieldName = te.getName();
293             if (!clas.jdoField.containsKey(fieldName)
294                     && !isContainerIdField(classJorm, te, clas))
295                 throw new SpeedoXMLError("Field '" + fieldName + "' of the class '"
296                             + clas.getFQName() + "' is not defined in the '"
297                             + clas.jdoPackage.jdoXMLDescriptor.xmlFile
298                             + "' file (found: " + clas.jdoField.keySet() + ").");
299         }
300
301         // for application identity key fields have to be compared
302
if (clas.identityType == SpeedoIdentity.USER_ID) {
303             boolean found = false;
304             for (Iterator JavaDoc it = pName.iterateField(); it.hasNext() && !found;) {
305                 String JavaDoc fn = (String JavaDoc) it.next();
306                 SpeedoField sf = (SpeedoField) clas.jdoField.get(fn);
307                 if (sf == null) {
308                     logger.log(BasicLevel.WARN, "Field " + fn + " defined in the identifier of "
309                             + clas.name
310                             + ".pd file but not availlable in the class (or .pd file).");
311                 } else if (!sf.primaryKey) {
312                     except.add("Field " + fn + " defined in the "
313                             + clas.name
314                             + ".pd file but is not marked as a primary key field in the "
315                             + clas.jdoPackage.jdoXMLDescriptor.xmlFile
316                             + " file.");
317                 }
318             }
319         }
320     }
321
322     private boolean isContainerIdField(Class JavaDoc clazz,
323                                        TypedElement te,
324                                        SpeedoClass sc) throws SpeedoException {
325         return sc.identityType == SpeedoIdentity.CONTAINER_ID
326                 && getClassNameDef(clazz).getNameRef()
327                 .getProjection().containsValue(te.getName());
328     }
329
330
331     private Mapping getMapping(Class JavaDoc clazz) throws SpeedoException {
332         ClassProject cp = clazz.getClassProject(scp.projectName);
333         if (cp == null) {
334             throw new SpeedoException("No classproject found for the class "
335                     + clazz.getFQName() + " and the project " + scp.projectName);
336         }
337         int idx = scp.mapperName.indexOf('.');
338         Mapping m = cp.getMapping(idx == -1
339                 ? scp.mapperName
340                 : scp.mapperName.substring(0, idx));
341         if (m == null) {
342             throw new SpeedoException("No mapping found for the class "
343                     + clazz.getFQName() + ", the project " + scp.projectName
344                     + " and the mapper " + scp.mapperName);
345         }
346         return m;
347     }
348
349     private NameDef getClassNameDef(Class JavaDoc clazz) throws SpeedoException {
350         return (NameDef) getMapping(clazz).getClassMapping()
351                 .getIdentifierMapping().getLinkedMO();
352     }
353
354     private void addMOClass(Class JavaDoc mo, Set JavaDoc mos) {
355         mos.add(mo);
356         for(Iterator JavaDoc it = mo.getSuperClasses().iterator();it.hasNext();) {
357             addMOClass((Class JavaDoc) it.next(), mos);
358         }
359     }
360 }
361
Popular Tags