KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > cmd > ListenCmd


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 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/cmd/ListenCmd.java,v 1.3 2005/12/25 03:45:38 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:38 $
28  */

29  
30 package com.sun.cli.jmx.cmd;
31
32 import java.io.File JavaDoc;
33 import java.io.PrintStream JavaDoc;
34
35 import javax.management.ObjectName JavaDoc;
36 import javax.management.AttributeChangeNotification JavaDoc;
37 import javax.management.MBeanServerNotification JavaDoc;
38 import javax.management.NotificationListener JavaDoc;
39 import javax.management.monitor.MonitorNotification JavaDoc;
40 import javax.management.Notification JavaDoc;
41
42 import com.sun.cli.util.stringifier.*;
43
44
45
46 class MyNotificationListener implements NotificationListener JavaDoc
47 {
48     MyCmdOutput mOutput;
49     boolean mPaused = false;
50     SmartStringifier mStringifier;
51     
52         static SmartStringifier
53     setupStringifier()
54     {
55         // set up our options the way we want them
56
final NotificationStringifier.Options options =
57                     new NotificationStringifier.Options();
58         options.mDelim = "\n";
59         
60         // create a new registry and put our versions in place
61
final StringifierRegistry myRegistry =
62             new StringifierRegistry( StringifierRegistry.DEFAULT );
63         
64         // register our stringifiers with desired options
65
myRegistry.add( Notification JavaDoc.class,
66             new NotificationStringifier( options ));
67         
68         myRegistry.add( AttributeChangeNotification JavaDoc.class,
69             new AttributeChangeNotificationStringifier( options ) );
70         
71         myRegistry.add( MBeanServerNotification JavaDoc.class,
72             new MBeanServerNotificationStringifier( options ) );
73         
74         myRegistry.add( MonitorNotification JavaDoc.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 JavaDoc notif, Object JavaDoc o)
99     {
100         if ( ! mPaused )
101         {
102             final String JavaDoc 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 JavaDoc theFile, boolean echoToStdOut )
134         throws java.io.IOException JavaDoc
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 JavaDoc o )
149     {
150         mFileOutput.print( o );
151         mStdOutput.print( o );
152     }
153     
154         public void
155     println( Object JavaDoc o )
156     {
157         print( o + "\n" );
158     }
159     
160         public void
161     printError( Object JavaDoc o )
162     {
163         println( o );
164     }
165     
166         public void
167     printDebug( Object JavaDoc 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 JavaDoc STOP_OPTION = "stop";
191     private final static String JavaDoc PAUSE_OPTION = "pause";
192     private final static String JavaDoc FILE_OPTION = "file";
193     private final static String JavaDoc STDOUT_OPTION = "stdout";
194     
195     static private final String JavaDoc 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 JavaDoc
215     getUsage()
216     {
217         return( CmdStrings.LISTEN_HELP.toString() );
218     }
219     
220         public static String JavaDoc []
221     getNames( )
222     {
223         return( new String JavaDoc [] { "listen" } );
224     }
225     
226     
227     private final static MyNotificationListener sNotificationListener =
228             new MyNotificationListener( new MyCmdOutput( ) );
229     
230         void
231     startListening( final String JavaDoc [] targets, String JavaDoc filename)
232         throws Exception JavaDoc
233     {
234         if ( filename != null )
235         {
236             boolean toStdOut = getBoolean( STDOUT_OPTION, null ) != null;
237             
238             final File JavaDoc theFile = new File JavaDoc( 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 JavaDoc [] targets )
249         throws Exception JavaDoc
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 JavaDoc
264     {
265         final String JavaDoc [] targets = getTargets();
266         
267         final String JavaDoc fileOption = getString( FILE_OPTION, null );
268         final Boolean JavaDoc stopOption = getBoolean( STOP_OPTION, null );
269         final Boolean JavaDoc pauseOption = getBoolean( PAUSE_OPTION, null );
270         
271         establishProxy();
272         if ( stopOption != null )
273         {
274             if ( fileOption != null )
275             {
276                 throw new IllegalArgumentException JavaDoc( STOP_OPTION + " cannot specify a file" );
277             }
278             if ( ! stopOption.booleanValue() )
279             {
280                 throw new IllegalArgumentException JavaDoc( 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 JavaDoc( PAUSE_OPTION + " cannot specify a file" );
289             }
290             if ( ! stopOption.booleanValue() )
291             {
292                 throw new IllegalArgumentException JavaDoc( 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             // if it's not a pause or stop, then it's a start
301
startListening( targets, fileOption );
302         }
303         else
304         {
305             throw new ArgHelper.IllegalOptionException( "illegal options" );
306         }
307     }
308 }
309
Popular Tags