org.tentackle.db.rmi
Class RemoteDbSessionImpl

java.lang.Object
  extended by java.rmi.server.RemoteObject
      extended by java.rmi.server.RemoteServer
          extended by java.rmi.server.UnicastRemoteObject
              extended by org.tentackle.db.rmi.RemoteDbSessionImpl
All Implemented Interfaces:
Serializable, Remote, RemoteDbSession
Direct Known Subclasses:
AppRemoteDbSessionImpl

public class RemoteDbSessionImpl
extends UnicastRemoteObject
implements RemoteDbSession

User session within the application server.

Author:
harald
See Also:
Serialized Form

Field Summary
protected static Set<WeakReference<RemoteDbSessionImpl>> sessions
          We keep an internal set of all sessions via WeakReferences, so the sessions still will be finalized when the session isn't used anymore.
 
Fields inherited from class java.rmi.server.RemoteObject
ref
 
Constructor Summary
RemoteDbSessionImpl(RemoteDbConnectionImpl con, UserInfo clientInfo, UserInfo serverInfo)
          Creates a session on a given connection.
 
Method Summary
protected  void cleanup(boolean crashed)
          Cleanup the session.
static void cleanupSessions()
          Cleans up the sessions.
 void close()
          Closes a session.
protected  void closeDb()
          Closes the database connection (and thus rolls back any pending transaction).
 void finalize()
          Cleanup in case someone forgot to logoff()
 RMIClientSocketFactory getClientSocketFactory()
          Gets the default client socket factory for all delegates.
 RMIClientSocketFactory getClientSocketFactory(int socketConfig)
          Gets the csf for a given socket type.
 UserInfo getClientUserInfo()
          Gets the client user info.
 RemoteDbConnectionImpl getConnection()
          Gets the server connection.
 Db getDb()
          Gets the session db connection
 Class getDbObjectClass(int delegateId)
          gets the DbObject class by delegateId.
 DbRemoteDelegate getDbRemoteDelegate()
          Gets the delegate for the remote db-connection.
 int getPort()
          Gets the default port for all delegates.
 int getPort(int socketConfig)
          Gets the predefined port for a given socket type.
 RemoteDelegate getRemoteDelegate(String classname, int delegateId)
          Gets the delegate for a given classname.
Per class the rmi-clients request a remote access there must be a RemoteDelegate.
 RMIServerSocketFactory getServerSocketFactory()
          Gets the default server socket factory for all delegates.
 RMIServerSocketFactory getServerSocketFactory(int socketConfig)
          Gets the ssf for a given socket type.
 UserInfo getServerUserInfo()
          Gets the server user info.
 boolean isOpen()
          Determines whether the session is open.
 void log(Logger.Level level, String message)
          Sends text based logging infos to the RMI-Server.
protected  Db openDb()
          Opens a new Db.
static void startCleanupThread(long ms)
          Starts the optional cleanup thread that will monitor the sessions for db-activity.
 String toString()
          Returns a String that represents the value of this remote object.
 
Methods inherited from class java.rmi.server.UnicastRemoteObject
clone, exportObject, exportObject, exportObject, unexportObject
 
Methods inherited from class java.rmi.server.RemoteServer
getClientHost, getLog, setLog
 
Methods inherited from class java.rmi.server.RemoteObject
equals, getRef, hashCode, toStub
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

sessions

protected static final Set<WeakReference<RemoteDbSessionImpl>> sessions
We keep an internal set of all sessions via WeakReferences, so the sessions still will be finalized when the session isn't used anymore. The set of sessions is used to determine stale sessions, i.e. ones with timed out db-connections. These are typically caused by clients not properly closing the rmi-session.

Constructor Detail

RemoteDbSessionImpl

public RemoteDbSessionImpl(RemoteDbConnectionImpl con,
                           UserInfo clientInfo,
                           UserInfo serverInfo)
                    throws RemoteException
Creates a session on a given connection.

Parameters:
con - the connection
clientInfo - the UserInfo from the client
serverInfo - the UserInfo to establish the connection to the database server
Throws:
RemoteException - if the session could not initiated.
Method Detail

cleanupSessions

public static void cleanupSessions()
Cleans up the sessions. Finds all db-groups or non-grouped db that are not alive anymore and closes their sessions. If _all_ dbs of a group have timed out, the whole group is closed. Usually the ModificationThread of a client connection will keep up a db-group alive. If the application uses only a single db-session, it must set the db-alive manually.


startCleanupThread

