com.sibvisions.rad.remote
Class AbstractSerializedConnection

java.lang.Object
  extended by com.sibvisions.rad.remote.AbstractSerializedConnection
All Implemented Interfaces:
IConnection
Direct Known Subclasses:
HttpConnection

public abstract class AbstractSerializedConnection
extends java.lang.Object
implements IConnection

This is the default IConnection implementation. It's independent of the communication protocol.


Field Summary
static java.lang.String PROP_SERIALIZER
          the property name for the serializer.
 
Fields inherited from interface javax.rad.remote.IConnection
COMPRESSION_BYTES, FLAG_ACKNOWLEDGE, FLAG_BROKEN, FLAG_ESTABLISHED, MET_SESSION_CREATE, MET_SESSION_DESTROY, MET_SESSION_GET_PROPERTIES, MET_SESSION_GET_PROPERTY, MET_SESSION_SET_NEW_PASSWORD, MET_SESSION_SET_PROPERTY, MET_SESSION_SETCHECKALIVE, MET_SESSION_SUBSESSION_CREATE, MODE_COMPRESSED, MODE_UNCOMPRESSED, NOPARAMETER, OBJ_SESSION, TYPE_CALL_ERROR, TYPE_CALL_RESULT, TYPE_CALLBACK_ERROR, TYPE_CALLBACK_RESULT, TYPE_CALLBACKRESULT_RESULT, TYPE_PROPERTY_RESULT
 
Constructor Summary
AbstractSerializedConnection(ISerializer pSerializer)
          Creates a new instance of AbstractSerializedConnection.
AbstractSerializedConnection(java.util.Properties pProperties)
          Creates a new instance of AbstractSerializedConnection with properties containing relevant information.
 
Method Summary
 void addCallBackResultListener(ICallBackResultListener pListener)
          Adds an ICallBackResultListener to the list of registered listeners.
 void addPropertyChangedListener(IConnectionPropertyChangedListener pListener)
          Adds an IConnectionPropertyChangedListener to the list of registered listeners.
 java.lang.Object[] call(ConnectionInfo pConnectionInfo, java.lang.String[] pObjectName, java.lang.String[] pMethod, java.lang.Object[][] pParams, ICallBackListener[] pCallBack)
          Calls desired methods from a remote server object.
 void close(ConnectionInfo pConnectionInfo)
          Close the connection to the server.
protected static ISerializer createSerializer(java.lang.String pClassName)
          Creates the serializer from a given class name.
protected  java.lang.Throwable fillInStackTrace(java.lang.Throwable pThrowable)
          Fills in the current call-stack in the given exception.
protected  void finalize()
          
abstract  java.io.InputStream getInputStream(ConnectionInfo pConnectionInfo)
          Gets the InputStream for reading the response from the server.
abstract  java.io.OutputStream getOutputStream(ConnectionInfo pConnectionInfo)
          Gets the OutputStream for submitting requests to the server.
 java.util.Hashtable<java.lang.String,java.lang.Object> getProperties(ConnectionInfo pConnectionInfo)
          Gets a clone of all connection properties.
 java.lang.Object getProperty(ConnectionInfo pConnectionInfo, java.lang.String pName)
          Gets a connection property.
 int getRetryCount()
          Gets the number of retries if a communication error occurs.
 int getRetryInterval()
          Gets the retry interval.
protected  ISerializer getSerializer()
          Gets the current serializer.
protected  java.lang.Object initCall(ConnectionInfo pConnectionInfo)
          Will be invoked before a call starts.
protected  boolean isAutoEOFEnabled()
          Gets whether automatic EOF should be detected for response stream.
 boolean isCalling()
          Gets whether a call is active.
 boolean isOpen(ConnectionInfo pConnectionInfo)
          Checks if the connection to the server is opened.
protected  boolean isReadingMagicByteEnabled()
          Whether this connection should read the magic byte sequence.
 boolean isRetryDuringAliveCheck()
          Gets whether alive check should retry if a communication error occurs.
 boolean isRetryDuringClose()
          Gets whether close should retry if a communication error occurs.
 boolean isRetryDuringOpen()
          Gets whether open should retry if a communication error occurs.
protected  boolean isWritingMagicByteEnabled()
          Whether this connection should write the magic byte sequence.
 void open(ConnectionInfo pConnectionInfo)
          Opens the connection to the server.
 void openSub(ConnectionInfo pConnectionInfo, ConnectionInfo pConnectionInfoSub)
          Opens a new sub connection.
