KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > TestDummy


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 package com.sun.enterprise.management.support;
24
25 import java.util.Map JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import java.io.Serializable JavaDoc;
33
34 import javax.management.ObjectName JavaDoc;
35 import javax.management.MBeanServer JavaDoc;
36 import javax.management.MBeanRegistration JavaDoc;
37 import javax.management.NotificationEmitter JavaDoc;
38 import javax.management.NotificationFilter JavaDoc;
39 import javax.management.NotificationListener JavaDoc;
40 import javax.management.Notification JavaDoc;
41 import javax.management.ListenerNotFoundException JavaDoc;
42 import javax.management.NotificationBroadcasterSupport JavaDoc;
43 import javax.management.MBeanNotificationInfo JavaDoc;
44 import javax.management.AttributeNotFoundException JavaDoc;
45
46 import javax.management.Attribute JavaDoc;
47 import javax.management.AttributeList JavaDoc;
48 import javax.management.MBeanInfo JavaDoc;
49 import javax.management.MBeanAttributeInfo JavaDoc;
50 import javax.management.DynamicMBean JavaDoc;
51
52 import com.sun.appserv.management.base.Sample;
53 import com.sun.appserv.management.base.AMXDebug;
54 import com.sun.appserv.management.util.jmx.JMXUtil;
55 import com.sun.appserv.management.util.jmx.NotificationBuilder;
56 import com.sun.appserv.management.util.misc.Output;
57 import com.sun.appserv.management.base.Util;
58
59 /**
60     This MBean is used to test the CustomMBean functionality.
61     It is not an AMX MBean.
62  */

