KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import java.io.Serializable JavaDoc;
31
32 import javax.management.NotificationEmitter JavaDoc;
33 import javax.management.Attribute JavaDoc;
34 import javax.management.MBeanInfo JavaDoc;
35 import javax.management.MBeanAttributeInfo JavaDoc;
36
37 import com.sun.appserv.management.base.Sample;
38 import com.sun.appserv.management.util.jmx.JMXUtil;
39
40 /**
41     @see Sample
42  */

43 public final class SampleImpl extends AMXImplBase
44 {
45     // all Attributes live in a Map
46
private final Map JavaDoc<String JavaDoc,Serializable JavaDoc> mAttributes;
47     private MBeanInfo JavaDoc mMBeanInfo;
48     
49         public void
50     emitNotifications( final Serializable JavaDoc data, final int numNotifs, final long interval )
51     {
52         if ( numNotifs <= 0 )
53         {
54             throw new IllegalArgumentException JavaDoc( "" + numNotifs );
55         }
56         
57         new EmitterThread( data, numNotifs, interval ).start();
58     }
59         
60         public
61     SampleImpl( )
62     {
63         mAttributes = Collections.synchronizedMap( new HashMap JavaDoc<String JavaDoc,Serializable JavaDoc>() );
64         mMBeanInfo = null;
65     }
66         
67         public void
68     addAttribute( final String JavaDoc name, final Serializable JavaDoc value )
69     {
70         if ( name == null || name.length() == 0 )
71         {
72             throw new IllegalArgumentException JavaDoc( );
73         }
74         
75         mAttributes.put( name, value );
76         mMBeanInfo = null;
77     }
78     
79         public void
80     removeAttribute( final String JavaDoc name )
81     {
82         mAttributes.remove( name );
83         mMBeanInfo = null;
84     }
85     
86     
87         public boolean
88     getMBeanInfoIsInvariant()
89     {
90         return( false );
91     }
92     
93         private synchronized MBeanInfo JavaDoc
94     createMBeanInfo()
95     {
96         final MBeanInfo JavaDoc baseMBeanInfo = super.getMBeanInfo();
97         
98         final MBeanAttributeInfo JavaDoc[] dynamicAttrInfos =
99             new MBeanAttributeInfo JavaDoc[ mAttributes.keySet().size() ];
100         final Iterator JavaDoc iter = mAttributes.keySet().iterator();
101         int i = 0;
102         while ( iter.hasNext() )
103         {
104             final String JavaDoc name = (String JavaDoc)iter.next();
105             final Object JavaDoc value = mAttributes.get( name );
106             final String JavaDoc type = value == null ? String JavaDoc.class.getName() : value.getClass().getName();
107             
108             dynamicAttrInfos[ i ] = new MBeanAttributeInfo JavaDoc( name, type, "dynamically-added Attribute",
109                                         true, true, false );
110             ++i;
111         }
112         
113         final MBeanAttributeInfo JavaDoc[] attrInfos =
114             JMXUtil.mergeMBeanAttributeInfos( dynamicAttrInfos, baseMBeanInfo.getAttributes() );
115         
116         return( JMXUtil.newMBeanInfo( baseMBeanInfo, attrInfos ) );
117     }
118     
119         public synchronized MBeanInfo JavaDoc
120     getMBeanInfo()
121     {
122         if ( mMBeanInfo == null )
123         {
124             mMBeanInfo = createMBeanInfo();
125         }
126         
127         return( mMBeanInfo );
128     }
129     
130         protected Serializable JavaDoc
131     getAttributeManually( final String JavaDoc name )
132     {
133         return( mAttributes.get( name ) );
134     }
135     
136     
137         protected void
138     setAttributeManually( final Attribute JavaDoc attr )
139     {
140         mAttributes.put( attr.getName(), Serializable JavaDoc.class.cast( attr.getValue() ) );
141     }
142     
143     
144     
145     private final class EmitterThread extends Thread JavaDoc
146     {
147         private final Serializable JavaDoc mData;
148         private final int mNumNotifs;
149         private final long mIntervalMillis;
150     
151         public EmitterThread( final Serializable JavaDoc data, final int numNotifs, final long intervalMillis )
152         {
153             mData = data;
154             mNumNotifs = numNotifs;
155             mIntervalMillis = intervalMillis;
156         }
157     
158             public void
159         run()
160         {
161             for( int i = 0; i < mNumNotifs; ++i )
162             {
163                 sendNotification( Sample.SAMPLE_NOTIFICATION_TYPE, Sample.USER_DATA_KEY, mData );
164                 
165                 try
166                 {
167                     Thread.sleep( mIntervalMillis );
168                 }
169                 catch( InterruptedException JavaDoc e )
170                 {
171                     break;
172                 }
173             }
174         }
175     }
176     
177     
178         public void
179     uploadBytes( final byte[] bytes )
180     {
181         // do nothing; just a bandwidth test
182
}
183     
184     private final static int MEGABYTE = 1024 * 1024;
185         public byte[]
186     downloadBytes( final int numBytes )
187     {
188         if ( numBytes <0 || numBytes > 10 * MEGABYTE )
189         {
190             throw new IllegalArgumentException JavaDoc( "Illegal count: " + numBytes );
191         }
192
193         final byte[] bytes = new byte[ numBytes ];
194         
195         return( bytes );
196     }
197 }
198
199
200
201
202
203
Popular Tags