1 23 package com.sun.enterprise.management.support; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Collections ; 28 import java.util.Iterator ; 29 30 import java.io.Serializable ; 31 32 import javax.management.NotificationEmitter ; 33 import javax.management.Attribute ; 34 import javax.management.MBeanInfo ; 35 import javax.management.MBeanAttributeInfo ; 36 37 import com.sun.appserv.management.base.Sample; 38 import com.sun.appserv.management.util.jmx.JMXUtil; 39 40 43 public final class SampleImpl extends AMXImplBase 44 { 45 private final Map <String ,Serializable > mAttributes; 47 private MBeanInfo mMBeanInfo; 48 49 public void 50 emitNotifications( final Serializable data, final int numNotifs, final long interval ) 51 { 52 if ( numNotifs <= 0 ) 53 { 54 throw new IllegalArgumentException ( "" + numNotifs ); 55 } 56 57 new EmitterThread( data, numNotifs, interval ).start(); 58 } 59 60 public 61 SampleImpl( ) 62 { 63 mAttributes = Collections.synchronizedMap( new HashMap <String ,Serializable >() ); 64 mMBeanInfo = null; 65 } 66 67 public void 68 addAttribute( final String name, final Serializable value ) 69 { 70 if ( name == null || name.length() == 0 ) 71 { 72 throw new IllegalArgumentException ( ); 73 } 74 75 mAttributes.put( name, value ); 76 mMBeanInfo = null; 77 } 78 79 public void 80 removeAttribute( final String 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 94 createMBeanInfo() 95 { 96 final MBeanInfo baseMBeanInfo = super.getMBeanInfo(); 97 98 final MBeanAttributeInfo [] dynamicAttrInfos = 99 new MBeanAttributeInfo [ mAttributes.keySet().size() ]; 100 final Iterator iter = mAttributes.keySet().iterator(); 101 int i = 0; 102 while ( iter.hasNext() ) 103 { 104 final String name = (String )iter.next(); 105 final Object value = mAttributes.get( name ); 106 final String type = value == null ? String .class.getName() : value.getClass().getName(); 107 108 dynamicAttrInfos[ i ] = new MBeanAttributeInfo ( name, type, "dynamically-added Attribute", 109 true, true, false ); 110 ++i; 111 } 112 113 final MBeanAttributeInfo [] attrInfos = 114 JMXUtil.mergeMBeanAttributeInfos( dynamicAttrInfos, baseMBeanInfo.getAttributes() ); 115 116 return( JMXUtil.newMBeanInfo( baseMBeanInfo, attrInfos ) ); 117 } 118 119 public synchronized MBeanInfo 120 getMBeanInfo() 121 { 122 if ( mMBeanInfo == null ) 123 { 124 mMBeanInfo = createMBeanInfo(); 125 } 126 127 return( mMBeanInfo ); 128 } 129 130 protected Serializable 131 getAttributeManually( final String name ) 132 { 133 return( mAttributes.get( name ) ); 134 } 135 136 137 protected void 138 setAttributeManually( final Attribute attr ) 139 { 140 mAttributes.put( attr.getName(), Serializable .class.cast( attr.getValue() ) ); 141 } 142 143 144 145 private final class EmitterThread extends Thread 146 { 147 private final Serializable mData; 148 private final int mNumNotifs; 149 private final long mIntervalMillis; 150 151 public EmitterThread( final Serializable 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 e ) 170 { 171 break; 172 } 173 } 174 } 175 } 176 177 178 public void 179 uploadBytes( final byte[] bytes ) 180 { 181 } 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 ( "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 |