Package org.xdoclet.plugin.ejb.entity

Source Code of org.xdoclet.plugin.ejb.entity.LookupObjectPlugin

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

import java.util.Map;

import org.generama.MergeableVelocityTemplateEngine;
import org.generama.WriterMapper;

import org.xdoclet.plugin.ejb.EjbConfig;
import org.xdoclet.plugin.ejb.EjbHomeUtils;
import org.xdoclet.plugin.ejb.EjbJavaClassBuilder;
import org.xdoclet.plugin.ejb.EjbJavaGeneratingPlugin;
import org.xdoclet.plugin.ejb.EjbRuntime;
import org.xdoclet.plugin.ejb.EjbUtils;
import org.xdoclet.plugin.ejb.interfaces.LocalHomeInterfacePlugin;
import org.xdoclet.plugin.ejb.interfaces.RemoteHomeInterfacePlugin;
import org.xdoclet.plugin.ejb.qtags.EjbUtilTag;
import org.xdoclet.plugin.ejb.qtags.TagLibrary;

import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaField;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaParameter;
import com.thoughtworks.qdox.model.Type;

/**
* Plugin for lookup object generation
*
* @author Diogo Quintela
* @version $Revision: 538 $
*/
public class LookupObjectPlugin extends EjbJavaGeneratingPlugin implements EjbJavaClassBuilder {
    /** Home utils */
    protected EjbHomeUtils ejbHomeUtils;
    /** Should we cache the home object resolution ? */
    protected boolean cachehomes = true;

    /** Should we include code for GUID generation ? */
    protected boolean includeGUID = true;

    public LookupObjectPlugin(MergeableVelocityTemplateEngine templateEngine,
            WriterMapper writerMapper, EjbConfig config) {
        super(templateEngine, writerMapper, config);

        // EjbRuntime.setPlugin(this);
        setPackageregex("beans");
        setPackagereplace("util");
        super.setFileregex(config.getEjbReplaceRegex());
        setFilereplace("Util");
        super.setMultioutput(true);
        this.ejbHomeUtils = new EjbHomeUtils(this.ejbUtils);
    }

    protected void populateContextMap(Map map) {
        super.populateContextMap(map);
        map.put("homeUtil", getEjbHomeUtils());
    }

    public boolean isCachehomes() {
        return this.cachehomes;
    }

    public boolean isIncludeGUID() {
        return this.includeGUID;
    }

    public EjbHomeUtils getEjbHomeUtils() {
        return this.ejbHomeUtils;
    }

    /**
     * Don't let multioutput be changed
     */
    public void setMultioutput(boolean multioutput) {
        throw new RuntimeException("Can't set multioutput for plugin");
    }

    public void setCachehomes(boolean cachehomes) {
        this.cachehomes = cachehomes;
    }

