public class NonSerializableFactory extends Object implements ObjectFactory
// 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(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);
ObjectFactory,
rebind(Context, String, Object)| Constructor and Description |
|---|
NonSerializableFactory() |
| Modifier and Type | Method and Description |
|---|---|
static void |
bind(String key,
Object target)
Place an object into the NonSerializableFactory namespace for subsequent
access by getObject.
|
Object |
getObjectInstance(Object obj,
Name name,
Context nameCtx,
Hashtable env)
Transform the obj Reference bound into the JNDI namespace into the
actual non-Serializable object.
|
static Object |
lookup(Name name)
Lookup a value from the NonSerializableFactory map.
|
static Object |
lookup(String key)
Lookup a value from the NonSerializableFactory map.
|
static void |
rebind(Context ctx,
String key,
Object target)
A convenience method that simplifies the process of rebinding a
non-serializable object into a JNDI context.
|
static void |
rebind(Context ctx,
String key,
Object target,
boolean createSubcontexts)
A convenience method that simplifies the process of rebinding a
non-serializable object into a JNDI context.
|
static void |
rebind(Name name,
Object target)
A convenience method that simplifies the process of rebinding a
non-serializable object into a JNDI context.
|
static void |
rebind(Name name,
Object target,
boolean createSubcontexts)
A convenience method that simplifies the process of rebinding a
non-serializable object into a JNDI context.
|
static void |
rebind(String key,
Object target)
Place or replace an object in the NonSerializableFactory namespce
for subsequent access by getObject.
|
static void |
unbind(Name name)
Remove a binding from the NonSerializableFactory map.
|
static void |
unbind(String key)
Remove a binding from the NonSerializableFactory map.
|
public static void bind(String key, Object target) throws NameAlreadyBoundException
key - the name to bind target under. This should typically be the
name that will be used to bind target in the JNDI namespace, but it does
not have to be.target - the non-Serializable object to bind.NameAlreadyBoundException - thrown if key already exists in the
NonSerializableFactory mappublic static void rebind(String key, Object target)
key - the name to bind target under. This should typically be the
name that will be used to bind target in the JNDI namespace, but it does
not have to be.target - the non-Serializable object to bind.public static void unbind(String key) throws NameNotFoundException
key - the key into the NonSerializableFactory map to remove.NameNotFoundException - thrown if key does not exist in the
NonSerializableFactory mappublic static void unbind(Name name) throws NameNotFoundException
name - the name for the key into NonSerializableFactory map to remove.
The key is obtained as name.toString().NameNotFoundException - thrown if key does not exist in the
NonSerializableFactory mappublic static Object lookup(String key)
key - public static Object lookup(Name name)
name - public static void rebind(Context ctx, String key, Object target) throws NamingException
ctx - the JNDI context to rebind to.key - the key to use in both the NonSerializableFactory map and JNDI. It
must be a valid name for use in ctx.bind().target - the non-Serializable object to bind.NamingException - thrown on failure to rebind key into ctx.public static void rebind(Name name, Object target) throws NamingException
name - the name to use as JNDI path name. The key into the
NonSerializableFactory map is obtained from the toString() value of name.
The name parameter cannot be a 0 length name.
Any subcontexts between the root and the name must exist.target - the non-Serializable object to bind.NamingException - thrown on failure to rebind key into ctx.public static void rebind(Context ctx, String key, Object target, boolean createSubcontexts) throws NamingException
ctx - the JNDI context to rebind to.key - the key to use in both the NonSerializableFactory map and JNDI. It
must be a valid name for use in ctx.bind().target - the non-Serializable object to bind.createSubcontexts - a flag indicating if subcontexts of name should
be created if they do not already exist.NamingException - thrown on failure to rebind key into ctx.public static void rebind(Name name, Object target, boolean createSubcontexts) throws NamingException
name - the name to use as JNDI path name. The key into the
NonSerializableFactory map is obtained from the toString() value of name.
The name parameter cannot be a 0 length name.target - the non-Serializable object to bind.createSubcontexts - a flag indicating if subcontexts of name should
be created if they do not already exist.NamingException - thrown on failure to rebind key into ctx.public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable env) throws Exception
getObjectInstance in interface ObjectFactoryobj - the object bound in the JNDI namespace. This must be an implementation
of javax.naming.Reference with a javax.naming.RefAddr of type "nns" whose
content is the String key used to location the non-Serializable object in the
NonSerializableFactory map.name - ignored.nameCtx - ignored.env - ignored.ExceptionCopyright © 2015 JBoss by Red Hat. All rights reserved.