/**
* Copyright (C) 2001-2005 France Telecom R&D
*/
package org.objectweb.speedo.generation;
import org.objectweb.speedo.ant.AntSpeedoScriptGenerator;
import org.objectweb.speedo.api.SpeedoException;
import org.objectweb.speedo.api.SpeedoProperties;
import org.objectweb.speedo.generation.api.GeneratorComponent;
import org.objectweb.speedo.generation.api.SpeedoCompilerParameter;
import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent;
import org.objectweb.speedo.lib.Personality;
import org.objectweb.speedo.metadata.SpeedoDefaults;
import org.objectweb.util.monolog.Monolog;
import org.objectweb.util.monolog.api.BasicLevel;
import java.util.HashMap;
/**
* Is the global engine of the Speedo enhancer/Compiler. This engine launches
* the different steps of the enhancement. Each step is realized by a
* GeneratorComponent implementation. This implementation is a GeneratorComponent
* itself. The list of used generator component is defined by the abstract
* method #getGeneratorComponents()
*
* @author S.Chassande-Barrioz
*/
public abstract class AbstractEnhancer extends AbstractGeneratorComponent {
private final static String LOGGER_NAME =
SpeedoProperties.LOGGER_NAME + ".generation.SpeedoCompiler";
/**
* @return the array of GeneratorComponent class to use. This array defines
* the different step of the Speedo enhancer.
* @see GeneratorComponent
*/
protected abstract GeneratorComponent[] getGeneratorComponents();
public AbstractEnhancer(Personality p) {
super(p);
}
// IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
//----------------------------------------------------//
public SpeedoCompilerParameter getSpeedoCompilerParameter() {
if (scp == null) {
scp = new SpeedoCompilerParameter();
scp.personality = personality;
}
return scp;
}
public boolean init() throws SpeedoException {
// Initialize the logger factory if necessary
if (scp.loggerFactory == null) {
if (scp.logPropFile == null) {
scp.loggerFactory = Monolog.initialize();
} else {
scp.loggerFactory = Monolog.getMonologFactory(scp.logPropFile);
}
logger = scp.loggerFactory.getLogger(LOGGER_NAME);
} else if (logger == null) {
logger = scp.loggerFactory.getLogger(LOGGER_NAME);
}
scp.nmf.setLogger(scp.loggerFactory.getLogger(SpeedoProperties.LOGGER_NAME + ".naming"));
scp.setXmldescriptor(new HashMap());
SpeedoDefaults.init(scp.personality);
return true;
}
public void process() throws SpeedoException {
if (scp.xml.isEmpty())
return;
long time = System.currentTimeMillis();
final GeneratorComponent[] gcs = getGeneratorComponents();
// Launch all steps of the speedo compiler
boolean run = true;
for (int i = 0; i < gcs.length && run; i++) {
long actionTime = System.currentTimeMillis();
gcs[i].setSpeedoCompilerParameter(scp);
if (gcs[i].init()) {
final String title = gcs[i].getTitle();
if (title != null) {
logger.log(BasicLevel.INFO, title);
}
gcs[i].process();
final String summary = gcs[i].getSummary();
if (summary != null) {
actionTime = System.currentTimeMillis() - actionTime;
logger.log(BasicLevel.DEBUG, "=>" + summary + ", " + AntSpeedoScriptGenerator.duration2str(actionTime));
}
} else {
run = false;
}
}
time = System.currentTimeMillis() - time;
logger.log(BasicLevel.INFO, "All Done in " + AntSpeedoScriptGenerator.duration2str(time));
}
}