1 10 package org.mmbase.bridge.remote.util; 11 12 import java.util.*; 13 import org.mmbase.bridge.*; 14 15 import org.mmbase.util.logging.Logging; 16 import org.mmbase.util.logging.Logger; 17 18 23 public class StubToLocalMapper { 24 static private final Logger log = Logging.getLoggerInstance(StubToLocalMapper.class); 25 28 private static final Map hash = new Hashtable(); 29 private static final Map refcount = new Hashtable(); 30 31 36 public static String add(Object object) { 37 if (object != null) { 38 39 String mapperCode = null; 40 if (object instanceof Node) { 41 Node node = (Node)object; 42 mapperCode = "node:" + node.getNodeManager().getName() + "->" + node.getNumber(); 43 } else if (object instanceof Cloud) { 44 Cloud cloud = (Cloud)object; 45 mapperCode = "cloud:" + cloud.getName(); 46 } else if (object instanceof Relation) { 47 Relation rel = (Relation)object; 48 mapperCode = "relation:" + rel.getNodeManager().getName() + "->" + rel.getNumber(); 49 } else if (object instanceof RelationManager) { 50 RelationManager relationManager = (RelationManager)object; 51 mapperCode = "relationmanager:" + relationManager.getName() + "->" + relationManager.getNumber(); 52 } else if (object instanceof NodeManager) { 53 NodeManager nodeManager = (NodeManager)object; 54 mapperCode = "nodemanager:" + nodeManager.getName() + "->" + nodeManager.getNumber(); 55 } else { 56 mapperCode = "" + object; 57 } 58 59 if (hash.get(mapperCode) != null) { 67 if (hash.get(mapperCode) != object) { 69 for (int counter = 1; true; counter++) { 70 String newMapperCode = mapperCode + "{" + counter + "}"; 71 if (!hash.containsKey(newMapperCode) || hash.get(newMapperCode) == object) { 72 mapperCode = newMapperCode; 73 break; 74 } 75 } 76 } 77 } 78 79 log.debug("add=(" + mapperCode + ")"); 80 int rcount = increaseRefCount(mapperCode); 81 if (rcount == 1) { 82 hash.put(mapperCode, object); 83 log.debug("add=(" + mapperCode + ")"); 84 } else { 85 log.debug("increace=(" + mapperCode + ")(" + rcount + ")"); 86 } 87 return mapperCode; 88 } 89 return ""; 90 } 91 92 97 private static int increaseRefCount(String mapperCode) { 98 Integer count = (Integer )refcount.get(mapperCode); 99 if (count == null) { 100 refcount.put(mapperCode, new Integer (1)); 101 return 1; 102 } else { 103 refcount.put(mapperCode, new Integer (count.intValue() + 1)); 104 return count.intValue() + 1; 105 } 106 107 } 108 109 114 private static int decreaseRefCount(String mapperCode) { 115 Integer count = (Integer )refcount.get(mapperCode); 116 if (count == null) { 117 log.warn("refcount entry not found for(" + mapperCode + ")"); 118 return 0; 119 } 120 int c = count.intValue(); 121 122 if (c == 1) { 123 refcount.remove(mapperCode); 124 return 0; 125 } else { 126 refcount.put(mapperCode, new Integer (c - 1)); 127 return c - 1; 128 } 129 } 130 131 136 public static Object get(String mapperCode) { 137 log.debug("access=(" + mapperCode + ")"); 138 Object o = hash.get(mapperCode); 139 return o; 140 } 141 142 148 public static boolean remove(String mapperCode) { 149 if (mapperCode != null && !mapperCode.equals("")) { 150 int rcount = decreaseRefCount(mapperCode); 151 if (rcount == 0) { 152 log.debug("remove=(" + mapperCode + ")"); 153 hash.remove(mapperCode); 154 return true; 155 } else { 156 log.debug("keep=(" + mapperCode + ") refcount=(" + rcount + ")"); 157 return false; 158 } 159 } 160 return false; 161 } 162 } 163 | Popular Tags |