hammant     02/04/10 15:46:18

  Added:       altrmi/src/java/org/apache/excalibur/altrmi/client/impl/socket
                        CallbackEnabledCustomSocketStreamHostContext.java
                        CallbackEnabledCustomSocketStreamInvocationHandler.java
               altrmi/src/java/org/apache/excalibur/altrmi/client/impl/stream
                        CallbackEnabledClientCustomStreamReadWriter.java
               altrmi/src/java/org/apache/excalibur/altrmi/common
                        AltrmiCallbackException.java
                        ExposedObjectProxy.java
               altrmi/src/java/org/apache/excalibur/altrmi/server/impl
                        CallbackEnabledCustomSocketStreamReadWriter.java
               altrmi/src/java/org/apache/excalibur/altrmi/server/impl/socket
                        CallbackEnabledCustomSocketStreamServer.java
  Log:
  New classes for callback from Vinay
  
  Revision  Changes    Path
  1.1                  
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/socket/CallbackEnabledCustomSocketStreamHostContext.java
  
  Index: CallbackEnabledCustomSocketStreamHostContext.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.altrmi.client.impl.socket;
  
  
  
  import org.apache.excalibur.altrmi.client.impl.AbstractHostContext;
  import org.apache.excalibur.altrmi.common.AltrmiConnectionException;
  import org.apache.excalibur.altrmi.common.*;
  import org.apache.excalibur.altrmi.common.AltrmiCallbackException;
  
  import java.net.Socket;
  /**
   * Class CallbackEnabledCustomSocketStreamHostContext
   * @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
   * @version $Revision: 1.1 $
   */
  public class CallbackEnabledCustomSocketStreamHostContext extends 
