KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > enhancer > PersistenceAwareEnhancer


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.generation.enhancer;
28
29 import org.objectweb.speedo.api.SpeedoException;
30 import org.objectweb.speedo.api.SpeedoProperties;
31 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
32 import org.objectweb.speedo.metadata.SpeedoPackage;
33 import org.objectweb.speedo.metadata.SpeedoClass;
34 import org.objectweb.speedo.generation.lib.NamingRules;
35 import org.objectweb.asm.ClassReader;
36 import org.objectweb.asm.ClassWriter;
37 import org.objectweb.asm.attrs.Attributes;
38 import org.objectweb.util.monolog.api.BasicLevel;
39
40 import java.util.Iterator JavaDoc;
41 import java.util.Collection JavaDoc;
42 import java.util.Set JavaDoc;
43 import java.util.TreeSet JavaDoc;
44 import java.io.File JavaDoc;
45
46 public class PersistenceAwareEnhancer extends EnhancerComponent {
47
48     public final static String JavaDoc LOGGER_NAME
49             = SpeedoProperties.LOGGER_NAME + ".generation.enhancer.aware";
50
51     /**
52      * Initializes this PersistenceAwareEnhancer
53      */

54     public boolean init() {
55         logger = scp.loggerFactory.getLogger(LOGGER_NAME);
56         //TODO: manage classes localized in a jar file
57
isSrcJar = false;
58         return !scp.getXmldescriptor().isEmpty();
59     }
60
61     /**
62      * Loads all persistence aware classes described by the Object Model and
63      * applies revelant modification to each of them.
64      *
65      * @exception org.objectweb.speedo.generation.enhancer.SpeedoEnhancerException if something goes wrong
66      */

67     public void process() throws SpeedoException {
68         if (scp.getXmldescriptor() == null || scp.getXmldescriptor().isEmpty())
69             return;
70
71         Set JavaDoc generatedClasses = computeGeneratedClasses();
72
73         Collection JavaDoc xmls = scp.getXmldescriptor().values();
74         Iterator JavaDoc i = scp.awareFiles.iterator();
75         while (i.hasNext()) {
76             String JavaDoc awareFile = (String JavaDoc) i.next();
77             String JavaDoc name = awareFile.replace(File.separatorChar, '.');
78             name = name.substring(0, name.length() - 6); // removes the ".class"
79

80             if (generatedClasses.contains(name)) {
81                 // do not enhance previously generated classes
82
continue;
83             }
84
85             logger.log(
86                     BasicLevel.DEBUG,
87                     "Enhancing persistent aware class '" + name + "'");
88
89             ClassReader cr = loadJavaClass(
90                     isSrcJar,
91                     name,
92                     scp.awareFilesDir,
93                     false);
94             ClassWriter cw = new ClassWriter(false);
95
96             PersistenceAwareClassModifier awarecmodifier =
97                     new PersistenceAwareClassModifier(cw, xmls, logger);
98
99             cr.accept(awarecmodifier, Attributes.getDefaultAttributes(), false);
100
101             writeJavaClass(name, cw, scp.output);
102         }
103     }
104
105     private Set JavaDoc computeGeneratedClasses() {
106         Set JavaDoc result = new TreeSet JavaDoc();
107         Collection JavaDoc xmls = scp.getXmldescriptor().values();
108         for (Iterator JavaDoc itDesc = xmls.iterator(); itDesc.hasNext();) {
109             SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next();
110             for (Iterator JavaDoc itPack = desc.jdoPackage.values().iterator(); itPack.hasNext();) {
111                 SpeedoPackage sp = (SpeedoPackage) itPack.next();
112                 for (Iterator JavaDoc itclass = sp.jdoClass.values().iterator(); itclass.hasNext();) {
113                     SpeedoClass jdoClass = (SpeedoClass) itclass.next();
114                     String JavaDoc name = sp.name + '.' + jdoClass.name;
115                     String JavaDoc proxyName = NamingRules.proxyName(name);
116                     String JavaDoc accessorName = NamingRules.accessorName(name);
117                     String JavaDoc bindingName = sp.name + '.' + scp.mapperName + '.' + NamingRules.bindingName(jdoClass.name);
118                     String JavaDoc mappingName = sp.name + '.' + scp.mapperName + '.' + NamingRules.mappingName(jdoClass.name);
119                     String JavaDoc iteratorName = sp.name + '.' + scp.mapperName + '.' + jdoClass.name + "ResultIterator";
120                     String JavaDoc fieldsName = NamingRules.fieldsName(name);
121                     result.add(name);
122                     result.add(proxyName);
123                     result.add(accessorName);
124                     result.add(bindingName);
125                     result.add(mappingName);
126                     result.add(iteratorName);
127                     result.add(fieldsName);
128                 }
129             }
130         }
131         return result;
132     }
133 }
134
Popular Tags