protected  java.lang.Throwable prepareAfterRetryException(java.lang.Throwable pThrowable)
          Prepares the final exception after last retry.
protected  java.lang.Throwable prepareException(java.lang.Throwable pThrowable)
          Prepares the given exception if needed.
protected  void releaseCall(ConnectionInfo pConnectionInfo, java.lang.Object pInit)
          Will be invoked after a call was executed, if initCall(ConnectionInfo) was successfully invoked.
 void removeCallBackResultListener(ICallBackResultListener pListener)
          Removes an ICallBackResultListener from the list of registered listeners.
 void removePropertyChangedListener(IConnectionPropertyChangedListener pListener)
          Removes an IConnectionPropertyChangedListener from the list of registered listeners.
 ConnectionInfo[] setAndCheckAlive(ConnectionInfo pConnectionInfo, ConnectionInfo[] pSubConnections)
          Sets the alive state for a connection, on the server, and validates the alive state of subconnections.
 void setNewPassword(ConnectionInfo pConnectionInfo, java.lang.String pOldPassword, java.lang.String pNewPassword)
          Sets a new password for the connected user.
 void setProperty(ConnectionInfo pConnectionInfo, java.lang.String pName, java.lang.Object pValue)
          Sets a connection property.
 void setRetryCount(int pRetryCount)
          The number of retries before the connection will be marked as broken.
 void setRetryDuringAliveCheck(boolean pRetryAlive)
          Sets whether alive check should retry if a communication error occurs.
 void setRetryDuringClose(boolean pRetryClose)
          Sets whether close should retry if a communication error occurs.
 void setRetryDuringOpen(boolean pRetryOpen)
          Sets whether open should retry if a communication error occurs.
 void setRetryInterval(int pInterval)
          Sets the retry interval in milliseconds.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROP_SERIALIZER

public static final java.lang.String PROP_SERIALIZER
the property name for the serializer.

See Also:
Constant Field Values
Constructor Detail

AbstractSerializedConnection

public AbstractSerializedConnection(java.util.Properties pProperties)
                             throws java.lang.ClassNotFoundException
Creates a new instance of AbstractSerializedConnection with properties containing relevant information. The supported property keys are:

Parameters:
pProperties - the properties for the connection
Throws:
java.lang.ClassNotFoundException - if the serializer is defined and could not be created

AbstractSerializedConnection

public AbstractSerializedConnection(ISerializer pSerializer)
Creates a new instance of AbstractSerializedConnection.

Parameters:
pSerializer - the serializer for the communication between client and server or null to use the default serializer
See Also:
ISerializer, ByteSerializer
Method Detail

open

public void open(ConnectionInfo pConnectionInfo)
          throws java.lang.Throwable
Opens the connection to the server. The credentials have to be a key/value mapping in the connection information.

Specified by:
open in interface IConnection
Parameters:
pConnectionInfo - the connection information
Throws:
java.lang.Throwable - if the connection can not be established

openSub

public void openSub(ConnectionInfo pConnectionInfo,
                    ConnectionInfo pConnectionInfoSub)
             throws java.lang.Throwable
Opens a new sub connection. The name of the sub connection has to be a key/value mapping in the sub connection information.

Specified by:
openSub in interface IConnection
Parameters:
pConnectionInfo - the connection information of the master connection
pConnectionInfoSub - the connection information of the sub connection
Throws:
java.lang.Throwable - communication error, security checks, invalid method, ...

isOpen

public boolean isOpen(ConnectionInfo pConnectionInfo)
Checks if the connection to the server is opened.

Specified by:
isOpen in interface IConnection
Parameters:
pConnectionInfo - the connection information
Returns:
true if the connection is open

close

public void close(ConnectionInfo pConnectionInfo)
           throws java.lang.Throwable
Close the connection to the server.

Specified by:
close in interface IConnection
Parameters:
pConnectionInfo - the connection information
Throws:
java.lang.Throwable - if the connection can not be established

call

public java.lang.Object[] call(ConnectionInfo pConnectionInfo,
                               java.lang.String[] pObjectName,
                               java.lang.String[] pMethod,
                               java.lang.Object[][] pParams,
                               ICallBackListener[] pCallBack)
                        throws java.lang.Throwable
Calls desired methods from a remote server object.

Specified by:
call in interface IConnection
Parameters:
pConnectionInfo - the connection information
pObjectName - list of server object names/aliases
pMethod - method names which should be called
pParams - parameters for the method calls
pCallBack - callback listeners for asynchronous or null for synchronous calls
Returns:
result list from the remote method calls
Throws:
java.lang.Throwable - communication error, security checks, invalid method, ...

