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

📄 method.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: Method.java,v 1.15 2003/06/19 12:46:37 artyomr Exp $ * @author Artem Rudoy */public class Method{    public static byte RETURN_OK = 0;    public static byte RETURN_VOID = 1;    public static byte RETURN_EXCEPTION = 2;    public String name = null;    public Attribute returnType = null;    public Attribute[] parameters = null;    public ExceptionDescription[] exceptions = null;    public Method(String name, Attribute returnType, Attribute[] parameters, ExceptionDescription[] exceptions)    {        this.name = name;        this.returnType = returnType;        this.parameters = parameters;        this.exceptions = exceptions;    }    public String getName()    {        return name;    }    public void generateServerCode(IndentPrintStream ps, String dataInputName, String dataOutputName, boolean supportCookie)    {        // Code to read parameters        ps.println("// Load parameters");        for(int i = 0; i < parameters.length; i++)        {            Attribute parameter = parameters[i];            ps.println(parameter.getFullTypeName() + " " + parameter.getName() + ";");            if(parameter.isArray())                parameter.getType().generateLoadArrayCode(ps, parameter.getName(), dataInputName);            else                parameter.getType().generateLoadCode(ps, parameter.getName(), dataInputName);            ps.println();        }        ps.println("// Invoke method");        if(exceptions.length > 0)        {            ps.println("try");            ps.println("{");            ps.increase();        }        if(returnType != null)        {            ps.print(returnType.getFullTypeName() + " methodResult = ");        }        ps.print(name + "(");        if(supportCookie)        {            ps.print("request, response");        }        for(int i = 0; i < parameters.length; i++)        {            if(i > 0 || supportCookie)                ps.print(", ");            ps.print(parameters[i].getName());        }        ps.println(");");        ps.println("// Write result");        if(returnType != null)        {            ps.println(dataOutputName + ".writeByte(" + RETURN_OK + ");");            if(returnType.isArray())                returnType.getType().generateWriteArrayCode(ps, "methodResult", dataOutputName);            else                returnType.getType().generateWriteCode(ps, "methodResult", dataOutputName);        }        else        {            ps.println(dataOutputName + ".writeByte(" + RETURN_VOID + ");");        }        if(exceptions.length > 0)        {            ps.decrease();            ps.println("}");            for(int i = 0; i < exceptions.length; i++)            {                ExceptionDescription exception = exceptions[i];                ps.println("catch(" + exception.getName() + " e)");                ps.println("{");                ps.increase();                ps.println(dataOutputName + ".writeByte(" + RETURN_EXCEPTION + ");");                ps.println(dataOutputName + ".writeUTF(\"" + exception.getFullName() + "\");");                ps.println("if(e.getMessage() == null)");                ps.println("    " + dataOutputName + ".writeBoolean(false);");                ps.println("else");                ps.println("{");                ps.increase();                ps.println(dataOutputName + ".writeBoolean(true);");                ps.println(dataOutputName + ".writeUTF(e.getMessage());");                ps.decrease();                ps.println("}");                ps.decrease();                ps.println("}");            }        }        //ps.println(dataOutputName + ".flush();");        ps.println(dataOutputName + ".close();");        ps.println(dataOutputName + " = null;");    }    public void generateAbstractMethod(IndentPrintStream ps, boolean supportCookie)    {        String returnString;        if(returnType == null)            returnString = "void";        else            returnString = returnType.getFullTypeName();        ps.print("public abstract " + returnString + " " + name + "(");        if(supportCookie)            ps.print("HttpServletRequest request, HttpServletResponse response");        for(int i = 0; i < parameters.length; i++)        {            if(i > 0 || supportCookie)                ps.print(", ");            Attribute parameter = parameters[i];            ps.print(parameter.getFullTypeName() + " " + parameter.getName());        }        ps.print(")");        if(exceptions.length > 0)        {            ps.print(" throws");            for(int i = 0; i < exceptions.length; i++)            {                if(i > 0)                    ps.print(",");                ps.print(" " + exceptions[i].getName());            }        }        ps.println(";");    }    public void generateJ2MECode(IndentPrintStream ps, boolean supportCookie)    {        String returnString;        if(returnType == null)            returnString = "void";        else            returnString = returnType.getFullTypeName();        ps.print("public " + returnString + " " + name + "(");        for(int i = 0; i < parameters.length; i++)        {            if(i > 0)                ps.print(", ");            Attribute parameter = parameters[i];            ps.print(parameter.getFullTypeName() + " " + parameter.getName());        }        ps.print(")");        ps.print(" throws ");        for(int i = 0; i < exceptions.length; i++)        {            ps.print(exceptions[i].getName() + ", ");        }        ps.print("IOException");        ps.println();        ps.println("{");        ps.increase();        ps.println("DataInputStream dataInput = null;");        ps.println("DataOutputStream dataOutput = null;");        ps.println("HttpConnection conn = null;");        ps.println("try");        ps.println("{");        ps.increase();        ps.println("conn = (HttpConnection)Connector.open(servantURL);");        ps.println("conn.setRequestMethod(HttpConnection.POST);");        if(supportCookie)        {            ps.println("if(Utils.cookie != null)");            ps.println("    conn.setRequestProperty(\"cookie\", Utils.cookie);");        }        //ps.println("dataOutput = new DataOutputStream(new EncodeOutputStream(((OutputConnection)conn).openOutputStream()));");        ps.println("dataOutput = new DataOutputStream(new EncodeOutputStream(conn.openOutputStream()));");        ps.println();        ps.println("// Write method name");        ps.println("dataOutput.writeUTF(\"" + name + "\");");        ps.println("// Write parameters");        for(int i = 0; i < parameters.length; i++)        {            Attribute parameter = parameters[i];            if(parameter.isArray())                parameter.getType().generateWriteArrayCode(ps, parameter.getName(), "dataOutput");            else                parameter.getType().generateWriteCode(ps, parameter.getName(), "dataOutput");            ps.println();        }        //ps.println("dataOutput.flush();");        ps.println("dataOutput.close();");        ps.println("dataOutput = null;");        ps.println();        ps.println("dataInput = new DataInputStream(new DecodeInputStream(conn.openInputStream()));");        ps.println("byte resultCode = dataInput.readByte();");        if(supportCookie)        {            ps.println("String tmpCookie = conn.getHeaderField(\"set-cookie\");");            ps.println("if(tmpCookie != null)");            ps.println("    Utils.cookie = tmpCookie;");        }        if(returnType != null)        {            ps.println("if(resultCode == " + RETURN_OK + ")");            ps.println("{");            ps.increase();            ps.println(returnType.getFullTypeName() + " methodResult;");            if(returnType.isArray())                returnType.getType().generateLoadArrayCode(ps, "methodResult", "dataInput");            else                returnType.getType().generateLoadCode(ps, "methodResult", "dataInput");            ps.println("return methodResult;");            ps.decrease();            ps.println("}");        }        else        {            ps.println("if(resultCode == " + RETURN_VOID + ")");            ps.println("    return;");        }        if(exceptions.length > 0)        {            ps.println("else if(resultCode == " + RETURN_EXCEPTION + ")");            ps.println("{");            ps.increase();            ps.println("String exceptionClass = dataInput.readUTF();");            ps.println("String exceptionMessage = null;");            ps.println("if(dataInput.readBoolean())");            ps.println("    exceptionMessage = dataInput.readUTF();");            for(int i = 0; i < exceptions.length; i++)            {                if(i > 0)                    ps.print("else ");                ExceptionDescription exceptionDescription = exceptions[i];                ps.println("if(exceptionClass.equals(\"" + exceptionDescription.getFullName() + "\"))");                ps.println("    throw new " + exceptionDescription.getName() + "(exceptionMessage);");            }            ps.println("else");            ps.println("    throw new IOException(\"Unknown exception\");");            ps.decrease();            ps.println("}");        }        ps.println("else");        ps.println("    throw new IOException(\"Unknown response\");");        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.println("try { conn.close(); } catch(Exception e) {}");        ps.decrease();        ps.println("}");        ps.decrease();        ps.println("}");    }}

⌨️ 快捷键说明

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