1 22 package org.jboss.util.naming; 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.InitialContext ; 30 import javax.naming.Name ; 31 import javax.naming.NameAlreadyBoundException ; 32 import javax.naming.NameNotFoundException ; 33 import javax.naming.NamingException ; 34 import javax.naming.Reference ; 35 import javax.naming.RefAddr ; 36 import javax.naming.StringRefAddr ; 37 import javax.naming.spi.ObjectFactory ; 38 39 88 public class NonSerializableFactory implements ObjectFactory 89 { 90 private static Map wrapperMap = Collections.synchronizedMap(new HashMap ()); 91 92 102 public static synchronized void bind(String key, Object target) throws NameAlreadyBoundException 103 { 104 if( wrapperMap.containsKey(key) == true ) 105 throw new NameAlreadyBoundException (key+" already exists in the NonSerializableFactory map"); 106 wrapperMap.put(key, target); 107 } 108 117 public static void rebind(String key, Object target) 118 { 119 wrapperMap.put(key, target); 120 } 121 122 128 public static void unbind(String key) throws NameNotFoundException 129 { 130 if( wrapperMap.remove(key) == null ) 131 throw new NameNotFoundException (key+" was not found in the NonSerializableFactory map"); 132 } 133 140 public static void unbind(Name name) throws NameNotFoundException 141 { 142 String key = name.toString(); 143 if( wrapperMap.remove(key) == null ) 144 throw new NameNotFoundException (key+" was not found in the NonSerializableFactory map"); 145 } 146 147 150 public static Object lookup(String key) 151 { 152 Object value = wrapperMap.get(key); 153 return value; 154 } 155 158 public static Object lookup(Name name) 159 { 160 String key = name.toString(); 161 Object value = wrapperMap.get(key); 162 return value; 163 } 164 165 174 public static synchronized void rebind(Context ctx, String key, Object target) throws NamingException 175 { 176 NonSerializableFactory.rebind(key, target); 177 String className = target.getClass().getName(); 179 String factory = NonSerializableFactory.class.getName(); 180 StringRefAddr addr = new StringRefAddr ("nns", key); 181 Reference memoryRef = new Reference (className, addr, factory, null); 182 ctx.rebind(key, memoryRef); 183 } 184 185 196 public static synchronized void rebind(Name name, Object target) throws NamingException 197 { 198 rebind(name, target, false); 199 } 200 201 213 public static synchronized void rebind(Name name, Object target, 214 boolean createSubcontexts) throws NamingException 215 { 216 String key = name.toString(); 217 InitialContext ctx = new InitialContext (); 218 if( createSubcontexts == true && name.size() > 1 ) 219 { 220 int size = name.size() - 1; 221 Util.createSubcontext(ctx, name.getPrefix(size)); 222 } 223 rebind(ctx, key, target); 224 } 225 226 241 public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable env) 242 throws Exception 243 { Reference ref = (Reference ) obj; 245 RefAddr addr = ref.get("nns"); 246 String key = (String ) addr.getContent(); 247 Object target = wrapperMap.get(key); 248 return target; 249 } 250 } 252 | Popular Tags |