public abstract class SessionManager
extends java.lang.Object
SessionManagerFactory
and is retrieved with the static get()
method.
This class may not be instantiated directly. Much of the work is performed by concrete subclasses, though common functionality is implemented here. The API, while asymmetric in places depending upon whether the current node is a leaf or router, is provided here in its entirety to simplify use of this class from both within and outside of, the net package. Leaf methods that are inappropriately invoked from a router node will raise exceptions, and vice versa.
The session manager is responsible for:
Modifier and Type | Class and Description |
---|---|
private class |
SessionManager.Stopper |
Modifier and Type | Field and Description |
---|---|
protected ContextLocal<BaseSession> |
activeSession
Active session for the current client context
|
protected java.util.Map<java.net.InetSocketAddress,java.lang.Integer> |
addressUseCount
Use count of the known addresses for this network node
|
protected java.util.Map<Queue,java.lang.String> |
appServers
Map of connected app servers; used on the P2J client-side.
|
private java.util.Set<java.lang.Thread> |
asyncThreads
A set containing threads which always send async requests.
|
protected BootstrapConfig |
config
Bootstrap configuration for this network node
|
protected java.util.Map<Queue,java.lang.String> |
identityByQueue
One-to-one map of identity to queues.
|
private static SessionManager |
instance
Singleton instance of this class
|
private InterruptHandler |
interruptHandler
The handler used to process interrupt requests.
|
protected java.lang.Object |
lock
Object on which to synchronize access to session manager state
|
protected static java.util.logging.Logger |
LOG
Logger
|
private int |
nextContextID
Next ID to be assigned for a local security context.
|
protected SecurityManager |
secMgr
Security manager
|
protected java.util.Map<Queue,java.util.Set<BaseSession>> |
sessionsByQueue
Many-to-one map of queues to all of their associated sessions
|
boolean |
shutdownDone
Mark end of shutdown process.
|
private boolean |
shutdownStarted
Mark start of shutdown process.
|
private java.lang.Thread |
stopper
Shutdown thread.
|
protected java.util.Map<java.net.InetSocketAddress,Queue> |
topology
Topology database for this network node
|
protected java.util.Map<java.lang.Integer,Control> |
virtualControlByContext
One-to-one map of multiplexed context IDs to its associated Control instance.
|
protected java.util.Map<Queue,java.lang.Object> |
virtualLocksByQueue
One-to-one map of virtual locks to queues
|
protected java.util.Map<java.lang.Integer,BaseSession> |
virtualSessionsByContext
One-to-one map of context IDs to sessions
|
Modifier | Constructor and Description |
---|---|
protected |
SessionManager()
Constructor usable only by concrete subclasses.
|
Modifier and Type | Method and Description |
---|---|
int |
authenticateRemote(java.lang.String acctID,
int contextID,
Queue queue)
Called by a requester to authenticate a virtual session.
|
protected int |
callAuthenticateRemote(java.lang.String acctID,
int contextID,
Queue queue)
Make a synchronous method call to a remote router node to authenticate
the specified account and retrieve the remote node's context ID for the
newly authenticated, virtual session.
|
private void |
callTerminateRemote(BaseSession session)
Make a synchronous method call to a remote router node to terminate the
remote side of this virtual session.
|
BootstrapConfig |
config()
Expose the
config used to start the P2J Client or Server. |
Session |
connectDirect(BootstrapConfig config,
InterruptHandler interruptHandler,
SessionListener notify)
Establish a direct connection to a network node using the specified
bootstrap configuration.
|
Session |
connectDirect(InterruptHandler interruptHandler,
SessionListener notify)
Establish a direct connection to a network node using the bootstrap
configuration stored during construction.
|
Session |
connectVirtual(BootstrapConfig config,
SessionListener notify)
Establish a virtual (multiplexed) connection to a network node at the
address specified by the given bootstrap configuration.
|
Session |
connectVirtual(java.net.InetSocketAddress address,
java.lang.String certAlias,
SessionListener notify)
Establish a virtual (multiplexed) connection to a network node at the
given address.
|
Session |
connectVirtual(java.net.InetSocketAddress address,
java.lang.String certAlias,
SessionListener notify,
java.lang.String account)
Establish a virtual (multiplexed) connection to a network node at the
given address.
|
protected boolean |
createDirectSession(Queue queue)
Create a direct session for the given queue and register it with the
session manager.
|
protected Queue |
createQueue(java.net.InetSocketAddress remoteAddr,
BootstrapConfig config)
Create a socket and the low level message transport queue, to be
used to communicate with the remote node at the given socket address.
|
void |
deregisterAsyncThread(java.lang.Thread thread)
DeRegister the given thread from the list of threads which always send async requests.
|
protected boolean |
deregisterSession(BaseSession session)
Deregister a session with the session manager.
|
protected void |
dropInitialContext()
Release the initial security context assigned to the calling thread.
|
protected void |
endSession(BaseSession session,
boolean interrupt,
boolean notify)
End the specified network session.
|
void |
executeInContext(Session session,
java.lang.Runnable task)
Execute the given task in the specified session's context.
|
static SessionManager |
get()
Method used to access the concrete, singleton session manager instance
for a network node.
|
java.lang.String |
getAppServer(Session session)
Get the appserver associated with this P2j client session.
|
InterruptHandler |
getInterruptHandler()
Get the interrupt handler.
|
int |
getNodeAddress()
Get the local node address of the active session, if any.
|
int |
getRemoteAddress()
Get the remote node address of the active session, if any.
|
(package private) AbstractResource |
getSecurityPlugin(java.lang.String resourceTypeName)
Get the security plugin which is responsible for the resources of the
specified type.
|
Session |
getSession()
Get the active session, if any.
|
java.lang.Object[] |
getSessionContexts(java.lang.String jar,
java.lang.String iface)
Get the contexts for all active sessions which have invoked APIs in the
given jar.
|
protected void |
init()
Initialize the session manager beyond what is done by the
initialize(BootstrapConfig) method. |
(package private) void |
initialize(BootstrapConfig config)
Initialize the session manager.
|
boolean |
isAsyncThread(java.lang.Thread thread)
Check if the given thread is in the list of threads which always send async requests.
|
boolean |
isLeaf()
Convenience method to detect whether this session manager operates on a
leaf node.
|
boolean |
isLocalNode(java.net.InetSocketAddress address)
Convenience method to detect whether the given socket address represents
the current node.
|
boolean |
isRouter()
Convenience method to detect whether this session manager operates on a
router node.
|
boolean |
isVirtualSession()
Check if the current session is a
VirtualSession . |
void |
listen(SessionListener notify)
Listen for incoming socket connections, authenticate each new
connection, and establish a new queue for each authenticated connection.
|
protected int |
nextContextID()
Generate a security context ID which uniquely identifies the security
context of a virtual session.
|
(package private) void |
notifyTermination(Queue queue)
For each session associated with the given queue, notify all of the
session's session listeners of the termination event.
|
(package private) void |
processEcho(Queue queue,
Message msg)
Process the reply to an inbound echo request, if a session can be found
for the message's context.
|
(package private) void |
queueStopped(Queue queue)
End all sessions associated with the given queue, since the queue has
been stopped.
|
void |
registerAsyncThread(java.lang.Thread thread)
Register the given thread in the list of threads which always send async requests.
|
protected void |
registerSession(int contextID,
java.net.InetSocketAddress remoteAddr,
BaseSession session)
Register a new session with the session manager.
|
protected void |
restoreContext()
Restore the current thread's security context to whatever it was before
it was last explicitly set.
|
protected void |
setContext(BaseSession session)
Set the security context for the current thread, using the security
context stored in the given session.
|
protected void |
setContext(Queue queue)
Set the security context for the current thread, using the session
identified by the given queue.
|
protected void |
setContext(RoutingKey key,
Queue queue)
Set the security context for the current thread, using the session
identified by the given routing key or transport queue.
|
protected void |
setInitialContext()
Initialize security context switching for the calling thread.
|
void |
setInterruptHandler(InterruptHandler handler)
Set the interrupt handler.
|
void |
shutdown()
Terminate ALL sessions managed by this session manager and shut down the
network node itself.
|
protected void |
terminateInput()
Terminate the socket connection listening loop for a router network
node.
|
void |
terminateRemote(int contextID)
Called by a requester to terminate a virtual session.
|
void |
terminateSessions(java.lang.String jar,
java.lang.String iface)
Terminate all active sessions which have invoked the given APIs in the
given jar.
|
void |
terminateVirtual(BaseSession session)
Called by a requester to initiate virtual session termination.
|
java.lang.String |
toString()
Return a string representation of the internal state of this object,
primarily for debugging purposes.
|
void |
waitForShutdown()
Wait for end of shutdown thread.
|
void |
waitUntilReady()
Block the calling thread until the network portions of the server are fully initialized.
|
protected static final java.util.logging.Logger LOG
private static SessionManager instance
protected final java.util.Map<java.net.InetSocketAddress,Queue> topology
protected final java.util.Map<java.net.InetSocketAddress,java.lang.Integer> addressUseCount
protected final java.util.Map<Queue,java.util.Set<BaseSession>> sessionsByQueue
protected final java.util.Map<java.lang.Integer,BaseSession> virtualSessionsByContext
protected final java.util.Map<Queue,java.lang.Object> virtualLocksByQueue
protected final java.util.Map<Queue,java.lang.String> identityByQueue
Queue
was successfully created.protected final java.util.Map<Queue,java.lang.String> appServers
protected final java.util.Map<java.lang.Integer,Control> virtualControlByContext
protected final ContextLocal<BaseSession> activeSession
protected final java.lang.Object lock
protected SecurityManager secMgr
protected BootstrapConfig config
private final java.util.Set<java.lang.Thread> asyncThreads
private InterruptHandler interruptHandler
private java.lang.Thread stopper
private boolean shutdownStarted
public boolean shutdownDone
private int nextContextID
protected SessionManager()
java.lang.IllegalStateException
- if called more than once per JVM instance.public static SessionManager get()
null
.public final void registerAsyncThread(java.lang.Thread thread)
thread
- An active thread instance.public final void deregisterAsyncThread(java.lang.Thread thread)
thread
- An active thread instance.public final boolean isAsyncThread(java.lang.Thread thread)
thread
- An active thread instance.true
if the thread is in the list of threads which always send async
requests.public final InterruptHandler getInterruptHandler()
public final void setInterruptHandler(InterruptHandler handler)
handler
- The interrupt handler instance.public boolean isLeaf()
true
if network node for this session manager is a
leaf node; else false
.public BootstrapConfig config()
config
used to start the P2J Client or Server.config
.public boolean isRouter()
true
if network node for this session manager is a
router node; else false
.public java.lang.String getAppServer(Session session)
session
- The P2J session.null
if the client is not started as an
appserver.public boolean isLocalNode(java.net.InetSocketAddress address)
This implementation throws an exception. It must be overridden to provide the functionality described above.
address
- Socket address to check.true
if an address match is detected, else
false
.java.lang.UnsupportedOperationException
- always.public int authenticateRemote(java.lang.String acctID, int contextID, Queue queue) throws java.lang.IllegalAccessException
This implementation throws an exception. It must be overridden to provide the functionality described above.
acctID
- Account identifier for the requesting party.contextID
- Value which uniquely identifies the security context of the
requesting party. This will be used in communications from
the provider side of the connection.queue
- Message transport queue for the provider side of the
connection.java.lang.UnsupportedOperationException
- always.java.lang.IllegalAccessException
- if authentication is not successful.public void terminateRemote(int contextID)
This implementation throws an exception. It must be overridden to provide the functionality described above.
contextID
- Value which uniquely identifies the security context of the
requesting party.java.lang.UnsupportedOperationException
- always.public void terminateVirtual(BaseSession session)
session
- The session which needs to be terminated.private void callTerminateRemote(BaseSession session)
public Session connectDirect(InterruptHandler interruptHandler, SessionListener notify) throws java.lang.Exception
This implementation calls connectDirect(BootstrapConfig,
InterruptHandler, SessionListener)
, with a null
bootstrap
config. So, the config
instance received during this manager's
creation
will be used instead.
interruptHandler
- An object that is notified when the current thread is
interrupted.notify
- Session listener to be notified of session events.java.lang.UnsupportedOperationException
- always.java.lang.Exception
- not thrown.public Session connectDirect(BootstrapConfig config, InterruptHandler interruptHandler, SessionListener notify) throws java.lang.Exception
This implementation throws an exception. It must be overridden to provide the functionality described above.
config
- Bootstrap configuration information which contains at least
enough information (host and port) to make a direct connection
from this node to another.interruptHandler
- An object that is notified when the current thread is
interrupted.notify
- Session listener to be notified of session events.java.lang.UnsupportedOperationException
- always.java.lang.Exception
- not thrown.public Session connectVirtual(java.net.InetSocketAddress address, java.lang.String certAlias, SessionListener notify) throws java.lang.Exception
This implementation throws an exception. It must be overridden to provide the functionality described above.
address
- Socket address of remote network node.certAlias
- Alias of certificate to be read from trust store to enable
certificate-based, server-to-server authentication. This
should match the server ID of the remote node.notify
- Session listener to be notified of session events.java.lang.UnsupportedOperationException
- always.java.lang.Exception
- not thrown.public Session connectVirtual(java.net.InetSocketAddress address, java.lang.String certAlias, SessionListener notify, java.lang.String account) throws java.lang.Exception
This implementation throws an exception. It must be overridden to provide the functionality described above.
address
- Socket address of remote network node.certAlias
- Alias of certificate to be read from trust store to enable
certificate-based, server-to-server authentication. This
should match the server ID of the remote node.notify
- Session listener to be notified of session events.account
- Explicit account to authenticate on the remote side. May be null
.java.lang.UnsupportedOperationException
- always.java.lang.Exception
- not thrown.public Session connectVirtual(BootstrapConfig config, SessionListener notify) throws java.lang.Exception
This implementation throws an exception. It must be overridden to provide the functionality described above.
config
- Bootstrap configuration information which contains at least
enough information (host and port) to make a virtual connection
from this node to another.notify
- Session listener to be notified of session events.java.lang.UnsupportedOperationException
- always.java.lang.Exception
- not thrown.public void listen(SessionListener notify) throws java.lang.Exception
This implementation throws an exception. It must be overridden to provide the functionality described above.
notify
- Session listener to be notified of session events.java.lang.UnsupportedOperationException
- always.java.lang.Exception
- not thrown.public Session getSession()
null
.public boolean isVirtualSession()
VirtualSession
.public java.lang.Object[] getSessionContexts(java.lang.String jar, java.lang.String iface)
null
, list all active
sessions.
Restricted use. This method checks the caller to be
com.goldencode.p2j.security.SecurityManager.getActiveSessions
.
jar
- The jar nameiface
- The interface for which we need the sessions;
if null
, search through the entire jar.public void terminateSessions(java.lang.String jar, java.lang.String iface) throws RestrictedUseException
null
, terminate all active
tracked sessions.
This method guarantees that all session termination listeners have been
executed.
Restricted use. This method checks the caller to be
com.goldencode.p2j.security.SecurityManager.terminateSessions
.
jar
- The jar name; if null
, terminate all active tracked
sessions.iface
- The interface for which we need to terminate the sessions; if
null
, terminate all sessions for the given jar.RestrictedUseException
- If called improperly.public int getNodeAddress()
public int getRemoteAddress()
public void shutdown() throws java.lang.NoSuchMethodException
java.lang.NoSuchMethodException
- if the caller's security context does not include sufficient
rights to perform the shutdown.public void waitUntilReady() throws java.lang.InterruptedException
java.lang.InterruptedException
public void waitForShutdown() throws java.lang.NoSuchMethodException
java.lang.NoSuchMethodException
- if the caller's security context does not include sufficient
rights to perform the shutdown.public java.lang.String toString()
toString
in class java.lang.Object
Object.toString()
public void executeInContext(Session session, java.lang.Runnable task) throws RestrictedUseException
Only a thread with the server's context is allowed to execute this.
session
- The client session.task
- The task to execute.RestrictedUseException
- If the current context is not a server context.protected boolean createDirectSession(Queue queue)
This implementation throws an exception. It must be overridden to provide the functionality described above.
queue
- Underlying message transport for the new session.true
if the session initialized successfully.protected Queue createQueue(java.net.InetSocketAddress remoteAddr, BootstrapConfig config) throws java.lang.Exception
The socket that is created is either secure or insecure, depending on
the value of net/connection/secure
bootstrap configuration
flag.
remoteAddr
- Address of the socket for the remote network node.config
- BootstrapConfig which should override that of the security
manager for this session. If null
, the security
manager's default configuration is used.java.lang.Exception
- if authentication fails.protected void terminateInput()
This implementation is a NOP; it must be overridden to provide the above functionality.
protected void endSession(BaseSession session, boolean interrupt, boolean notify)
Dispatcher
or Conversation
thread or session;
session
- Session that is ending.interrupt
- true
to interrupt any long running service thread
asynchronously.notify
- true
to notify registered listeners of the
termination. This should be false
if the queue
itself triggered the termination, since in that case the
notification will already have occurred.protected void registerSession(int contextID, java.net.InetSocketAddress remoteAddr, BaseSession session)
contextID
- An integer which uniquely identifies a virtual session on the
local node. This ID is used by the peer side of the session to
multiplex communications with multiple virtual sessions over a
single queue.remoteAddr
- Socket address of the network node on the remote side of the
session.session
- Session being registered.protected boolean deregisterSession(BaseSession session)
If the session being deregistered is the last session to use its queue, this fact is indicated by the method's return value.
This method is tolerant of being called multiple times for the same session instance, since there are several reasons this method may be called at session termination:
Session.terminate()
;
session
- Session being deregistered.true
if the session's underlying queue is still
needed by other sessions; false
if this was the
last session using its queue, and the queue can now be stopped.protected void setContext(RoutingKey key, Queue queue)
This implementation is a NOP; it must be overridden to provide the above functionality.
key
- Routing key for an incoming message.queue
- Message transport queue.protected void setContext(Queue queue)
This implementation is a NOP; it must be overridden to provide the above functionality.
queue
- Queue which uniquely identifies the session whose security
context is to be used.protected void setContext(BaseSession session)
session
- Session whose security context is to be associated with the
current thread.protected void restoreContext()
This implementation is a NOP; it must be overridden to provide the above functionality.
protected void setInitialContext()
This implementation is a NOP; it must be overridden to provide the above functionality.
protected void dropInitialContext()
This implementation is a NOP; it must be overridden to provide the above functionality.
protected void init() throws ConfigurationException
initialize(BootstrapConfig)
method.
This implementation does nothing. It should be overridden by subclasses with special initialization requirements.
ConfigurationException
- if there is any configuration error.protected int nextContextID()
protected int callAuthenticateRemote(java.lang.String acctID, int contextID, Queue queue) throws java.lang.Exception
acctID
- User or process account identifier; must be known to the
remote node.contextID
- Value which uniquely identifies the security context of this
side of the virtual session.queue
- Queue used to transact the authentication method call.java.lang.Exception
- if an exception is thrown by the remote method invoked.final void initialize(BootstrapConfig config) throws ConfigurationException
SessionManagerFactory
after the session manager instance has been
constructed or retrieved for reuse.config
- Bootstrap configuration for this network node.ConfigurationException
- if there is any configuration error.AbstractResource getSecurityPlugin(java.lang.String resourceTypeName)
SecurityManager
. If
the current node is a leaf node, null
is returned.
Important Note: a security context should be set before this method is
invoked. Otherwise, null
will be returned.
resourceTypeName
- Type of resource managed by the security plugin.AbstractResource
object, or null
.void notifyTermination(Queue queue)
queue
- Queue which is terminating.queueStopped(Queue)
void queueStopped(Queue queue)
queue
- Queue which has been stopped.notifyTermination(Queue)