/*
* Copyright (c) 2005
* XDoclet Team
* All rights reserved.
*/
package org.xdoclet.plugin.ejb.interfaces;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import org.generama.MergeableVelocityTemplateEngine;
import org.generama.WriterMapper;
import org.xdoclet.plugin.ejb.EjbBusinessUtils;
import org.xdoclet.plugin.ejb.EjbConfig;
import org.xdoclet.plugin.ejb.EjbJavaClassBuilder;
import org.xdoclet.plugin.ejb.EjbRuntime;
import org.xdoclet.plugin.ejb.EjbUtils;
import org.xdoclet.plugin.ejb.qtags.EjbInterfaceTag;
import org.xdoclet.plugin.ejb.qtags.TagLibrary;
import org.xdoclet.plugin.ejb.util.DuplicatedJavaMethod;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.Type;
/**
* DOCUMENT ME!
*
* @author Aslak Hellesøy
* @author Diogo Quintela
* @version $Revision: 538 $
*/
public class LocalInterfacePlugin extends LocalInterfaceBase implements EjbJavaClassBuilder {
private EjbBusinessUtils ejbBusinessUtils;
// TODO: Support ejb:bean local-business-interface
public LocalInterfacePlugin(MergeableVelocityTemplateEngine templateEngine,
WriterMapper writerMapper, EjbConfig config) {
super(templateEngine, writerMapper, config);
EjbRuntime.setPlugin(this);
setPackageregex("beans");
setPackagereplace("interfaces");
super.setFileregex(config.getEjbReplaceRegex());
setFilereplace("Local");
super.setMultioutput(true);
this.ejbBusinessUtils = new EjbBusinessUtils(ejbUtils);
}
/**
* JavaClass that describes the generated artifacts
*
* @return The generated artifacts JavaClass otherwise null
*/
public JavaClass[] buildFor(JavaClass metadata) {
if (!shouldGenerate(metadata)) {
return null;
}
JavaMethod method;
JavaClass retVal = createDynamicJavaClass(getDestinationClassname(metadata), getDestinationPackage(metadata), null, getMetadataProvider());
retVal.setInterface(true);
retVal.setModifiers(new String[]{"public"});
String[] extendz = getExtends(metadata);
Type[] extendzTypes = new Type[extendz.length];
for (int j = 0; j < extendz.length; j++) {
extendzTypes[j] = new Type(extendz[j]);
}
retVal.setImplementz(extendzTypes);
Collection methods = getInterfaceMethods(metadata);
for (Iterator iter = methods.iterator(); iter.hasNext();) {
method = new DuplicatedJavaMethod((JavaMethod)iter.next());
method.setModifiers(new String[0]);
retVal.addMethod(method);
}
return new JavaClass[]{retVal};
}
public String[] getExtends(JavaClass javaClass) {
Collection extendsLst = new ArrayList();
EjbInterfaceTag interfaceTag = (EjbInterfaceTag) javaClass.getTagByName(TagLibrary.EJB_INTERFACE);
if ((interfaceTag != null) && (interfaceTag.getLocalExtends() != null)) {
extendsLst.addAll(Arrays.asList(interfaceTag.getLocalExtends()));
}
if (!extendsLst.contains("javax.ejb.EJBLocalObject")) {
extendsLst.add("javax.ejb.EJBLocalObject");
}
return (String[]) extendsLst.toArray(new String[0]);
}
public boolean shouldGenerate(Object metadata) {
JavaClass javaClass = (JavaClass) metadata;
EjbInterfaceTag interfaceTag = (EjbInterfaceTag) javaClass.getTagByName(TagLibrary.EJB_INTERFACE);
boolean generate = super.shouldGenerate(metadata);
generate = generate && ((interfaceTag == null) || Arrays.asList(interfaceTag.getGenerate()).contains("local"));
if (generate) generate = isDestinationDirty(javaClass);
if (generate && verbose) System.out.println(
"Generating Local Interface for " + javaClass.getName());
return generate;
}
protected String getLocalyDefinedFullClassName(JavaClass clazz) {
EjbInterfaceTag interfaceTag = (EjbInterfaceTag) clazz.getTagByName(TagLibrary.EJB_INTERFACE);
return interfaceTag != null ? interfaceTag.getLocalClass() : super.getLocalyDefinedFullClassName(clazz);
}
protected String getLocalyDefinedPackageName(JavaClass clazz) {
EjbInterfaceTag interfaceTag = (EjbInterfaceTag) clazz.getTagByName(TagLibrary.EJB_INTERFACE);
String definedPackage = null;
if (interfaceTag != null) {
definedPackage = interfaceTag.getLocalPackage();
if (definedPackage == null) {
definedPackage = interfaceTag.getPackage();
}
}
return definedPackage != null ? definedPackage : super.getLocalyDefinedPackageName(clazz);
}
protected String getPatternBasedUnqualifiedName(JavaClass clazz) {
EjbInterfaceTag interfaceTag = (EjbInterfaceTag) clazz.getTagByName(TagLibrary.EJB_INTERFACE);
String pattern = null;
if (interfaceTag != null) {
pattern = interfaceTag.getLocalPattern();
if ((pattern == null) && (interfaceTag.getPattern() != null)) {
pattern = interfaceTag.getPattern() + "Local";
}
}
return pattern != null ? ejbUtils.expandPattern(pattern, clazz) : super.getPatternBasedUnqualifiedName(clazz);
}
/**
* Don't let multioutput be changed
*/
public void setMultioutput(boolean multioutput) {
throw new RuntimeException("Can't set multioutput for plugin");
}
/**
* Don't let fileregex be changed
*/
public void setFileregex(String fileregex) {
throw new RuntimeException("Can't set fileregex for plugin. Try setting it in " + EjbConfig.class.getName());
}
public Collection getInterfaceMethods(final JavaClass clazz) {
return ejbBusinessUtils.getInterfaceMethods(clazz, EjbUtils.LOCAL);
}
}