    /**
     * 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());
    }

    /**
     * JavaClass that describes the generated artifacts
     *
     * @return The generated artifacts JavaClass otherwise null
     */
    public JavaClass[] buildFor(JavaClass metadata) {
        if (!shouldGenerate(metadata)) {
            return null;
        }

        JavaField field;
        JavaMethod method;
        JavaClass retVal = createDynamicJavaClass(getDestinationClassname(metadata),
                getDestinationPackage(metadata), null, getMetadataProvider());
        retVal.setModifiers(new String[] { "public" });
        if (getEjbUtils().isMessageDrivenBean(metadata)) {
            field = new JavaField(new Type(String.class.getName()), "DESTINATION_JNDI_NAME");
            field.setModifiers(new String[] { "private", "final", "static" });
            retVal.addField(field);
            field = new JavaField(new Type(String.class.getName()), "CONNECTION_FACTORY_JNDI_NAME");
            field.setModifiers(new String[] { "private", "final", "static" });
            retVal.addField(field);
        }
        if (isCachehomes()) {
            if (getEjbUtils().isMessageDrivenBean(metadata)) {
                field = new JavaField(new Type("javax.jms.Queue"), "cachedQueue");
                field.setModifiers(new String[] { "private", "static" });
                retVal.addField(field);
                field = new JavaField(new Type("javax.jms.QueueConnectionFactory"), "cachedConnectionFactory");
                field.setModifiers(new String[] { "private", "static" });
                retVal.addField(field);
            } else {
                if (hasRemoteView(metadata)) {
                    field = new JavaField(new Type(getRemoteHomeInterfacePlugin().getVirtualType(metadata)
                            .toString()), "cachedRemoteHome");
                    field.setModifiers(new String[] { "private", "static" });
                    retVal.addField(field);
                }
                if (hasLocalView(metadata)) {
                    field = new JavaField(new Type(getLocalHomeInterfacePlugin().getVirtualType(metadata)
                            .toString()), "cachedHome");
                    field.setModifiers(new String[] { "private", "static" });
                    retVal.addField(field);
                }
            }
        }

        method = new JavaMethod(getDestinationClassname(metadata));
        method.setConstructor(true);
        method.setModifiers(new String[] { "private" });
        retVal.addMethod(method);

        if (getEjbUtils().isMessageDrivenBean(metadata)) {
            method = new JavaMethod("getQueue");
            method.setModifiers(new String[] { "public", "static" });
            method.setExceptions(new Type[] { new Type("javax.naming.NamingException") });
            method.setReturns(new Type("javax.jms.Queue"));
            retVal.addMethod(method);
            method = new JavaMethod("getQueue");
            method.setModifiers(new String[] { "public", "static" });
            method.setParameters(new JavaParameter[] { new JavaParameter(new Type("java.util.Hashtable"),
                    "environment") });
            method.setExceptions(new Type[] { new Type("javax.naming.NamingException") });
            method.setReturns(new Type("javax.jms.Queue"));
            retVal.addMethod(method);
            method = new JavaMethod("getQueueConnection");
            method.setModifiers(new String[] { "public", "static" });
            method.setExceptions(new Type[] { new Type("javax.naming.NamingException"),
                    new Type("javax.jms.JMSException") });
            method.setReturns(new Type("javax.jms.QueueConnection"));
            retVal.addMethod(method);
            method = new JavaMethod("getQueueConnection");
            method.setModifiers(new String[] { "public", "static" });
            method.setParameters(new JavaParameter[] { new JavaParameter(new Type("java.util.Hashtable"),
                    "environment") });
            method.setExceptions(new Type[] { new Type("javax.naming.NamingException"),
                    new Type("javax.jms.JMSException") });
            method.setReturns(new Type("javax.jms.QueueConnection"));
            retVal.addMethod(method);
        } else {
            if (hasRemoteView(metadata)) {
                method = new JavaMethod("getHome");
                method.setModifiers(new String[] { "public", "static" });
                method.setExceptions(new Type[] { new Type("javax.naming.NamingException") });
                method
                        .setReturns(new Type(getRemoteHomeInterfacePlugin().getVirtualType(metadata)
                                .toString()));
                retVal.addMethod(method);
                method = new JavaMethod("getHome");
                method.setModifiers(new String[] { "public", "static" });
                method.setParameters(new JavaParameter[] { new JavaParameter(new Type("java.util.Hashtable"),
                        "env") });
                method.setExceptions(new Type[] { new Type("javax.naming.NamingException") });
                method
                        .setReturns(new Type(getRemoteHomeInterfacePlugin().getVirtualType(metadata)
                                .toString()));
                retVal.addMethod(method);
            }
            if (hasLocalView(metadata)) {
                method = new JavaMethod("getLocalHome");
                method.setModifiers(new String[] { "public", "static" });
                method.setExceptions(new Type[] { new Type("javax.naming.NamingException") });
                method
                        .setReturns(new Type(getLocalHomeInterfacePlugin().getVirtualType(metadata)
                                .toString()));
                retVal.addMethod(method);
                method = new JavaMethod("getLocalHome");
                method.setModifiers(new String[] { "public", "static" });
                method.setParameters(new JavaParameter[] { new JavaParameter(new Type("java.util.Hashtable"),
                        "env") });
                method.setExceptions(new Type[] { new Type("javax.naming.NamingException") });
                method
                        .setReturns(new Type(getLocalHomeInterfacePlugin().getVirtualType(metadata)
                                .toString()));
                retVal.addMethod(method);
            }
            method = new JavaMethod("lookupHome");
            method.setModifiers(new String[] { "private", "static" });
            method.setParameters(new JavaParameter[] {
                    new JavaParameter(new Type("java.util.Hashtable"), "env"),
                    new JavaParameter(new Type("java.lang.String"), "jndiName"),
                    new JavaParameter(new Type("java.lang.Class"), "narrowTo") });
            method.setExceptions(new Type[] { new Type("javax.naming.NamingException") });
            method.setReturns(new Type("java.lang.Object"));
            retVal.addMethod(method);
        }

        method = new JavaMethod("lookup");
        method.setModifiers(new String[] { "public", "static" });
        method.setParameters(new JavaParameter[] { new JavaParameter(new Type("java.util.Hashtable"), "env"),
                new JavaParameter(new Type("java.lang.String"), "jndiName") });
        method.setExceptions(new Type[] { new Type("javax.naming.NamingException") });
        method.setReturns(new Type("java.lang.Object"));
        retVal.addMethod(method);

        if (isIncludeGUID()) {
            method = new JavaMethod("generateGUID");
            method.setModifiers(new String[] { "public", "final", "static" });
            method.setParameters(new JavaParameter[] {

            new JavaParameter(new Type("java.lang.Object"), "o") });

            method.setReturns(new Type("java.lang.String"));
            retVal.addMethod(method);
            field = new JavaField(new Type("java.lang.String"), "hexServerIP");
            field.setModifiers(new String[] { "private", "static" });
            retVal.addField(field);
            field = new JavaField(new Type("java.security.SecureRandom"), "seeder");
            field.setModifiers(new String[] { "private", "final", "static" });
            retVal.addField(field);
            method = new JavaMethod("hexFormat");
            method.setModifiers(new String[] { "private", "static" });
            method.setParameters(new JavaParameter[] {

            new JavaParameter(new Type("byte", 1), "value"), new JavaParameter(new Type("int"), "length") });

            method.setReturns(new Type("java.lang.String"));
            retVal.addMethod(method);
            method = new JavaMethod("hexFormat");
            method.setModifiers(new String[] { "private", "static" });
            method.setParameters(new JavaParameter[] {

            new JavaParameter(new Type("int"), "value"), new JavaParameter(new Type("int"), "length") });

            method.setReturns(new Type("java.lang.String"));
            retVal.addMethod(method);
        }

        return new JavaClass[] { retVal };
    }

