Package org.xdoclet.plugin.ejb.interfaces

Source Code of org.xdoclet.plugin.ejb.interfaces.RemoteInterfacePlugin

/*
* Copyright (c) 2005
* XDoclet Team
* All rights reserved.
*/
package org.xdoclet.plugin.ejb.interfaces;

import java.rmi.RemoteException;

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;

/**
* @author Diogo Quintela
* @author Aslak Hellesøy
* @version $Revision: 538 $
*/
public class RemoteInterfacePlugin extends RemoteInterfaceBase implements EjbJavaClassBuilder {
    private EjbBusinessUtils ejbBusinessUtils;

    // TODO: Support ejb:bean remote-business-interface
    public RemoteInterfacePlugin(MergeableVelocityTemplateEngine templateEngine,
            WriterMapper writerMapper, EjbConfig config) {
        super(templateEngine, writerMapper, config);
        EjbRuntime.setPlugin(this);
        setPackageregex("beans");
        setPackagereplace("interfaces");
        super.setFileregex(config.getEjbReplaceRegex());
        setFilereplace("Remote");
        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.getExtends() != null)) {
            extendsLst.addAll(Arrays.asList(interfaceTag.getExtends()));
        }

        if (!extendsLst.contains("javax.ejb.EJBObject")) {
            extendsLst.add("javax.ejb.EJBObject");
        }

        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("remote"));
        if (generate) generate = isDestinationDirty(javaClass);
        if (generate && verbose) System.out.println(
                "Generating Remote Interface for " + javaClass.getName());
        return generate;
    }

    protected String getLocalyDefinedFullClassName(JavaClass clazz) {
        EjbInterfaceTag interfaceTag = (EjbInterfaceTag) clazz.getTagByName(TagLibrary.EJB_INTERFACE);
        return interfaceTag != null ? interfaceTag.getRemoteClass() : super.getLocalyDefinedFullClassName(clazz);
    }

    protected String getLocalyDefinedPackageName(JavaClass clazz) {
        EjbInterfaceTag interfaceTag = (EjbInterfaceTag) clazz.getTagByName(TagLibrary.EJB_INTERFACE);
        String definedPackage = null;

        if (interfaceTag != null) {
            definedPackage = interfaceTag.getRemotePackage();

            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.getRemotePattern();

            if ((pattern == null) && (interfaceTag.getPattern() != null)) {
                pattern = interfaceTag.getPattern() + "Remote";
            }
        }

        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 ejbUtils.injectMethodThrowException(ejbBusinessUtils.getInterfaceMethods(clazz, EjbUtils.REMOTE),
            RemoteException.class);
    }
}
TOP

Related Classes of org.xdoclet.plugin.ejb.interfaces.RemoteInterfacePlugin

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.