1 18 package org.apache.beehive.netui.pageflow.requeststate; 19 20 import javax.servlet.http.HttpSession ; 21 import java.lang.ref.WeakReference ; 22 import java.util.HashMap ; 23 24 27 public class NameService 28 { 29 private static final String NAME_SERVICE = "netui.nameService"; 30 31 private HashMap _nameMap; 32 private int _nextValue; 33 34 37 private NameService() { 38 _nameMap = new HashMap (); 39 _nextValue = 0; 40 } 41 42 48 public static NameService instance(HttpSession session) 49 { 50 synchronized (session) { 53 NameService nameService = (NameService) session.getAttribute(NAME_SERVICE); 54 if (nameService == null) { 55 nameService = new NameService(); 56 session.setAttribute(NAME_SERVICE,nameService); 57 } 58 assert(nameService != null) : "Found invalid null name service"; 59 return nameService; 60 } 61 } 62 63 71 public synchronized void nameObject(String namePrefix, INameable object) 72 { 73 String name = namePrefix + Integer.toString(_nextValue++); 74 object.setObjectName(name); 75 } 76 77 82 public void debugSetNameIntValue(int val) { 83 _nextValue = val; 84 } 85 86 90 public void put(INameable object) { 91 if (object == null) 92 throw new IllegalStateException ("object must not be null"); 93 String name = object.getObjectName(); 94 if (name == null) 95 throw new IllegalStateException ("object has not been named"); 96 97 _nameMap.put(name,new WeakReference (object)); 98 } 99 100 105 public INameable get(String name) { 106 if (name == null) 107 throw new IllegalStateException ("name must not be null"); 108 WeakReference wr = (WeakReference ) _nameMap.get(name); 109 if (wr == null) 110 return null; 111 INameable object = (INameable) wr.get(); 112 if (object == null) { 113 _nameMap.remove(name); 114 } 115 return object; 116 } 117 } 118 | Popular Tags |