KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > examples > scandir > TestUtils


1 /*
2  * TestUtils.java
3  *
4  * Created on July 12, 2006, 8:14 PM
5  *
6  * @(#)TestUtils.java 1.2 06/08/02
7  *
8  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * -Redistribution of source code must retain the above copyright notice, this
14  * list of conditions and the following disclaimer.
15  *
16  * -Redistribution in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
21  * be used to endorse or promote products derived from this software without
22  * specific prior written permission.
23  *
24  * This software is provided "AS IS," without a warranty of any kind. ALL
25  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
26  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
27  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
28  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
29  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
30  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
31  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
32  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
33  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
34  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
35  *
36  * You acknowledge that this software is not designed, licensed or intended
37  * for use in the design, construction, operation or maintenance of any
38  * nuclear facility.
39  */

40
41 package com.sun.jmx.examples.scandir;
42
43 import java.lang.reflect.InvocationHandler JavaDoc;
44 import java.lang.reflect.Proxy JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46 import javax.management.JMX JavaDoc;
47 import javax.management.MBeanServerConnection JavaDoc;
48 import javax.management.MBeanServerInvocationHandler JavaDoc;
49 import javax.management.NotificationEmitter JavaDoc;
50 import javax.management.ObjectName JavaDoc;
51
52 /**
53  * A utility class defining static methods used by our tests.
54  *
55  * @author Sun Microsystems, 2006 - All rights reserved.
56  */

57 public class TestUtils {
58     
59     /**
60      * A logger for this class.
61      **/

62     private static final Logger JavaDoc LOG =
63             Logger.getLogger(TestUtils.class.getName());
64     
65     /** Creates a new instance of TestUtils */
66     private TestUtils() {
67     }
68     
69     /**
70      * Returns the ObjectName of the MBean that a proxy object
71      * is proxying.
72      **/

73     public static ObjectName JavaDoc getObjectName(Object JavaDoc proxy) {
74         if (!(proxy instanceof Proxy JavaDoc))
75             throw new IllegalArgumentException JavaDoc("not a "+Proxy JavaDoc.class.getName());
76         final Proxy JavaDoc p = (Proxy JavaDoc) proxy;
77         final InvocationHandler JavaDoc handler =
78                 Proxy.getInvocationHandler(proxy);
79         if (handler instanceof MBeanServerInvocationHandler JavaDoc)
80             return ((MBeanServerInvocationHandler JavaDoc)handler).getObjectName();
81         throw new IllegalArgumentException JavaDoc("not a JMX Proxy");
82     }
83     
84     /**
85      * Transfroms a proxy implementing T in a proxy implementing T plus
86      * NotificationEmitter
87      *
88      **/

89     public static <T> T makeNotificationEmitter(T proxy,
90                         Class JavaDoc<T> mbeanInterface) {
91         if (proxy instanceof NotificationEmitter JavaDoc)
92             return proxy;
93         if (proxy == null) return null;
94         if (!(proxy instanceof Proxy JavaDoc))
95             throw new IllegalArgumentException JavaDoc("not a "+Proxy JavaDoc.class.getName());
96         final Proxy JavaDoc p = (Proxy JavaDoc) proxy;
97         final InvocationHandler JavaDoc handler =
98                 Proxy.getInvocationHandler(proxy);
99         if (!(handler instanceof MBeanServerInvocationHandler JavaDoc))
100             throw new IllegalArgumentException JavaDoc("not a JMX Proxy");
101         final MBeanServerInvocationHandler JavaDoc h =
102                 (MBeanServerInvocationHandler JavaDoc)handler;
103         final ObjectName JavaDoc name = h.getObjectName();
104         final MBeanServerConnection JavaDoc mbs = h.getMBeanServerConnection();
105         final boolean isMXBean = h.isMXBean();
106         final T newProxy;
107         if (isMXBean)
108             newProxy = JMX.newMXBeanProxy(mbs,name,mbeanInterface,true);
109         else
110             newProxy = JMX.newMBeanProxy(mbs,name,mbeanInterface,true);
111         return newProxy;
112     }
113     
114 }
115
Popular Tags