1 7 8 package org.jboss.media.util.registry; 9 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 15 import org.jboss.logging.Logger; 16 17 23 public class MapRegistry implements Registry 24 { 25 private static final Logger log = Logger.getLogger(MapRegistry.class); 26 27 private static final Map entries = 28 Collections.synchronizedMap(new HashMap ()); 29 30 public MapRegistry() 31 { 32 } 33 34 public MapRegistry(Map initialEntries) throws ObjectAlreadyBoundException 35 { 36 this(); 37 38 Iterator keys = initialEntries.keySet().iterator(); 39 40 while (keys.hasNext()) 41 { 42 Object key = keys.next(); 43 Object value = initialEntries.get(key); 44 bind(key, value); 45 } 46 } 47 48 public void bind(final Object key, final Object value) 49 throws ObjectAlreadyBoundException 50 { 51 if (key == null || value == null) 52 { 53 throw new NullPointerException (); 54 } 55 56 if (entries.containsKey(key)) 57 { 58 throw new ObjectAlreadyBoundException(); 59 } 60 61 entries.put(key, value); 62 63 if (log.isTraceEnabled()) 64 log.trace("bound " + key + "=" + value); 65 } 66 67 public void rebind(final Object key, final Object value) 68 { 69 if (key == null || value == null) 70 { 71 throw new NullPointerException (); 72 } 73 74 entries.put(key, value); 75 76 if (log.isTraceEnabled()) 77 log.trace("rebind " + key + "=" + value); 78 } 79 80 public Object unbind(final Object key) throws ObjectNotBoundException 81 { 82 if (key == null) 83 { 84 throw new NullPointerException (); 85 } 86 87 if (!entries.containsKey(key)) 88 { 89 throw new ObjectNotBoundException(); 90 } 91 92 Object object = entries.remove(key); 93 94 if (log.isTraceEnabled()) 95 log.trace("unbound " + key + "=" + object); 96 97 return object; 98 } 99 100 public Object lookup(final Object key) throws ObjectNotBoundException 101 { 102 if (!entries.containsKey(key)) 103 { 104 throw new ObjectNotBoundException(); 105 } 106 107 Object object = entries.get(key); 108 109 if (log.isTraceEnabled()) 110 log.trace("lookup " + key + "=" + object); 111 112 return object; 113 } 114 115 public Iterator keyIterator() 116 { 117 synchronized (entries) 118 { 119 return entries.keySet().iterator(); 120 } 121 } 122 } 123 | Popular Tags |