1 22 package org.jboss.mx.metadata; 23 24 import javax.management.MBeanAttributeInfo ; 25 import javax.management.MBeanInfo ; 26 import javax.management.MBeanOperationInfo ; 27 import javax.management.MBeanParameterInfo ; 28 29 39 public class AttributeOperationResolver 40 { 41 private Node opRoot = null; 42 private Node atRoot = null; 43 44 47 public AttributeOperationResolver() 48 { 49 } 50 51 56 public AttributeOperationResolver(MBeanInfo info) 57 { 58 this(info.getAttributes(), info.getOperations()); 59 } 60 61 66 public AttributeOperationResolver(MBeanAttributeInfo [] attributes, MBeanOperationInfo [] operations) 67 { 68 int attributeCount = (attributes != null) ? attributes.length : 0; 69 for (int i = 0; i < attributeCount; i++) 70 { 71 store(attributes[i].getName(), new Integer (i)); 72 } 73 74 int operationCount = (operations != null) ? operations.length : 0; 75 for (int i = 0; i < operationCount; i++) 76 { 77 MBeanOperationInfo operation = operations[i]; 78 MBeanParameterInfo [] params = operation.getSignature(); 79 String [] signature = new String [params.length]; 80 for (int j = 0; j < signature.length; j++) 81 { 82 signature[j] = params[j].getType(); 83 } 84 store(operation.getName(), signature, new Integer (i)); 85 } 86 } 87 88 public Integer lookup(String actionName, String [] signature) 89 { 90 String word = actionName; 91 int wordh = word.hashCode(); 92 int wordpos = -1; 93 int maxword = (signature != null) ? signature.length : 0; 94 95 Node node = opRoot; 96 Integer rval = null; 97 OUTER_NODE: while (node != null) 98 { 99 if (wordh < node.hash) 100 { 101 node = node.loKid; 102 } 103 else if (wordh > node.hash) 104 { 105 node = node.hiKid; 106 } 107 else 108 { 109 for (int i = node.eqKid.length - 1; i > -1; i--) 110 { 111 if (word.equals(node.eqKid[i].val)) 112 { 113 if (++wordpos < maxword) 114 { 115 node = node.eqKid[i]; 116 word = signature[wordpos]; 117 wordh = word.hashCode(); 118 continue OUTER_NODE; 119 } 120 else 121 { 122 rval = node.eqKid[i].code; 123 break OUTER_NODE; 124 } 125 } 126 } 127 } 128 } 129 return rval; 130 } 131 132 public Integer lookup(String attrName) 133 { 134 int attrh = attrName.hashCode(); 135 136 Node node = atRoot; 137 Integer rval = null; 138 OUTER_NODE: while (node != null) 139 { 140 if (attrh < node.hash) 141 { 142 node = node.loKid; 143 } 144 else if (attrh > node.hash) 145 { 146 node = node.hiKid; 147 } 148 else 149 { 150 for (int i = node.eqKid.length - 1; i > -1; i--) 151 { 152 if (attrName.equals(node.eqKid[i].val)) 153 { 154 rval = node.eqKid[i].code; 155 break OUTER_NODE; 156 } 157 } 158 } 159 } 160 return rval; 161 } 162 163 public void store(String mname, String [] signature, Integer code) 164 { 165 if (opRoot == null) 166 { 167 opRoot = createNode(mname); 168 createValueNode(opRoot, mname); 169 } 170 171 int word = -1; 172 int maxword = (signature != null) ? signature.length : 0; 173 174 Node current = createOrGetNode(opRoot, mname); 175 while (++word < maxword) 176 { 177 current = createOrGetNode(current, signature[word]); 178 } 179 180 current.code = code; 181 } 182 183 public void store(String attrName, Integer code) 184 { 185 Node current = null; 186 187 if (atRoot == null) 188 { 189 atRoot = createNode(attrName); 190 current = createValueNode(atRoot, attrName); 191 } 192 else 193 { 194 current = createOrGetNode(atRoot, attrName); 195 } 196 197 current.code = code; 198 } 199 200 protected Node createNode(String key) 201 { 202 Node h = new Node(); 203 h.hash = key.hashCode(); 204 h.val = key; 205 return h; 206 } 207 208 protected Node createValueNode(Node parent, String key) 209 { 210 Node h = new Node(); 211 h.val = key; 212 h.hash = key.hashCode(); 213 int insertAt = 0; 214 if (parent.eqKid == null) 215 { 216 parent.eqKid = new Node[1]; 217 } 218 else 219 { 220 Node[] old = parent.eqKid; 221 insertAt = old.length; 222 parent.eqKid = new Node[insertAt + 1]; 223 System.arraycopy(old, 0, parent.eqKid, 0, insertAt); 224 } 225 226 parent.eqKid[insertAt] = h; 227 return h; 228 } 229 230 protected Node createOrGetNode(Node parent, String key) 231 { 232 Node realParent = parent; 233 int keycode = key.hashCode(); 234 235 while (true) 236 { 237 if (keycode < realParent.hash) 238 { 239 if (realParent.loKid == null) 240 { 241 realParent.loKid = createNode(key); 242 return createValueNode(realParent.loKid, key); 243 } 244 realParent = realParent.loKid; 245 } 246 else if (keycode > realParent.hash) 247 { 248 if (realParent.hiKid == null) 249 { 250 realParent.hiKid = createNode(key); 251 return createValueNode(realParent.hiKid, key); 252 } 253 realParent = realParent.hiKid; 254 } 255 else 256 { 257 if (realParent.eqKid != null) 258 { 259 for (int i = 0; i < realParent.eqKid.length; i++) 260 { 261 if (key.equals(realParent.eqKid[i].val)) 262 { 263 return realParent.eqKid[i]; 264 } 265 } 266 } 267 return createValueNode(realParent, key); 268 } 269 } 270 } 271 272 273 public static class Node 274 { 275 public int hash = 0; 276 public String val = null; 277 278 public Node hiKid = null; 279 public Node loKid = null; 280 public Node[] eqKid = null; 281 282 public Integer code = null; 283 } 284 } 285 | Popular Tags |