    public boolean shouldGenerate(Object metadata) {
        JavaClass javaClass = (JavaClass) metadata;
        EjbUtilTag utilTag = (EjbUtilTag) javaClass.getTagByName(TagLibrary.EJB_UTIL);
        boolean generate = ejbUtils.shouldGenerate(metadata);
        generate = generate && ((utilTag == null) || (utilTag.isGenerate()));
        if (generate) generate = isDestinationDirty(javaClass);
        if (generate && verbose) System.out.println(
                "Generating Lookup Object for " + javaClass.getName());
        return generate;
    }

    public LocalHomeInterfacePlugin getLocalHomeInterfacePlugin() {
        return EjbRuntime.getLocalHomeInterfacePlugin();
    }

    public RemoteHomeInterfacePlugin getRemoteHomeInterfacePlugin() {
        return EjbRuntime.getRemoteHomeInterfacePlugin();
    }

    public boolean hasRemoteView(JavaClass javaClass) {
        return EjbUtils.hasFlag(ejbUtils.getViewType(javaClass), EjbUtils.REMOTE) &&
        getRemoteHomeInterfacePlugin().shouldGenerate(javaClass);
    }

    public boolean hasLocalView(JavaClass javaClass) {
        return EjbUtils.hasFlag(ejbUtils.getViewType(javaClass), EjbUtils.LOCAL) &&
        getLocalHomeInterfacePlugin().shouldGenerate(javaClass);
    }

    public String lookupVarName(JavaClass javaClass) {
        EjbUtilTag utilTag = (EjbUtilTag) javaClass.getTagByName(TagLibrary.EJB_UTIL);
        return ((utilTag == null) || "logical".equals(utilTag.getLookupKind())) ? ejbHomeUtils.getJndiNameConst()
                                                                                : ejbHomeUtils.getCompleteNameConst();
    }
}
TOP

Related Classes of org.xdoclet.plugin.ejb.entity.LookupObjectPlugin

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.