AbstractHostContext {
  
      public CallbackEnabledCustomSocketStreamHostContext(String host, int 
port) throws AltrmiConnectionException {
          super(new CallbackEnabledCustomSocketStreamInvocationHandler(host, 
port, SocketCustomStreamHostContext.class.getClassLoader()));
      }
  
      public CallbackEnabledCustomSocketStreamHostContext(String host, int 
port, ClassLoader classLoader) throws AltrmiConnectionException {
          super(new CallbackEnabledCustomSocketStreamInvocationHandler(host, 
port, classLoader));
      }
        
        public boolean exposeObject(Object tobeExposedObject,Class 
tobeExposedInterface) throws AltrmiCallbackException
        {
                return 
((CallbackEnabledCustomSocketStreamInvocationHandler)getInvocationHandler()).exposeObject(tobeExposedObject,tobeExposedInterface);
        }
  
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/socket/CallbackEnabledCustomSocketStreamInvocationHandler.java
  
  Index: CallbackEnabledCustomSocketStreamInvocationHandler.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.altrmi.client.impl.socket;
  
  
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  import org.apache.excalibur.altrmi.client.impl.stream.ClientStreamReadWriter;
  import org.apache.excalibur.altrmi.common.*;
  import org.apache.excalibur.altrmi.common.AltrmiConnectionException;
  import org.apache.excalibur.altrmi.common.AltrmiCallbackException;
  import org.apache.excalibur.altrmi.client.impl.stream.*;
  import 
org.apache.excalibur.altrmi.client.impl.stream.CallbackEnabledClientCustomStreamReadWriter;
  
  
  /**
   * Class CallbackEnabledCustomSocketStreamInvocationHandler
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
   * @version $Revision: 1.1 $
   */
  public final class CallbackEnabledCustomSocketStreamInvocationHandler
          extends AbstractSocketStreamInvocationHandler {
  
        private CallbackEnabledClientCustomStreamReadWriter 
mCallbackEnabledClientCustomStreamReadWriter;
        /*
         * @see 
AbstractSocketStreamInvocationHandler#AbstractSocketStreamInvocationHandler(String,
 int, ClassLoader)
         */
      public CallbackEnabledCustomSocketStreamInvocationHandler(String host, 
int port, ClassLoader classLoader)
              throws AltrmiConnectionException {
          super(host, port, classLoader);
      }
  
        /* 
         * @see 
AbstractSocketStreamInvocationHandler#createClientStreamReadWriter(InputStream, 
OutputStream)
         */
      protected ClientStreamReadWriter createClientStreamReadWriter(
              InputStream in, OutputStream out) throws IOException {
                if(mCallbackEnabledClientCustomStreamReadWriter==null)
                {
                        mCallbackEnabledClientCustomStreamReadWriter = new 
CallbackEnabledClientCustomStreamReadWriter(in, out, mInterfacesClassLoader);
                }
          return mCallbackEnabledClientCustomStreamReadWriter;
      }
  
        
        public boolean exposeObject(Object tobeExposedObject,Class 
tobeExposedInterface) throws AltrmiCallbackException
        {
                return 
mCallbackEnabledClientCustomStreamReadWriter.exposeObject(tobeExposedObject,tobeExposedInterface);
        }
        public String  getPublishedName(Object tobeExposedObject)
        {
                return 
mCallbackEnabledClientCustomStreamReadWriter.getPublishedName(tobeExposedObject);
        }
  }
  
  
  
  
  1.1                  
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/stream/CallbackEnabledClientCustomStreamReadWriter.java
  
  Index: CallbackEnabledClientCustomStreamReadWriter.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.altrmi.client.impl.stream;
  
  import java.io.BufferedOutputStream;
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.HashMap;
  
  import org.apache.excalibur.altrmi.common.AltrmiCallbackException;
  import org.apache.excalibur.altrmi.common.AltrmiReply;
  import org.apache.excalibur.altrmi.common.AltrmiRequest;
  import org.apache.excalibur.altrmi.common.SerializationHelper;
  import org.apache.excalibur.altrmi.server.AltrmiServer;
  import org.apache.excalibur.altrmi.server.PublicationException;
  import 
org.apache.excalibur.altrmi.server.impl.classretrievers.PlainClassRetriever;
  import org.apache.excalibur.altrmi.server.impl.direct.DirectServer;
  
  /**
   * Class CallbackEnabledClientCustomStreamReadWriter
   *    This class embedds within itself a AltrmiServer which holds the 
   *    exposedObjects for the client and upon which the Server can make
   *    callbacks.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
   * @version $Revision: 1.1 $
   */
  public class CallbackEnabledClientCustomStreamReadWriter 
                        extends ClientStreamReadWriter 
                        implements Runnable
  {
        //Mutex to block the client till a reply reaches back from server
      private Object mReplyLock = new Object();
      private AltrmiReply mReply =null;
        private Thread mMessageLoopThread =null;
        private boolean mIsStopped=false;
      private DataInputStream mDataInputStream;
      private DataOutputStream mDataOutputStream;
      private ClassLoader mInterfacesClassLoader;
        private HashMap mExposedObjPublishNameHash_ =  new HashMap();
        private AltrmiServer mClientServerHostingExposedObjects =new 
DirectServer();
        private static int mRandom__=0;
  
  
      /**
       * Constructor ClientCustomStreamReadWriter
       *
       *
       * @param inputStream
       * @param outputStream
       *
       * @throws IOException
       *
       */
      public CallbackEnabledClientCustomStreamReadWriter(InputStream 
inputStream, OutputStream outputStream, ClassLoader interfacesClassLoader)
              throws IOException {
          mDataOutputStream = new DataOutputStream(new 
BufferedOutputStream(outputStream));
          mDataInputStream = new DataInputStream(inputStream);
  
          mInterfacesClassLoader = interfacesClassLoader;
          //This thread recv's all the messages sent from the server
          mMessageLoopThread =  new Thread(this);
          mMessageLoopThread.start();
          //start the internal AltrmiServer which holds the exposed Objects
          // for the server to make callbacks upon.
                try
                {
                        mClientServerHostingExposedObjects.start();
                        
mClientServerHostingExposedObjects.setClassRetriever(new PlainClassRetriever());
                }
                catch(Exception e)
                {
                        throw new IOException("Error Starting Internal 
AltrmiServer");
                }
      }
  
        /*
         * @see Runnable#run()
         */
        public void run()
        {
                while(!mIsStopped)
                {
                try 
                {
                                int byteArraySize = (int) 
mDataInputStream.readInt();
                            byte[] byteArray = new byte[byteArraySize];
                        
                            mDataInputStream.read(byteArray);
                        
                            
//_replyQueue.addFirst(SerializationHelper.getInstanceFromBytes(byteArray, 
mInterfacesClassLoader));
                            Object 
obj=SerializationHelper.getInstanceFromBytes(byteArray, mInterfacesClassLoader);
  
                                if(obj instanceof AltrmiReply)
                                {
                                    mReply=(AltrmiReply)obj;
                                        synchronized(mReplyLock)
                                        {
                                                mReplyLock.notify();
                                        }
                                }
                                else if(obj instanceof AltrmiRequest)
                                {
                                                AltrmiReply altrmiReply = 
mClientServerHostingExposedObjects.handleInvocation((AltrmiRequest)obj);
                                                postReply(altrmiReply);
                                }
                                else
                                {
                                        //NEVER OCCURS 
                                }
  
                        } 
                        catch(IOException e) 
                        {
                                // :-?
                                if(e instanceof java.net.SocketTimeoutException)
                                        continue;
                                e.printStackTrace();
                                mIsStopped=true;
                                return;
                        } 
                        catch(ClassNotFoundException e) 
                        {
                                e.printStackTrace();
                        }
                
                }
        }
        
        /**
         * Method getReplyFromMessageLoop.
         *      Returns the reply recevied from the server
         * @return AltrmiReply
         */
        public AltrmiReply getReplyFromMessageLoop()
        {
                if(mReply==null)
                {
                        synchronized(mReplyLock)
                        {
                                try 
                                {
                                        mReplyLock.wait();
                                } 
                                catch(InterruptedException e) 
                                {
                                        e.printStackTrace();
                                }
                        }
                }
                return mReply;
        }
        
        
        /*
         * @see ClientStreamReadWriter#postRequest(AltrmiRequest)
         */
      protected  AltrmiReply postRequest(AltrmiRequest altrmiRequest)
              throws IOException, ClassNotFoundException {
          if(mIsStopped)
                throw new IOException("Server Stoppped");
          writeRequest(altrmiRequest);
          AltrmiReply r = readReply();
          mReply=null;
          return r;
      }
  
        /**
         * Method writeRequest.
         * @param altrmiRequest
         * @throws IOException
         */
      private void writeRequest(AltrmiRequest altrmiRequest) throws IOException 
{
  
          byte[] aBytes = 
SerializationHelper.getBytesFromInstance(altrmiRequest);
  
          mDataOutputStream.writeInt(aBytes.length);
          mDataOutputStream.write(aBytes);
          mDataOutputStream.flush();
      }
  
        /**
         * Method readReply.
         * @return AltrmiReply
         * @throws IOException
         * @throws ClassNotFoundException
         */
      private AltrmiReply readReply() throws IOException, 
ClassNotFoundException {
                
                return getReplyFromMessageLoop();
      }
  
        /**
         * Method postReply.
         * @param altrmiReply
         * @throws IOException
         * @throws ClassNotFoundException
         */
        public  void postReply(AltrmiReply altrmiReply) throws IOException 
,ClassNotFoundException
        {
          byte[] aBytes = SerializationHelper.getBytesFromInstance(altrmiReply);
  
          mDataOutputStream.writeInt(aBytes.length);
          mDataOutputStream.write(aBytes);
          mDataOutputStream.flush();
        }
  
  
  
  
        /**
         * Method exposeObject.
         *      Expose the Object by adding it to the internal AltrmiServer 
         *      and by assigning a unique PublishedName for the same.
         * @param tobeExposedObject
         * @param tobeExposedInterface
         * @return boolean
         * @throws AltrmiCallbackException
         */
        public boolean exposeObject(Object tobeExposedObject,Class 
tobeExposedInterface) throws AltrmiCallbackException
        {
                if(mExposedObjPublishNameHash_.get(tobeExposedObject)!=null)
                        return false;
                String 
_uniquePublishedName=getUniqueNameForExposedObject(tobeExposedObject);
                
mExposedObjPublishNameHash_.put(tobeExposedObject,_uniquePublishedName);
                try
                {
                        
mClientServerHostingExposedObjects.publish(tobeExposedObject,_uniquePublishedName,tobeExposedInterface);
                }
                catch(PublicationException pce)
                {
                        throw new AltrmiCallbackException(pce.getMessage());
                }
                return true;
        }
        /**
         * Method getUniqueNameForExposedObject.
         *      Generate Unqiue name for the exposedObject.
         *  Right now its as simple as ClassName_1,ClassName_2  etc....
         * @param tobeExposedObject
         * @return String
         */
        private String getUniqueNameForExposedObject(Object tobeExposedObject)
        {
                mRandom__++;
                return 
tobeExposedObject.getClass().getName().substring(tobeExposedObject.getClass().getName().lastIndexOf(".")+1)+"_"+mRandom__;
        }
        /**
         * Method getPublishedName.
         *      Use while marshalling the exposedObject as an argument 
         *  to the server.
         * @param tobeExposedObject
         * @return String
         */
        public String getPublishedName(Object tobeExposedObject)
        {
                return 
(String)mExposedObjPublishNameHash_.get(tobeExposedObject);
        }
      
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/common/AltrmiCallbackException.java
  
  Index: AltrmiCallbackException.java
  ===================================================================
  
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.altrmi.common;
  
  
  
  /**
   * AltrmiCallbackException occurs on failure to expose 
   * the object for calbacks
   *
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
   * @version $Revision: 1.1 $
   */
  public class AltrmiCallbackException extends Exception {
  
      /**
       * Constructor AltrmiCallbackException 
       *
       *
       * @param s
       *
       */
      public AltrmiCallbackException (String s) {
          super(s);
      }
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/common/ExposedObjectProxy.java
  
  Index: ExposedObjectProxy.java
  ===================================================================
  
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.altrmi.common;
  
  
  
  import java.io.ObjectOutput;
  import java.io.IOException;
  import java.io.ObjectInput;
  
  
  /**
   * ExposedObjectProxy wraps the exposed client objects PublishedName
   * when sent across to the server as the argument.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
   * @version $Revision: 1.1 $
   */
  public final class ExposedObjectProxy extends AltrmiReply {
  
        static final long serialVersionUID = -204050172209495725L;
        private String mPublishedName;
      /**
       * Constructor ExposedObjectProxy
       *
       *
       *
       * @param beanClassBytes
       * @param interfaceImplClassBytes
       *
       */
      public ExposedObjectProxy(String publishedName) {
                this.mPublishedName=publishedName;
      }
  
      /**
       * Constructor ExposedObjectProxy
       *
       *
       */
      public ExposedObjectProxy() {}    // for Externalization
  
  
      /**
       * Method getPublishedName
       *
       *
       * @return
       *
       */
      public String getPublishedName() {
          return mPublishedName;
      }
  
      /**
       * Method getReplyCode.  This is quicker than instanceof for type 
checking.
       *
       *
       * @return
       *
       */
      public int getReplyCode() {
          return CLASSREPLY;
      }
  
      /**
       * The object implements the writeExternal method to save its contents
       * by calling the methods of DataOutput for its primitive values or
       * calling the writeObject method of ObjectOutput for objects, strings,
       * and arrays.
       *
       * @serialData Overriding methods should use this tag to describe
       *             the data layout of this Externalizable object.
       *             List the sequence of element types and, if possible,
       *             relate the element to a public/protected field and/or
       *             method of this Externalizable class.
       *
       * @param out the stream to write the object to
       * @exception IOException Includes any I/O exceptions that may occur
       */
      public void writeExternal(ObjectOutput out) throws IOException {
                out.writeObject(mPublishedName);
      }
  
      /**
       * The object implements the readExternal method to restore its
       * contents by calling the methods of DataInput for primitive
       * types and readObject for objects, strings and arrays.  The
       * readExternal method must read the values in the same sequence
       * and with the same types as were written by writeExternal.
       *
       * @param in the stream to read data from in order to restore the object
       * @exception IOException if I/O errors occur
       * @exception ClassNotFoundException If the class for an object being
       *              restored cannot be found.
       */
      public void readExternal(ObjectInput in) throws IOException, 
ClassNotFoundException {
                mPublishedName=(String)in.readObject();
      }
  
        
        /*
         * @see Object#toString()
         */
        public String  toString()
        {
                return "ExposedObjectProxy.PublishedName["+mPublishedName+"]";
        }
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/CallbackEnabledCustomSocketStreamReadWriter.java
  
  Index: CallbackEnabledCustomSocketStreamReadWriter.java
  ===================================================================
  
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.altrmi.server.impl;
  
  
  
  import org.apache.excalibur.altrmi.common.AltrmiRequest;
  import org.apache.excalibur.altrmi.common.MethodRequest;
  import org.apache.excalibur.altrmi.common.AltrmiReply;
  import org.apache.excalibur.altrmi.client.AltrmiHostContext;
  import org.apache.excalibur.altrmi.client.impl.AbstractHostContext;
  import org.apache.excalibur.altrmi.client.AltrmiClientInvocationHandler;
  import org.apache.excalibur.altrmi.common.ExposedObjectProxy;
  import org.apache.excalibur.altrmi.common.SerializationHelper;
  import org.apache.excalibur.altrmi.client.impl.stream.StreamInvocationHandler;
  import org.apache.excalibur.altrmi.client.impl.stream.ClientStreamReadWriter;
  import org.apache.excalibur.altrmi.client.AltrmiFactory;
  import org.apache.excalibur.altrmi.client.impl.ServerClassAltrmiFactory;
  import org.apache.excalibur.altrmi.client.impl.BaseServedObject;
  import org.apache.excalibur.altrmi.common.AltrmiConnectionException;
  import org.apache.excalibur.altrmi.common.AltrmiConnectionClosedException;
  import org.apache.excalibur.altrmi.common.ExceptionReply;
  import org.apache.excalibur.altrmi.common.LookupRequest;
  import org.apache.excalibur.altrmi.common.LookupReply;
  import org.apache.excalibur.altrmi.common.NotPublishedReply;
  import org.apache.excalibur.altrmi.common.AltrmiAuthentication;
  
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.BufferedOutputStream;
  
  
  /**
   * CallbackEnabledCustomSocketStreamReadWriter 
   *  This StreamReadWriter is responsible for unmarshalling 
   *    exposedClient objects too and devliering it to the Remote Object.
   *  The calls made for the exposed Client Object is also marshalled
   *    out here with the aid of the internal InvocationHandler.
   *  The callbacks are multiplexed over the same connection and thus
   *    there are no extra network connections needed to enable callbacks.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
   * @version $Revision: 1.1 $
   */
  public class CallbackEnabledCustomSocketStreamReadWriter extends 
ServerStreamReadWriter implements Runnable 
  {
  
      private DataInputStream mDataInputStream;
      private DataOutputStream mDataOutputStream;
        private Thread mMessageLoopThread =null;
        private boolean mIsStopped=false;
        private Object mRequestLock= new Object();
        private Object mReplyLock= new Object();
      private AltrmiRequest mRequest=null;
      private AltrmiReply mReply=null;
        private CallbackServerClientReadWriter mCallbackServerClientReadWriter;
        private CallbackStreamInvocationHandler 
mCallbackStreamInvocationHandler;
        private CallbackHostContext mCallbackHostContext;
        private CallbackServerClassAltrmiFactory  mAltrmiFactory;
  
  
        /*
         * @see ServerStreamReadWriter#initialize()
         */
      protected void initialize() throws IOException {
          mDataInputStream = new DataInputStream(mInputStream);
          mDataOutputStream = new DataOutputStream(new 
BufferedOutputStream(mOutputStream));
                //start the receiving message loop
                mMessageLoopThread =  new Thread(this);
                mMessageLoopThread.start();
        
      }
  
        /**
         * Message Loop . 
         * Notifies the Reply or Request monitor depending upon the type of 
message.
         * @see Runnable#run()
         */
        public void run()
        {
                mCallbackServerClientReadWriter  = new 
CallbackServerClientReadWriter(this);
                mCallbackStreamInvocationHandler =  new 
CallbackStreamInvocationHandler(this.getClass().getClassLoader());
                
mCallbackStreamInvocationHandler.setObjectReadWriter(mCallbackServerClientReadWriter);
                mCallbackHostContext    =  new 
CallbackHostContext(mCallbackStreamInvocationHandler);
                mAltrmiFactory  = new CallbackServerClassAltrmiFactory(false);
                mAltrmiFactory.setInitializedHostContext(mCallbackHostContext);
  
                while(!mIsStopped)
                {
                try 
                {
                                //recv packets from the client
                        int byteArraySize = (int) mDataInputStream.readInt();
                            byte[] byteArray = new byte[byteArraySize];
                            mDataInputStream.read(byteArray);
                            Object 
obj=SerializationHelper.getInstanceFromBytes(byteArray);
                                
                                //interpret it as a request or reply & notify 
the corresponding listeners
                                // SHLD we need a queue here to hold the 
arriving packets
                                // TODO:WORKAROUND: Dont receive until the data 
is handled some way
                                if(obj instanceof AltrmiRequest)
                                {
                                    mRequest=(AltrmiRequest)obj;
                                        synchronized(mRequestLock)
                                        {
                                                mRequestLock.notify();
                                        }
                                }
                                else if(obj instanceof AltrmiReply)
                                {
                                    mReply=(AltrmiReply)obj;
                                        synchronized(mReplyLock)
                                        {
                                                mReplyLock.notify();
                                        }
                                }
                                else
                                {
                                        //SHLD never occur
                                }
  
                        }
                        catch(java.net.SocketException se) 
                        {
                                mIsStopped=true;
                                synchronized(mRequestLock)
                                {
                                        mRequestLock.notify();
                                }
                                synchronized(mReplyLock)
                                {
                                        mReplyLock.notify();
                                }
                                return;
                        } 
                        catch(IOException e) 
                        {
                                e.printStackTrace();
                                mIsStopped=true;
                                return;
                        } 
                        catch(ClassNotFoundException ce) 
                        {
                                ce.printStackTrace();
                                mIsStopped=true;
                                return;
                        }
                }
                
        }
  
        
  
        /**
         * Method getRequestFromMessageLoop.
         * @return AltrmiRequest
         */
        private AltrmiRequest getRequestFromMessageLoop()
        {
                //if(_replyQueue.size()==0)
                if(mRequest==null)
                {
                        synchronized(mRequestLock)
                        {
                                try 
                                {
                                        mRequestLock.wait();
                                } 
                                catch(InterruptedException e) 
                                {
                                        e.printStackTrace();
                                }
                        }
                }
                return mRequest;
        }
  
        /**
         * Method getReplyFromMessageLoop.
         * @return AltrmiReply
         */
        private AltrmiReply getReplyFromMessageLoop()
        {
  
                //if(_replyQueue.size()==0)
                if(mReply==null)
                {
  
                        synchronized(mReplyLock)
                        {
                                try 
                                {
                                        mReplyLock.wait();
                                } 
                                catch(InterruptedException e) 
                                {
                                        e.printStackTrace();
                                }
                        }
                }
                return mReply;
        }
  
        /*
         * @see ServerStreamReadWriter#writeReplyAndGetRequest(AltrmiReply)
         */
      protected  AltrmiRequest writeReplyAndGetRequest(AltrmiReply altrmiReply)
              throws IOException, ClassNotFoundException {
  
          if (altrmiReply != null) {
              writeReply(altrmiReply);
          }
                AltrmiRequest req= readRequest();
                mRequest=null;
          return req;
      }
  
        /**
         * Method writeReply.
         * @param altrmiReply
         * @throws IOException
         */
      private void writeReply(AltrmiReply altrmiReply) throws IOException {
  
          byte[] aBytes = SerializationHelper.getBytesFromInstance(altrmiReply);
  
          mDataOutputStream.writeInt(aBytes.length);
          mDataOutputStream.write(aBytes);
          mDataOutputStream.flush();
      }
  
        /**
         * Method readRequest.
         * @return AltrmiRequest
         * @throws IOException
         * @throws ClassNotFoundException
         */
      private AltrmiRequest readRequest() throws IOException, 
ClassNotFoundException {
  
                AltrmiRequest altrmiRequest=getRequestFromMessageLoop();
                if(altrmiRequest instanceof MethodRequest)
                        correctArgs(((MethodRequest)altrmiRequest).getArgs());
          return altrmiRequest;
      }
  
  //client side    
        /**
         * Method postRequest.
         * @param altrmiRequest
         * @return AltrmiReply
         * @throws IOException
         * @throws ClassNotFoundException
         */
        protected  AltrmiReply postRequest(AltrmiRequest altrmiRequest)
              throws IOException, ClassNotFoundException {
  
                if(mIsStopped)
                        throw new AltrmiConnectionClosedException("Client 
Closed Connection");
                writeRequest(altrmiRequest);
          AltrmiReply r = readReply();
                if(r==null)
                        throw new java.io.InterruptedIOException("Client 
Connection Closed");
  
          mReply=null;
          return r;
      }
  
        /**
         * Method writeRequest.
         * @param altrmiRequest
         * @throws IOException
         */
      private void writeRequest(AltrmiRequest altrmiRequest) throws IOException 
{
  
          byte[] aBytes = 
SerializationHelper.getBytesFromInstance(altrmiRequest);
  
          mDataOutputStream.writeInt(aBytes.length);
          mDataOutputStream.write(aBytes);
          mDataOutputStream.flush();
  
      }
  
        /**
         * Method readReply.
         * @return AltrmiReply
         * @throws IOException
         * @throws ClassNotFoundException
         */
      private AltrmiReply readReply() throws IOException, 
ClassNotFoundException {
                
                return getReplyFromMessageLoop();
      }
  
        
        /**
         * Method correctArgs.
         *      UnMarshall the arguments .
         * @param args
         */
        public void correctArgs(Object[] args)
        {
                for (int i = 0; i < args.length; i++) 
                {
                        if(args[i] instanceof ExposedObjectProxy)
                        {
                                ExposedObjectProxy exposedObjectProxy 
=(ExposedObjectProxy)args[i];
  
                                try
                                {
                                        //lookup the client-side exported 
object.
                                        Object obj = 
mAltrmiFactory.lookup(exposedObjectProxy.getPublishedName());
                                        args[i]=obj;
  
                                }
                                catch(Exception altrmiConnectionException)
                                {
                                        
altrmiConnectionException.printStackTrace();
                                }
                        }
                }
        }
  
        /**
         * Class CallbackHostContext wraps a StreamInvocationHandler 
         *
         * @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
         * @version $Revision: 1.1 $
         */
        private class CallbackHostContext extends  AbstractHostContext
        {
                /**
                 * Method CallbackHostContext.
                 * @param streamInvocationHandler
                 */
                CallbackHostContext(StreamInvocationHandler 
streamInvocationHandler)
                {
                        super(streamInvocationHandler);
                }
                
  
        };
  
        /**
         * Class CallbackStreamInvocationHandler 
         *      provides public access to the setObjectReadWriter function
         *
         * @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
         * @version $Revision: 1.1 $
         */
        
        private class CallbackStreamInvocationHandler extends 
StreamInvocationHandler
        {
                /*
                 * @see 
StreamInvocationHandler#StreamInvocationHandler(ClassLoader)
                 */
                CallbackStreamInvocationHandler(ClassLoader cl)
                {
                        super(cl);
                }
                /*
                 * @see AbstractClientInvocationHandler#tryReconnect()
                 */
                public boolean tryReconnect()
                {
                        return true;
                }
                /*
                 * @see 
StreamInvocationHandler#setObjectReadWriter(ClientStreamReadWriter)
                 */
                public void setObjectReadWriter(ClientStreamReadWriter 
clientStreamReadWriter)
                {
                        super.setObjectReadWriter(clientStreamReadWriter);
                }
        };
  
        /**
         * Class CallbackServerClientReadWriter 
         *      forwards the  postRequest made on it to the 
         *  CallbackEnabledSocketStreamReadWriter
         *
         * @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
         * @version $Revision: 1.1 $
         */
  
        private class CallbackServerClientReadWriter extends 
ClientStreamReadWriter
        {
                private CallbackEnabledCustomSocketStreamReadWriter 
mCallbackEnabledCustomSocketStreamReadWriter;
                
CallbackServerClientReadWriter(CallbackEnabledCustomSocketStreamReadWriter 
callbackEnabledCustomSocketStreamReadWriter)
                {
                        
this.mCallbackEnabledCustomSocketStreamReadWriter=callbackEnabledCustomSocketStreamReadWriter;
                }
                protected AltrmiReply postRequest(AltrmiRequest altrmiRequest) 
throws IOException,ClassNotFoundException
                {
                        AltrmiReply altrmiReply = 
mCallbackEnabledCustomSocketStreamReadWriter.postRequest(altrmiRequest);
                        return altrmiReply;
                }
        };
  
        /**
         * Class CallbackServerClassAltrmiFactory :
         *      Provides a hook for setting the hostContext 
         *      for the AltrmiFactory WITHOUT having to exchange
         *      OpenConnectionRequestMessage & 
         *      also to provides the hacks the lookup() mechanism by modifying 
         *      PublishedName during this part of the protocol.
         *
         * @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
         * @version $Revision: 1.1 $
         */
  
        private class CallbackServerClassAltrmiFactory extends 
ServerClassAltrmiFactory
        {
                CallbackServerClassAltrmiFactory (boolean b)
                {
                        super(b);
                }
                //hack method to influence OpenConnectionRequest/reply sequence
                public void setInitializedHostContext(AltrmiHostContext 
hostContext) 
                {
                        mHostContext = (AbstractHostContext)hostContext;
                }
  
                /**
             * Method lookup
             *  PublishedName is modified during the lookup so that
             *  the client-side Server doesNOT have to generate stubs 
             *  for each and every such PublishedName
             *  
             *
             * @param publishedServiceName
             * @param altrmiAuthentication
             * @return
             * @throws AltrmiConnectionException
             */
            public Object lookup(String publishedServiceName, 
AltrmiAuthentication altrmiAuthentication)
                    throws AltrmiConnectionException {
        
                        String modifiedPublishedName = 
publishedServiceName.substring( 0,publishedServiceName.lastIndexOf("_") );
  
                AltrmiReply ar =
                    mHostContext.getInvocationHandler()
                        .handleInvocation(new 
LookupRequest(modifiedPublishedName, altrmiAuthentication,
                                                            mSession));
        
                if (ar.getReplyCode() >= AltrmiReply.PROBLEMREPLY) {
                    if (ar instanceof NotPublishedReply) {
                        throw new AltrmiConnectionException("Service " + 
publishedServiceName
                                                            + " not published");
                    } else if (ar instanceof ExceptionReply) {
                        ExceptionReply er = (ExceptionReply) ar;
        
                        throw (AltrmiConnectionException) 
er.getReplyException();
                    } else {
                        throw new AltrmiConnectionException("Problem doing 
lookup on service");
                    }
                }
        
                LookupReply lr = (LookupReply) ar;
                BaseServedObject baseObj = new BaseServedObject(this,
                                                                
mHostContext.getInvocationHandler(),
                                                                
publishedServiceName, "Main",
                                                                
lr.getReferenceID(), mSession);
                Object retVal = getInstance(modifiedPublishedName, "Main", 
baseObj, isBeanOnly());
        
                baseObj.registerImplObject(retVal);
        
                return retVal;
            }
  
        }
  
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/socket/CallbackEnabledCustomSocketStreamServer.java
  
  Index: CallbackEnabledCustomSocketStreamServer.java
  ===================================================================
  
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.altrmi.server.impl.socket;
  
  
  
  import org.apache.excalibur.altrmi.server.AltrmiServer;
  import org.apache.excalibur.altrmi.server.AltrmiServerException;
  import org.apache.excalibur.altrmi.server.impl.AbstractServer;
  import org.apache.excalibur.altrmi.server.impl.ServerStreamReadWriter;
  import 
org.apache.excalibur.altrmi.server.impl.adapters.InvocationHandlerAdapter;
  import org.apache.excalibur.altrmi.server.impl.*;
  import 
org.apache.excalibur.altrmi.server.impl.CallbackEnabledCustomSocketStreamReadWriter;
  import org.apache.excalibur.altrmi.common.AltrmiRequest;
  import org.apache.excalibur.altrmi.common.AltrmiReply;
  
  import java.net.ServerSocket;
  import java.net.Socket;
  
  import java.io.IOException;
  
  
  /**
   * Class CallbackEnabledCustomSocketStreamServer
   *
   [EMAIL PROTECTED] <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
   * @version $Revision: 1.1 $
   */
  public class CallbackEnabledCustomSocketStreamServer extends 
AbstractCompleteSocketStreamServer {
  
  
        /*
         * @see 
AbstractCompleteSocketStreamServer#AbstractCompleteSocketStreamServer(int)
         */
      public CallbackEnabledCustomSocketStreamServer(int port) throws 
AltrmiServerException {
          super(port);
      }
  
        /*
         * @see 
AbstractCompleteSocketStreamServer#AbstractCompleteSocketStreamServer(InvocationHandlerAdapter,
 int)
         */
      public CallbackEnabledCustomSocketStreamServer(InvocationHandlerAdapter 
invocationHandlerAdapter, int port) throws AltrmiServerException {
          super(invocationHandlerAdapter, port);
      }
  
  
  
        /*
         * @see 
AbstractCompleteSocketStreamServer#createServerStreamReadWriter()
         */
      protected ServerStreamReadWriter createServerStreamReadWriter() {
          return new CallbackEnabledCustomSocketStreamReadWriter();
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to