harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r406944 [16/30] - in /incubator/harmony/enhanced/classlib/trunk/modules/rmi2: ./ build/ doc/ doc/testing/ doc/testing/rmi http tunneling/ doc/testing/rmi http tunneling/Results - ITC/ doc/testing/rmi http tunneling/Results - SUN/ doc/testin...
Date Tue, 16 May 2006 13:52:07 GMT
Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpInputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpInputStream.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpInputStream.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpInputStream.java Tue May 16 06:51:00 2006
@@ -0,0 +1,301 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.http;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * This class is used for client and server purposes. <br>
+ * Implements a byte array stream in which the data is read. On the other side,
+ * the constructor receive a socket's input stream. <br>
+ * Fills only once the internal buffer of the wrapped input stream (at the time 
+ * that requeries any operation from it) with the http's content and delegates the 
+ * all the calls to such input stream.   
+ * 
+ * @author Diego Raúl Mercado
+ */
+class HttpInputStream extends InputStream {
+
+    /** Exposed buffer */
+    private byte[] buffer;
+
+    /** Available int to read */
+    private int available;
+
+    /** Wrapped ByteArrayInputStream */
+    private ByteArrayInputStream bais;
+
+    /** Indicates CARRY-RETURN */
+    private static final int CR = 0xa;
+
+    /** Indicates LINE-FEEDBACK */
+    private static final int LF = 0xd;
+
+    /** Stores the header's value that has been parsed */
+    private Map<HttpHeaders, String> headers;
+
+    /** If true, indicates that the buffer has been filled */
+    private boolean isBufferFilled;
+
+    /** The socket's input stream */
+    private InputStream socketInputStream;
+
+    /** The path that is specified in the request/status line */
+    private String requestPath;
+
+    /**
+     * Statically stores the exception that may thrown if cannot parse the
+     * http's headers
+     */
+    private static final IOException HEADERS_EXCEPTION = new IOException(
+            "Cannot parse http headers");
+
+    /**
+     * Initializes the inherit's buffer and the map <code>headers</code>.
+     * Then, calls <code>fillBuffer()</code> method
+     * 
+     * @param is
+     *            the socket's input stream
+     */
+    public HttpInputStream(InputStream is) {
+        headers = new ConcurrentHashMap<HttpHeaders, String>();
+        isBufferFilled = false;
+        socketInputStream = is;
+    }
+
+    /**
+     * Parses any http's headers and fill the inherits' buffer with the http's
+     * content
+     * 
+     * @throws IOException
+     *             if an I/O exception ocurrs or cannot parse the http's header
+     */
+    private synchronized final void fillBuffer() throws IOException {
+        if (isBufferFilled == false) {
+            String line;
+            String value;
+            String[] splitLine;
+            boolean isCRLFPresent = false;
+            int contentLength = 0;
+
+            /*
+             * READ REQUEST/STATUS LINE
+             */
+
+            line = readLine(socketInputStream).trim();
+            splitLine = line.split(" ");
+            requestPath = splitLine[0].trim().contains("/") ? splitLine[0]
+                    .trim() : null;
+
+            /*
+             * READ HEADERS
+             */
+            while ((line = readLine(socketInputStream).trim()) != null) {
+                if (line.equals("")) { // if CRLF
+                    isCRLFPresent = true;
+
+                    // contentLength
+                    value = headers.get(HttpHeaders.CONTENT_LENGTH_HEADER);
+                    if (value == null) {
+                        contentLength = socketInputStream.available();
+                    } else {
+                        contentLength = Integer.parseInt(value);
+                        if (contentLength < 0) {
+                            throw HEADERS_EXCEPTION;
+                        }
+                    }
+
+                    break;
+                }
+
+                if (!line.contains(":")) {
+                    throw HEADERS_EXCEPTION;
+                }
+                putValueInHeader(headers, line.substring(0,
+                        line.indexOf(":")).trim(), // header
+                        (line.substring(line.indexOf(":") + 1)).trim()); // value
+            }
+
+            if (!isCRLFPresent) {
+                throw new IOException("HTTP POST must have content");
+            }
+
+            /*
+             * READ BODY
+             */
+
+            int readCount = contentLength;
+            byte[] bytes = new byte[readCount];
+            int temp = readCount;
+            while (readCount > 0) {
+                bytes[temp - readCount] = (byte) socketInputStream.read();
+                readCount--;
+            }
+
+            // Initializes the buffer of the wrapped input stream
+            buffer = new byte[contentLength];
+            bais = new ByteArrayInputStream(buffer);
+
+            // copy the content into the buffer
+            System.arraycopy(bytes, 0, buffer, 0, bytes.length);
+            available = bytes.length;
+            isBufferFilled = true;
+        }
+    }
+
+    /**
+     * If the header is a known Enum then inserts into <code>headers</code>
+     * its <code>value</code> with the correspondent HttpHeaders' type as key
+     * 
+     * @param headers
+     *            the name of the Enum
+     * @param header
+     *            the key to add 
+     * @param value
+     *            the value of the Header
+     */
+    private static final void putValueInHeader(Map<HttpHeaders, String> headers,
+            String header, String value) {
+        HttpHeaders hc = HttpHeaders.getEnum(header);
+        if (hc != null) {
+            // recognized type..
+            headers.put(hc, value);
+        }
+    }
+
+    /**
+     * Reads a line from this <code>is<code> and returns it     
+     * 
+     * @param is 
+     *            the socket's input stream
+     * @return 
+     *            a line from this <code>is<code> 
+     * @throws IOException 
+     *             If cannot read an CRLF
+     */
+    private static final String readLine(InputStream is) throws IOException {
+        StringBuilder sb = new StringBuilder();
+        int actual;
+        int last = is.read();
+
+        while ((actual = is.read()) != -1) {
+            if (last == LF && actual == CR) {
+                return sb.toString();
+            }
+            sb.append((char) last);
+            last = actual;
+        }
+        throw HEADERS_EXCEPTION;
+    }
+
+    /**
+     * @return the path that has been specified in the http's status/request
+     *         line
+     */
+    public final String getRequestPath() {
+        return requestPath;
+    }
+
+    @Override
+    public final int available() throws IOException {
+        fillBuffer();
+        return available;
+    }
+
+    @Override
+    public final void close() throws IOException {
+        fillBuffer();
+        bais.close();
+    }
+
+    @Override
+    public final void mark(int arg0) {
+        bais.mark(arg0);
+    }
+
+    @Override
+    public final boolean markSupported() {
+        return bais.markSupported();
+    }
+
+    @Override
+    public final void reset() throws IOException {
+        fillBuffer();
+        bais.reset();
+    }
+
+    @Override
+    public final long skip(long arg0) throws IOException {
+        fillBuffer();
+        return bais.skip(arg0);
+    }
+
+    /**
+     * @throws IOException
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public synchronized final int read() throws IOException {
+        fillBuffer();
+        if (available == 0) {
+            try {
+                wait();
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+        }
+        available--;
+        if (available > 0) {
+            notify();
+        }
+        return bais.read();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final int read(byte[] arg0) throws IOException {
+        return read(arg0, 0, arg0.length);
+    }
+
+    /**
+     * @throws IOException
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public synchronized final int read(byte[] arg0, int arg1, int arg2)
+            throws IOException {
+        fillBuffer();
+        if (available == 0) {
+            try {
+                wait();
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+        }
+        if (available > 0) {
+            notify();
+        }
+        int bytesRead = bais.read(arg0, arg1, arg2);
+        available -= bytesRead;
+        return bytesRead;
+    }
+}
\ No newline at end of file

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpInputStream.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpOutputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpOutputStream.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpOutputStream.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpOutputStream.java Tue May 16 06:51:00 2006
@@ -0,0 +1,161 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.http;
+
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+
+import ar.org.fitc.rmi.utils.PropertiesReader;
+
+/**
+ * This class is used for client and server purposes.
+ * <br>
+ * Implements a byte array stream in which the data is written. On the other
+ * side, the constructor receive a socket's output stream.
+ * <br>
+ * All the data is written in the byte array's buffer until the
+ * {@link #flush()} method is invoked. Then, a http's POST/RESPONSE 
+ * header is attached at the beginning of the socket's stream and all the buffered 
+ * bytes are written as the content of this header.
+ * 
+ * @author Diego Raúl Mercado
+ */
+abstract class HttpOutputStream extends ByteArrayOutputStream {
+    
+    private final static String CRLF = String.valueOf("\r\n");
+    
+    private final static String DEFAULT_ENCODING_CONTENT_TYPE = 
+        String.valueOf("application/octet-stream");
+    
+    private final static String DEFAULT_ACCEPT = 
+        String.valueOf("text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
+    
+    /** The value of the http's tag <code>User-Agent</code> */
+    private final static String JAVA_VERSION =
+        PropertiesReader.readString("java.version");
+    
+    /**
+     * The value of the http's header field <code>host</code> that is specified 
+     * at the constructor of this object
+     */
+    private String localHostAddress;
+
+    /** The http's status/resquest line that must be set at any of the subclasses */
+    protected String header;
+
+    /** The stream of the socket that is specified in this constructor */
+    protected OutputStream socketOutput;
+
+    /**
+     * Indicates if this object has been flushed. The <code>reset()</code>
+     * method initializes this value
+     */
+    private boolean isFlushed;
+
+    /**
+     * Constructor.
+     * 
+     * @param socketOutput
+     *            the socket's output stream
+     * @param localHostAddress
+     *            the localHostAddress attached in the http's HOST header
+     */
+    protected HttpOutputStream(OutputStream socketOutput,
+            String localHostAddress) {
+        this.socketOutput = socketOutput;
+        this.localHostAddress = localHostAddress;
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     * Attaches a http's post header at the beginning of the socket's stream and
+     * all the buffered bytes are written as the content of this http's 
+     * POST/RESPONSE header. Then, invoke the <code>flush</code> method of
+     * the socket's stream
+     */
+    @Override
+    public synchronized final void flush() throws IOException {
+        if (!isFlushed) {
+            writeOnSocket();
+            isFlushed = true;
+        }
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     * Resets this stream and sets the variable <code>isFlushed</code> to
+     * false
+     */
+    @Override
+    public final void reset() {
+        super.reset();
+        isFlushed = false;
+    }
+
+    /**
+     * Writes the http's POST/RESPONSE into the socket's output stream
+     * @throws IOException if cannot write the header
+     */
+    private final void writeOnSocket() throws IOException {
+        try {
+            BufferedOutputStream writer = new BufferedOutputStream(socketOutput);
+
+            writer.write((header
+                            + CRLF
+                            + HttpHeaders.CONTENT_TYPE_HEADER
+                            + ": "
+                            + DEFAULT_ENCODING_CONTENT_TYPE
+                            + CRLF
+                            + HttpHeaders.CACHE_CONTROL_HEADER
+                            + ": "
+                            + "no-cache"
+                            + CRLF
+                            + HttpHeaders.PRAGMA_HEADER
+                            + ": "
+                            + "no-cache"
+                            + CRLF
+                            + HttpHeaders.USER_AGENT_HEADER
+                            + ": Java/"
+                            + JAVA_VERSION
+                            + CRLF
+                            + HttpHeaders.HOST_HEADER
+                            + ": "
+                            + localHostAddress
+                            + CRLF
+                            + HttpHeaders.CONTENT_LENGTH_HEADER
+                            + ": "
+                            + super.toByteArray().length
+                            + CRLF
+                            + HttpHeaders.ACCEPT_HEADER
+                            + ": "
+                            + DEFAULT_ACCEPT
+                            + CRLF
+                            + HttpHeaders.CONNECTION_HEADER + ": "
+                            + "keep-alive" 
+                            + CRLF 
+                            + CRLF)
+                            .getBytes());
+
+            writer.write(super.toByteArray());
+            writer.flush();
+        } catch (UnsupportedEncodingException e) {
+            new AssertionError(e);
+        }
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpOutputStream.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpServerOutputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpServerOutputStream.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpServerOutputStream.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpServerOutputStream.java Tue May 16 06:51:00 2006
@@ -0,0 +1,45 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.http;
+
+import java.io.OutputStream;
+
+/**
+ * Concrete class that additionally sets the <code>header</code> required 
+ * for the http's status line (RESPONSE) 
+ * 
+ * @author Diego Raúl Mercado
+ */
+final class HttpServerOutputStream extends HttpOutputStream {
+
+    /**
+     * Calls the inherit constructor with the parameters
+     * <code>socketOutput</code> and <code>localHostAddress</code>. <br>
+     * Then, sets <code>header</code> required for the http's status line 
+     * (RESPONSE) 
+     * 
+     * @param socketOutput
+     *            the socket's output stream
+     * @param localHostAddress
+     *            the localHostAddress attached in the http's host header
+     */
+    protected HttpServerOutputStream(OutputStream socketOutput,
+            String localHostAddress) {
+        super(socketOutput, localHostAddress);
+        header = "HTTP/1.0 200 OK";
+    }
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpServerOutputStream.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpSocketClientSide.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpSocketClientSide.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpSocketClientSide.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpSocketClientSide.java Tue May 16 06:51:00 2006
@@ -0,0 +1,96 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.net.URL;
+import java.net.UnknownHostException;
+
+/**
+ * For client purposes. Specify other functionality overriding the methods
+ * <code>getInputStream()</code> and <code>getOutputStream()</code>.
+ * 
+ * @author Diego Raúl Mercado
+ */
+public final class HttpSocketClientSide extends Socket {
+
+    /** The socket's input stream */
+    private InputStream input;
+
+    /** The socket's output stream */
+    private OutputStream output;
+
+    /**
+     * It's required for the constructor of this object and the http's request
+     * line
+     */
+    private URL url;
+
+    /**
+     * Constructor.
+     * 
+     * @param url
+     *            it's used for socket connection and http's request line
+     * @throws UnknownHostException
+     *             indicate that the IP address of a host could not be
+     *             determined
+     * @throws IOException
+     *             if an I/O error occurs when creating this socket
+     */
+    public HttpSocketClientSide(URL url) throws UnknownHostException,
+            IOException {
+        super(url.getHost(), url.getPort());
+        this.url = url;
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref returns a <code>HTTPInputStream</code> instance
+     *                       with the input stream of the superclass of this
+     *                       object as the constructor parameter
+     */
+    @Override
+    public synchronized final InputStream getInputStream() throws IOException {
+        if (input == null) {
+            input = new HttpInputStream(super.getInputStream());
+        }
+        return input;
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref returns a <code>HTTPClientOutputStream</code>
+     *                       instance with the input stream of the superclass of
+     *                       this object as one of the constructor parameters
+     */
+    @Override
+    public synchronized final OutputStream getOutputStream() throws IOException {
+        if (output == null) {
+            if (url.getQuery() == null) {
+                // proxy... (serverPath: http://90.0.0.1:1099/)
+                output = new HttpClientOutputStream(super.getOutputStream(),
+                        url.getPath(), getInetAddress().getHostAddress());
+            } else {
+                // proxy + cgi... (serverPath:
+                // http://90.0.0.1:80/cgi-bin/java-rmi?forward=1099)
+                output = new HttpClientOutputStream(super.getOutputStream(),
+                        url.getFile(), getInetAddress().getHostAddress());
+            }
+        }
+        return output;
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpSocketClientSide.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpSocketServerSide.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpSocketServerSide.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpSocketServerSide.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpSocketServerSide.java Tue May 16 06:51:00 2006
@@ -0,0 +1,398 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.nio.channels.SocketChannel;
+
+/**
+ * For server purposes. Acts as a wrapper for a socket that is specified at the
+ * constructor. Overrides all the socket's methods and delegates to the wrapped
+ * socket. Specify other functionality overriding the methods
+ * <code>getInputStream()</code> and <code>getOutputStream()</code>.
+ * 
+ * @author Diego Raúl Mercado
+ */
+public final class HttpSocketServerSide extends Socket {
+
+    /** The socket's input stream */
+    private InputStream input;
+
+    /** The socket's output stream */
+    private OutputStream output;
+
+    /** The wrapped socket */
+    private Socket serverSocket;
+
+    /**
+     * Constructor.
+     * 
+     * @param socket
+     *            the socket to be wrapped
+     */
+    public HttpSocketServerSide(Socket socket) {
+        serverSocket = socket;
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref returns a <code>HTTPInputStream</code> instance
+     *                       with the input stream of the wrapped socket as the
+     *                       constructor parameter
+     */
+    @Override
+    public synchronized final InputStream getInputStream() throws IOException {
+        if (input == null) {
+            input = new HttpInputStream(serverSocket.getInputStream());
+        }
+        return input;
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref returns a <code>HTTPClientOutputStream</code>
+     *                       instance with the input stream of the wrapped
+     *                       socket as one of the constructor parameters
+     */
+    @Override
+    public synchronized final OutputStream getOutputStream() throws IOException {
+        if (output == null) {
+            output = new HttpServerOutputStream(serverSocket.getOutputStream(),
+                    getLocalAddress().isAnyLocalAddress() ? "127.0.0.1"
+                            : getLocalAddress().getHostAddress());
+        }
+        return output;
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void bind(SocketAddress arg0) throws IOException {
+        serverSocket.bind(arg0);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public synchronized final void close() throws IOException {
+        serverSocket.close();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void connect(SocketAddress arg0, int arg1) throws IOException {
+        serverSocket.connect(arg0, arg1);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void connect(SocketAddress arg0) throws IOException {
+        serverSocket.connect(arg0);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final SocketChannel getChannel() {
+        return serverSocket.getChannel();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final InetAddress getInetAddress() {
+        return serverSocket.getInetAddress();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final boolean getKeepAlive() throws SocketException {
+        return serverSocket.getKeepAlive();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final InetAddress getLocalAddress() {
+        return serverSocket.getLocalAddress();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final int getLocalPort() {
+        return serverSocket.getLocalPort();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final SocketAddress getLocalSocketAddress() {
+        return serverSocket.getLocalSocketAddress();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final boolean getOOBInline() throws SocketException {
+        return serverSocket.getOOBInline();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final int getPort() {
+        return serverSocket.getPort();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final synchronized int getReceiveBufferSize() throws SocketException {
+        return serverSocket.getReceiveBufferSize();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final SocketAddress getRemoteSocketAddress() {
+        return serverSocket.getRemoteSocketAddress();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final boolean getReuseAddress() throws SocketException {
+        return serverSocket.getReuseAddress();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final synchronized int getSendBufferSize() throws SocketException {
+        return serverSocket.getSendBufferSize();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final int getSoLinger() throws SocketException {
+        return serverSocket.getSoLinger();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final synchronized int getSoTimeout() throws SocketException {
+        return serverSocket.getSoTimeout();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final boolean getTcpNoDelay() throws SocketException {
+        return serverSocket.getTcpNoDelay();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final int getTrafficClass() throws SocketException {
+        return serverSocket.getTrafficClass();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final boolean isBound() {
+        return serverSocket.isBound();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final boolean isClosed() {
+        return serverSocket.isClosed();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final boolean isConnected() {
+        return serverSocket.isConnected();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final boolean isInputShutdown() {
+        return serverSocket.isInputShutdown();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final boolean isOutputShutdown() {
+        return serverSocket.isOutputShutdown();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void sendUrgentData(int arg0) throws IOException {
+        serverSocket.sendUrgentData(arg0);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void setKeepAlive(boolean arg0) throws SocketException {
+        serverSocket.setKeepAlive(arg0);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void setOOBInline(boolean arg0) throws SocketException {
+        serverSocket.setOOBInline(arg0);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void setPerformancePreferences(int arg0, int arg1, int arg2) {
+        serverSocket.setPerformancePreferences(arg0, arg1, arg2);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public synchronized final void setReceiveBufferSize(int arg0)
+            throws SocketException {
+        serverSocket.setReceiveBufferSize(arg0);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void setReuseAddress(boolean arg0) throws SocketException {
+        serverSocket.setReuseAddress(arg0);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public synchronized void setSendBufferSize(int arg0) throws SocketException {
+        serverSocket.setSendBufferSize(arg0);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void setSoLinger(boolean arg0, int arg1)
+            throws SocketException {
+        serverSocket.setSoLinger(arg0, arg1);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public synchronized final void setSoTimeout(int arg0)
+            throws SocketException {
+        serverSocket.setSoTimeout(arg0);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void setTcpNoDelay(boolean arg0) throws SocketException {
+        serverSocket.setTcpNoDelay(arg0);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void setTrafficClass(int arg0) throws SocketException {
+        serverSocket.setTrafficClass(arg0);
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void shutdownInput() throws IOException {
+        serverSocket.shutdownInput();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final void shutdownOutput() throws IOException {
+        serverSocket.shutdownOutput();
+    }
+
+    /**
+     * @ar.org.fitc.spec_ref
+     */
+    @Override
+    public final String toString() {
+        return serverSocket.toString();
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/http/HttpSocketServerSide.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ClientProtocolHandler.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ClientProtocolHandler.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ClientProtocolHandler.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ClientProtocolHandler.java Tue May 16 06:51:00 2006
@@ -0,0 +1,204 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.jrmp;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.rmi.MarshalException;
+import java.rmi.UnmarshalException;
+import java.rmi.server.ObjID;
+import java.rmi.server.UID;
+
+import ar.org.fitc.rmi.transport.EndpointID;
+import ar.org.fitc.rmi.transport.ProtocolException;
+
+/**
+ * Implements the management Client view of the messages of the JRMP protocol.
+ * Each of the methods here represent a message or a protocol handshake.
+ * 
+ * @author Gustavo Petri
+ */
+public class ClientProtocolHandler {
+
+    /**
+     * The local
+     * {@link java.io.InputStream}
+     * to interact with the server
+     */
+    private DataInputStream in;
+
+    /**
+     * The local
+     * {@link java.io.OutputStream}
+     * to interact with the server.
+     */
+    private DataOutputStream out;
+
+    /**
+     * Constructor of {@link ClientProtocolHandler}
+     * 
+     * @param in
+     *            the
+     *            {@link ar.org.fitc.rmi.transport.RMIObjectInputStream RMIObjectInputStream}
+     * @param out
+     *            the
+     *            {@link ar.org.fitc.rmi.transport.RMIObjectOutputStream RMIObjectOutputStream}
+     */
+    public ClientProtocolHandler(DataInputStream in, DataOutputStream out) {
+        this.out = out;
+        this.in = in;
+    }
+
+    /**
+     * Reads the {@link HeaderResponse#PROTOCOL_ACK PROTOCOL_ACK}.
+     * 
+     * @throws UnmarshalException
+     *             if an I/O exceptions or a read of an unexpected byte ocurrs
+     */
+    public final void readHandshakeResponse() throws UnmarshalException {
+
+        HeaderResponse rep;
+        try {
+        	rep = HeaderResponse.createHeaderResponse(in.readByte());
+        } catch (IOException e) {
+        	throw new UnmarshalException("Exception reading the Header response", e);
+        }
+        if (!rep.equals(HeaderResponse.PROTOCOL_ACK)) {
+        	throw new UnmarshalException("Protocol Not Supported");
+        }    }
+
+    /**
+     * Reads the {@link MessageResponse#PING_ACK PING_ACK}.
+     * 
+     * @throws ProtocolException
+     *             if an I/O exceptions or a read of an unexpected byte ocurrs
+     */
+    public final void readPingAck() throws ProtocolException {
+        try {
+            MessageResponse msgResp = MessageResponse.createMessageResponse(in
+                    .readByte());
+            if (!msgResp.equals(MessageResponse.PING_ACK)) {
+                throw new ProtocolException("I/O Error Reading PingAck");
+            }
+        } catch (IOException e) {
+            throw new ProtocolException("I/O Error PingAck", e);
+        }
+    }
+
+    /**
+     * Constructs a ReturnMessage object and sets its returns value. <br>
+     * 
+     * Then, calls the
+     * {@link ReturnMessage#read(DataInputStream) read(DataInputStream)} method
+     * in order to set this object which is finally returned.
+     * 
+     * @param waitResult
+     *            to set the <code>ReturnMessage</code> object
+     * @return
+     *            the <code>ReturnMessage</code> object
+     * @throws Exception
+     *             any exception
+     */
+    public final ReturnMessage readResult(boolean waitResult) throws Exception {
+        ReturnMessage result = new ReturnMessage();
+        result.setReturnsValue(waitResult);
+        result.read(in);
+        return result;
+    }
+
+    /**
+     * Constructs a message with this parameters and writes the Call
+     * 
+     * @param objId
+     *            the object ID
+     * @param hash
+     *            the hash
+     * @param args
+     *            the arguments to invoke
+     * @throws Exception
+     *             any exception
+     */
+    public final void writeCall(ObjID objId, long hash, Object[] args)
+            throws Exception {
+        new Message(objId, hash, args).writeExternal(out);
+    }
+
+    /**
+     * Constructs a message with the <code>uid</code> and then, writes the
+     * DGCAck.
+     * 
+     * @param uid
+     *            a unique identifier
+     * @throws ProtocolException
+     *             if an I/O exceptions ocurrs
+     */
+    public final void writeDGCAck(UID uid) throws ProtocolException {
+
+        try {
+            new Message(uid).writeExternal(out);
+        } catch (IOException e) {
+            throw new ProtocolException("Error while sending DGCAck", e);
+        }
+    }
+
+    /**
+     * Writes the protocol header, version and type of connection
+     * 
+     * @param type
+     *            the protocol's type
+     * @throws MarshalException
+     *             if an I/O exceptions ocurrs
+     */
+    public final void writeHandshake(ProtocolType type) throws MarshalException {
+
+        Header header = new Header(type);
+        try {
+            header.writeExternal(out);
+        } catch (IOException e) {
+            throw new MarshalException("Exception marshaling JRMP Header", e);
+        }
+    }
+
+    /**
+     * Writes the host and port
+     * 
+     * @throws ProtocolException
+     *             if an I/O exceptions ocurrs
+     */
+    public final void writeHandshakeResponse() throws ProtocolException {
+        try {
+            new EndpointID().write(out);
+        } catch (IOException e) {
+            throw new ProtocolException(
+                    "Error marshaling the protocolHandshake response", e);
+        }
+    }
+
+    /**
+     * Writes the Ping request.
+     * 
+     * @throws ProtocolException
+     *             if an I/O exceptions ocurrs
+     */
+    public final void writePing() throws ProtocolException {
+        try {
+            new Message().writeExternal(out);
+        } catch (IOException e) {
+            throw new ProtocolException("Socket Closed while pinging", e);
+        }
+    }
+}
\ No newline at end of file

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ClientProtocolHandler.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/Header.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/Header.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/Header.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/Header.java Tue May 16 06:51:00 2006
@@ -0,0 +1,114 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.jrmp;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
+import ar.org.fitc.rmi.transport.ProtocolException;
+
+/**
+ * A class representing the Header of the JRMI protocol.
+ * 
+ * @author Gustavo Petri
+ */
+public class Header {
+
+	/**
+	 * ID for serialization purposes
+	 */
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * A byte array representing the version of the JRMI protocol
+	 */
+	static final byte[] JRMI_PROTOCOL_VERSION = new byte[] { 0x00, 0x01 };
+
+	/**
+	 * A {@link ar.org.fitc.rmi.transport.jrmp.ProtocolType} object that
+	 * indicates the header type
+	 */
+	private ProtocolType type;
+
+	/**
+	 * Constructor. Does nothing
+	 */
+	public Header() {
+		super();
+	}
+
+	/**
+	 * Constructor. Sets {@link Header#type type} with the parameter
+	 * <code>type</code>
+	 * 
+	 * @param type
+	 */
+	public Header(ProtocolType type) {
+		this.type = type;
+	}
+
+	/**
+	 * Returns the protocol type.
+	 * 
+	 * @return the wrapped {@link Header#type type}
+	 */
+	public ProtocolType getProtocolType() {
+		return type;
+	}
+
+	/**
+	 * Writes the header representation on the stream.
+	 * 
+	 * @param out
+	 *            the output stream to write
+	 * @throws IOException
+	 *             if an exception occurs when writing on the stream
+	 */
+	public final void writeExternal(DataOutputStream out) throws IOException {
+		out.write(ProtocolHeader.JRMI_PROTOCOL_HEADER.getValue());
+		out.write(JRMI_PROTOCOL_VERSION);
+		out.write(type.getValue());
+	}
+
+	/**
+	 * Reads a header from the input stream, and sets the {@link Header#type}
+	 * attribute to the received protocol type.
+	 * 
+	 * @param in
+	 *            the input stream used to read the data
+	 * @throws IOException
+	 *             if an exception occurs reading the header bytes
+	 */
+	public final void readExternal(DataInputStream in) throws IOException {
+
+		if (type.equals(ProtocolType.SINGLE_OP)) {
+			for (byte b : ProtocolHeader.JRMI_PROTOCOL_HEADER.getValue()) {
+				if (in.readByte() != b) {
+					throw new ProtocolException(
+							"Error reading JRMP protocol header");
+				}
+			}
+		}
+		for (byte b : JRMI_PROTOCOL_VERSION) {
+			if (in.readByte() != b) {
+				throw new ProtocolException(
+						"Error reading JRMP protocol header");
+			}
+		}
+		type = ProtocolType.createProtocolType(in.readByte());
+	}
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/Header.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/HeaderResponse.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/HeaderResponse.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/HeaderResponse.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/HeaderResponse.java Tue May 16 06:51:00 2006
@@ -0,0 +1,82 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.jrmp;
+
+import ar.org.fitc.rmi.transport.ProtocolException;
+
+/**
+ * An enum representing the valid header responses of the JRMP protocol
+ * 
+ * @author Gustavo Petri
+ * 
+ */
+enum HeaderResponse {
+
+	/**
+	 * A 0x4e byte representing the protocol acknowledge value
+	 */
+	PROTOCOL_ACK((byte) 0x4e),
+	/**
+	 * A 0x4f byte representing the protocol not supported value
+	 */
+	PROTOCOL_NOT_SUPPORTED((byte) 0x4f);
+
+	/**
+	 * The current value
+	 */
+	private byte value;
+
+	/**
+	 * Private constructor, sets the internal {@link HeaderResponse#value} 
+	 * 
+	 * @param value
+	 *            the value for this enum
+	 */
+	private HeaderResponse(byte value) {
+		this.value = value;
+	}
+
+	/**
+	 * Getter method which returns the <code>byte</code> value of this enum
+	 * 
+	 * @return the {@link HeaderResponse#value} of this enum
+	 */
+	final byte getValue() {
+		return value;
+	}
+
+	/**
+	 * Creates a new {@link HeaderResponse}
+	 * 
+	 * @param value
+	 *            the <code>byte</code> value of the {@link HeaderResponse}
+	 * @return the new {@link HeaderResponse}
+	 * @throws ProtocolException
+	 *             if the <code>value</code> parameters is not a valid JRMP
+	 *             header response value
+	 */
+	static final HeaderResponse createHeaderResponse(byte value)
+			throws ProtocolException {
+		if (value == PROTOCOL_ACK.getValue()) {
+			return PROTOCOL_ACK;
+		} else if (value == PROTOCOL_NOT_SUPPORTED.getValue()) {
+			return PROTOCOL_NOT_SUPPORTED;
+		} else {
+			throw new ProtocolException("Unrecognized Header Response: "
+					+ Integer.toHexString(value));
+		}
+	}
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/HeaderResponse.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/Message.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/Message.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/Message.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/Message.java Tue May 16 06:51:00 2006
@@ -0,0 +1,336 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.jrmp;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.rmi.MarshalException;
+import java.rmi.NoSuchObjectException;
+import java.rmi.UnmarshalException;
+import java.rmi.server.ObjID;
+import java.rmi.server.UID;
+
+import ar.org.fitc.rmi.runtime.RemoteReferenceManager;
+import ar.org.fitc.rmi.transport.ProtocolException;
+import ar.org.fitc.rmi.transport.RMIObjectInputStream;
+import ar.org.fitc.rmi.transport.RMIObjectOutputStream;
+
+/**
+ * Encapsulates all the messages that a RMI Client can send. It is a Tagged 
+ * Union Type.
+ * 
+ * @author Gustavo Petri
+ */
+public final class Message {
+
+    /**
+     * Serialization ID
+     */
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * This constant is used as a part of the JRMI protocol
+     */
+    private static final int UNUSED_OPERATION = -1;
+
+    /*
+     * Data Structures used for a Call message.
+     */
+    /**
+     * The hash of the method to be invoked
+     */
+    private long hash;
+
+    /**
+     * The {@link ObjID} of the Object to which a method will be invoked
+     */
+    private ObjID objID;
+
+    /**
+     * The operation number of the operation to be invoked (Actually not used)
+     */
+    private int operation;
+
+    /**
+     * Argumenst to execute the method 
+     */
+    private Object[] values;
+    
+    /**
+     * Type of the message to be sent. Defines the type in the tagged union
+     */
+    private MessageType type;
+
+    /*
+     * UID used to for the DGCAck message.
+     */
+    /**
+     * {@link UID} for DGCAck purposes. 
+     */
+    private UID uid;
+
+    /**
+     * Constructs a Ping Message
+     */
+    public Message() {
+        this.type = MessageType.PING;
+        this.operation = UNUSED_OPERATION;
+    }
+
+    /**
+     * Constructs a Call message
+     * 
+     * @param objID 
+     *          the identifier of the Object which's method will be executed 
+     * @param hash 
+     *          the hash of the method to be executed
+     * @param values 
+     *          the arguments to execute the method
+     */
+    public Message(ObjID objID, long hash, Object... values) {
+        this.type = MessageType.CALL;
+        this.operation = UNUSED_OPERATION;
+        this.objID = objID;
+        this.hash = hash;
+        this.values = values;
+    }
+
+    /**
+     * Constructs a DGCAck message
+     * @param uid
+     *          the {@link UID} to issue the DGCAck
+     */
+    public Message(UID uid) {
+        this.type = MessageType.DGCACK;
+        this.uid = uid;
+    }
+
+    /**
+     * Reads a Call message
+     * @param in 
+     *          the {@link java.io.InputStream} to be read
+     * @throws UnmarshalException 
+     *          if not able to read the Call
+     * @throws NoSuchObjectException
+     *          if the object ID is not exported in this server  
+     */
+    private final void readCall(DataInputStream in) throws UnmarshalException,
+            NoSuchObjectException {
+        RMIObjectInputStream objIn;
+        RemoteReferenceManager refMgr;
+        int argsCount = 0;
+
+        try {
+            objIn = new RMIObjectInputStream(in);
+            this.objID = ObjID.read(objIn);
+            this.operation = objIn.readInt();
+            this.hash = objIn.readLong();
+        } catch (IOException e) {
+            throw new UnmarshalException("I/O Error Unmarshaling Call Header",
+                    e);
+        }
+        refMgr = RemoteReferenceManager.getRemoteReferenceManager();
+        argsCount = refMgr.getArgsCount(objID, hash);
+        values = new Object[argsCount];
+        try {
+            for (int i = 0; i < argsCount; i++) {
+                values[i] = objIn.readObject();
+            }
+        } catch (ClassNotFoundException e) {
+            throw new UnmarshalException(
+                    "I/O Error Unmarshaling the Arguments", e);
+        } catch (IOException e) {
+            throw new UnmarshalException(
+                    "I/O Error Unmarshaling the Arguments", e);
+        }
+    }
+
+    /**
+     * Reads a DGCAck message
+     * @param in 
+     *          the {@link java.io.InputStream} to be read
+     * @throws ProtocolException
+     *          if not able to read the DGCAck
+     */
+    private final void readDGCAck(DataInputStream in) throws ProtocolException {
+        try {
+            uid = UID.read(in);
+        } catch (IOException e) {
+            throw new ProtocolException("I/O Error Reading the DGCAck");
+        }
+    }
+
+    /**
+     * Writes a DGCAck 
+     * @param out
+     *          the {@link java.io.OutputStream} to be written
+     * @throws ProtocolException
+     *          if not able to write the DGCAck
+     */
+    private final void writeDgcAck(DataOutputStream out)
+            throws ProtocolException {
+        try {
+            out.writeByte(MessageType.DGCACK.getValue());
+            uid.write(out);
+        } catch (IOException e) {
+            throw new ProtocolException("I/O Error Sending DGCAck", e);
+        }
+    }
+
+    /**
+     * Writes a Ping message
+     * @param out
+     *          the {@link java.io.OutputStream} to be written
+     * @throws ProtocolException
+     *          if not able to write the Ping
+     */
+    private final void writePing(DataOutputStream out) throws ProtocolException {
+        try {
+            out.writeByte(MessageType.PING.getValue());
+        } catch (IOException e) {
+            throw new ProtocolException("Socket Closed while pinging", e);
+        }
+    }
+
+    /**
+     * Writes a Call message
+     * @param out
+     *          the {@link java.io.OutputStream} to be written
+     * @throws MarshalException
+     *          if not able to write the Call
+     */
+    private final void writeCall(DataOutputStream out) throws MarshalException {
+
+        RMIObjectOutputStream objOut = null;
+        try {
+            out.writeByte(MessageType.CALL.getValue());
+            objOut = new RMIObjectOutputStream(out);
+            objID.write(objOut);
+            objOut.writeInt(operation);
+            objOut.writeLong(hash);
+        } catch (Exception e) {
+            throw new MarshalException("I/O Error Marshaling Call Header", e);
+        }
+        try {
+            if (values != null)
+                for (Object o : values) {
+                    objOut.writeObject(o);
+                }
+        } catch (Exception e) {
+            throw new MarshalException("I/O Error Marshaling Arguments", e);
+        } finally {
+            try {
+                objOut.drain();
+            } catch (IOException e) {
+                throw new MarshalException("I/O Error Marshaling Arguments", e);
+            }
+        }
+    }
+        
+    /**
+     * Gets the arguments of a Call
+     * @return 
+     *          the arguments of a Call
+     */
+    public final Object[] getArguments() {
+        return values;
+    }
+
+    /**
+     * Gets the hash of a mathod Call
+     * @return the hash of a mathod Call
+     */
+    public final long getHash() {
+        return hash;
+    }
+
+    /**
+     * Gets the {@link ObjID} of a mathod Call
+     * @return 
+     *          the {@link ObjID} of a mathod Call
+     */
+    public final ObjID getObjID() {
+        return objID;
+    }
+
+    /**
+     * Gets the Operation number of a mathod to be Called
+     * @return 
+     *          the Operation number of a mathod to be Called
+     */
+    public final int getOperation() {
+        return operation;
+    }
+
+    /**
+     * Gets the message type of this message
+     * @return the message type of this message
+     */
+    public final MessageType getType() {
+        return type;
+    }
+
+    /**
+     * Gets the UID of a DGCAck
+     * @return the UID of a DGCAck
+     */
+    public final UID getUID() {
+        return uid;
+    }
+
+    /**
+     * Reads a message from the specified {@link DataInputStream}
+     * @param in
+     *          the {@link DataInputStream} to be read
+     * @throws IOException
+     *          if unable to read the message
+     */
+    public final void readExternal(DataInputStream in) throws IOException {
+
+        type = MessageType.createMessageType(in.readByte());
+
+        if (type.equals(MessageType.CALL)) {
+            readCall(in);
+        } else if (type.equals(MessageType.DGCACK)) {
+            readDGCAck(in);
+        } else if (type.equals(MessageType.PING)) {
+        } else {
+            throw new ProtocolException("Message not supported");
+        }
+
+    }
+
+    /**
+     * Writes the Message to the specified {@link DataOutputStream}
+     * @param out 
+     *          the {@link DataOutputStream} to be written 
+     * @throws IOException
+     *          if unable to write the Message
+     */
+    public final void writeExternal(DataOutputStream out) throws IOException {
+
+        if (type == MessageType.CALL) {
+            writeCall(out);
+        } else if (type == MessageType.PING) {
+            writePing(out);
+        } else if (type == MessageType.DGCACK) {
+            writeDgcAck(out);
+        } else {
+            throw new ProtocolException("Message not supported");
+        }
+    }
+}
\ No newline at end of file

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/Message.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/MessageResponse.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/MessageResponse.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/MessageResponse.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/MessageResponse.java Tue May 16 06:51:00 2006
@@ -0,0 +1,80 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.jrmp;
+
+/**
+ * This Enumerated Type describes the possible return types received from a RMI
+ * Server.
+ * 
+ * @author Gustavo Petri
+ * 
+ */
+enum MessageResponse {
+
+	/**
+	 * A 0x51 <code>byte</code> representing a return message
+	 */
+	RETURN((byte) 0x51),
+
+	/**
+	 * A 0x53 <code>byte</code> representing a PingAck message
+	 */
+	PING_ACK((byte) 0x53);
+
+	/**
+	 * The internal <code>byte</code> value for this enum
+	 */
+	private byte value;
+
+	/**
+	 * Private constructor, sets the internal {@link MessageResponse#value}
+	 * 
+	 * @param value
+	 */
+	private MessageResponse(byte value) {
+		this.value = value;
+	}
+
+	/**
+	 * Getter method which returns the <code>byte</code> value of this enum
+	 * 
+	 * @return the {@link MessageResponse#value} of this enumeration
+	 */
+	byte getValue() {
+		return value;
+	}
+
+	/**
+	 * Creates a new {@link MessageResponse}
+	 * 
+	 * @param value
+	 *            the <code>byte</code> value of the {@link MessageResponse}
+	 * @return a new {@link MessageResponse}
+	 * @throws IllegalArgumentException
+	 *             if the <code>value</code> parameter is not a valid JRMP
+	 *             return type.
+	 */
+	static MessageResponse createMessageResponse(byte value) {
+		if (value == RETURN.getValue()) {
+			return RETURN;
+		} else if (value == PING_ACK.getValue()) {
+			return PING_ACK;
+		} else {
+			throw new IllegalArgumentException("Unrecognized value: "
+					+ Integer.toHexString(value));
+		}
+	}
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/MessageResponse.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/MessageType.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/MessageType.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/MessageType.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/MessageType.java Tue May 16 06:51:00 2006
@@ -0,0 +1,90 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.jrmp;
+
+import ar.org.fitc.rmi.transport.ProtocolException;
+
+/**
+ * This Enumerated Type describes the possible Messages received by a RMI
+ * Server.
+ * 
+ * @author Gustavo Petri
+ * 
+ */
+public enum MessageType {
+
+	/**
+	 * A 0x50 <code>byte</code> representing a CALL message
+	 */
+	CALL((byte) 0x50),
+
+	/**
+	 * A 0x52 <code>byte</code> representing a PING message
+	 */
+	PING((byte) 0x52),
+
+	/**
+	 * A 0x54 <code>byte</code> representing a DGCACK message
+	 */
+	DGCACK((byte) 0x54);
+
+	/**
+	 * The internal <code>byte</code> value for this enum
+	 */
+	private byte value;
+
+	/**
+	 * Private constructor, sets the internal {@link MessageType#value}
+	 * 
+	 * @param value
+	 */
+	private MessageType(byte value) {
+		this.value = value;
+	}
+
+	/**
+	 * Getter method which returns the <code>byte</code> value of this enum
+	 * 
+	 * @return the {@link MessageType#value} of this enum
+	 */
+	final byte getValue() {
+		return value;
+	}
+
+	/**
+	 * Creates a new {@link MessageType}
+	 * 
+	 * @param value
+	 *            the <code>byte</code> value of the {@link MessageType}
+	 * @return a new {@link MessageType}
+	 * @throws ProtocolException
+	 *             if the <code>value</code> parameter is not a valid JRMP
+	 *             header message type
+	 */
+	static final MessageType createMessageType(byte value)
+			throws ProtocolException {
+		if (value == CALL.getValue()) {
+			return CALL;
+		} else if (value == PING.getValue()) {
+			return PING;
+		} else if (value == DGCACK.getValue()) {
+			return DGCACK;
+		} else {
+			throw new ProtocolException("Unrecognized Message Header "
+					+ Integer.toHexString(value));
+		}
+	}
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/MessageType.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ProtocolHeader.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ProtocolHeader.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ProtocolHeader.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ProtocolHeader.java Tue May 16 06:51:00 2006
@@ -0,0 +1,89 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.jrmp;
+
+import java.util.Arrays;
+
+import ar.org.fitc.rmi.transport.ProtocolException;
+
+/**
+ * This Enumerated Type describes the possible protocol headers defined by the 
+ * JRMP specification.
+ * 
+ * @author Gustavo Petri
+ * 
+ */
+public enum ProtocolHeader {
+
+	/**
+	 * The <code>byte[]</code> sequence corresponding to the JRMP protocol initial header
+	 */
+	JRMI_PROTOCOL_HEADER(new byte[] {0x4a, 0x52, 0x4d, 0x49}), 
+
+	/**
+	 * The <code>byte[]</code> sequence corresponding to the HTTP protocol initial header
+	 */
+	HTTP_PROTOCOL_HEADER(new byte[] {0x50, 0x4f, 0x53, 0x54});
+    
+	/**
+	 * The internal <code>byte[]</code> value for this enumeration
+	 */
+    private byte[] value;
+
+	/**
+	 * Private constructor, sets the internal {@link ProtocolHeader#value}
+	 * 
+	 * @param value
+	 */
+    private ProtocolHeader(byte[] value) {
+        this.value = value.clone();
+    }
+
+	/**
+	 * Getter method which returns the <code>byte[]</code> value of this enumeration
+	 * 
+	 * @return the {@link ProtocolHeader#value} of this enumeration
+	 */
+    final byte[] getValue() {
+        return value;
+    }
+
+	/**
+	 * Creates a new {@link ProtocolHeader}
+	 * 
+	 * @param value
+	 *            the <code>byte[]</code> value of the {@link ProtocolHeader}
+	 * @return a new {@link ProtocolHeader}
+	 * @throws ProtocolException
+	 *             if the <code>value</code> parameter is not a valid JRMP
+	 *             initial protocol header sequence.
+	 */
+    public static final ProtocolHeader createProtocolHeader(byte[] value)
+            throws ProtocolException {
+        if (Arrays.equals(value, JRMI_PROTOCOL_HEADER.getValue())) {
+            return JRMI_PROTOCOL_HEADER;
+        } else if (Arrays.equals(value, HTTP_PROTOCOL_HEADER.getValue())) {
+            return HTTP_PROTOCOL_HEADER;
+        } else {
+            StringBuilder str = new StringBuilder();
+            for (byte b : value) {
+                str.append(Integer.toHexString(b) + ", ");
+            }
+            throw new ProtocolException("Protocol Header not recognized: "
+                    + str.toString());
+        }
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ProtocolHeader.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ProtocolType.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ProtocolType.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ProtocolType.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ProtocolType.java Tue May 16 06:51:00 2006
@@ -0,0 +1,86 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.jrmp;
+
+/**
+ * This Enumerated Type describes the possible Protocol types defined by the
+ * JRMP specification.
+ * 
+ * @author Gustavo Petri
+ * 
+ */
+public enum ProtocolType {
+
+	/**
+	 * A 0x4c <code>byte</code> representing the SingleOp protocol
+	 */
+	SINGLE_OP((byte) 0x4c),
+
+	/**
+	 * A 0x4b <code>byte</code> representing the Stream protocol
+	 */
+	STREAM((byte) 0x4b),
+
+	/**
+	 * A 0x4d <code>byte</code> representing the Multiplex protocol
+	 */
+	MULTIPLEX((byte) 0x4d);
+
+	/**
+	 * The internal <code>byte</code> value for this enum
+	 */
+	private byte value;
+
+	/**
+	 * Private constructor, sets the internal {@link ProtocolType#value}
+	 * 
+	 * @param value
+	 */
+	private ProtocolType(byte value) {
+		this.value = value;
+	}
+
+	/**
+	 * Getter method which returns the <code>byte</code> value of this enum
+	 * 
+	 * @return the {@link ProtocolType#value} of this enum
+	 */
+	final byte getValue() {
+		return value;
+	}
+
+	/**
+	 * Creates a new {@link ProtocolType}
+	 * 
+	 * @param value
+	 *            the <code>byte</code> value of the {@link ProtocolType}
+	 * @return a new {@link ProtocolType}
+	 * @throws IllegalArgumentException
+	 *             if the <code>value</code> parameter is not a valid JRMP
+	 *             protocol type
+	 */
+	public static final ProtocolType createProtocolType(byte value) {
+		if (value == SINGLE_OP.getValue()) {
+			return SINGLE_OP;
+		} else if (value == STREAM.getValue()) {
+			return STREAM;
+		} else if (value == MULTIPLEX.getValue()) {
+			return MULTIPLEX;
+		} else {
+			throw new IllegalArgumentException("Unrecognized value");
+		}
+	}
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ProtocolType.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ReturnMessage.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ReturnMessage.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ReturnMessage.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ReturnMessage.java Tue May 16 06:51:00 2006
@@ -0,0 +1,260 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.jrmp;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.rmi.server.UID;
+
+import ar.org.fitc.rmi.transport.RMIObjectInputStream;
+import ar.org.fitc.rmi.transport.RMIObjectOutputStream;
+import ar.org.fitc.rmi.utils.Pair;
+
+/**
+ * A tagged union type which represents a message returned from the RMI Server
+ * 
+ * @author Gustavo Petri
+ * 
+ */
+public class ReturnMessage {
+
+	/**
+	 * ID used for serialization purposes
+	 */
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * A {@link MessageResponse} which defines the type of return message.
+	 */
+	private MessageResponse type;
+
+	/**
+	 * A flag that identifies if this {@link ReturnMessage} represents an
+	 * exception.
+	 */
+	private boolean isException;
+
+	/**
+	 * This attribute is used to store the exception returned from the server,
+	 * when {@link ReturnMessage#type} is <code>true</code>
+	 */
+	private Exception exception = null;
+
+	/**
+	 * This attribute is used to store the object returned from the server, when
+	 * {@link ReturnMessage#type} is <code>false</code>
+	 */
+	private Object result = null;
+
+	/**
+	 * This flag is used to determine if this {@link ReturnMessage} should
+	 * read/write a return value from/to the stream
+	 */
+	private boolean returnsValue;
+
+	/**
+	 * This flag is set to <code>true</code> when the read result is a remote
+	 * object, and a DGCAck message should be sent to the server
+	 */
+	private boolean sendDGCAck;
+
+	/**
+	 * The identificator which is sent and received with the return message
+	 */
+	private UID uid;
+
+	/**
+	 * Creates a new {@link ReturnMessage}. By default, a
+	 * {@link MessageResponse#PING_ACK} message response is created.
+	 */
+	public ReturnMessage() {
+		this.type = MessageResponse.PING_ACK;
+	}
+
+	/**
+	 * Creates a new {@link ReturnMessage} of the {@link MessageResponse#RETURN}
+	 * type, which will represent an exception returned from the server.
+	 * 
+	 * @param e
+	 *            the exception that will be encapsulated by this
+	 *            {@link ReturnMessage}
+	 */
+	public ReturnMessage(Exception e) {
+		this.type = MessageResponse.RETURN;
+		this.isException = true;
+		this.exception = e;
+	}
+
+	/**
+	 * Creates a new {@link ReturnMessage} of the {@link MessageResponse#RETURN}
+	 * type, which will represent an object returned from the server.
+	 * 
+	 * @param o
+	 *            the object that will be encapsulated by this
+	 *            {@link ReturnMessage}
+	 * @param returnsValue
+	 *            A flag that indicates if this {@link ReturnMessage} should
+	 *            read/write a return value from/to the stream
+	 */
+	public ReturnMessage(Object o, boolean returnsValue) {
+		this.type = MessageResponse.RETURN;
+		this.isException = false;
+		this.returnsValue = returnsValue;
+		this.result = o;
+	}
+
+	/**
+	 * Returns the {@link ReturnMessage#type} of this {@link ReturnMessage}
+	 * 
+	 * @return the {@link ReturnMessage#type} of this {@link ReturnMessage}
+	 */
+	public final MessageResponse getReturnType() {
+		return type;
+	}
+
+	/**
+	 * Returns the {@link ReturnMessage#isException} of this
+	 * {@link ReturnMessage}
+	 * 
+	 * @return the {@link ReturnMessage#isException} of this
+	 *         {@link ReturnMessage}
+	 */
+	public final boolean isException() {
+		return isException;
+	}
+
+	/**
+	 * Returns the {@link ReturnMessage#exception} of this {@link ReturnMessage}
+	 * 
+	 * @return the {@link ReturnMessage#exception} of this {@link ReturnMessage}
+	 */
+	public final Exception getException() {
+		return exception;
+	}
+
+	/**
+	 * Returns the {@link ReturnMessage#result} of this {@link ReturnMessage}
+	 * 
+	 * @return the {@link ReturnMessage#result} of this {@link ReturnMessage}
+	 */
+	public final Object getResult() {
+		return result;
+	}
+
+	/**
+	 * Returns the {@link ReturnMessage#sendDGCAck} of this
+	 * {@link ReturnMessage}
+	 * 
+	 * @return the {@link ReturnMessage#sendDGCAck} of this
+	 *         {@link ReturnMessage}
+	 */
+	public final boolean sendsDGCAck() {
+		return sendDGCAck;
+	}
+
+	/**
+	 * Returns the {@link ReturnMessage#uid} of this {@link ReturnMessage}
+	 * 
+	 * @return the {@link ReturnMessage#uid} of this {@link ReturnMessage}
+	 */
+	public final UID getUID() {
+		return uid;
+	}
+
+	/**
+	 * Sets the {@link ReturnMessage#returnsValue} of this {@link ReturnMessage}
+	 * 
+	 * @param returnsValue
+	 *            The flag that indicates if this {@link ReturnMessage} should
+	 *            read/write a return value from/to the stream
+	 * 
+	 */
+	public final void setReturnsValue(boolean returnsValue) {
+		this.returnsValue = returnsValue;
+	}
+
+	/**
+	 * Writes this {@link ReturnMessage} to a {@link DataOutputStream}
+	 * 
+	 * @param out
+	 *            the ouput stream in which the {@link ReturnMessage} will be
+	 *            written to
+	 * @return the {@link UID} that identificates the written
+	 *         {@link ReturnMessage}
+	 * @throws IOException
+	 *             if an exception occurs writing the {@link ReturnMessage} to
+	 *             the stream
+	 */
+	public final UID write(DataOutputStream out) throws IOException {
+		RMIObjectOutputStream objOut;
+		boolean writesRemote = false;
+
+		objOut = new RMIObjectOutputStream(out);
+		objOut.writeByte(type.getValue());
+		if (type == MessageResponse.RETURN) {
+			if (!isException) {
+				objOut.writeByte(ReturnType.RETURN_HEADER.getValue());
+				uid = new UID();
+				uid.write(objOut);
+				if (returnsValue) {
+					writesRemote = objOut.writeResultObject(result);
+				}
+			} else {
+				objOut.writeByte(ReturnType.EXCEPTION_HEADER.getValue());
+				uid = new UID();
+				uid.write(objOut);
+				writesRemote = objOut.writeResultObject(exception);
+			}
+		}
+		objOut.drain(); // don't propagate through the socket's output stream
+		return writesRemote ? uid : null;
+	}
+
+	/**
+	 * Reads a {@link ReturnMessage} from a {@link DataInputStream} and sets its
+	 * internal attributes.
+	 * 
+	 * @param in
+	 *            the input stream where the {@link ReturnMessage} will be read
+	 *            from
+	 * @throws IOException
+	 *             if an exception occurs when reading the {@link ReturnMessage}
+	 * @throws ClassNotFoundException
+	 *             if a required class is not found
+	 */
+	public final void read(DataInputStream in) throws IOException,
+			ClassNotFoundException {
+		RMIObjectInputStream objIn = null;
+
+		objIn = new RMIObjectInputStream(in);
+		type = MessageResponse.createMessageResponse(objIn.readByte());
+		if (type == MessageResponse.RETURN) {
+			ReturnType retType = ReturnType.createReturnType(objIn.readByte());
+			uid = UID.read(objIn);
+			if (retType.equals(ReturnType.RETURN_HEADER) && returnsValue) {
+				Pair<Object, Boolean> pair = objIn.readResultObject();
+				result = pair.getFirst();
+				sendDGCAck = pair.getSecond();
+			} else if (retType.equals(ReturnType.EXCEPTION_HEADER)) {
+				Pair<Object, Boolean> pair = objIn.readResultObject();
+				isException = true;
+				exception = (Exception) pair.getFirst();
+				sendDGCAck = pair.getSecond();
+			}
+		}
+	}
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ReturnMessage.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ReturnType.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ReturnType.java?rev=406944&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ReturnType.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ReturnType.java Tue May 16 06:51:00 2006
@@ -0,0 +1,85 @@
+/* 
+*  Copyright 2005 The Apache Software Foundation or its licensors, as applicable. 
+* 
+*  Licensed under the Apache License, Version 2.0 (the "License"); 
+*  you may not use this file except in compliance with the License. 
+*  You may obtain a copy of the License at 
+* 
+*    http://www.apache.org/licenses/LICENSE-2.0 
+* 
+*  Unless required by applicable law or agreed to in writing, software 
+*  distributed under the License is distributed on an "AS IS" BASIS, 
+*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+*  See the License for the specific language governing permissions and 
+*  limitations under the License. 
+*/
+package ar.org.fitc.rmi.transport.jrmp;
+
+import ar.org.fitc.rmi.transport.ProtocolException;
+
+/**
+ * This Enumerated Type describes the possible return message types received
+ * from a RMI Server.
+ * 
+ * @author Gustavo Petri
+ * 
+ */
+enum ReturnType {
+
+	/**
+	 * A 0x01 <code>byte</code> representing a return message, when the call
+	 * execution on the remote server was successful
+	 */
+	RETURN_HEADER((byte) 0x01),
+
+	/**
+	 * A 0x02 <code>byte</code> representing a return message, when the call
+	 * execution on the remote server threw and exception
+	 */
+	EXCEPTION_HEADER((byte) 0x02);
+
+	/**
+	 * The internal <code>byte</code> value for this enum
+	 */
+	private byte value;
+
+	/**
+	 * Private constructor, sets the internal {@link ReturnType#value}
+	 * 
+	 * @param value
+	 */
+	private ReturnType(byte value) {
+		this.value = value;
+	}
+
+	/**
+	 * Getter method which returns the <code>byte</code> value of this enum
+	 * 
+	 * @return the {@link ReturnType#value} of this enumeration
+	 */
+	final byte getValue() {
+		return value;
+	}
+
+	/**
+	 * Creates a new {@link ReturnType}
+	 * 
+	 * @param value
+	 *            the <code>byte</code> value of the {@link ReturnType}
+	 * @return a new {@link ReturnType}
+	 * @throws ProtocolException
+	 *             if the <code>value</code> parameter is not a valid JRMP
+	 *             return message type.
+	 */
+	final static ReturnType createReturnType(byte value)
+			throws ProtocolException {
+		if (value == RETURN_HEADER.getValue()) {
+			return RETURN_HEADER;
+		} else if (value == EXCEPTION_HEADER.getValue()) {
+			return EXCEPTION_HEADER;
+		} else {
+			throw new ProtocolException("Invalid return type byte "
+					+ Integer.toHexString(value));
+		}
+	}
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/rmi2/src/ar/org/fitc/rmi/transport/jrmp/ReturnType.java
------------------------------------------------------------------------------
    svn:executable = *



Mime
View raw message