63 public final class TestDummy
64     implements TestDummyMBean, DynamicMBean JavaDoc, MBeanRegistration JavaDoc
65 {
66     private final NotificationBroadcasterSupport JavaDoc mBroadcaster;
67     
68     // all Attributes live in a Map
69
private final Map JavaDoc<String JavaDoc,Object JavaDoc> mAttributes;
70     private MBeanInfo JavaDoc mMBeanInfo;
71     private Output mDebug;
72     
73     private MBeanServer JavaDoc mServer;
74     private ObjectName JavaDoc mSelfObjectName;
75         
76     
77         public
78     TestDummy( )
79     {
80         mAttributes = Collections.synchronizedMap( new HashMap JavaDoc<String JavaDoc,Object JavaDoc>() );
81         mMBeanInfo = null;
82         
83         mDebug = AMXDebug.getInstance().getOutput( getDebugID() );
84         //AMXDebug.getInstance().setDebug( getDebugID(), true );
85

86         mBroadcaster = new NotificationBroadcasterSupport JavaDoc();
87     }
88     
89         public String JavaDoc
90     getAttr1()
91     {
92         return (String JavaDoc)mAttributes.get( "Attr1" );
93     }
94     
95         public void
96     setAttr1( final String JavaDoc value )
97     {
98         setAttribute( "Attr1", value);
99     }
100     
101         public String JavaDoc
102     getAttr2()
103     {
104         return (String JavaDoc)mAttributes.get( "Attr2" );
105     }
106     
107         public void
108     setAttr2( final String JavaDoc value )
109     {
110         setAttribute( "Attr2", value);
111     }
112     
113         private void
114     debug( final Object JavaDoc o )
115     {
116         mDebug.println( o );
117     }
118         
119         private String JavaDoc
120     getDebugID()
121     {
122         return this.getClass().getName();
123     }
124     
125         public void
126     addAttribute( final String JavaDoc name, final Object JavaDoc value )
127     {
128         if ( name == null || name.length() == 0 )
129         {
130             debug( "Illegal Attribute name: " + name );
131             throw new IllegalArgumentException JavaDoc( );
132         }
133         
134         mAttributes.put( name, value );
135         mMBeanInfo = null;
136         debug( "added Attribute: " + name );
137     }
138     
139         public void
140     removeAttribute( final String JavaDoc name )
141     {
142         mAttributes.remove( name );
143         mMBeanInfo = null;
144         debug( "removed Attribute: " + name );
145     }
146     
147         private synchronized MBeanInfo JavaDoc
148     createMBeanInfo()
149     {
150         debug( "createMBeanInfo");
151         final MBeanInfo JavaDoc baseMBeanInfo =
152             MBeanInfoConverter.getInstance().convert( TestDummy.class, null );
153         
154         final List JavaDoc<MBeanAttributeInfo JavaDoc> dynamicAttrInfos = new ArrayList JavaDoc<MBeanAttributeInfo JavaDoc>();
155
156         int i = 0;
157         for( final String JavaDoc name : mAttributes.keySet() )
158         {
159             final Object JavaDoc value = mAttributes.get( name );
160             final String JavaDoc type = value == null ?
161                 String JavaDoc.class.getName() : value.getClass().getName();
162             
163             final MBeanAttributeInfo JavaDoc info =
164                 new MBeanAttributeInfo JavaDoc( name, type, "dynamically-added Attribute",
165                                         true, true, false );
166             dynamicAttrInfos.add( info );
167         }
168         
169         final MBeanAttributeInfo JavaDoc[] dynInfos = new MBeanAttributeInfo JavaDoc[ dynamicAttrInfos.size() ];
170         dynamicAttrInfos.toArray( dynInfos );
171         
172         final MBeanAttributeInfo JavaDoc[] attrInfos =
173             JMXUtil.mergeMBeanAttributeInfos( dynInfos, baseMBeanInfo.getAttributes() );
174         
175         return( JMXUtil.newMBeanInfo( baseMBeanInfo, attrInfos ) );
176     }
177     
178         public synchronized MBeanInfo JavaDoc
179     getMBeanInfo()
180     {
181         if ( mMBeanInfo == null )
182         {
183             mMBeanInfo = createMBeanInfo();
184         }
185         
186         return( mMBeanInfo );
187     }
188     
189         public Object JavaDoc
190     getAttribute( final String JavaDoc name )
191         throws AttributeNotFoundException JavaDoc
192     {
193         if ( ! mAttributes.containsKey( name ) )
194         {
195             throw new AttributeNotFoundException JavaDoc( name );
196         }
197         
198         return( mAttributes.get( name ) );
199     }
200     
201         public AttributeList JavaDoc
202     getAttributes( final String JavaDoc[] names )
203     {
204         final AttributeList JavaDoc attrs = new AttributeList JavaDoc();
205         
206         if ( names != null )
207         {
208             for( int i = 0; i < names.length; ++i )
209             {
210                 try
211                 {
212                     final Object JavaDoc result = getAttribute( names[ i ] );
213                     attrs.add( new Attribute JavaDoc( names[ i ], result ) );
214                 }
215                 catch( AttributeNotFoundException JavaDoc e )
216                 {
217                 }
218             }
219         }
220         
221         return attrs;
222     }
223     
224         public AttributeList JavaDoc
225     setAttributes( final AttributeList JavaDoc attrs )
226     {
227         throw new RuntimeException JavaDoc(
228             "TestDummy: setAttributes() not yet implemented" );
229     }
230     
231         public Object JavaDoc
232     invoke(
233         final String JavaDoc methodName,
234         final Object JavaDoc[] args,
235         final String JavaDoc[] signature )
236     {
237         Object JavaDoc result = null;
238         final int numArgs = args == null ? 0 : args.length;
239         
240         if ( "addAttribute".equals( methodName ) && numArgs == 2 )
241         {
242             addAttribute( (String JavaDoc)args[ 0 ], (Object JavaDoc)args[ 1 ] );
243         }
244         else if ( "removeAttribute".equals( methodName ) && numArgs == 1 )
245         {
246             removeAttribute( (String JavaDoc)args[ 0 ] );
247         }
248         else if ( numArgs == 2 &&
249             "emitNotifications".equals( methodName ) )
250         {
251             final String JavaDoc type = (String JavaDoc)args[0];
252             final int howMany = (Integer JavaDoc)args[1];
253             result = emitNotifications( type, howMany );
254         }
255         else
256         {
257             throw new RuntimeException JavaDoc( "invoke: no such method " + methodName );
258         }
259         return result;
260     }
261     
262         public void
263     setAttribute( final String JavaDoc name, final Object JavaDoc value )
264     {
265         debug( "setAttribute" + name + "=" + value );
266         
267         addAttribute( name, value );
268     }
269     
270         public void
271     setAttribute( final Attribute JavaDoc attr )
272     {
273         setAttribute( attr.getName(), attr.getValue() );
274     }
275     
276         protected ObjectName JavaDoc
277     preRegisterModifyName(
278         final MBeanServer JavaDoc server,
279         final ObjectName JavaDoc nameIn )
280     {
281         //final String EXTRA = ",epoch=" + System.currentTimeMillis();
282
final String JavaDoc EXTRA = "";
283         
284         final ObjectName JavaDoc nameOut = Util.newObjectName( nameIn.toString() + EXTRA );
285
286         return( nameOut );
287     }
288     
289     
290         public ObjectName JavaDoc
291     preRegister(
292         final MBeanServer JavaDoc server,
293         final ObjectName JavaDoc nameIn)
294         throws Exception JavaDoc
295     {
296         mServer = server;
297         mSelfObjectName = preRegisterModifyName( server, nameIn );
298         
299         return( mSelfObjectName );
300     }
301     
302         public void
303     postRegister( Boolean JavaDoc registrationSucceeded )
304     {
305     }
306     
307         public void
308     preDeregister()
309     {
310     }
311     
312         public void
313     postDeregister()
314     {
315     }
316     
317     
318         public long
319     emitNotifications(
320         final String JavaDoc notifType,
321         final int howMany )
322     {
323         final NotificationBuilder builder =
324             new NotificationBuilder( notifType, mSelfObjectName );
325             
326         final long start = System.currentTimeMillis();
327         
328         for( int i = 0; i < howMany; ++i )
329         {
330             final Notification JavaDoc notif = builder.buildNew( "test Notification" );
331             
332             sendNotification( notif );
333         }
334         
335         final long elapsed = System.currentTimeMillis() - start;
336         return elapsed;
337     }
338     
339         public void
340     addNotificationListener(final NotificationListener JavaDoc listener )
341     {
342         mBroadcaster.addNotificationListener( listener, null, null );
343     }
344     
345         public void
346     addNotificationListener(
347         final NotificationListener JavaDoc listener,
348         final NotificationFilter JavaDoc filter,
349         final Object JavaDoc handback)
350     {
351         mBroadcaster.addNotificationListener( listener, filter, handback );
352     }
353
354         public void
355     removeNotificationListener( final NotificationListener JavaDoc listener)
356         throws ListenerNotFoundException JavaDoc
357     {
358         mBroadcaster.removeNotificationListener( listener );
359     }
360  
361         public void
362     removeNotificationListener(
363         final NotificationListener JavaDoc listener,
364         final NotificationFilter JavaDoc filter,
365         final Object JavaDoc handback)
366         throws ListenerNotFoundException JavaDoc
367     {
368         mBroadcaster.removeNotificationListener( listener, filter, handback );
369     }
370
371         public void
372     sendNotification( final Notification JavaDoc notification)
373     {
374         mBroadcaster.sendNotification( notification );
375     }
376     
377         public MBeanNotificationInfo JavaDoc[]
378     getNotificationInfo()
379     {
380         return new MBeanNotificationInfo JavaDoc[0];
381     }
382 }
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
Popular Tags