KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > monitor > JMXMonitorBase


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.management.monitor;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.Attribute JavaDoc;
32 import javax.management.MBeanAttributeInfo JavaDoc;
33 import javax.management.MBeanException JavaDoc;
34 import javax.management.ReflectionException JavaDoc;
35 import javax.management.AttributeNotFoundException JavaDoc;
36 import javax.management.InvalidAttributeValueException JavaDoc;
37 import javax.management.monitor.Monitor JavaDoc;
38
39 import com.sun.appserv.management.base.XTypes;
40 import com.sun.appserv.management.base.AMX;
41 import com.sun.appserv.management.base.Util;
42 import com.sun.appserv.management.monitor.JMXMonitorMgr;
43
44 import com.sun.enterprise.management.support.AMXImplBase;
45 import com.sun.enterprise.management.support.Delegate;
46
47 import com.sun.appserv.management.util.misc.ClassUtil;
48 import com.sun.appserv.management.util.jmx.JMXUtil;
49
50
51 /**
52     Implementation of javax.management.monitor.StringMonitor which is an AMX.
53 */

54 public class JMXMonitorBase extends AMXImplBase
55     // implements AMXStringMonitor
56
{
57     final Monitor JavaDoc mMonitor;
58     
59     /**
60         Because we want our implementation to be an AMX, we will extend appropriately,
61         but this means that we must delegate javax.management.StringMonitor functionality
62         to an instance of that class.
63      */

64
65         protected
66     JMXMonitorBase( final Monitor JavaDoc mon )
67     {
68         mMonitor = mon;
69     }
70     
71     
72     
73         public String JavaDoc
74     getGroup()
75     {
76         return( AMX.GROUP_UTILITY );
77     }
78     
79     
80         protected Object JavaDoc
81     getAttributeManually( String JavaDoc name )
82         throws AttributeNotFoundException JavaDoc
83     {
84         final MBeanAttributeInfo JavaDoc attrInfo = (MBeanAttributeInfo JavaDoc)getAttributeInfos().get( name );
85         assert( attrInfo != null ); // getAttributeManually() should not have been called otherwise
86

87         final String JavaDoc prefix = attrInfo.isIs() ? JMXUtil.IS : JMXUtil.GET;
88         final String JavaDoc operationName = prefix + name;
89         
90         Object JavaDoc result = null;
91         try
92         {
93             result = invokeManually( operationName, null, null );
94         }
95         catch( Exception JavaDoc e )
96         {
97             throw new AttributeNotFoundException JavaDoc( name );
98         }
99         
100         return( result );
101     }
102     
103         protected void
104     setAttributeManually( final Attribute JavaDoc attr )
105         throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc
106     {
107         final String JavaDoc operationName = JMXUtil.SET + attr.getName();
108         final MBeanAttributeInfo JavaDoc attrInfo = (MBeanAttributeInfo JavaDoc)getAttributeInfos().get( attr.getName() );
109         
110         Object JavaDoc result = null;
111         try
112         {
113             final Object JavaDoc value = attr.getValue();
114             // won't work to get the class from the value; must use MBeanInfo
115
final Class JavaDoc theClass = ClassUtil.getClassFromName( attrInfo.getType() );
116             
117             result = invokeSig( operationName,
118                 new Object JavaDoc[] { value }, new Class JavaDoc[] { theClass } );
119             assert( result == null );
120         }
121         catch( Exception JavaDoc e )
122         {
123             throw new AttributeNotFoundException JavaDoc( attr.getName() );
124         }
125     }
126     
127         private Object JavaDoc
128     invokeSig(
129         String JavaDoc operationName,
130         Object JavaDoc[] args,
131         Class JavaDoc[] sig )
132         throws MBeanException JavaDoc, ReflectionException JavaDoc, NoSuchMethodException JavaDoc
133     {
134         Object JavaDoc result = null;
135         
136         try
137         {
138             final Method JavaDoc m = mMonitor.getClass().getMethod( operationName, sig );
139             
140             result = m.invoke( mMonitor, args );
141         }
142         catch( Exception JavaDoc e )
143         {
144             throw new RuntimeException JavaDoc( e );
145         }
146
147         return( result );
148     }
149     
150     
151         protected Object JavaDoc
152     invokeManually(
153         String JavaDoc operationName,
154         Object JavaDoc[] args,
155         String JavaDoc[] types )
156         throws MBeanException JavaDoc, ReflectionException JavaDoc, NoSuchMethodException JavaDoc
157     {
158         Object JavaDoc result = null;
159         
160         try
161         {
162             final Class JavaDoc[] sig = ClassUtil.signatureFromClassnames( types );
163             
164             result = invokeSig( operationName, args, sig );
165         }
166         catch( Exception JavaDoc e )
167         {
168             e.printStackTrace();
169             throw new RuntimeException JavaDoc( e );
170         }
171
172         return( result );
173     }
174     
175         public void
176     preRegisterDone()
177         throws Exception JavaDoc
178     {
179         final ObjectName JavaDoc x = mMonitor.preRegister( getMBeanServer(), getObjectName() );
180     }
181     
182         public void
183     postRegisterHook( Boolean JavaDoc registrationDone )
184     {
185         super.postRegisterHook( registrationDone );
186         
187         mMonitor.postRegister( registrationDone );
188     }
189     
190         public void
191     preDeregisterHook()
192     {
193         super.preDeregisterHook( );
194         
195         try
196         {
197             mMonitor.preDeregister( );
198         }
199         catch( Exception JavaDoc e )
200         {
201             throw new RuntimeException JavaDoc( e );
202         }
203     }
204     
205         public void
206     postDeregisterHook()
207     {
208         super.postDeregisterHook( );
209         
210         mMonitor.postDeregister( );
211     }
212     
213     
214     
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228
Popular Tags