⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interfacedescription.java

📁 MobilerRPC enables remote procedure calls between J2ME applications and java sevlet server.
💻 JAVA
字号:
/*
 * Copyright (c) 2003, Artem Rudoy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.sourceforge.mobilerpc;

/**
 * $Id: InterfaceDescription.java,v 1.10 2003/06/19 12:46:37 artyomr Exp $
 * @author Artem Rudoy
 */
public class InterfaceDescription
{
    private String name = null;
    private String commonPackageName = null;
    private boolean supportCookie = false;
    private Method[] methods = null;
    private Constant[] constants = null;

    public InterfaceDescription(String name, String commonPackageName, boolean supportCookie, Method[] methods, Constant[] constants)
    {
        this.name = name;
        this.commonPackageName = commonPackageName;
        this.supportCookie = supportCookie;
        this.methods = methods;
        this.constants = constants;
    }

    public String getName()
    {
        return name;
    }

    public void generateServerFile(IndentPrintStream ps, String packageName)
    {
        ps.println("package " + packageName + ";");
        ps.println();
        ps.println("import java.io.*;");
        ps.println("import javax.servlet.*;");
        ps.println("import javax.servlet.http.*;");
        ps.println();
        ps.println("import " + commonPackageName + ".*;");
        ps.println();
        ps.println(Generator.FILE_HEADER);
        ps.println("public abstract class " + getName() + " extends HttpServlet");
        ps.println("{");
        ps.increase();

        // Generate constants
        if(constants != null && constants.length > 0)
        {
            for(int i = 0; i < constants.length; i++)
            {
                constants[i].generateCode(ps);
            }
            ps.println();
        }

        ps.println("public void doGet(HttpServletRequest request, HttpServletResponse response) " +
                "throws ServletException, IOException");
        ps.println("{");
        ps.println("    doPost(request, response);");
        ps.println("}");
        ps.println();
        ps.println("public void doPost(HttpServletRequest request, HttpServletResponse response) " +
                "throws ServletException, IOException");
        ps.println("{");
        ps.increase();
        ps.println("DataInputStream dataInput = null;");
        ps.println("DataOutputStream dataOutput = null;");
        ps.println("response.setContentType(\"text/plain\");");
        ps.println("try");
        ps.println("{");
        ps.increase();
        ps.println("dataInput = new DataInputStream(new DecodeInputStream(new BufferedInputStream(request.getInputStream())));");
        ps.println("dataOutput = new DataOutputStream(new EncodeOutputStream(new BufferedOutputStream(response.getOutputStream())));");
        ps.println("String methodName = dataInput.readUTF();");
        // Generate method invocations
        for(int i = 0; i < methods.length; i++)
        {
            if(i > 0)
                ps.print("else ");
            Method method = methods[i];
            ps.println("if(methodName.equals(\"" + method.getName() + "\"))");
            ps.println("{");
            ps.increase();
            method.generateServerCode(ps, "dataInput", "dataOutput", supportCookie);
            ps.decrease();
            ps.println("}");
        }
        ps.decrease();
        ps.println("}");
        ps.println("finally");
        ps.println("{");
        ps.increase();
        ps.println("try { dataInput.close(); } catch(Exception e) {}");
        ps.println("try { if(dataOutput != null) dataOutput.close(); } catch(Exception e) {}");
        ps.decrease();
        ps.println("}");
        ps.decrease();
        ps.println("}");
        ps.println();


        // Generate abstract methods
        for(int i = 0; i < methods.length; i++)
        {
            methods[i].generateAbstractMethod(ps, supportCookie);
        }
        ps.decrease();
        ps.println("}");
    }

    public void generateJ2MEFile(IndentPrintStream ps, String packageName)
    {
        ps.println("package " + packageName + ";");
        ps.println();
        ps.println("import java.io.*;");
        ps.println("import javax.microedition.io.*;");
        ps.println();
        ps.println("import " + commonPackageName + ".*;");
        ps.println();
        ps.println(Generator.FILE_HEADER);
        ps.println("public class " + getName());
        ps.println("{");
        ps.increase();

        // Generate constants
        if(constants != null && constants.length > 0)
        {
            for(int i = 0; i < constants.length; i++)
            {
                constants[i].generateCode(ps);
            }
            ps.println();
        }

        ps.println("private String servantURL;");
        ps.println();

        // Generate constructor
        ps.println("public " + getName() + "(String servantURL)");
        ps.println("{");
        ps.println("    this.servantURL = servantURL;");
        ps.println("}");
        ps.println();

        // Generate setter for servantURL
        ps.println("public void setServantURL(String servantURL)");
        ps.println("{");
        ps.println("    this.servantURL = servantURL;");
        ps.println("}");
        ps.println();

        // Generate getter for servantURL
        ps.println("public String getServantURL()");
        ps.println("{");
        ps.println("    return servantURL;");
        ps.println("}");
        ps.println();

        for(int i = 0; i < methods.length; i++)
        {
            if(i > 0)
                ps.println();
            methods[i].generateJ2MECode(ps, supportCookie);
        }
        ps.decrease();
        ps.println("}");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -