1 22 package org.jboss.ejb3; 23 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Hashtable ; 27 import java.util.Map ; 28 import javax.naming.Context ; 29 import javax.naming.Name ; 30 import javax.naming.NamingException ; 31 import javax.naming.RefAddr ; 32 import javax.naming.Reference ; 33 import javax.naming.StringRefAddr ; 34 import javax.naming.spi.ObjectFactory ; 35 import org.jboss.naming.Util; 36 37 50 public class NonSerializableFactory implements ObjectFactory 51 { 52 private static Map wrapperMap = Collections.synchronizedMap(new HashMap ()); 53 54 public static void unbind(Context ctx, String strName) throws NamingException 55 { 56 Name name = ctx.getNameParser("").parse(strName); 57 int size = name.size(); 58 String atom = name.get(size - 1); 59 Context parentCtx = Util.createSubcontext(ctx, name.getPrefix(size - 1)); 60 String key = parentCtx.getNameInNamespace() + "/" + atom; 61 wrapperMap.remove(key); 62 Util.unbind(ctx, strName); 63 64 } 65 66 public static void rebind(Context ctx, String strName, Object value) throws javax.naming.NamingException 67 { 68 Name name = ctx.getNameParser("").parse(strName); 69 int size = name.size(); 70 String atom = name.get(size - 1); 71 Context parentCtx = Util.createSubcontext(ctx, name.getPrefix(size - 1)); 72 String key = parentCtx.getNameInNamespace() + "/" + atom; 73 wrapperMap.put(key, value); 74 String className = value.getClass().getName(); 75 String factory = NonSerializableFactory.class.getName(); 76 StringRefAddr addr = new StringRefAddr ("nns", key); 77 Reference memoryRef = new Reference (className, addr, factory, null); 78 parentCtx.rebind(atom, memoryRef); 79 } 80 81 public static void bind(Context ctx, String strName, Object value) throws javax.naming.NamingException 82 { 83 Name name = ctx.getNameParser("").parse(strName); 84 int size = name.size(); 85 String atom = name.get(size - 1); 86 Context parentCtx = Util.createSubcontext(ctx, name.getPrefix(size - 1)); 87 String key = parentCtx.getNameInNamespace() + "/" + atom; 88 wrapperMap.put(key, value); 89 String className = value.getClass().getName(); 90 String factory = NonSerializableFactory.class.getName(); 91 StringRefAddr addr = new StringRefAddr ("nns", key); 92 Reference memoryRef = new Reference (className, addr, factory, null); 93 94 parentCtx.bind(atom, memoryRef); 95 } 96 97 public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable env) 98 throws Exception 99 { Reference ref = (Reference ) obj; 101 RefAddr addr = ref.get("nns"); 102 String key = (String ) addr.getContent(); 103 return wrapperMap.get(key); 104 } 105 } 107 | Popular Tags |