1 23 24 29 30 package com.sun.cli.jmx.cmd; 31 32 import java.io.File ; 33 import java.io.PrintStream ; 34 35 import javax.management.ObjectName ; 36 import javax.management.AttributeChangeNotification ; 37 import javax.management.MBeanServerNotification ; 38 import javax.management.NotificationListener ; 39 import javax.management.monitor.MonitorNotification ; 40 import javax.management.Notification ; 41 42 import com.sun.cli.util.stringifier.*; 43 44 45 46 class MyNotificationListener implements NotificationListener 47 { 48 MyCmdOutput mOutput; 49 boolean mPaused = false; 50 SmartStringifier mStringifier; 51 52 static SmartStringifier 53 setupStringifier() 54 { 55 final NotificationStringifier.Options options = 57 new NotificationStringifier.Options(); 58 options.mDelim = "\n"; 59 60 final StringifierRegistry myRegistry = 62 new StringifierRegistry( StringifierRegistry.DEFAULT ); 63 64 myRegistry.add( Notification .class, 66 new NotificationStringifier( options )); 67 68 myRegistry.add( AttributeChangeNotification .class, 69 new AttributeChangeNotificationStringifier( options ) ); 70 71 myRegistry.add( MBeanServerNotification .class, 72 new MBeanServerNotificationStringifier( options ) ); 73 74 myRegistry.add( MonitorNotification .class, 75 new MonitorNotificationStringifier( options ) ); 76 77 final SmartStringifier s = new SmartStringifier( myRegistry, ",", true); 78 79 return( s ); 80 } 81 82 public 83 MyNotificationListener( MyCmdOutput output ) 84 { 85 mOutput = output; 86 mStringifier = setupStringifier(); 87 } 88 89 public void 90 setOutput( MyCmdOutput output ) 91 { 92 assert( mOutput != null ); 93 mOutput.close(); 94 mOutput = output; 95 } 96 97 public void 98 handleNotification( Notification notif, Object o) 99 { 100 if ( ! mPaused ) 101 { 102 final String msg = mStringifier.stringify( notif ) + "\n"; 103 104 mOutput.println( msg ); 105 } 106 } 107 108 public void 109 togglePaused( ) 110 { 111 mPaused = ! mPaused; 112 } 113 114 public void 115 setPaused( boolean paused ) 116 { 117 mPaused = paused; 118 } 119 120 }; 121 122 class MyCmdOutput implements CmdOutput 123 { 124 private final CmdOutput mFileOutput; 125 private final CmdOutput mStdOutput; 126 127 MyCmdOutput() 128 { 129 mFileOutput = new CmdOutputNull(); 130 mStdOutput = new CmdOutputImpl( System.out, System.err ); 131 } 132 133 MyCmdOutput( File theFile, boolean echoToStdOut ) 134 throws java.io.IOException 135 { 136 mFileOutput = new CmdOutputToFile( theFile ); 137 138 if ( echoToStdOut ) 139 { 140 mStdOutput = new CmdOutputImpl( System.out, System.err); 141 } 142 else 143 { 144 mStdOutput = new CmdOutputNull(); 145 } 146 } 147 public void 148 print( Object o ) 149 { 150 mFileOutput.print( o ); 151 mStdOutput.print( o ); 152 } 153 154 public void 155 println( Object o ) 156 { 157 print( o + "\n" ); 158 } 159 160 public void 161 printError( Object o ) 162 { 163 println( o ); 164 } 165 166 public void 167 printDebug( Object o ) 168 { 169 println( o ); 170 } 171 172 public void 173 close() 174 { 175 if ( mFileOutput instanceof CmdOutputToFile ) 176 { 177 ((CmdOutputToFile)mFileOutput).close(); 178 } 179 } 180 } 181 182 public class ListenCmd extends JMXCmd 183 { 184 public 185 ListenCmd( final CmdEnv env ) 186 { 187 super( env ); 188 } 189 190 private final static String STOP_OPTION = "stop"; 191 private final static String PAUSE_OPTION = "pause"; 192 private final static String FILE_OPTION = "file"; 193 private final static String STDOUT_OPTION = "stdout"; 194 195 static private final String OPTIONS_INFO = 196 STOP_OPTION + 197 " " + PAUSE_OPTION + 198 " " + FILE_OPTION + ",1" + 199 " " + STDOUT_OPTION; 200 201 ArgHelper.OptionsInfo 202 getOptionInfo() 203 throws ArgHelper.IllegalOptionException 204 { 205 return( new ArgHelperOptionsInfo( OPTIONS_INFO ) ); 206 } 207 208 int 209 getNumRequiredOperands() 210 { 211 return( 0 ); 212 } 213 214 public String 215 getUsage() 216 { 217 return( CmdStrings.LISTEN_HELP.toString() ); 218 } 219 220 public static String [] 221 getNames( ) 222 { 223 return( new String [] { "listen" } ); 224 } 225 226 227 private final static MyNotificationListener sNotificationListener = 228 new MyNotificationListener( new MyCmdOutput( ) ); 229 230 void 231 startListening( final String [] targets, String filename) 232 throws Exception 233 { 234 if ( filename != null ) 235 { 236 boolean toStdOut = getBoolean( STDOUT_OPTION, null ) != null; 237 238 final File theFile = new File ( filename ); 239 240 sNotificationListener.setOutput( new MyCmdOutput( theFile, toStdOut ) ); 241 } 242 243 getProxy().mbeanListen( true, targets, sNotificationListener, null, null ); 244 sNotificationListener.setPaused( false ); 245 } 246 247 void 248 stopListening( final String [] targets ) 249 throws Exception 250 { 251 getProxy().mbeanListen( false, targets, sNotificationListener, null, null ); 252 } 253 254 void 255 pauseListening() 256 { 257 sNotificationListener.togglePaused( ); 258 } 259 260 261 void 262 executeInternal() 263 throws Exception 264 { 265 final String [] targets = getTargets(); 266 267 final String fileOption = getString( FILE_OPTION, null ); 268 final Boolean stopOption = getBoolean( STOP_OPTION, null ); 269 final Boolean pauseOption = getBoolean( PAUSE_OPTION, null ); 270 271 establishProxy(); 272 if ( stopOption != null ) 273 { 274 if ( fileOption != null ) 275 { 276 throw new IllegalArgumentException ( STOP_OPTION + " cannot specify a file" ); 277 } 278 if ( ! stopOption.booleanValue() ) 279 { 280 throw new IllegalArgumentException ( STOP_OPTION + " cannot be false" ); 281 } 282 stopListening( targets ); 283 } 284 else if ( pauseOption != null ) 285 { 286 if ( fileOption != null ) 287 { 288 throw new IllegalArgumentException ( PAUSE_OPTION + " cannot specify a file" ); 289 } 290 if ( ! stopOption.booleanValue() ) 291 { 292 throw new IllegalArgumentException ( PAUSE_OPTION + " cannot be false" ); 293 } 294 295 pauseListening(); 296 } 297 else if ( getBoolean( STOP_OPTION, null ) == null && 298 getBoolean( PAUSE_OPTION, null ) == null ) 299 { 300 startListening( targets, fileOption ); 302 } 303 else 304 { 305 throw new ArgHelper.IllegalOptionException( "illegal options" ); 306 } 307 } 308 } 309 | Popular Tags |