This is useful if you are passing an object over an API that admits only
strings (such as
and where tricks such as
ThreadLocal do not work. The callee needs
to be on the same JVM, but other than that, the object does not need to have
any special properties. In particular, it does not need to be serializable.
First, register the object to obtain a lock box entry. Every lock box entry has a string moniker that is very difficult to guess, is unique, and is not recycled. Pass that moniker to the callee, and from that moniker the callee can retrieve the entry and with it the object.
The entry cannot be forged and cannot be copied. If you lose the entry,
you can no longer retrieve the object, and the entry will eventually be
garbage-collected. If you call
deregister(Entry), callees will no
longer be able to look up an entry from its moniker.
The same is not true of the moniker string. Having the moniker string
does not guarantee that the entry will not be removed. Therefore, the
creator who called
register(Object) and holds the entry controls
The moniker consists of the characters A..Z, a..z, 0..9, $, #, and is thus a valid (case-sensitive) identifier.
All methods are thread-safe.
|Modifier and Type||Class and Description|
Entry in a
|Constructor and Description|
Creates a LockBox.
|Modifier and Type||Method and Description|
Removes an entry from the lock box.
Retrieves an entry using its string moniker.
Adds an object to the lock box, and returns a key for it.
public LockBox.Entry register(Object o)
The object may be null. The same object may be registered multiple times; each time it is registered, a new entry with a new string moniker is generated.
o- Object to register. May be null.
public boolean deregister(LockBox.Entry entry)
It is safe to call this method multiple times.
entry- Entry to deregister
public LockBox.Entry get(String moniker)
Successive calls for the same moniker do not necessarily return
Entry object, but those entries'
LockBox.Entry.getValue() will nevertheless return the same
moniker- Moniker of the lock box entry