public final class Dispatcher extends java.lang.Object implements MessageTypes
Modifier and Type | Class and Description |
---|---|
private class |
Dispatcher.DispatcherStub
This class is used to start threads running
dispatch()
method. |
private static class |
Dispatcher.KeyMapEntry
This class is used as an entry in the Key Map.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
closing
Flag indicating the dispatcher is closing down.
|
private static java.lang.Class[] |
criteria
List of throwables to exclude from logging.
|
private static Dispatcher |
dispatcher
Dispatcher shared instance.
|
(package private) static java.lang.ThreadGroup |
dispThreads
Thread group for the dispatcher threads.
|
private java.util.Vector<Dispatcher.KeyMapEntry> |
keyMap
This variable is a part of EIR (Exported Interface Registry) and
contains names of groups and methods.
|
private static java.util.logging.Logger |
LOG
Logger (safe as a JVM-wide value rather than as context-local).
|
private java.util.HashMap<RoutingKey,MethodInvoker> |
methodMap
This variable is a part of EIR (Exported Interface Registry) and
contains mapping between instances of RoutingKey and methods.
|
private java.util.concurrent.ConcurrentMap<RoutingKey,java.lang.reflect.Method> |
methodsByKey
Map routing key to method
|
private java.util.HashMap<java.lang.Object,java.util.Set<RoutingKey>> |
modTokens
Token which authenticates the caller as allowed to modify the EIR.
|
private static SessionManager |
sessMgr
Session manager (safe as a JVM-wide value).
|
private java.util.Map<java.lang.reflect.Method,java.util.concurrent.Semaphore> |
staticMethodLocks
A map containing semaphores for each static exported method.
|
private java.lang.Thread[] |
threads
Dispatcher thread pool array.
|
private boolean |
tracing
true if tracing is enabled. |
ADDRESS_REPLY, ADDRESS_REQUEST, BEGIN_REPLY, ECHO, ECHO_REPLY, END_REPLY, INIT_CONVERSATION, INIT_REPLY, INIT_ROUTER, INIT_STANDARD, REPLY_ASYNC, REPLY_EXCEPTION_ASYNC, REPLY_EXCEPTION_SYNC, REPLY_SYNC, REQUEST_ASYNCH, REQUEST_SYNCH, UNKNOWN
Modifier | Constructor and Description |
---|---|
private |
Dispatcher(int num)
Constructs an instance of
Dispatcher , including
initialization of thread pool. |
Modifier and Type | Method and Description |
---|---|
static java.lang.Object |
addMethod(RoutingKey key,
java.lang.Object obj,
java.lang.reflect.Method method,
java.lang.Object modToken)
Add a method to the Exported Interface Registry (EIR).
|
static java.lang.Object |
addMethod(RoutingKey key,
java.lang.Object obj,
java.lang.String name,
java.lang.Class[] signature,
java.lang.Class cls,
java.lang.Object modToken)
Add a method to the Exported Interface Registry (EIR).
|
static java.lang.Object |
addMethod(RoutingKey key,
java.lang.Object obj,
java.lang.String name,
java.lang.Class[] signature,
java.lang.String className,
java.lang.Object modToken)
Add a method to the Exported Interface Registry (EIR).
|
private java.lang.Object |
addMethodImpl(RoutingKey key,
java.lang.Object obj,
java.lang.reflect.Method method,
java.lang.Object modToken)
Add a method to the Exported Interface Registry (EIR).
|
(package private) void |
dispatch()
Main message processing method, core of the
Dispatcher
class. |
static RoutingKey |
generateRoutingKey(java.lang.String group,
java.lang.String method)
Returns
RoutingKey instance using provided method and
method group names. |
private java.lang.String |
getGroupName(RoutingKey key)
Gets the group name corresponding to the specified routing key.
|
(package private) static Dispatcher |
getInstance()
Returns the singleton instance of this class.
|
private java.lang.Class<?> |
getInterface(RoutingKey key)
Get the interface used to export the method with the given
key , assuming that it is an interface. |
private java.lang.reflect.Method |
getMethod(RoutingKey key)
Get the method exported with the given
key , assuming
that it is defined by an interface. |
private java.lang.String |
getMethodName(RoutingKey key)
Gets the method name corresponding to the specified routing key.
|
static RoutingKey |
getRoutingKey(java.lang.String group,
java.lang.String method)
Returns
RoutingKey instance using provided method and
method group names. |
private RoutingKey |
getRoutingKey(java.lang.String group,
java.lang.String method,
boolean canAdd)
Get
RoutingKey instance using provided method and group
names. |
(package private) static void |
initialize(int num)
Start or re-start the dispatcher.
|
static boolean |
interruptSession(boolean request,
int operationId,
int[] pendingReplies)
Services interrupt request from the remote end.
|
(package private) static boolean |
isRunning()
Checks whether this class is initialized.
|
private boolean |
lockMethod(java.lang.reflect.Method method)
Locks the given method, without checking if there are active sessions.
|
(package private) static boolean |
lockMethods(java.lang.Class<?>[] list,
java.lang.reflect.Method[][] methods,
java.lang.Object modToken,
boolean register)
Lock all the given methods.
|
(package private) static boolean |
lockMethods(java.lang.Class<?>[] list,
java.lang.reflect.Method[][] methods,
java.lang.Object modToken,
boolean register,
boolean checkActive)
Lock all the given methods.
|
private boolean |
lockMethodsImpl(java.lang.Class<?>[] list,
java.lang.reflect.Method[][] methods,
java.lang.Object modToken,
boolean register,
boolean checkActive)
Lock all the given methods.
|
(package private) void |
processInbound(InboundRequest request,
boolean set,
NetResource nr)
Core worker for processing of inbound requests.
|
private void |
releaseMethod(java.lang.reflect.Method method)
Release the lock for the given method.
|
(package private) static void |
releaseMethods(java.lang.reflect.Method[][] methods)
Release all locks for the given methods.
|
private void |
releaseMethodsImpl(java.lang.reflect.Method[][] methods)
Release all locks for the given methods.
|
(package private) static void |
removeMethod(RoutingKey key)
Remove the exported API represented by the given key.
|
private void |
removeMethodImpl(RoutingKey key)
Remove the exported API represented by the given key.
|
private java.lang.Object |
trace(MethodInvoker method,
java.lang.Object[] args)
Tracing worker that handles the local method call to the exported
interface and logs when the call is made (at FINER level) and logs
parameters, return values and an elapsed time in millis (at FINEST
level).
|
static final java.lang.ThreadGroup dispThreads
private static final java.util.logging.Logger LOG
private static final java.lang.Class[] criteria
private static SessionManager sessMgr
private static Dispatcher dispatcher
private final java.util.Vector<Dispatcher.KeyMapEntry> keyMap
private final java.util.concurrent.ConcurrentMap<RoutingKey,java.lang.reflect.Method> methodsByKey
private final java.util.HashMap<RoutingKey,MethodInvoker> methodMap
private final java.util.HashMap<java.lang.Object,java.util.Set<RoutingKey>> modTokens
private final java.util.Map<java.lang.reflect.Method,java.util.concurrent.Semaphore> staticMethodLocks
private java.lang.Thread[] threads
private boolean closing
private boolean tracing
true
if tracing is enabled.private Dispatcher(int num)
Dispatcher
, including
initialization of thread pool.
Enables tracing of method invocations at logging level
Level.FINER
. Enables tracing of parameters/result values
and elapsed time in milliseconds at logging level
Level.FINEST
.
num
- Number of threads in the thread pool.public static java.lang.Object addMethod(RoutingKey key, java.lang.Object obj, java.lang.reflect.Method method, java.lang.Object modToken) throws java.lang.SecurityException
Method
is provided by the application. If a method with
the specified key is already present in the EIR it is only replaced if
the previously returned authentication token is given here, otherwise a
SecurityException
is thrown.key
- RoutingKey instance which is used as a key for adding method.obj
- Reference to the instance of the object which will handle the
requests. Should be null
for static methods.method
- Instance of java.lang.reflect.Method
which will
handle requests.modToken
- The modification token retured from the original registration
call (to this same method). May be null
on the
first call for this routing key (a new token will be created
and returned in this case). If non-null
on the
first call, that object will be used as the modification token
from then on.java.lang.SecurityException
- is forwarded from addMethodImpl
method.public static java.lang.Object addMethod(RoutingKey key, java.lang.Object obj, java.lang.String name, java.lang.Class[] signature, java.lang.Class cls, java.lang.Object modToken) throws java.lang.NoSuchMethodException, java.lang.SecurityException
Method
is provided by the application. If a method with
the specified key is already present in the EIR it is only replaced if
the previously returned authentication token is given here, otherwise a
SecurityException
is thrown.key
- RoutingKey instance which is used as a key for adding method.obj
- Reference to the instance of the object which will handle the
requests. Should be null
for static methods.name
- Name of the method in the target classsignature
- List of types of parameters of the method in the target class.cls
- Target classmodToken
- The modification token retured from the original registration
call (to this same method). May be null
on the
first call for this routing key (a new token will be created
and returned in this case). If non-null
on the
first call, that object will be used as the modification token
from then on.java.lang.NoSuchMethodException
- is forwarded from Class.getMethod
method.java.lang.SecurityException
- is forwarded from Class.getMethod
or
addMethodImpl
methods.public static java.lang.Object addMethod(RoutingKey key, java.lang.Object obj, java.lang.String name, java.lang.Class[] signature, java.lang.String className, java.lang.Object modToken) throws java.lang.NoSuchMethodException, java.lang.SecurityException, java.lang.ClassNotFoundException
Method
is provided by the application. If a method with
the specified key is already present in the EIR it is only replaced if
the previously returned authentication token is given here, otherwise a
SecurityException
is thrown.key
- RoutingKey instance which is used as a key for adding method.obj
- Reference to the instance of the object which will handle the
requests. Should be null
for static methods.name
- Name of the method in the target classsignature
- List of types of parameters of the method in the target class.className
- Target class name.modToken
- The modification token retured from the original registration
call (to this same method). May be null
on the
first call for this routing key (a new token will be created
and returned in this case). If non-null
on the
first call, that object will be used as the modification token
from then on.java.lang.NoSuchMethodException
- is forwarded from Class.getMethod
method.java.lang.SecurityException
- is forwarded from Class.getMethod
or
addMethodImpl
methods.java.lang.ClassNotFoundException
- is forwarded from
ClassLoader.getSystemClassLoader().loadClass
method.public static RoutingKey generateRoutingKey(java.lang.String group, java.lang.String method)
RoutingKey
instance using provided method and
method group names. If group or method names are not present in the EIR,
then appropriate entries added to EIR.group
- Method group name.method
- Method name.public static RoutingKey getRoutingKey(java.lang.String group, java.lang.String method)
RoutingKey
instance using provided method and
method group names. If group or method are not registered in the EIR
then null
is returned.group
- Method group name.method
- Method name.static void initialize(int num)
num
- Number of threads in the thread pool.public static boolean interruptSession(boolean request, int operationId, int[] pendingReplies)
request
- The remote operation type. true
for request.operationId
- The remote operation ID.pendingReplies
- The list of remote outstanding replies.true
if the interrupt request was successfully
delivered.static boolean isRunning()
true
if this class is running.static Dispatcher getInstance()
null
if this class
has not yet been initialized.void processInbound(InboundRequest request, boolean set, NetResource nr) throws RestrictedUseException, java.lang.IllegalAccessException, java.lang.NoSuchMethodException, java.lang.InterruptedException
request
- The inbound request to process. Cannot be null
.set
- Override the current thread's security context based on the
context associated with the queue.nr
- The network resource security plugin to check for access
rights. Can be null
on the client or if
network entry point protection is disabled.RestrictedUseException
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
java.lang.InterruptedException
void dispatch()
Dispatcher
class. It retrieves requests one by one from InboundQueue and processes
them: locates requested method in EIR, calls it and if necessary -
prepares reply message with result or exception.static boolean lockMethods(java.lang.Class<?>[] list, java.lang.reflect.Method[][] methods, java.lang.Object modToken, boolean register)
This is the same as calling
lockMethods(classList, methods, modToken, register, true)
.
Locking a method is possible if there is no other locking in effect and there is no active session which has invoked the method.
list
- The interfaces to export. Each one will be exported using
the fully qualified interface name as the group and each
method will be exported by its name.methods
- The exported methods, grouped by defining interfaces.modToken
- When not null, it is the modification token from the original
registration call.register
- flag indicating that this is method is called when registrating
a new API, so semaphores need to be created.true
if all methods were locked.static boolean lockMethods(java.lang.Class<?>[] list, java.lang.reflect.Method[][] methods, java.lang.Object modToken, boolean register, boolean checkActive)
list
- The interfaces to export. Each one will be exported using
the fully qualified interface name as the group and each
method will be exported by its name.methods
- The exported methods, grouped by defining interfaces.modToken
- When not null, it is the modification token from the original
registration call.register
- flag indicating that this is method is called when registrating
a new API, so semaphores need to be created.checkActive
- flag indicating no lock is possible if there are active
sessions which have invoked this API.true
if all methods were locked.static void releaseMethods(java.lang.reflect.Method[][] methods)
methods
- The exported methods, grouped by defining interfaces.static void removeMethod(RoutingKey key)
key
- The routing key for exported API.private java.lang.Object trace(MethodInvoker method, java.lang.Object[] args) throws java.lang.Throwable
method
- Method to be invoked through reflection.args
- Arguments, if any, to method
.java.lang.Throwable
- if an exception/error occurs upon the invocation target,
or if any problem occurs during pre- or post-processing.private java.lang.Object addMethodImpl(RoutingKey key, java.lang.Object obj, java.lang.reflect.Method method, java.lang.Object modToken) throws java.lang.SecurityException
Method
is provided by the application. If a method with
the specified key is already present in the EIR it is only replaced if
the previously returned authentication token is given here, otherwise a
SecurityException
is thrown.
This method is an "end-point" of all other addMethod(...) methods.
key
- Routing data which is used as a key for adding the method.obj
- Reference to the instance of the object which will handle the
requests. Should be null
for static methods.method
- Instance of java.lang.reflect.Method
which will
handle requests.modToken
- The modification token retured from the original registration
call (to this same method). May be null
on the
first call for this routing key (a new token will be created
and returned in this case). If non-null
on the
first call, that object will be used as the modification token
from then on.java.lang.SecurityException
- If the caller is does not have the rights to register this
method or group OR if the caller does not have the proper
authentication token when trying to replace an existing method.private RoutingKey getRoutingKey(java.lang.String group, java.lang.String method, boolean canAdd)
RoutingKey
instance using provided method and group
names. If group or method are not registered in the EIR and creation
is not allowed then null
is returned. Otherwise the
appropriate entries are added into EIR and a valid key is generated.
This method implements proper access rights checks.
Must be synchronized as calls to this method can happen from any thread.
group
- Group name.method
- Method name.canAdd
- Is creation of new instances is allowed or not.
true
if allowed.private java.lang.Class<?> getInterface(RoutingKey key)
key
, assuming that it is an interface. If the
key
does not belong to an interface, then return
null
.key
- The key
for which its exported method needs
to be resolved.private java.lang.reflect.Method getMethod(RoutingKey key)
key
, assuming
that it is defined by an interface. If the key
does
not belong to an interface or the method can not be found in the key's
interface hierarchy, then return null
key
- The key
for which its exported method needs
to be resolved.private java.lang.String getGroupName(RoutingKey key)
key
- routing keyprivate java.lang.String getMethodName(RoutingKey key)
key
- routing keyprivate boolean lockMethodsImpl(java.lang.Class<?>[] list, java.lang.reflect.Method[][] methods, java.lang.Object modToken, boolean register, boolean checkActive)
list
- The interfaces to export. Each one will be exported using
the fully qualified interface name as the group and each
method will be exported by its name.methods
- The exported methods, grouped by defining interfaces.modToken
- When not null, it is the modification token from the original
registration call.register
- flag indicating that this is method is called when registrating
a new API, so semaphores need to be created.checkActive
- flag indicating no lock is possible if there are active
sessions which have invoked this API.true
if all methods were locked.private void releaseMethodsImpl(java.lang.reflect.Method[][] methods)
methods
- The exported methods, grouped by defining interfaces.private void releaseMethod(java.lang.reflect.Method method)
method
- The exported method.private boolean lockMethod(java.lang.reflect.Method method) throws java.lang.InterruptedException
method
- The exported method.java.lang.InterruptedException
- If thread is interrupted during semaphore acquire.private void removeMethodImpl(RoutingKey key)
key
- The routing key for exported API.