setAndCheckAlive

public ConnectionInfo[] setAndCheckAlive(ConnectionInfo pConnectionInfo,
                                         ConnectionInfo[] pSubConnections)
                                  throws java.lang.Throwable
Sets the alive state for a connection, on the server, and validates the alive state of subconnections.

Specified by:
setAndCheckAlive in interface IConnection
Parameters:
pConnectionInfo - the connection information
pSubConnections - the connection information of the sub connections, for the alive validation
Returns:
the invalid/expired sub connections
Throws:
java.lang.Throwable - communication error, security checks, invalid method, ...

setProperty

public void setProperty(ConnectionInfo pConnectionInfo,
                        java.lang.String pName,
                        java.lang.Object pValue)
                 throws java.lang.Throwable
Sets a connection property.

Specified by:
setProperty in interface IConnection
Parameters:
pConnectionInfo - the connection information
pName - the property name
pValue - the value of the property or null to delete the property
Throws:
java.lang.Throwable - communication error, security checks, invalid method, ...
java.lang.SecurityException - if it's not allowed to set the property

getProperty

public java.lang.Object getProperty(ConnectionInfo pConnectionInfo,
                                    java.lang.String pName)
                             throws java.lang.Throwable
Gets a connection property.

Specified by:
getProperty in interface IConnection
Parameters:
pConnectionInfo - the connection information
pName - the property name
Returns:
the value of the property or null if the property is not set
Throws:
java.lang.Throwable - communication error, security checks, invalid method, ...

getProperties

public java.util.Hashtable<java.lang.String,java.lang.Object> getProperties(ConnectionInfo pConnectionInfo)
                                                                     throws java.lang.Throwable
Gets a clone of all connection properties.

Specified by:
getProperties in interface IConnection
Parameters:
pConnectionInfo - the connection information
Returns:
a Hashtable with property names and values
Throws:
java.lang.Throwable - communication error, security checks, invalid method, ...

setNewPassword

public void setNewPassword(ConnectionInfo pConnectionInfo,
                           java.lang.String pOldPassword,
                           java.lang.String pNewPassword)
                    throws java.lang.Throwable
Sets a new password for the connected user.

Specified by:
setNewPassword in interface IConnection
Parameters:
pConnectionInfo - the connection information
pOldPassword - the old password
pNewPassword - the new password
Throws:
java.lang.Throwable - communication error, security checks, invalid method, ...

isCalling

public boolean isCalling()
Gets whether a call is active.

Specified by:
isCalling in interface IConnection
Returns:
true if a call is active, false otherwise

addPropertyChangedListener

public void addPropertyChangedListener(IConnectionPropertyChangedListener pListener)
Adds an IConnectionPropertyChangedListener to the list of registered listeners.

Specified by:
addPropertyChangedListener in interface IConnection
Parameters:
pListener - the new property changed listener.

removePropertyChangedListener

public void removePropertyChangedListener(IConnectionPropertyChangedListener pListener)
Removes an IConnectionPropertyChangedListener from the list of registered listeners.

Specified by:
removePropertyChangedListener in interface IConnection
Parameters:
pListener - the property changed listener to remove

addCallBackResultListener

public void addCallBackResultListener(ICallBackResultListener pListener)
Adds an ICallBackResultListener to the list of registered listeners.

Specified by:
addCallBackResultListener in interface IConnection
Parameters:
pListener - the new callback result listener.

removeCallBackResultListener

public void removeCallBackResultListener(ICallBackResultListener pListener)
Removes an ICallBackResultListener from the list of registered listeners.

Specified by:
removeCallBackResultListener in interface IConnection
Parameters:
pListener - the callback result listener to remove

getOutputStream

public abstract java.io.OutputStream getOutputStream(ConnectionInfo pConnectionInfo)
                                              throws java.lang.Throwable
Gets the OutputStream for submitting requests to the server.

Parameters:
pConnectionInfo - the connection information
Returns:
output stream
Throws:
java.lang.Throwable - if it's not possible to get the output stream

getInputStream

public abstract java.io.InputStream getInputStream(ConnectionInfo pConnectionInfo)
                                            throws java.lang.Throwable
Gets the InputStream for reading the response from the server.

Parameters:
pConnectionInfo - the connection information
Returns:
input stream
Throws:
java.lang.Throwable - if it's not possible to get the the input stream

finalize

protected void finalize()
                 throws java.lang.Throwable

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

