001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2005 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.generation.enhancer.aware;
027:
028: import org.objectweb.asm.ClassReader;
029: import org.objectweb.asm.ClassVisitor;
030: import org.objectweb.asm.ClassWriter;
031: import org.objectweb.asm.attrs.Attributes;
032: import org.objectweb.speedo.api.SpeedoException;
033: import org.objectweb.speedo.api.SpeedoProperties;
034: import org.objectweb.speedo.generation.enhancer.common.AbstractEnhancerComponent;
035: import org.objectweb.speedo.generation.lib.NamingRules;
036: import org.objectweb.speedo.lib.Personality;
037: import org.objectweb.speedo.metadata.SpeedoClass;
038: import org.objectweb.speedo.metadata.SpeedoPackage;
039: import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
040: import org.objectweb.util.monolog.api.BasicLevel;
041:
042: import java.io.File;
043: import java.util.Collection;
044: import java.util.Iterator;
045: import java.util.Set;
046: import java.util.TreeSet;
047:
048: public class PersistenceAwareEnhancer extends AbstractEnhancerComponent {
049: public final static String LOGGER_NAME = SpeedoProperties.LOGGER_NAME
050: + ".generation.enhancer.aware";
051: int enhanced;
052:
053: public PersistenceAwareEnhancer(Personality p) {
054: super (p);
055: }
056:
057: /**
058: * Initializes this PersistenceAwareEnhancer
059: */
060: public boolean init() {
061: logger = scp.loggerFactory.getLogger(LOGGER_NAME);
062: //TODO: manage classes localized in a jar file
063: isSrcJar = false;
064: enhanced = 0;
065: return !scp.getXmldescriptor().isEmpty();
066: }
067:
068: public String getTitle() {
069: return "Enhancing Persistent aware classes...";
070: }
071:
072: public String getSummary() {
073: return enhanced + " persistence aware classe(s) enhanced.";
074: }
075:
076: /**
077: * Loads all persistence aware classes described by the Object Model and
078: * applies revelant modification to each of them.
079: *
080: * @exception org.objectweb.speedo.generation.enhancer.common.SpeedoEnhancerException if something goes wrong
081: */
082: public void process() throws SpeedoException {
083: if (scp.getXmldescriptor() == null
084: || scp.getXmldescriptor().isEmpty())
085: return;
086: final Set generatedClasses = computeGeneratedClasses();
087: for (Iterator i = scp.awareFiles.iterator(); i.hasNext();) {
088: final String awareFile = (String) i.next();
089: final String name = awareFile.replace(File.separatorChar,
090: '.').substring(0, awareFile.length() - 6); // removes the ".class"
091: if (generatedClasses.contains(name)) {
092: // do not enhance previously generated classes
093: continue;
094: }
095: enhanced++;
096: logger.log(BasicLevel.DEBUG,
097: "Enhancing persistent aware class '" + name + "'");
098: ClassWriter cw = new ClassWriter(false);
099: ClassVisitor current = cw;
100: current = new PersistenceAwareClassModifier(cw, scp.smi,
101: logger, personality);
102:
103: ClassReader cr = loadJavaClass(isSrcJar, name,
104: scp.awareFilesDir, false);
105: cr
106: .accept(current, Attributes.getDefaultAttributes(),
107: false);
108:
109: writeJavaClass(name, cw, scp.output);
110: }
111: }
112:
113: private Set computeGeneratedClasses() {
114: Set result = new TreeSet();
115: Collection xmls = scp.getXmldescriptor().values();
116: for (Iterator itDesc = xmls.iterator(); itDesc.hasNext();) {
117: SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc
118: .next();
119: for (Iterator itPack = desc.packages.values().iterator(); itPack
120: .hasNext();) {
121: SpeedoPackage sp = (SpeedoPackage) itPack.next();
122: for (Iterator itclass = sp.classes.values().iterator(); itclass
123: .hasNext();) {
124: SpeedoClass sc = (SpeedoClass) itclass.next();
125: String name = sp.name + '.' + sc.name;
126: String accessorName = NamingRules
127: .accessorName(name);
128: String mappingName = sp.name + '.' + scp.mapperName
129: + '.' + NamingRules.mappingName(sc.name);
130: String iteratorName = sp.name + '.'
131: + scp.mapperName + '.' + sc.name
132: + "ResultIterator";
133: String fieldsName = NamingRules.fieldsName(name);
134: result.add(name);
135: result.add(accessorName);
136: result.add(mappingName);
137: result.add(iteratorName);
138: result.add(fieldsName);
139: }
140: }
141: }
142: return result;
143: }
144: }
|