public final class ServerContext extends Object
ServerContext
class contains static methods to
execute some action within an appropriate server context for the
current thread and to obtain the server context for the current thread.
For example, an Exporter
implementation may supply context
information for incoming calls dispatched to its exported remote objects
via the ServerContext.doWithServerContext
method.
ClientHost
,
ClientSubject
Modifier and Type | Class and Description |
---|---|
static interface |
ServerContext.Spi
Defines the interface for server context providers, which are
consulted by
getServerContext() if no server context
has been explicitly associated with the current thread via a previous
call to doWithServerContext(java.lang.Runnable, java.util.Collection) . |
Modifier and Type | Method and Description |
---|---|
static void |
doWithServerContext(Runnable runnable,
Collection context)
Sets the server context for the current thread to the supplied
context collection and invokes the run method
of the supplied runnable object. |
static Collection |
getServerContext()
Returns the server context collection for the current thread.
|
static Object |
getServerContextElement(Class type)
Returns the first element in the current server context collection
(obtained by calling
ServerContext.getServerContext ) that is an instance of the given
type type . |
public static void doWithServerContext(Runnable runnable, Collection context)
context
collection and invokes the run
method
of the supplied runnable
object. When this method
returns, the thread's server context is unset.
If a server context is currently set for the current thread,
that server context cannot be reset; that is, a server context
cannot be overwritten or nested. If a server context is already
set for the current thread, an IllegalStateException
is
thrown.
runnable
- the action to perform in the server contextcontext
- the context to setNullPointerException
- if context
or
runnable
is null
IllegalStateException
- if the context is already
set for this threadgetServerContextElement(java.lang.Class)
public static Collection getServerContext() throws ServerNotActiveException
ServerContext.doWithServerContext
, then an ordered list of
providers (obtained as specified below) implementing the ServerContext.Spi
interface is consulted. ServerContext.Spi.getServerContext()
is called on each provider in
turn; the first non-null
return value is returned by
this method. If no provider is able to supply a server context
collection, then a ServerNotActiveException
is thrown.
The list of server context providers is obtained as follows. For
each resource named
META-INF/services/net.jini.export.ServerContext$Spi
that is visible to the system class loader, the contents of the
resource are parsed as UTF-8 text to produce a list of class names.
The resource must contain a list of fully-qualified class names, one per
line. Space and tab characters surrounding each name, as well as blank
lines, are ignored. The comment character is '#'; all
characters on each line starting with the first comment character are
ignored. Each class name (that is not a duplicate of any previous class
name) is loaded through the system class loader, and the resulting
class must be assignable to ServerContext.Spi
and have a public
no-argument constructor. The constructor is invoked to create a fallback
context provider instance. An implementation is permitted to search for
provider instances eagerly (i.e., upon startup), and cache the
verifier instances rather than recreating them on every call.
The contents of the collection are determined by the caller of
ServerContext.doWithServerContext
. The
context information available from a given element of the collection
is determined by that element's type. Examples of types that a
given element might implement include ClientHost
and ClientSubject
.
The order of the elements in the collection is insignificant. The collection may be empty.
The caller of this method cannot assume that the returned collection is modifiable.
ServerNotActiveException
- if no context is set for the
current threadpublic static Object getServerContextElement(Class type) throws ServerNotActiveException
ServerContext.getServerContext
) that is an instance of the given
type type
. If no element in the collection is an
instance of the type type
, then null
is
returned.type
- the type of the elementtype
or null
ServerNotActiveException
- if no server context is set for
the current thread