KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > metadata > AttributeOperationResolver


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.metadata;
23
24 import javax.management.MBeanAttributeInfo JavaDoc;
25 import javax.management.MBeanInfo JavaDoc;
26 import javax.management.MBeanOperationInfo JavaDoc;
27 import javax.management.MBeanParameterInfo JavaDoc;
28
29 /**
30  * AttributeOperationResolver is a modified TST for mapping an Integer code against attribute and operation keys.
31  *
32  * Note that this implementation was chosen to allow fast resolution of compound keys - namely the
33  * operationName and signature[] passed to an MBean's invoke() method. For consistency it also
34  * keeps track of attribute names (which are a single key), although for those a hashmap would
35  * have done just as well.
36  *
37  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
38  */

39 public class AttributeOperationResolver
40 {
41    private Node opRoot = null;
42    private Node atRoot = null;
43
44    /**
45     * Default constructor.
46     */

47    public AttributeOperationResolver()
48    {
49    }
50
51    /**
52     * Uses the AttributeInfo and OperationInfo arrays in the MBeanInfo to configure the
53     * resolver. Each attribute and operation will be assigned a code which corresponds
54     * to it's position in the info array.
55     */

56    public AttributeOperationResolver(MBeanInfo JavaDoc info)
57    {
58       this(info.getAttributes(), info.getOperations());
59    }
60
61    /**
62     * Uses the AttributeInfo and OperationInfo arrays to configure the resolver.
63     * Each attribute and operation will be assigned a code which corresponds to it's
64     * position in the info array.
65     */

66    public AttributeOperationResolver(MBeanAttributeInfo JavaDoc[] attributes, MBeanOperationInfo JavaDoc[] 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 JavaDoc(i));
72       }
73
74       int operationCount = (operations != null) ? operations.length : 0;
75       for (int i = 0; i < operationCount; i++)
76       {
77          MBeanOperationInfo JavaDoc operation = operations[i];
78          MBeanParameterInfo JavaDoc[] params = operation.getSignature();
79          String JavaDoc[] signature = new String JavaDoc[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 JavaDoc(i));
85       }
86    }
87
88    public Integer JavaDoc lookup(String JavaDoc actionName, String JavaDoc[] signature)
89    {
90       String JavaDoc 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 JavaDoc 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 JavaDoc lookup(String JavaDoc attrName)
133    {
134       int attrh = attrName.hashCode();
135
136       Node node = atRoot;
137       Integer JavaDoc 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 JavaDoc mname, String JavaDoc[] signature, Integer JavaDoc 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 JavaDoc attrName, Integer JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc val = null;
277
278       public Node hiKid = null;
279       public Node loKid = null;
280       public Node[] eqKid = null;
281
282       public Integer JavaDoc code = null;
283    }
284 }
285
Popular Tags