public static void startCleanupThread(long ms)
Starts the optional cleanup thread that will monitor the sessions for db-activity.

Parameters:
ms - is the interval in ms

getDbObjectClass

public Class getDbObjectClass(int delegateId)
gets the DbObject class by delegateId. This is not a remote function (only called in server-part of the delegates in case it needs to know the id of a delegate of another class)

Parameters:
delegateId - the class for the delegate id
Returns:
the class

getDb

public Db getDb()
Gets the session db connection

Returns:
the db connection

getConnection

public RemoteDbConnectionImpl getConnection()
Gets the server connection.

Returns:
the connection

getClientUserInfo

public UserInfo getClientUserInfo()
Gets the client user info.

Returns:
the client user info

getServerUserInfo

public UserInfo getServerUserInfo()
Gets the server user info.

Returns:
the server user info

getPort

public int getPort()
Gets the default port for all delegates.

Returns:
the default port for all delegates

getClientSocketFactory

public RMIClientSocketFactory getClientSocketFactory()
Gets the default client socket factory for all delegates.

Returns:
the default csf for all delegates

getServerSocketFactory

public RMIServerSocketFactory getServerSocketFactory()
Gets the default server socket factory for all delegates.

Returns:
the default ssf for all delegates

getPort

public int getPort(int socketConfig)
Gets the predefined port for a given socket type.

Parameters:
socketConfig - is of SOCKETCONFIG_...
Returns:
the port

getClientSocketFactory

public RMIClientSocketFactory getClientSocketFactory(int socketConfig)
Gets the csf for a given socket type.

Parameters:
socketConfig - is of SOCKETCONFIG_...
Returns:
the default csf for all delegates

getServerSocketFactory

public RMIServerSocketFactory getServerSocketFactory(int socketConfig)
Gets the ssf for a given socket type.

Parameters:
socketConfig - is of SOCKETCONFIG_...
Returns:
the default ssf for all delegates

openDb

protected Db openDb()
             throws LoginFailedException
Opens a new Db. The default implementation opens a new Db. Can be overridden if, for example, pools are used instead.

Returns:
the db connection
Throws:
LoginFailedException - if opening the db failed

cleanup

protected void cleanup(boolean crashed)
Cleanup the session.

The method is invoked whenever the session is closed due to an ordinary logout or client crash. The default implementation rolls back any pending transaction.

Parameters:
crashed - true if client crashed, else regular logout

closeDb

protected void closeDb()
Closes the database connection (and thus rolls back any pending transaction). If the db is pooled, it will be returned to the pool instead of being closed.


toString

public String toString()
Description copied from class: java.rmi.server.RemoteObject
Returns a String that represents the value of this remote object.

Overrides:
toString in class RemoteObject
Returns:
a string representation of the object.

isOpen

public boolean isOpen()
Determines whether the session is open.

Returns:
true if session is open

finalize

public void finalize()
Cleanup in case someone forgot to logoff()

Overrides:
finalize in class Object

close

public void close()
           throws RemoteException
Description copied from interface: RemoteDbSession
Closes a session.

Specified by:
close in interface RemoteDbSession
Throws:
RemoteException

log

public void log(Logger.Level level,
                String message)
         throws RemoteException
Description copied from interface: RemoteDbSession
Sends text based logging infos to the RMI-Server.

Specified by:
log in interface RemoteDbSession
Throws:
RemoteException

getRemoteDelegate

public RemoteDelegate getRemoteDelegate(String classname,
                                        int delegateId)
                                 throws RemoteException
Description copied from interface: RemoteDbSession
Gets the delegate for a given classname.
Per class the rmi-clients request a remote access there must be a RemoteDelegate. The delegate is determined from the classname of the client class as follows:
<package>.rmi.<last-element-of-classname>.class on the client-side and
<package>.rmi.<last-element-of-classname>Impl.class on the server-side.
The delegates are cached on the client side in order to speed up access. Delegates are bound to a session, i.e. there will be one delegate for each session and class.

Specified by:
getRemoteDelegate in interface RemoteDbSession
Parameters:
classname - is the name of class
delegateId - is the client-side ID of the delegate
Returns:
the delegate
Throws:
RemoteException

getDbRemoteDelegate

public DbRemoteDelegate getDbRemoteDelegate()
                                     throws RemoteException
Description copied from interface: RemoteDbSession
Gets the delegate for the remote db-connection.

Specified by:
getDbRemoteDelegate in interface RemoteDbSession
Returns:
the delegate
Throws:
RemoteException


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de