KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > mxbean > CompositeDataInvocationHandler


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.mxbean;
23
24 import java.lang.reflect.InvocationHandler JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Proxy JavaDoc;
27 import java.lang.reflect.Type JavaDoc;
28
29 import javax.management.openmbean.CompositeData JavaDoc;
30 import javax.management.openmbean.InvalidKeyException JavaDoc;
31
32 import org.jboss.util.UnreachableStatementException;
33
34 /**
35  * CompositeDataInvocationHandler.
36  *
37  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
38  * @version $Revision: 1.1 $
39  */

40 public class CompositeDataInvocationHandler implements InvocationHandler JavaDoc
41 {
42    /** The composite data */
43    private CompositeData JavaDoc compositeData;
44    
45    /**
46     * Create a new CompositeDataInvocationHandler.
47     *
48     * @param compositeData
49     */

50    public CompositeDataInvocationHandler(CompositeData JavaDoc compositeData)
51    {
52       if (compositeData == null)
53          throw new IllegalArgumentException JavaDoc("Null compositeData");
54       this.compositeData = compositeData;
55    }
56
57    /**
58     * Get the compositeData.
59     *
60     * @return the compositeData.
61     */

62    public CompositeData JavaDoc getCompositeData()
63    {
64       return compositeData;
65    }
66    
67    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
68    {
69       if (Object JavaDoc.class.equals(method.getDeclaringClass()))
70          return handleObjectInvocation(method.getName(), args);
71       
72       Object JavaDoc value = getValue(method);
73
74       Type JavaDoc returnType = method.getGenericReturnType();
75       return MXBeanUtils.reconstruct(returnType, value, method);
76    }
77    
78    private Object JavaDoc getValue(Method JavaDoc method)
79    {
80       String JavaDoc key = MXBeanUtils.getCompositeDataKey(method);
81       if (key == null)
82          throw new IllegalArgumentException JavaDoc("Unsupported method '" + method + "'; it must be a property getter.");
83       try
84       {
85          return compositeData.get(key);
86       }
87       catch (InvalidKeyException JavaDoc e)
88       {
89          throw new IllegalArgumentException JavaDoc("Unsupported method '" + method + "'; it must be a property getter for one of the item names of the composite data: " + compositeData, e);
90       }
91    }
92    
93    private Object JavaDoc handleObjectInvocation(String JavaDoc name, Object JavaDoc[] args) throws Throwable JavaDoc
94    {
95       if ("equals".equals(name))
96       {
97          Object JavaDoc object = args[0];
98          if (object == null || object instanceof Proxy JavaDoc == false)
99             return false;
100          InvocationHandler JavaDoc handler = Proxy.getInvocationHandler(object);
101          if (handler == this)
102             return true;
103          if (handler == null || handler instanceof CompositeDataInvocationHandler == false)
104             return false;
105          
106          CompositeDataInvocationHandler other = (CompositeDataInvocationHandler) handler;
107          return getCompositeData().equals(other.getCompositeData());
108       }
109       else if ("hashCode".equals(name))
110          return getCompositeData().hashCode();
111       else if ("toString".equals(name))
112          return getCompositeData().toString();
113       throw new UnreachableStatementException();
114    }
115 }
116
Popular Tags