/*
* Copyright (c) 2005
* XDoclet Team
* All rights reserved.
*/
package org.xdoclet.plugin.ejb.descriptor;
import java.io.File;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.generama.JellyTemplateEngine;
import org.generama.WriterMapper;
import org.generama.defaults.XMLOutputValidator;
import org.xdoclet.plugin.ejb.AbstractEjbJarXmlPlugin;
import org.xdoclet.plugin.ejb.EjbBeanResolver;
import org.xdoclet.plugin.ejb.EjbConfig;
import org.xdoclet.plugin.ejb.EjbIds;
import org.xdoclet.plugin.ejb.EjbRuntime;
import org.xdoclet.plugin.ejb.EjbVersion;
import org.xdoclet.plugin.ejb.Relation;
import org.xdoclet.plugin.ejb.RelationManager;
import org.xdoclet.plugin.ejb.entity.PrimaryKeyClassPlugin;
import org.xdoclet.plugin.ejb.interfaces.LocalHomeInterfacePlugin;
import org.xdoclet.plugin.ejb.interfaces.LocalInterfacePlugin;
import org.xdoclet.plugin.ejb.interfaces.RemoteHomeInterfacePlugin;
import org.xdoclet.plugin.ejb.interfaces.RemoteInterfacePlugin;
import org.xdoclet.plugin.ejb.interfaces.ServiceEndpointPlugin;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;
/**
* @author Aslak Hellesøy
* @author Diogo Quintela
* @version $Revision: 538 $
*/
public class EjbJarXmlPlugin extends AbstractEjbJarXmlPlugin {
private String description;
private String displayname;
private String smallicon;
private String largeicon;
private boolean useids = true;
/** Default name for generated file */
private String fileName = "ejb-jar.xml";
private String clientjar;
private EjbBeanResolver beanResolver = null;
// DONE: Pending fix to be applied: http://issues.apache.org/jira/browse/JELLY-213
// DONE: Update jelly tags dependency
// DONE: Will XMLOutputValidator support validation of schemas ?? Yes, needs my patch
// TODO: Some common DTD/XSD Locations should be moved into generama (near XMLOutputValidator / MapPairsEntityResolver) ?
// DONE: EjbInterfaceTag: @qtags.comma-list
// DONE: EjbRuntimeSingleton instead of EjbUtils.get(config);
public EjbJarXmlPlugin(JellyTemplateEngine jellyTemplateEngine, //QDoxCapableMetadataProvider metadataProvider,
WriterMapper writerMapper, EjbConfig config) {
super(jellyTemplateEngine, /* new QDoxCachedMetadataProvider(metadataProvider), */ writerMapper, config);
EjbRuntime.setPlugin(this);
Map dtds = EjbVersion.fillEntityResolverMap(new HashMap());
super.setMultioutput(false);
setOutputValidator(new XMLOutputValidator(dtds));
}
protected void populateContextMap(Map map) {
super.populateContextMap(map);
map.put("util", getEjbUtils());
map.put("version", getVersion());
}
/**
* Don't let multioutput be changed
*/
public void setMultioutput(boolean multioutput) {
throw new RuntimeException("Can't set multioutput for plugin");
}
/**
* Setter for fileName property
*
* @param fileName The value for the property
*
* @throws NullPointerException if fileName == null
*/
public void setFilename(String fileName) {
if (fileName == null) {
throw new NullPointerException();
}
this.fileName = fileName;
}
/**
* Getter or the fileName property.
*/
public String getFileName() {
return this.fileName;
}
/**
* Utility method called from jelly script to resolve a mergeFile reference
*
* @param mergeFile The mergeFile to look for
* @param clazz JavaClass name for name expansion
*
* @return A File for mergeFile
*/
public File getMergeFile(String mergeFile, JavaClass clazz) {
return getMergeFile(expandFileName(mergeFile, clazz));
}
public String expandFileName(String mergeFile, JavaClass clazz) {
return MessageFormat.format(mergeFile, new String[] {clazz.getName()});
}
public LocalInterfacePlugin getLocalInterfacePlugin() {
return EjbRuntime.getLocalInterfacePlugin();
}
public RemoteInterfacePlugin getRemoteInterfacePlugin() {
return EjbRuntime.getRemoteInterfacePlugin();
}
public boolean shouldGenerate(Object metadata) {
return ejbUtils.shouldGenerate(metadata);
}
public String idFor(JavaClass javaClass) {
return useids ? EjbIds.escapeId(ejbUtils.entityType(javaClass) + ' ' + ejbUtils.getEjbName(javaClass)) : null;
}
public String prefixedId(String prefix) {
return useids ? EjbIds.get().prefixedId(prefix) : null;
}
public Collection getRelationships(Collection metadata) {
RelationManager relationManager = ejbUtils.createRelationManager(metadata);
return CollectionUtils.collect(Arrays.asList(relationManager.getRelations()),
new Transformer() {
public Object transform(Object arg0) {
return new RelationHolder((Relation) arg0);
}
});
}
public LocalHomeInterfacePlugin getLocalHomeInterfacePlugin() {
return EjbRuntime.getLocalHomeInterfacePlugin();
}
public RemoteHomeInterfacePlugin getRemoteHomeInterfacePlugin() {
return EjbRuntime.getRemoteHomeInterfacePlugin();
}
public PrimaryKeyClassPlugin getPrimaryKeyClassPlugin() {
return EjbRuntime.getPrimaryKeyClassPlugin();
}
public ServiceEndpointPlugin getServiceEndpointPlugin() {
return EjbRuntime.getServiceEndpointPlugin();
}
public String getClientjar() {
return this.clientjar;
}
public void setClientjar(String clientjar) {
this.clientjar = clientjar;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDisplayname() {
return this.displayname;
}
public void setDisplayname(String displayname) {
this.displayname = displayname;
}
public String getLargeicon() {
return this.largeicon;
}
public void setLargeicon(String largeicon) {
this.largeicon = largeicon;
}
public String getSmallicon() {
return this.smallicon;
}
public void setSmallicon(String smallicon) {
this.smallicon = smallicon;
}
public void setUseids(boolean useids) {
this.useids = useids;
}
/**
* Overrides pluggin start. Used for late setting of generated file name, in case the user whishes to
* change it
*/
public void start() {
setFilereplace(fileName);
beanResolver = ejbUtils.createEjbBeanResolver(getMetadata());
super.start();
}
public JavaClass findEjbRef(String ejbName) {
return ejbUtils.findEjbRef(ejbName, beanResolver);
}
public class RelationHolder {
public final static String ONE = "One";
public final static String MANY = "Many";
private final Relation relation;
public RelationHolder(Relation relation) {
this.relation = relation;
}
public String getName() {
return this.relation.getName();
}
public String getLeftRoleName() {
return this.relation.getLeftRoleName();
}
public boolean isLeftCascadeDelete() {
return this.relation.isLeftCascadeDelete();
}
public boolean isRightCascadeDelete() {
return this.relation.isRightCascadeDelete();
}
public JavaMethod getLeftMethod() {
return this.relation.getLeftMethod();
}
public JavaMethod getRightMethod() {
return this.relation.getRightMethod();
}
public boolean isLeftMany() {
return this.relation.isLeftMany();
}
public boolean isRightMany() {
return this.relation.isRightMany();
}
public String getRightRoleName() {
return this.relation.getRightRoleName();
}
public String getLeftEJBName() {
return this.relation.getLeftEJBName();
}
public String getRightEJBName() {
return this.relation.getRightEJBName();
}
public String getRightMultiplicity() {
return this.relation.isRightMany() ? MANY : ONE;
}
public String getLeftMultiplicity() {
return this.relation.isLeftMany() ? MANY : ONE;
}
}
}