Package org.objectweb.speedo.generation.enhancer.aware

Source Code of org.objectweb.speedo.generation.enhancer.aware.PersistenceAwareEnhancer

/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2005 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*
*
* Contact: speedo@objectweb.org
*
* Authors: S.Chassande-Barrioz.
*
*/
package org.objectweb.speedo.generation.enhancer.aware;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.attrs.Attributes;
import org.objectweb.speedo.api.SpeedoException;
import org.objectweb.speedo.api.SpeedoProperties;
import org.objectweb.speedo.generation.enhancer.common.AbstractEnhancerComponent;
import org.objectweb.speedo.generation.lib.NamingRules;
import org.objectweb.speedo.lib.Personality;
import org.objectweb.speedo.metadata.SpeedoClass;
import org.objectweb.speedo.metadata.SpeedoPackage;
import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
import org.objectweb.util.monolog.api.BasicLevel;

import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class PersistenceAwareEnhancer extends AbstractEnhancerComponent {
    public final static String LOGGER_NAME
            = SpeedoProperties.LOGGER_NAME + ".generation.enhancer.aware";
    int enhanced;

    public PersistenceAwareEnhancer(Personality p) {
      super(p);
    }
   
    /**
     * Initializes this PersistenceAwareEnhancer
     */
    public boolean init() {
        logger = scp.loggerFactory.getLogger(LOGGER_NAME);
        //TODO: manage classes localized in a jar file
        isSrcJar = false;
        enhanced = 0;
      return !scp.getXmldescriptor().isEmpty();
    }
    public String getTitle() {
        return "Enhancing Persistent aware classes...";
    }

    public String getSummary() {
        return enhanced + " persistence aware classe(s) enhanced.";
    }

    /**
     * Loads all persistence aware classes described by the Object Model and
     * applies revelant modification to each of them.
     *
     * @exception org.objectweb.speedo.generation.enhancer.common.SpeedoEnhancerException if something goes wrong
     */
    public void process() throws SpeedoException {
        if (scp.getXmldescriptor() == null || scp.getXmldescriptor().isEmpty())
            return;
        final Set generatedClasses = computeGeneratedClasses();
        for(Iterator i = scp.awareFiles.iterator(); i.hasNext();) {
            final String awareFile = (String) i.next();
            final String name = awareFile.replace(File.separatorChar, '.')
                .substring(0, awareFile.length() - 6); // removes the ".class"
            if (generatedClasses.contains(name)) {
                // do not enhance previously generated classes
                continue;
            }
            enhanced++;
            logger.log(BasicLevel.DEBUG, "Enhancing persistent aware class '"
                    + name + "'");
            ClassWriter cw = new ClassWriter(false);
            ClassVisitor current = cw;
            current = new PersistenceAwareClassModifier(cw, scp.smi, logger, personality);

            ClassReader cr = loadJavaClass(
                    isSrcJar, name, scp.awareFilesDir, false);
            cr.accept(current, Attributes.getDefaultAttributes(), false);

            writeJavaClass(name, cw, scp.output);
        }
    }

    private Set computeGeneratedClasses() {
        Set result = new TreeSet();
        Collection xmls = scp.getXmldescriptor().values();
        for (Iterator itDesc = xmls.iterator(); itDesc.hasNext();) {
            SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next();
            for (Iterator itPack = desc.packages.values().iterator(); itPack.hasNext();) {
                SpeedoPackage sp = (SpeedoPackage) itPack.next();
                for (Iterator itclass = sp.classes.values().iterator(); itclass.hasNext();) {
                    SpeedoClass sc = (SpeedoClass) itclass.next();
                    String name = sp.name + '.' + sc.name;
                    String accessorName = NamingRules.accessorName(name);
                    String mappingName = sp.name + '.' + scp.mapperName + '.' + NamingRules.mappingName(sc.name);
                    String iteratorName = sp.name + '.' + scp.mapperName + '.' + sc.name + "ResultIterator";
                    String fieldsName = NamingRules.fieldsName(name);
                    result.add(name);
                    result.add(accessorName);
                    result.add(mappingName);
                    result.add(iteratorName);
                    result.add(fieldsName);
                }
            }
        }
        return result;
    }
}
TOP

Related Classes of org.objectweb.speedo.generation.enhancer.aware.PersistenceAwareEnhancer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.