hammant 2003/02/03 17:25:14
Modified: altrmi/src/java/org/apache/excalibur/altrmi/client/impl
DumbConnectionListener.java
altrmi/src/java/org/apache/excalibur/altrmi/client/impl/callback/stream
CallbackEnabledClientCustomStreamReadWriter.java
altrmi/src/java/org/apache/excalibur/altrmi/client/impl/direct
DirectMarshalledInvocationHandler.java
altrmi/src/java/org/apache/excalibur/altrmi/common
ClassLoaderObjectInputStream.java
SerializationHelper.java
altrmi/src/java/org/apache/excalibur/altrmi/server/impl/adapters
MarshalledInvocationHandlerAdapter.java
altrmi/src/java/org/apache/excalibur/altrmi/server/impl/callback/socket
CallbackEnabledSocketCustomStreamReadWriter.java
Added: altrmi/src/java/org/apache/excalibur/altrmi/client/impl/callback/stream
ReqRepBytes.java
Log:
Rework of callback to have better exception reporting
Revision Changes Path
1.6 +2 -1
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/DumbConnectionListener.java
Index: DumbConnectionListener.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/DumbConnectionListener.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- DumbConnectionListener.java 19 Jan 2003 11:09:59 -0000 1.5
+++ DumbConnectionListener.java 4 Feb 2003 01:25:13 -0000 1.6
@@ -70,6 +70,7 @@
public void serviceSuspended( final Request altrmiRequest, final int attempt,
final int suggestedWaitMillis )
{
+ throw new InvocationException( "Service suspended" );
}
/**
1.14 +108 -78
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/callback/stream/CallbackEnabledClientCustomStreamReadWriter.java
Index: CallbackEnabledClientCustomStreamReadWriter.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/callback/stream/CallbackEnabledClientCustomStreamReadWriter.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- CallbackEnabledClientCustomStreamReadWriter.java 2 Feb 2003 14:10:05 -0000
1.13
+++ CallbackEnabledClientCustomStreamReadWriter.java 4 Feb 2003 01:25:13 -0000
1.14
@@ -15,6 +15,7 @@
import java.io.OutputStream;
import java.io.EOFException;
import java.util.HashMap;
+
import org.apache.excalibur.altrmi.client.impl.ClientStreamReadWriter;
import org.apache.excalibur.altrmi.common.CallbackException;
import org.apache.excalibur.altrmi.common.Reply;
@@ -23,6 +24,8 @@
import org.apache.excalibur.altrmi.common.ConnectionException;
import org.apache.excalibur.altrmi.common.ThreadContextProvider;
import org.apache.excalibur.altrmi.common.ThreadContext;
+import org.apache.excalibur.altrmi.common.ExceptionReply;
+import org.apache.excalibur.altrmi.common.InvocationException;
import org.apache.excalibur.altrmi.server.Server;
import org.apache.excalibur.altrmi.server.PublicationException;
import org.apache.excalibur.altrmi.server.ServerException;
@@ -36,10 +39,11 @@
* callbacks.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vinay Chandran</a>
+ * @author Paul Hammant
* @version $Revision$
*/
public class CallbackEnabledClientCustomStreamReadWriter
- implements ClientStreamReadWriter, Runnable
+ implements ClientStreamReadWriter, Runnable
{
//Mutex to block the client till a reply reaches back from server
private Object m_replyLock = new Object();
@@ -64,12 +68,12 @@
* @throws ConnectionException The IO Exception
*
*/
- public CallbackEnabledClientCustomStreamReadWriter( ThreadContextProvider
threadContextProvider, InputStream inputStream, OutputStream outputStream, ClassLoader
interfacesClassLoader )
- throws ConnectionException
+ public CallbackEnabledClientCustomStreamReadWriter(ThreadContextProvider
threadContextProvider, InputStream inputStream, OutputStream outputStream, ClassLoader
interfacesClassLoader)
+ throws ConnectionException
{
this.m_threadContextProvider = threadContextProvider;
- m_dataOutputStream = new DataOutputStream( new BufferedOutputStream(
outputStream ) );
- m_dataInputStream = new DataInputStream( inputStream );
+ m_dataOutputStream = new DataOutputStream(new
BufferedOutputStream(outputStream));
+ m_dataInputStream = new DataInputStream(inputStream);
m_interfacesClassLoader = interfacesClassLoader;
//This thread recv's all the messages sent from the server
@@ -80,11 +84,11 @@
try
{
m_clientServerHostingExposedObjects.start();
- m_clientServerHostingExposedObjects.setClassRetriever( new
PlainClassRetriever() );
+ m_clientServerHostingExposedObjects.setClassRetriever(new
PlainClassRetriever());
}
- catch( ServerException e )
+ catch (ServerException e)
{
- throw new ConnectionException( "Error Starting Internal Server" );
+ throw new ConnectionException("Error Starting Internal Server");
}
}
@@ -93,63 +97,90 @@
*/
public void run()
{
- boolean readingInt = false;
- while( !m_isStopped )
+ while (!m_isStopped)
{
- try
- {
-
- readingInt = true;
- int byteArraySize = m_dataInputStream.readInt();
- readingInt = false;
- byte[] byteArray = new byte[ byteArraySize ];
-
- m_dataInputStream.read( byteArray );
+ ReqRepBytes reqRepBytes = readByteArrayFromInputStream();
-
//_replyQueue.addFirst(SerializationHelper.getInstanceFromBytes(byteArray,
m_interfacesClassLoader));
- Object obj = SerializationHelper.getInstanceFromBytes( byteArray,
m_interfacesClassLoader );
-
- if( obj instanceof Reply )
+ if (reqRepBytes.getIOException() != null)
+ {
+ IOException ioe = reqRepBytes.getIOException();
+ if (ioe instanceof EOFException)
{
- m_reply = (Reply)obj;
- synchronized( m_replyLock )
+ if (reqRepBytes.ioeDuringReadInt())
{
- m_replyLock.notify();
+ m_isStopped = true;
+ // restart ?
+ return;
}
}
- else if( obj instanceof Request )
+ System.out.println("--> ##1 " + reqRepBytes.getIOException());
+ }
+
+ // Is a reply.
+ if (!(reqRepBytes.isRequest().booleanValue()))
+ {
+ try
+ {
+ m_reply = (Reply)
SerializationHelper.getInstanceFromBytes(reqRepBytes.getBytes(),
m_interfacesClassLoader);
+ ;
+ }
+ catch (ClassNotFoundException e)
{
- Reply altrmiReply =
m_clientServerHostingExposedObjects.handleInvocation( (Request)obj, "callback" );
- postReply( altrmiReply );
+ m_reply = new ExceptionReply(new
InvocationException("ClassNotFoundException", e));
}
- else
+ synchronized (m_replyLock)
{
- //NEVER OCCURS, or a hack?
+ m_replyLock.notify();
}
-
}
- catch( IOException e )
+ // Is a request.
+ else if (reqRepBytes.isRequest().booleanValue())
{
- if (e instanceof EOFException && readingInt)
+ try
{
- // do nothing, connection closed
+ Object obj = null;
+ Reply reply = null;
+ try
+ {
+ obj =
SerializationHelper.getInstanceFromBytes(reqRepBytes.getBytes(),
m_interfacesClassLoader);
+ reply =
m_clientServerHostingExposedObjects.handleInvocation((Request) obj, "callback");
+ }
+ catch (ClassNotFoundException e)
+ {
+ reply = new ExceptionReply(new
InvocationException("ClassNotFoundException", e));
+ }
+ postReply(reply);
}
- else
+ catch (IOException ioe)
{
- // :-?
- if( e.getClass().getName().equals(
"java.net.SocketTimeoutException" ) )
- continue;
- e.printStackTrace();
+ if (ioe instanceof EOFException)
+ {
+ m_isStopped = true;
+ return;
+ }
}
- m_isStopped = true;
- return;
- }
- catch( ClassNotFoundException e )
- {
- e.printStackTrace();
}
+ }
+ }
+ private ReqRepBytes readByteArrayFromInputStream()
+ {
+ int byteArraySize = 0;
+ Boolean isRequest = null;
+ byte[] byteArray = null;
+ IOException ioe = null;
+ try
+ {
+ byteArraySize = m_dataInputStream.readInt();
+ isRequest = Boolean.valueOf(m_dataInputStream.readBoolean());
+ byteArray = new byte[byteArraySize];
+ m_dataInputStream.read(byteArray);
}
+ catch (IOException e)
+ {
+ ioe = e;
+ }
+ return new ReqRepBytes(byteArraySize, byteArray, isRequest, ioe);
}
/**
@@ -159,15 +190,15 @@
*/
public Reply getReplyFromMessageLoop()
{
- if( m_reply == null )
+ if (m_reply == null)
{
- synchronized( m_replyLock )
+ synchronized (m_replyLock)
{
try
{
m_replyLock.wait();
}
- catch( InterruptedException e )
+ catch (InterruptedException e)
{
e.printStackTrace();
}
@@ -179,12 +210,12 @@
/*
* @see ClientStreamReadWriter#postRequest(Request)
*/
- public Reply postRequest( Request altrmiRequest )
- throws IOException, ClassNotFoundException
+ public Reply postRequest(Request altrmiRequest)
+ throws IOException, ClassNotFoundException
{
- if( m_isStopped )
- throw new IOException( "Server Stoppped" );
- writeRequest( altrmiRequest );
+ if (m_isStopped)
+ throw new IOException("Server Stoppped");
+ writeRequest(altrmiRequest);
Reply r = readReply();
m_reply = null;
return r;
@@ -195,23 +226,22 @@
* @param altrmiRequest
* @throws IOException
*/
- private void writeRequest( Request altrmiRequest ) throws IOException
+ private void writeRequest(Request altrmiRequest) throws IOException
{
- byte[] aBytes = SerializationHelper.getBytesFromInstance( altrmiRequest );
+ byte[] aBytes = SerializationHelper.getBytesFromInstance(altrmiRequest);
- m_dataOutputStream.writeInt( aBytes.length );
- m_dataOutputStream.write( aBytes );
+ m_dataOutputStream.writeInt(aBytes.length);
+ m_dataOutputStream.writeBoolean(true);
+ m_dataOutputStream.write(aBytes);
m_dataOutputStream.flush();
}
/**
* Method readReply.
* @return Reply
- * @throws IOException
- * @throws ClassNotFoundException
*/
- private Reply readReply() throws IOException, ClassNotFoundException
+ private Reply readReply()
{
return getReplyFromMessageLoop();
@@ -221,14 +251,14 @@
* Method postReply.
* @param altrmiReply
* @throws IOException
- * @throws ClassNotFoundException
*/
- public void postReply( Reply altrmiReply ) throws IOException,
ClassNotFoundException
+ public void postReply(Reply altrmiReply) throws IOException
{
- byte[] aBytes = SerializationHelper.getBytesFromInstance( altrmiReply );
+ byte[] aBytes = SerializationHelper.getBytesFromInstance(altrmiReply);
- m_dataOutputStream.writeInt( aBytes.length );
- m_dataOutputStream.write( aBytes );
+ m_dataOutputStream.writeInt(aBytes.length);
+ m_dataOutputStream.writeBoolean(false);
+ m_dataOutputStream.write(aBytes);
m_dataOutputStream.flush();
}
@@ -241,19 +271,19 @@
* @return boolean
* @throws CallbackException
*/
- public boolean exposeObject( Object tobeExposedObject, Class
tobeExposedInterface ) throws CallbackException
+ public boolean exposeObject(Object tobeExposedObject, Class
tobeExposedInterface) throws CallbackException
{
- if( m_exposedObjPublishNameHash_.get( tobeExposedObject ) != null )
+ if (m_exposedObjPublishNameHash_.get(tobeExposedObject) != null)
return false;
- String _uniquePublishedName = getUniqueNameForExposedObject(
tobeExposedObject );
- m_exposedObjPublishNameHash_.put( tobeExposedObject, _uniquePublishedName );
+ String _uniquePublishedName =
getUniqueNameForExposedObject(tobeExposedObject);
+ m_exposedObjPublishNameHash_.put(tobeExposedObject, _uniquePublishedName);
try
{
- m_clientServerHostingExposedObjects.publish( tobeExposedObject,
_uniquePublishedName, tobeExposedInterface );
+ m_clientServerHostingExposedObjects.publish(tobeExposedObject,
_uniquePublishedName, tobeExposedInterface);
}
- catch( PublicationException pce )
+ catch (PublicationException pce)
{
- throw new CallbackException( pce.getMessage() );
+ throw new CallbackException(pce.getMessage());
}
return true;
}
@@ -265,10 +295,10 @@
* @param tobeExposedObject
* @return String
*/
- private String getUniqueNameForExposedObject( Object tobeExposedObject )
+ private String getUniqueNameForExposedObject(Object tobeExposedObject)
{
m_random++;
- return tobeExposedObject.getClass().getName().substring(
tobeExposedObject.getClass().getName().lastIndexOf( "." ) + 1 ) + "_" + m_random;
+ return
tobeExposedObject.getClass().getName().substring(tobeExposedObject.getClass().getName().lastIndexOf(".")
+ 1) + "_" + m_random;
}
/**
@@ -278,9 +308,9 @@
* @param tobeExposedObject
* @return String
*/
- public String getPublishedName( Object tobeExposedObject )
+ public String getPublishedName(Object tobeExposedObject)
{
- return (String)m_exposedObjPublishNameHash_.get( tobeExposedObject );
+ return (String) m_exposedObjPublishNameHash_.get(tobeExposedObject);
}
}
1.1
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/callback/stream/ReqRepBytes.java
Index: ReqRepBytes.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.IOException;
/**
* ReqRepBytes
*
* @author Paul Hammant
* @version $Revision: 1.1 $
*/
public class ReqRepBytes {
int m_byteSize;
byte[] m_bytes;
Boolean m_isRequest;
IOException m_ioe;
public ReqRepBytes(int byteSize, byte[] bytes, Boolean isRequest, IOException
ioe)
{
m_byteSize = byteSize;
m_bytes = bytes;
m_isRequest = isRequest;
m_ioe = ioe;
}
public boolean ioeDuringReadInt()
{
return (m_ioe != null & m_byteSize == 0);
}
public int getByteSize()
{
return m_byteSize;
}
public byte[] getBytes()
{
return m_bytes;
}
// request or reply
public Boolean isRequest()
{
return m_isRequest;
}
public IOException getIOException()
{
return m_ioe;
}
}
1.9 +1 -7
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/direct/DirectMarshalledInvocationHandler.java
Index: DirectMarshalledInvocationHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/client/impl/direct/DirectMarshalledInvocationHandler.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- DirectMarshalledInvocationHandler.java 2 Feb 2003 14:10:05 -0000 1.8
+++ DirectMarshalledInvocationHandler.java 4 Feb 2003 01:25:13 -0000 1.9
@@ -74,12 +74,6 @@
return (Reply)SerializationHelper.getInstanceFromBytes( serReply,
m_interfacesClassLoader );
}
- catch( IOException e )
- {
- e.printStackTrace();
-
- return new RequestFailedReply( "Some IO Exception on server side : " +
e.getMessage() );
- }
catch( ClassNotFoundException e )
{
e.printStackTrace();
1.7 +4 -3
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/common/ClassLoaderObjectInputStream.java
Index: ClassLoaderObjectInputStream.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/common/ClassLoaderObjectInputStream.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ClassLoaderObjectInputStream.java 4 Jan 2003 21:31:46 -0000 1.6
+++ ClassLoaderObjectInputStream.java 4 Feb 2003 01:25:13 -0000 1.7
@@ -12,6 +12,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.StreamCorruptedException;
+import java.io.ByteArrayInputStream;
/**
* A special ObjectInputStream to handle highly transient classes hosted
@@ -29,18 +30,18 @@
*
*
* @param classLoader the classloader that containes the classes that may be
deserialized
- * @param inputStream the input stream from which deserialization will occur
+ * @param byteArray tye bytes for the thing to be deserialized.
*
* @throws IOException from super
* @throws StreamCorruptedException from super
*
*/
public ClassLoaderObjectInputStream(
- final ClassLoader classLoader, final InputStream inputStream )
+ final ClassLoader classLoader, byte[] byteArray )
throws IOException, StreamCorruptedException
{
- super( inputStream );
+ super( new ByteArrayInputStream( byteArray ) );
m_classLoader = classLoader;
}
1.4 +32 -25
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/common/SerializationHelper.java
Index: SerializationHelper.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/common/SerializationHelper.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SerializationHelper.java 23 May 2002 21:37:19 -0000 1.3
+++ SerializationHelper.java 4 Feb 2003 01:25:13 -0000 1.4
@@ -7,7 +7,6 @@
*/
package org.apache.excalibur.altrmi.common;
-import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
@@ -31,24 +30,31 @@
*
* @return the byte array
*
- * @throws IOException if IO problem
*
*/
- public static byte[] getBytesFromInstance( Object instance ) throws IOException
+ public static byte[] getBytesFromInstance( Object instance )
{
ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
ObjectOutputStream oOs;
- oOs = new ObjectOutputStream( bAOS );
-
- oOs.writeObject( instance );
- oOs.flush();
-
- byte[] aBytes = bAOS.toByteArray();
-
- oOs.close();
- bAOS.close();
+ byte[] aBytes = new byte[0];
+ try
+ {
+ oOs = new ObjectOutputStream( bAOS );
+
+ oOs.writeObject( instance );
+ oOs.flush();
+
+ aBytes = bAOS.toByteArray();
+
+ oOs.close();
+ bAOS.close();
+ }
+ catch (IOException e)
+ {
+ throw new AltrmiRuntimeException("Really out of the ordinary
IOException",e);
+ }
return aBytes;
}
@@ -62,11 +68,10 @@
* @return the instance
*
* @throws ClassNotFoundException if the class-def can't be resolved.
- * @throws IOException if IO problem
*
*/
public static Object getInstanceFromBytes( byte[] byteArray )
- throws IOException, ClassNotFoundException
+ throws ClassNotFoundException
{
return getInstanceFromBytes( byteArray,
SerializationHelper.class.getClassLoader() );
}
@@ -81,20 +86,22 @@
* @return the instance
*
* @throws ClassNotFoundException if the class-def can't be resolved.
- * @throws IOException if IO problem
*
*/
public static Object getInstanceFromBytes( byte[] byteArray, ClassLoader
classLoader )
- throws IOException, ClassNotFoundException
+ throws ClassNotFoundException
{
- ByteArrayInputStream bAIS = new ByteArrayInputStream( byteArray );
- ObjectInputStream oIs = //new ObjectInputStream(bAIS);
- new ClassLoaderObjectInputStream( classLoader, bAIS );
- Object obj = oIs.readObject();
-
- oIs.close();
-
- return obj;
+ try
+ {
+ ObjectInputStream oIs = new ClassLoaderObjectInputStream( classLoader,
byteArray );
+ Object obj = oIs.readObject();
+ oIs.close();
+ return obj;
+ }
+ catch (IOException ioe)
+ {
+ return null;
+ }
}
}
1.7 +1 -7
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/adapters/MarshalledInvocationHandlerAdapter.java
Index: MarshalledInvocationHandlerAdapter.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/adapters/MarshalledInvocationHandlerAdapter.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- MarshalledInvocationHandlerAdapter.java 2 Feb 2003 14:10:06 -0000 1.6
+++ MarshalledInvocationHandlerAdapter.java 4 Feb 2003 01:25:13 -0000 1.7
@@ -84,12 +84,6 @@
return SerializationHelper.getBytesFromInstance( reply );
}
- catch( IOException e )
- {
- e.printStackTrace();
-
- return null;
- }
catch( ClassNotFoundException e )
{
e.printStackTrace();
1.12 +5 -2
jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/callback/socket/CallbackEnabledSocketCustomStreamReadWriter.java
Index: CallbackEnabledSocketCustomStreamReadWriter.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/altrmi/src/java/org/apache/excalibur/altrmi/server/impl/callback/socket/CallbackEnabledSocketCustomStreamReadWriter.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- CallbackEnabledSocketCustomStreamReadWriter.java 1 Feb 2003 11:33:50 -0000
1.11
+++ CallbackEnabledSocketCustomStreamReadWriter.java 4 Feb 2003 01:25:13 -0000
1.12
@@ -91,7 +91,8 @@
{
//recv packets from the client
- int byteArraySize = (int)m_dataInputStream.readInt();
+ int byteArraySize = m_dataInputStream.readInt();
+ boolean isRequest = m_dataInputStream.readBoolean();
byte[] byteArray = new byte[ byteArraySize ];
m_dataInputStream.read( byteArray );
@@ -242,6 +243,7 @@
byte[] aBytes = SerializationHelper.getBytesFromInstance( altrmiReply );
m_dataOutputStream.writeInt( aBytes.length );
+ m_dataOutputStream.writeBoolean( false );
m_dataOutputStream.write( aBytes );
m_dataOutputStream.flush();
}
@@ -308,6 +310,7 @@
byte[] aBytes = SerializationHelper.getBytesFromInstance( altrmiRequest );
m_dataOutputStream.writeInt( aBytes.length );
+ m_dataOutputStream.writeBoolean( true );
m_dataOutputStream.write( aBytes );
m_dataOutputStream.flush();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]