j2semsgserviceproxy.java

来自「cqME :java framework for TCK test.」· Java 代码 · 共 177 行

JAVA
177
字号
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.j2me.services.messagingService;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.net.URI;import java.net.URISyntaxException;import com.sun.tck.j2me.services.commService.Utils;/** * * */public class J2SEMsgServiceProxy implements MessageExchanger {    // Private constructor, objects are created via getInstance().    private J2SEMsgServiceProxy() {}    /**     * Creates and returns a <b>new</b> J2SEMsgServiceProxy instance.     */    public static J2SEMsgServiceProxy getInstance() {        // there is no need to have a singleton here, really        return new J2SEMsgServiceProxy();    }    public synchronized Message[] exchangeMessages(String from, Message[] messages) throws IOException {               ByteArrayOutputStream baos =  new ByteArrayOutputStream();        DataOutputStream dos = new DataOutputStream(baos);        try {            dos.writeUTF(from);            if (messages == null) {                dos.writeInt(-1);            }            dos.writeInt(messages.length);            for (int i = 0; i < messages.length; i++) {                messages[i].write(dos);            }        } finally {            if (dos != null) {                try {                    dos.close();                } catch (IOException e1) {                    // Do nothing                }            }        }        byte[] packet = baos.toByteArray();        out.writeInt(packet.length);        out.write(packet);        out.flush();        int respPacketLength = in.readInt();        byte[] respPacket = new byte[respPacketLength];        in.readFully(respPacket);        ByteArrayInputStream bais = new ByteArrayInputStream(respPacket);        DataInputStream dis = new DataInputStream(bais);        byte status = dis.readByte();        if (status == Utils.STATUS_OK) {            int length = dis.readInt();            if (length < 0) {                throw new RuntimeException(                        "Invalid response from Messaging Service."                        + " Negative number of messages.");            }            Message[] responseMessages = new Message[length];            for (int i = 0; i < responseMessages.length; i++) {                responseMessages[i] = Message.read(dis);            }            return responseMessages;        } else {            throw new RuntimeException("Messaging Service reported error.");        }    }    /**     * Initializes Messaging Service Proxy.     * @throws RuntimeException if impossible to initialize.     */    public void init(String msgSwitchAddr) {        try {            URI uri = new URI("socket://" + msgSwitchAddr);            String host = uri.getHost();            int port = uri.getPort();            createSocket(host, port);        } catch (URISyntaxException uriEx) {            throw new RuntimeException(                    "Cannot initialize J2SE Messaging Service Proxy: "                    + uriEx.getMessage(), uriEx);        }    }    private void createSocket(String host, int port) {        try {            s = new Socket(host, port);            in = new DataInputStream(s.getInputStream());            out = new DataOutputStream(s.getOutputStream());        } catch (IOException e) {            e.printStackTrace();            close();            throw new RuntimeException(                    "Cannot initialize J2SE Messaging Service Proxy: "                    + e.getMessage());        }    }    /**     *     */    public void close() {        if (out != null) {            try {                // Send shutdown notification to the server                out.writeInt(0);            } catch (IOException e1) {                // Do nothing            }            try {                out.close();            } catch (IOException e) {                // Do nothing            }        }        if (in != null) {            try {                in.close();            } catch (IOException e) {                // Do nothing            }        }        if (s != null) {            try {                s.close();            } catch (IOException e) {                // Do nothing            }        }    }    private Socket s;    private DataInputStream in;    private DataOutputStream out;}

⌨️ 快捷键说明

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