/*
* Copyright (c) 2005
* XDoclet Team
* All rights reserved.
*/
package org.xdoclet.plugin.ejb;
import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.generama.QDoxCapableMetadataProvider;
import org.generama.TemplateEngine;
import org.generama.WriterMapper;
import org.generama.defaults.QDoxPlugin;
import org.xdoclet.plugin.ejb.qtags.TagLibrary;
import com.thoughtworks.qdox.model.JavaClass;
/**
* Base QDoxPlugin for EjbRelated plugins
*
* @author Diogo Quintela
* @author Ive Hellemans
* @version $Revision: 538 $
*/
public abstract class EjbQDoxPlugin extends QDoxPlugin {
/** The log object */
protected Log log;
/** Ejb Configuration object */
protected final EjbConfig config;
/** Ejb Utils */
protected final EjbUtils ejbUtils;
/** Directory of merge file(s) */
private File mergeDir;
/** Whether or not the creation of a target file should be forced. */
protected boolean force = false;
/** True if this plugin should explain what it's doing. */
protected boolean verbose;
public EjbQDoxPlugin(TemplateEngine templateEngine,
WriterMapper writerMapper, EjbConfig config) {
super(templateEngine, config.getMetadataProvider(), writerMapper);
this.config = config;
this.ejbUtils = new EjbUtils(config);
this.log = getLog();
registerTagLibraries(metadataProvider);
if (log.isTraceEnabled()) {
log.trace(getClass().getName() + " constructor was called.");
}
}
protected void registerTagLibraries(QDoxCapableMetadataProvider provider) {
new TagLibrary(provider);
}
public static Log getLog(Class clazz) {
return LogFactory.getLog(clazz);
}
public Log getLog() {
return getLog(getClass());
}
public Log getLogEx() {
StackTraceElement[] stack = new Exception().getStackTrace();
StackTraceElement callingMethod = stack[1];
return LogFactory.getLog(callingMethod.getClassName() + '.' + callingMethod.getMethodName());
}
public EjbConfig getConfig() {
return config;
}
public EjbVersion getVersion() {
return getConfig().getEjbVersion();
}
public EjbUtils getEjbUtils() {
return ejbUtils;
}
/**
* Setter for mergeDir property
*
* @param mergeDir The value for the property
*/
public void setMergedir(File mergeDir) {
this.mergeDir = mergeDir;
}
/**
* Utility method called from jelly script to resolve a mergeFile reference
*
* @param mergeFile The mergeFile to look for
*
* @return A File for mergeFile
*/
public File getMergeFile(String mergeFile) {
if ((mergeFile != null) && (mergeDir != null) && mergeDir.isDirectory()) {
// The listing of mergeDir's files avoid possibly security issues in path resolving (paranoid?)
File[] files = mergeDir.listFiles();
for (int i = 0; i < files.length; i++) {
if (mergeFile.trim().equals(files[i].getName())) {
return files[i];
}
}
}
return null;
}
/**
* Sets the force flag. When set to true, the destination file is re-generated
* even if it is more recent then any of the beans.
*
* @param force
*/
public void setForce(boolean force) {
this.force = force;
}
/**
* Returns whether or not this plugin is being ran in verbose mode.
*/
public boolean isVerbose() {
return verbose;
}
/**
* Sets the verbose flag. When verbose = true, the plugin will explain
* what it's doing, otherwise it will run silently.
*/
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
/**
* Displays a message saying that a file is being created (when
* ran in verbose mode).
*/
protected void preGenerate() {
super.preGenerate();
if (verbose) System.out.println("Generating " + getFileName());
}
/**
* Returns a File object representing the Source File represented
* by the given JavaClass
* @param javaClass The source JavaClass
* @return A File Object representing the source file.
*/
protected File getSourceFile(JavaClass javaClass) {
return new File(javaClass.getSource().getURL().getFile());
}
/**
* Returns a File Object representing the
* @return
*/
protected File getDestinationFile() {
return new File(getDestdirFile(), getFileName());
}
/**
* Returns the name of the file to generate.
*/
public abstract String getFileName();
}