User: stark   
  Date: 01/03/14 16:42:39

  Added:       src/main/org/jboss/test/jrmp/ejb
                        CompressionClientSocketFactory.java
                        CompressionConstants.java
                        CompressionInputStream.java
                        CompressionOutputStream.java
                        CompressionServerSocket.java
                        CompressionServerSocketFactory.java
                        CompressionSocket.java StatelessSessionBean.java
  Log:
  Tests of the JRMPContainerInvoker custom socket configuration capability.
  
  Revision  Changes    Path
  1.1                  
jbosstest/src/main/org/jboss/test/jrmp/ejb/CompressionClientSocketFactory.java
  
  Index: CompressionClientSocketFactory.java
  ===================================================================
  package org.jboss.test.jrmp.ejb;
  
  import java.io.IOException;
  import java.io.Serializable;
  import java.net.Socket;
  import java.rmi.server.RMIClientSocketFactory;
  
  /** The CompressionClientSocketFactory from the RMI custom socket
  factory tutorial.
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class CompressionClientSocketFactory implements RMIClientSocketFactory, 
Serializable
  {
      /** Create a client socket connected to the specified host and port.
       * @param host - the host name
       * @param port - the port number
       * @return a socket connected to the specified host and port.
       * @exception IOException if an I/O error occurs during socket creation.
       */
      public Socket createSocket(String host, int port) throws IOException
      {
          Socket s = new CompressionSocket(host, port);
          
System.out.println("CompressionClientSocketFactory.createSocket("+host+','+port+") : 
"+s);
          return s;
      }
  
      public boolean equals(Object obj)
      {
          return obj instanceof CompressionClientSocketFactory;
      }
  
      public int hashCode()
      {
          return getClass().getName().hashCode();
      }
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/jrmp/ejb/CompressionConstants.java
  
  Index: CompressionConstants.java
  ===================================================================
  /*
   * Copyright (c) 1998, 1999 Sun Microsystems, Inc. All Rights Reserved.
   *
   * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
   * modify and redistribute this software in source and binary code form,
   * provided that i) this copyright notice and license appear on all copies of
   * the software; and ii) Licensee does not utilize the software in a manner
   * which is disparaging to Sun.
   *
   * This software is provided "AS IS," without a warranty of any kind. ALL
   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
   * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
   * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
   * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
   * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
   * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
   * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
   * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
   * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
   * POSSIBILITY OF SUCH DAMAGES.
   *
   * This software is not designed or intended for use in on-line control of
   * aircraft, air traffic, aircraft navigation or aircraft communications; or in
   * the design, construction, operation or maintenance of any nuclear
   * facility. Licensee represents and warrants that it will not use or
   * redistribute the Software for such purposes.
   */
  package org.jboss.test.jrmp.ejb;
  
  interface CompressionConstants
  {
   
      /** Constants for 6-bit code values. */
  
      /** No operation: used to pad words on flush. */
      static final int NOP     = 0;  
  
      /** Introduces raw byte format. */
      static final int RAW     = 1;  
  
      /** Format indicator for characters found in lookup table. */
      static final int BASE    = 2;  
  
      /** A character's code is it's index in the lookup table. */
      static final String codeTable =
          "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ,.!?\"'()";
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/jrmp/ejb/CompressionInputStream.java
  
  Index: CompressionInputStream.java
  ===================================================================
  /*
   * Copyright (c) 1998, 1999 Sun Microsystems, Inc. All Rights Reserved.
   *
   * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
   * modify and redistribute this software in source and binary code form,
   * provided that i) this copyright notice and license appear on all copies of
   * the software; and ii) Licensee does not utilize the software in a manner
   * which is disparaging to Sun.
   *
   * This software is provided "AS IS," without a warranty of any kind. ALL
   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
   * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
   * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
   * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
   * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
   * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
   * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
   * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
   * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
   * POSSIBILITY OF SUCH DAMAGES.
   *
   * This software is not designed or intended for use in on-line control of
   * aircraft, air traffic, aircraft navigation or aircraft communications; or in
   * the design, construction, operation or maintenance of any nuclear
   * facility. Licensee represents and warrants that it will not use or
   * redistribute the Software for such purposes.
   */
  package org.jboss.test.jrmp.ejb;
  
  import java.io.*;
  import java.net.*;
  
  class CompressionInputStream extends FilterInputStream
      implements CompressionConstants
  {
      /*
       * Constructor calls constructor of superclass
       */
      public CompressionInputStream(InputStream in) {
          super(in);
      }
   
      /* 
       * Buffer of unpacked 6-bit codes 
       * from last 32 bits read.
       */
      int buf[] = new int[5];
   
      /*
       * Position of next code to read in buffer (5 signifies end). 
       */ 
      int bufPos = 5;
   
      /*
       * Reads in format code and decompresses character accordingly.
       */
  
      public int read() throws IOException {
          try {
              int code;
  
              // Read in and ignore empty bytes (NOP's) as long as they
              // arrive. 
              do {
              code = readCode();
            } while (code == NOP);      
   
              if (code >= BASE) {
                  // Retrieve index of character in codeTable if the
                  // code is in the correct range.
                  return codeTable.charAt(code - BASE);
              } else if (code == RAW) {
                  // read in the lower 4 bits and the higher 4 bits,
                  // and return the reconstructed character
                  int high = readCode();
                  int low = readCode();
                  return (high << 4) | low;
              } else 
                  throw new IOException("unknown compression code: " + code);
          } catch (EOFException e) {
              // Return the end of file code
              return -1;
          }
      }
   
      /* 
       * This method reads up to len bytes from the input stream. 
       * Returns if read blocks before len bytes are read.
       */ 
      public int read(byte b[], int off, int len) throws IOException {
  
        if (len <= 0) {
            return 0;
        }
  
        int c = read();
        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;
  
        int i = 1;
          // Try to read up to len bytes or until no
          // more bytes can be read without blocking.
                try {
                for (; (i < len) && (in.available() > 0); i++) {
                c = read();
                if (c == -1) {
                    break;
                }
                if (b != null) {
                    b[off + i] = (byte)c;
                }
            }
        } catch (IOException ee) {
        }
        return i;
      }
  
      /*
       * If there is no more data to decode left in buf, read the
       * next four bytes from the wire. Then store each group of 6
       * bits in an element of buf.  Return one element of buf.
       */
      private int readCode() throws IOException {
          // As soon as all the data in buf has been read
          // (when bufPos == 5) read in another four bytes.
          if (bufPos == 5) {
              int b1 = in.read();
              int b2 = in.read();
              int b3 = in.read();
              int b4 = in.read();
  
              // make sure none of the bytes signify the
              // end of the data in the stream
              if ((b1 | b2 | b3 | b4) < 0) {
                  throw new EOFException();
              }
              // Assign each group of 6 bits to an element of
              // buf
              int pack = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
              buf[0] = (pack >>> 24) & 0x3F;
              buf[1] = (pack >>> 18) & 0x3F;
              buf[2] = (pack >>> 12) & 0x3F;
              buf[3] = (pack >>>  6) & 0x3F;
              buf[4] = (pack >>>  0) & 0x3F;
              bufPos = 0;
          }
          return buf[bufPos++];
      }
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/jrmp/ejb/CompressionOutputStream.java
  
  Index: CompressionOutputStream.java
  ===================================================================
  /*
   * Copyright (c) 1998, 1999 Sun Microsystems, Inc. All Rights Reserved.
   *
   * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
   * modify and redistribute this software in source and binary code form,
   * provided that i) this copyright notice and license appear on all copies of
   * the software; and ii) Licensee does not utilize the software in a manner
   * which is disparaging to Sun.
   *
   * This software is provided "AS IS," without a warranty of any kind. ALL
   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
   * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
   * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
   * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
   * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
   * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
   * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
   * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
   * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
   * POSSIBILITY OF SUCH DAMAGES.
   *
   * This software is not designed or intended for use in on-line control of
   * aircraft, air traffic, aircraft navigation or aircraft communications; or in
   * the design, construction, operation or maintenance of any nuclear
   * facility. Licensee represents and warrants that it will not use or
   * redistribute the Software for such purposes.
   */
  
  package org.jboss.test.jrmp.ejb;
  
  import java.io.*;
   
  class CompressionOutputStream extends FilterOutputStream
      implements CompressionConstants
  {
  
      /*
       * Constructor calls constructor of superclass.
       */
      public CompressionOutputStream(OutputStream out) {
          super(out);
      }
   
      /* 
       * Buffer of 6-bit codes to pack into next 32-bit word
       * Five 6-bit codes fit into 4 words. 
       */
      int buf[] = new int[5];
   
      /*
       * Index of valid codes waiting in buf. 
       */
      int bufPos = 0;
   
  
      /*  
       * This method writes one byte to the socket stream. 
       */ 
      public void write(int b) throws IOException {
          // force argument to one byte
          b &= 0xFF;  
  
          // Look up pos in codeTable to get its encoding. 
          int pos = codeTable.indexOf((char)b);
  
          if (pos != -1){
              // If pos is in the codeTable, write BASE + pos into buf. 
              // By adding BASE to pos, we know that the characters in
              // the codeTable will always have a code between 2 and 63
              // inclusive. This allows us to use RAW (RAW is equal to
              // 1) to signify that the next two groups of 6-bits are 
              // necessary for decompression of the next character.
            
              writeCode(BASE + pos);
          } else {
              // Otherwise, write RAW into buf to signify that the
              // Character is being sent in 12 bits.
              writeCode(RAW);
  
              // Write the last 4 bits of b into the buf.
              writeCode(b >> 4);
  
              // Truncate b to contain data in only the first 4 bits, 
              // and write the first 4 bits of b into buf.
              writeCode(b & 0xF);
          }
      }
   
      /* 
       * This method writes up to len bytes to the socket stream. 
       */
      public void write(byte b[], int off, int len) throws IOException {
          /*
           * This implementation is quite inefficient because it has to
           * call the other write method for every byte in the array.  It
           * could be optimized for performance by doing all the processing
           * in this method.
           */
          for (int i = 0; i < len; i++)
              write(b[off + i]);
      }
   
  
     /* 
      * Clears buffer of all data (zeroes it out). 
      */ 
    public void flush() throws IOException {
      while (bufPos > 0)
        writeCode(NOP);
    }
   
      /* 
       * This method actually puts the data into the output stream after
       * packing the data from all 5 bytes in buf into one word.
       * Remember, each byte has, at most, 6 significant bits.
       */
      private void writeCode(int c) throws IOException {
          buf[bufPos++] = c;
          if (bufPos == 5) {      // write next word when we have 5 codes
              int pack = (buf[0] << 24) | (buf[1] << 18) | (buf[2] << 12) |
                         (buf[3] << 6) | buf[4];
              out.write((pack >>> 24) & 0xFF);
              out.write((pack >>> 16) & 0xFF);
              out.write((pack >>> 8)  & 0xFF);
              out.write((pack >>> 0)  & 0xFF);
              bufPos = 0;
          }
      }
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/jrmp/ejb/CompressionServerSocket.java
  
  Index: CompressionServerSocket.java
  ===================================================================
  package org.jboss.test.jrmp.ejb;
  
  import java.io.IOException;
  import java.net.Socket;
  import java.net.ServerSocket;
  
  /** A custom server socket that uses the GZIPInputStream and GZIPOutputStream
  streams for compression.
  
  @see java.net.ServerSocket
  @see java.util.zip.GZIPInputStream
  @see java.util.zip.GZIPOutputStream
  
  @author  [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  class CompressionServerSocket extends ServerSocket
  {
      private boolean closed;
  
      public CompressionServerSocket(int port) throws IOException 
      {
          super(port);
      }
  
      public Socket accept() throws IOException
      {
          Socket s = new CompressionSocket();
          implAccept(s);
          System.out.println("CompressionServerSocket.accept : "+s);
          return s;
      }
  
      public int getLocalPort()
      {
          if( closed == true )
              return -1;
          return super.getLocalPort();
      }
  
      public void close() throws IOException
      {
          closed = true;
          super.close();
      }
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/jrmp/ejb/CompressionServerSocketFactory.java
  
  Index: CompressionServerSocketFactory.java
  ===================================================================
  package org.jboss.test.jrmp.ejb;
  
  import java.io.IOException;
  import java.io.Serializable;
  import java.net.ServerSocket;
  import java.rmi.server.RMIServerSocketFactory;
  
  /** The CompressionServerSocketFactory from the RMI custom socket
  factory tutorial.
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class CompressionServerSocketFactory implements RMIServerSocketFactory, 
Serializable
  {
      /**
       * Create a server socket on the specified port (port 0 indicates
       * an anonymous port).
       * @param  port the port number
       * @return the server socket on the specified port
       * @exception IOException if an I/O error occurs during server socket
       * creation
       * @since 1.2
       */
      public ServerSocket createServerSocket(int port) throws IOException
      {
          ServerSocket activeSocket = new CompressionServerSocket(port);
          
System.out.println("CompressionServerSocketFactory.createServerSocket("+port+") : 
"+activeSocket);
          return activeSocket;
      }
  
      public boolean equals(Object obj)
      {
          return obj instanceof CompressionServerSocketFactory;
      }
  
      public int hashCode()
      {
          return getClass().getName().hashCode();
      }
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/jrmp/ejb/CompressionSocket.java
  
  Index: CompressionSocket.java
  ===================================================================
  package org.jboss.test.jrmp.ejb;
  
  import java.io.InputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.net.Socket;
  import java.util.zip.GZIPInputStream;
  import java.util.zip.GZIPOutputStream;
  
  /** A custom socket that uses the GZIPInputStream and GZIPOutputStream streams
  for compression.
  
  @see java.net.ServerSocket
  @see java.util.zip.GZIPInputStream
  @see java.util.zip.GZIPOutputStream
  
  @author  [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  class CompressionSocket extends Socket
  {
  
      /* InputStream used by socket */
      private InputStream in;
      /* OutputStream used by socket */
      private OutputStream out;
  
      /* 
       * No-arg constructor for class CompressionSocket  
       */
      public CompressionSocket()
      {
          super();
      }
  
      /* 
       * Constructor for class CompressionSocket 
       */
      public CompressionSocket(String host, int port) throws IOException 
      {
          super(host, port);
      }
  
      /* 
       * Returns a stream of type CompressionInputStream 
       */
      public InputStream getInputStream() throws IOException 
      {
          if (in == null)
          {
              in = new CompressionInputStream(super.getInputStream());
          }
          return in;
      }
  
      /* 
       * Returns a stream of type CompressionOutputStream 
       */
      public OutputStream getOutputStream() throws IOException 
      {
          if (out == null)
          {
              out = new CompressionOutputStream(super.getOutputStream());
          }
          return out;
      }
  
      /*
       * Flush the CompressionOutputStream before 
       * closing the socket.
       */
      public synchronized void close() throws IOException
      {
          OutputStream o = getOutputStream();
          o.flush();
        super.close();
      }
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/jrmp/ejb/StatelessSessionBean.java
  
  Index: StatelessSessionBean.java
  ===================================================================
  package org.jboss.test.jrmp.ejb;
  
  import java.rmi.RemoteException;
  import javax.ejb.CreateException;
  import javax.ejb.SessionBean;
  import javax.ejb.SessionContext;
  
  /** A simple session bean for testing access over custom RMI sockets.
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class StatelessSessionBean implements SessionBean
  {
      private SessionContext sessionContext;
  
      public void ejbCreate() throws CreateException
      {
          System.out.println("StatelessSessionBean.ejbCreate() called");
      }
  
      public void ejbActivate()
      {
          System.out.println("StatelessSessionBean.ejbActivate() called");
      }
  
      public void ejbPassivate()
      {
          System.out.println("StatelessSessionBean.ejbPassivate() called");
      }
  
      public void ejbRemove()
      {
          System.out.println("StatelessSessionBean.ejbRemove() called");
      }
  
      public void setSessionContext(SessionContext context)
      {
          sessionContext = context;
      }
  
      public String echo(String arg)
      {
          System.out.println("StatelessSessionBean.echo, arg="+arg);
          return arg;
      }
  }
  
  
  

Reply via email to