A utility class that allows one to bind a non-serializable object into a
local JNDI context. The binding will only be valid for the lifetime of the
VM in which the JNDI InitialContext lives. An example usage code snippet is:
// The non-Serializable object to bind
Object nonserializable = ...;
// An arbitrary key to use in the StringRefAddr. The best key is the jndi
// name that the object will be bound under.
String key = ...;
// This places nonserializable into the NonSerializableFactory hashmap under key
NonSerializableFactory.rebind(key, nonserializable);
Context ctx = new InitialContext();
// Bind a reference to nonserializable using NonSerializableFactory as the ObjectFactory
String className = nonserializable.getClass().getName();
String factory = NonSerializableFactory.class.getName();
StringRefAddr addr = new StringRefAddr("nns", key);
Reference memoryRef = new Reference(className, addr, factory, null);
ctx.rebind(key, memoryRef);
Or you can use the
#rebind(javax.naming.Context,String,Object) convenience
method to simplify the number of steps to:
Context ctx = new InitialContext();
// The non-Serializable object to bind
Object nonserializable = ...;
// The jndiName that the object will be bound into ctx with
String jndiName = ...;
// This places nonserializable into the NonSerializableFactory hashmap under key
NonSerializableFactory.rebind(ctx, jndiName, nonserializable);
To unbind the object, use the following code snippet:
new InitialContext().unbind(key);
NonSerializableFactory.unbind(key);