createSerializer

protected static ISerializer createSerializer(java.lang.String pClassName)
                                       throws java.lang.ClassNotFoundException
Creates the serializer from a given class name.

Parameters:
pClassName - the ISerializer implementation class
Returns:
the serializer implementation
Throws:
java.lang.ClassNotFoundException - if the serializer could not be created

initCall

protected java.lang.Object initCall(ConnectionInfo pConnectionInfo)
Will be invoked before a call starts.

Parameters:
pConnectionInfo - the connection info
Returns:
a custom object that will be used as parameter for -releaseCall(ConnectionInfo, Object)

releaseCall

protected void releaseCall(ConnectionInfo pConnectionInfo,
                           java.lang.Object pInit)
Will be invoked after a call was executed, if initCall(ConnectionInfo) was successfully invoked.

Parameters:
pConnectionInfo - the connection info
pInit - the object whic was created in initCall(ConnectionInfo)

setRetryCount

public void setRetryCount(int pRetryCount)
The number of retries before the connection will be marked as broken.

Parameters:
pRetryCount - the number of retries

getRetryCount

public int getRetryCount()
Gets the number of retries if a communication error occurs.

Returns:
the number of retries
See Also:
setRetryCount(int)

setRetryDuringOpen

public void setRetryDuringOpen(boolean pRetryOpen)
Sets whether open should retry if a communication error occurs.

Parameters:
pRetryOpen - true to retry if open failed at first attempt

isRetryDuringOpen

public boolean isRetryDuringOpen()
Gets whether open should retry if a communication error occurs.

Returns:
true if open will retry if failed at first attempt, false to throw an Exception after first attempt (immediate)

setRetryDuringClose

public void setRetryDuringClose(boolean pRetryClose)
Sets whether close should retry if a communication error occurs.

Parameters:
pRetryClose - true to retry if close failed at first attempt

isRetryDuringClose

public boolean isRetryDuringClose()
Gets whether close should retry if a communication error occurs.

Returns:
true if close will retry if failed at first attempt, false to throw an Exception after first attempt (immediate)

setRetryDuringAliveCheck

public void setRetryDuringAliveCheck(boolean pRetryAlive)
Sets whether alive check should retry if a communication error occurs.

Parameters:
pRetryAlive - true to retry if alive check failed at first attempt

isRetryDuringAliveCheck

public boolean isRetryDuringAliveCheck()
Gets whether alive check should retry if a communication error occurs.

Returns:
true if alive check will retry if failed at first attempt, false to throw an Exception after first attempt (immediate)

setRetryInterval

public void setRetryInterval(int pInterval)
Sets the retry interval in milliseconds. The interval is the wait time between two requests.

Parameters:
pInterval - the interval

getRetryInterval

public int getRetryInterval()
Gets the retry interval.

Returns:
the interval in milliseconds.
See Also:
setRetryInterval(int)

getSerializer

protected ISerializer getSerializer()
Gets the current serializer.

Returns:
the serializer

isReadingMagicByteEnabled

protected boolean isReadingMagicByteEnabled()
Whether this connection should read the magic byte sequence.

Returns:
true

isWritingMagicByteEnabled

protected boolean isWritingMagicByteEnabled()
Whether this connection should write the magic byte sequence.

Returns:
true

isAutoEOFEnabled

protected boolean isAutoEOFEnabled()
Gets whether automatic EOF should be detected for response stream. This option needs isReadingMagicByteEnabled() to be enabled. The default implementation doesn't use this option (returns false).

Returns:
true if auto EOF detection is enabled, false otherwise.
See Also:
isReadingMagicByteEnabled(), MagicByteInputStream.MagicByteInputStream(InputStream, byte[], boolean)

prepareException

protected java.lang.Throwable prepareException(java.lang.Throwable pThrowable)
Prepares the given exception if needed. It's possible to change the stack trace or to create a completely new exception.

Parameters:
pThrowable - the original exception
Returns:
the exception to use

prepareAfterRetryException

protected java.lang.Throwable prepareAfterRetryException(java.lang.Throwable pThrowable)
Prepares the final exception after last retry. This methods forwards the exception to prepareException(Throwable).

Parameters:
pThrowable - the original exception
Returns:
the exception to use

fillInStackTrace

protected java.lang.Throwable fillInStackTrace(java.lang.Throwable pThrowable)
Fills in the current call-stack in the given exception.

Parameters:
pThrowable - the exception from a call
Returns:
the "marked" exception or null if given exception is null


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.