You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ha...@apache.org on 2002/04/20 15:24:28 UTC

cvs commit: jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/socket CallbackEnabledCustomSocketStreamServer.java PartialCallbackEnabledCustomSocketStreamServer.java

hammant     02/04/20 06:24:28

  Modified:    altrmi   callback.xml
               altrmi/src/java/org/apache/excalibur/altrmi/client/impl
                        DefaultProxyHelper.java
               altrmi/src/test/org/apache/excalibur/altrmi/test/callback
                        CallBackTestClient.java CallBackTestServer.java
  Added:       altrmi/src/java/org/apache/excalibur/altrmi/client/impl/callback/socket
                        CallbackEnabledCustomSocketStreamHostContext.java
                        CallbackEnabledCustomSocketStreamInvocationHandler.java
               altrmi/src/java/org/apache/excalibur/altrmi/client/impl/callback/stream
                        CallbackEnabledClientCustomStreamReadWriter.java
               altrmi/src/java/org/apache/excalibur/altrmi/server/impl/callback
                        CallbackHostContext.java
                        CallbackServerClassAltrmiFactory.java
               altrmi/src/java/org/apache/excalibur/altrmi/server/impl/callback/socket
                        CallbackEnabledCustomSocketStreamReadWriter.java
                        CallbackEnabledCustomSocketStreamServer.java
                        CallbackServerClientReadWriter.java
                        PartialCallbackEnabledCustomSocketStreamServer.java
               altrmi/src/java/org/apache/excalibur/altrmi/server/impl/callback/stream
                        CallbackStreamInvocationHandler.java
  Removed:     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/server/impl
                        CallbackEnabledCustomSocketStreamReadWriter.java
               altrmi/src/java/org/apache/excalibur/altrmi/server/impl/socket
                        CallbackEnabledCustomSocketStreamServer.java
                        PartialCallbackEnabledCustomSocketStreamServer.java
  Log:
  Refactoring of callback into separate packages with no inner classes
  
  Revision  Changes    Path
  1.4       +3 -0      jakarta-avalon-excalibur/altrmi/callback.xml
  
  Index: callback.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/altrmi/callback.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- callback.xml	10 Apr 2002 22:33:10 -0000	1.3
  +++ callback.xml	20 Apr 2002 13:24:27 -0000	1.4
  @@ -24,6 +24,7 @@
     <!-- For running of tests -->
     <path id="test.classpath">
       <pathelement location="${build.home}/classes"/>
  +    <pathelement location="${build.home}/testclasses"/>        
       <pathelement location="${build.home}/genclasses"/>
       <pathelement location="${avalon-framework.jar}"/>
     </path>      
  @@ -55,6 +56,7 @@
           interfaces="org.apache.excalibur.altrmi.test.callback.CallBackTest">
         <classpath>
           <pathelement location="${build.home}/classes"/>
  +        <pathelement location="${build.home}/testclasses"/>            
         </classpath>
       </altrmiproxies>    
       
  @@ -63,6 +65,7 @@
           interfaces="org.apache.excalibur.altrmi.test.callback.CallBackTestListener">
         <classpath>
           <pathelement location="${build.home}/classes"/>
  +        <pathelement location="${build.home}/testclasses"/>            
         </classpath>
       </altrmiproxies>     
       
  
  
  
  1.3       +2 -2      jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/DefaultProxyHelper.java
  
  Index: DefaultProxyHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/DefaultProxyHelper.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultProxyHelper.java	13 Apr 2002 08:03:01 -0000	1.2
  +++ DefaultProxyHelper.java	20 Apr 2002 13:24:27 -0000	1.3
  @@ -30,7 +30,7 @@
   
   //callback
   import org.apache.excalibur.altrmi.common.ExposedObjectProxy;
  -import org.apache.excalibur.altrmi.client.impl.socket.CallbackEnabledCustomSocketStreamInvocationHandler;
  +import org.apache.excalibur.altrmi.client.impl.callback.socket.CallbackEnabledCustomSocketStreamInvocationHandler;
   import java.util.StringTokenizer;
   import org.apache.excalibur.altrmi.common.AltrmiCallbackException;
   
  @@ -40,7 +40,7 @@
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
    * @author Vinay Chandrasekharan <a href="mailto:vinay_chandran@users.sourceforge.net">vinay_chandran@users.sourceforge.net</a>
  - * @version $Revision: 1.2 $
  + * @version $Revision: 1.3 $
    */
   public final class DefaultProxyHelper implements ProxyHelper {
   
  
  
  
  1.4       +1 -0      jakarta-avalon-excalibur/altrmi/src/test/org/apache/excalibur/altrmi/test/callback/CallBackTestClient.java
  
  Index: CallBackTestClient.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/altrmi/src/test/org/apache/excalibur/altrmi/test/callback/CallBackTestClient.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CallBackTestClient.java	10 Apr 2002 22:33:11 -0000	1.3
  +++ CallBackTestClient.java	20 Apr 2002 13:24:27 -0000	1.4
  @@ -15,6 +15,7 @@
   import org.apache.excalibur.altrmi.common.AltrmiConnectionException;
   import org.apache.excalibur.altrmi.client.impl.ServerClassAltrmiFactory;
   import org.apache.excalibur.altrmi.client.impl.ClientClassAltrmiFactory;
  +import org.apache.excalibur.altrmi.client.impl.callback.socket.CallbackEnabledCustomSocketStreamHostContext;
   import org.apache.excalibur.altrmi.test.*;
   import org.apache.excalibur.altrmi.client.impl.socket.*;
   
  
  
  
  1.4       +1 -0      jakarta-avalon-excalibur/altrmi/src/test/org/apache/excalibur/altrmi/test/callback/CallBackTestServer.java
  
  Index: CallBackTestServer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/altrmi/src/test/org/apache/excalibur/altrmi/test/callback/CallBackTestServer.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CallBackTestServer.java	10 Apr 2002 22:33:11 -0000	1.3
  +++ CallBackTestServer.java	20 Apr 2002 13:24:27 -0000	1.4
  @@ -12,6 +12,7 @@
   
   import org.apache.excalibur.altrmi.server.impl.classretrievers.JarFileClassRetriever;
   import org.apache.excalibur.altrmi.server.impl.AbstractServer;
  +import org.apache.excalibur.altrmi.server.impl.callback.socket.CallbackEnabledCustomSocketStreamServer;
   import org.apache.excalibur.altrmi.test.*;
   import org.apache.excalibur.altrmi.server.impl.socket.*;
   import org.apache.excalibur.altrmi.server.PublicationDescription;
  
  
  
  1.1                  jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/callback/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.callback.socket;
  
  
  
  import org.apache.excalibur.altrmi.client.impl.AbstractHostContext;
  import org.apache.excalibur.altrmi.client.impl.socket.SocketCustomStreamHostContext;
  import org.apache.excalibur.altrmi.common.AltrmiConnectionException;
  import org.apache.excalibur.altrmi.common.AltrmiCallbackException;
  
  import java.net.Socket;
  /**
   * Class CallbackEnabledCustomSocketStreamHostContext
   * @author <a href="mailto:vinayc77@yahoo.com">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/callback/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.callback.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.AltrmiConnectionException;
  import org.apache.excalibur.altrmi.common.AltrmiCallbackException;
  import org.apache.excalibur.altrmi.client.impl.callback.stream.CallbackEnabledClientCustomStreamReadWriter;
  import org.apache.excalibur.altrmi.client.impl.socket.AbstractSocketStreamInvocationHandler;
  
  
  /**
   * Class CallbackEnabledCustomSocketStreamInvocationHandler
   *
   * @author <a href="mailto:vinayc77@yahoo.com">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/callback/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.callback.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;
  import org.apache.excalibur.altrmi.client.impl.stream.ClientStreamReadWriter;
  
  /**
   * 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:vinayc77@yahoo.com">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.getClass().getName().equals("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/server/impl/callback/CallbackHostContext.java
  
  Index: CallbackHostContext.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.callback;
  
  
  
  import org.apache.excalibur.altrmi.client.impl.AbstractHostContext;
  import org.apache.excalibur.altrmi.client.impl.stream.StreamInvocationHandler;
  
  
  /**
    * Class CallbackHostContext wraps a StreamInvocationHandler
    *
    * @author <a href="mailto:vinayc77@yahoo.com">Vinay Chandran</a>
    * @version $Revision: 1.1 $
    */
  public class CallbackHostContext extends AbstractHostContext
  {
  
      /**
       * Method CallbackHostContext.
       * @param streamInvocationHandler
       */
      public CallbackHostContext(StreamInvocationHandler streamInvocationHandler)
      {
          super(streamInvocationHandler);
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/callback/CallbackServerClassAltrmiFactory.java
  
  Index: CallbackServerClassAltrmiFactory.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.callback;
  
  
  
  import org.apache.excalibur.altrmi.client.impl.ServerClassAltrmiFactory;
  import org.apache.excalibur.altrmi.client.impl.AbstractHostContext;
  import org.apache.excalibur.altrmi.client.impl.DefaultProxyHelper;
  import org.apache.excalibur.altrmi.client.AltrmiHostContext;
  import org.apache.excalibur.altrmi.common.AltrmiAuthentication;
  import org.apache.excalibur.altrmi.common.AltrmiConnectionException;
  import org.apache.excalibur.altrmi.common.AltrmiReply;
  import org.apache.excalibur.altrmi.common.LookupRequest;
  import org.apache.excalibur.altrmi.common.NotPublishedReply;
  import org.apache.excalibur.altrmi.common.ExceptionReply;
  import org.apache.excalibur.altrmi.common.LookupReply;
  
  
  /**
   * 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:vinayc77@yahoo.com">Vinay Chandran</a>
   * @version $Revision: 1.1 $
   */
  public class CallbackServerClassAltrmiFactory extends ServerClassAltrmiFactory
  {
  
      /**
       * Constructor CallbackServerClassAltrmiFactory
       *
       *
       * @param b
       *
       */
      public CallbackServerClassAltrmiFactory(boolean b)
      {
          super(b);
      }
  
      //hack method to influence OpenConnectionRequest/reply sequence
  
      /**
       * Method setInitializedHostContext
       *
       *
       * @param hostContext
       *
       */
      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;
          DefaultProxyHelper baseObj = new DefaultProxyHelper(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/callback/socket/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.callback.socket;
  
  
  
  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.DefaultProxyHelper;
  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 org.apache.excalibur.altrmi.server.impl.ServerStreamReadWriter;
  import org.apache.excalibur.altrmi.server.impl.callback.stream.CallbackStreamInvocationHandler;
  import org.apache.excalibur.altrmi.server.impl.callback.CallbackHostContext;
  import org.apache.excalibur.altrmi.server.impl.callback.CallbackServerClassAltrmiFactory;
  
  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:vinayc77@yahoo.com">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();
  				}
  			}
  		}
  	}
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/callback/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.callback.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.socket.AbstractCompleteSocketStreamServer;
  import org.apache.excalibur.altrmi.server.impl.adapters.InvocationHandlerAdapter;
  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
   *
   *@author <a href="mailto:vinayc77@yahoo.com">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();
      }
  
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/callback/socket/CallbackServerClientReadWriter.java
  
  Index: CallbackServerClientReadWriter.java
  ===================================================================
  
  /*
   * Created by IntelliJ IDEA.
   * User: Administrator
   * Date: 20-Apr-02
   * Time: 14:04:24
   * To change template for new class use
   * Code Style | Class Templates options (Tools | IDE Options).
   */
  package org.apache.excalibur.altrmi.server.impl.callback.socket;
  
  
  
  import org.apache.excalibur.altrmi.client.impl.stream.ClientStreamReadWriter;
  import org.apache.excalibur.altrmi.common.AltrmiReply;
  import org.apache.excalibur.altrmi.common.AltrmiRequest;
  
  import java.io.IOException;
  
  
  /**
   * Class CallbackServerClientReadWriter
   *      forwards the  postRequest made on it to the
   *  CallbackEnabledSocketStreamReadWriter
   *
   * @author <a href="mailto:vinayc77@yahoo.com">Vinay Chandran</a>
   * @version $Revision: 1.1 $
   */
  public class CallbackServerClientReadWriter extends ClientStreamReadWriter
  {
  
      private CallbackEnabledCustomSocketStreamReadWriter mCallbackEnabledCustomSocketStreamReadWriter;
  
      /**
       * Constructor CallbackServerClientReadWriter
       *
       *
       * @param callbackEnabledCustomSocketStreamReadWriter
       *
       */
      CallbackServerClientReadWriter(
              CallbackEnabledCustomSocketStreamReadWriter callbackEnabledCustomSocketStreamReadWriter)
      {
          this.mCallbackEnabledCustomSocketStreamReadWriter =
              callbackEnabledCustomSocketStreamReadWriter;
      }
  
      protected AltrmiReply postRequest(AltrmiRequest altrmiRequest)
              throws IOException, ClassNotFoundException
      {
  
          AltrmiReply altrmiReply =
              mCallbackEnabledCustomSocketStreamReadWriter.postRequest(altrmiRequest);
  
          return altrmiReply;
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/callback/socket/PartialCallbackEnabledCustomSocketStreamServer.java
  
  Index: PartialCallbackEnabledCustomSocketStreamServer.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.callback.socket;
  
  
  
  import org.apache.excalibur.altrmi.server.impl.ServerStreamReadWriter;
  import org.apache.excalibur.altrmi.server.impl.socket.AbstractPartialSocketStreamServer;
  import org.apache.excalibur.altrmi.server.impl.adapters.InvocationHandlerAdapter;
  
  /**
   * Class CallbackEnabledCustomSocketStreamServer
   *
   * @author <a href="mailto:vinayc77@yahoo.com">Vinay Chandran</a>
   * @author Peter Royal
   * @version $Revision: 1.1 $
   */
  public class PartialCallbackEnabledCustomSocketStreamServer extends AbstractPartialSocketStreamServer {
      public PartialCallbackEnabledCustomSocketStreamServer()
      {
      }
  
      public PartialCallbackEnabledCustomSocketStreamServer(InvocationHandlerAdapter inovcationHandlerAdapter)
      {
          super(inovcationHandlerAdapter);
      }
  
      /*
  	 * @see AbstractPartialSocketStreamServer#createServerStreamReadWriter()
  	 */
      protected ServerStreamReadWriter createServerStreamReadWriter() {
          return new CallbackEnabledCustomSocketStreamReadWriter();
      }
  
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/callback/stream/CallbackStreamInvocationHandler.java
  
  Index: CallbackStreamInvocationHandler.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.callback.stream;
  
  
  
  import org.apache.excalibur.altrmi.client.impl.stream.StreamInvocationHandler;
  import org.apache.excalibur.altrmi.client.impl.stream.ClientStreamReadWriter;
  
  
  /**
   * Class CallbackStreamInvocationHandler
   *      provides public access to the setObjectReadWriter function
   *
   * @author <a href="mailto:vinayc77@yahoo.com">Vinay Chandran</a>
   * @version $Revision: 1.1 $
   */
  public class CallbackStreamInvocationHandler extends StreamInvocationHandler
  {
  
      /*
       * @see StreamInvocationHandler#StreamInvocationHandler(ClassLoader)
       */
  
      /**
       * Constructor CallbackStreamInvocationHandler
       *
       *
       * @param cl
       *
       */
      public CallbackStreamInvocationHandler(ClassLoader cl)
      {
          super(cl);
      }
  
      /*
       * @see AbstractClientInvocationHandler#tryReconnect()
       */
  
      /**
       * Method tryReconnect
       *
       *
       * @return
       *
       */
      public boolean tryReconnect()
      {
          return true;
      }
  
      /*
       * @see StreamInvocationHandler#setObjectReadWriter(ClientStreamReadWriter)
       */
  
      /**
       * Method setObjectReadWriter
       *
       *
       * @param clientStreamReadWriter
       *
       */
      public void setObjectReadWriter(ClientStreamReadWriter clientStreamReadWriter)
      {
          super.setObjectReadWriter(clientStreamReadWriter);
      }
  }
  
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>