Here is an example of binding a non-Serializable object into the jboss JNDI namespace.
I have a MBean that binds a JNDI implementation of a attributed filesystem into the
jboss app server at startup. The DirContext/Context objects in this implementation are
not serializable. Using Reference, RefAddr & ObjectFactory you can put basically
anything into a JNDI namespace. Below are the relevant code fragments. The trick here
is to first associated the projectContext object with the inmemory Hashtable of the
NonSerializableWrapper class. I then bind a Reference at the location where I want
to access the projectContext object. When the lookup against this location is 
performed:
   // Test a lookup against the projectContext
   DirContext dirContext = (DirContext) ctx.lookup("inmemory:/test");

The jboss JNDI provider resolves the reference to the object using the ObjectFactory
associated with the Reference. There is a good tutorial on all of this on the javasoft 
site:
http://java.sun.com/products/jndi/tutorial/index.html


public class ProjectDirContext extends NotificationBroadcasterSupport
 implements MBeanRegistration, NotificationBroadcaster, ProjectDirContextMBean
{

 public static class NonSerializableWrapper implements ObjectFactory
 {
  private static Hashtable wrapperTable = new Hashtable();

  public static void add(String key, Object target)
  {
   wrapperTable.put(key, target);
  }

  public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable 
env) throws Exception
  { // Use the last name component as the key atom
   Reference ref = (Reference) obj;
   RefAddr addr = ref.get("Hashtable.key");
   String key = (String) addr.getContent();
   Object target = wrapperTable.get(key);
System.out.println("getObjectInstance, key="+key+",name="+name+" -> "+target);
   return target;
  }
 }

 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception
 {
  try
  { // Create the filesystem root context
   env.put(Context.INITIAL_CONTEXT_FACTORY, 
"com.dscape.projects.documents.jndi.FSDirInitialContextFactory");
   env.put(Context.PROVIDER_URL, "file:///temp/project_repository/NewProject/");
   InitialDirContext projectContext = new InitialDirContext(env);
   BasicAttributes attributes = new BasicAttributes();
   attributes.put("elementType", "ProjectFolder");
   projectContext.createSubcontext("test", attributes);
   NonSerializableWrapper.add("ProjectDirContext", projectContext);

   Context ctx = (Context) new InitialContext();
   // Bind a reference to the projectContext using NonSerializableWrapper as the 
ObjectFactory
   String className = "javax.naming.directory.DirContext";
   String factory = NonSerializableWrapper.class.getName();
   StringRefAddr addr = new StringRefAddr("Hashtable.key", "ProjectDirContext");
   Reference memoryRef = new Reference(className, addr, factory, null);
   ctx.rebind("inmemory:", memoryRef);
   // Test a lookup against the projectContext
   DirContext dirContext = (DirContext) ctx.lookup("inmemory:/test");
  }
  catch(NamingException e)
  {
   throw new MBeanRegistrationException(e,
    "Failed to bind project DirContext into JNDI");
  }
  return new ObjectName(OBJECT_NAME);
 }
...
}



----- Original Message ----- 
From: "marc fleury" <[EMAIL PROTECTED]>
To: "jBoss" <[EMAIL PROTECTED]>
Sent: Tuesday, November 21, 2000 6:24 PM
Subject: RE: [jBoss-User] Sharing a single object instance with JNDI


> We store serializable object by default.  Yes it is a feature we might add.
> Also I believe that Rickard coded a VM only namespace under java: (?
> rickard)
> 
> marc
> 
> 




--
--------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Problems?:           [EMAIL PROTECTED]

Reply via email to