KickJava   Java API By Example, From Geeks To Geeks.

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


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/FindCmd.java,v 1.3 2005/12/25 03:45:35 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:35 $
28  */

29  
30 package com.sun.cli.jmx.cmd;
31
32 import java.util.Arrays JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.TreeSet JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 import javax.management.ObjectName JavaDoc;
38
39 import com.sun.cli.jmx.support.ResultsForGetSet;
40 import com.sun.cli.jmx.support.CLISupportMBeanProxy;
41 import com.sun.cli.util.stringifier.*;
42 import com.sun.cli.util.TokenizerImpl;
43
44 /*
45     Find MBeans by name (alias, full or partial name).
46  */

47 public class FindCmd extends JMXCmd
48 {
49     static final String JavaDoc ENV_CURRENT_SET = "FIND_COMMAND_CURRENT_SET";
50     
51         public
52     FindCmd( final CmdEnv env )
53     {
54         super( env );
55     }
56     
57         int
58     getNumRequiredOperands()
59     {
60         // require 1, by default
61
return( 0 );
62     }
63     
64         public String JavaDoc
65     getUsage()
66     {
67         return( CmdStrings.FIND_HELP.toString() );
68     }
69         public static String JavaDoc []
70     getNames( )
71     {
72         return( new String JavaDoc [] { "find", "f" } );
73     }
74     
75     
76     static private final String JavaDoc OPTIONS_INFO =
77         "current add remove regex,1 java-regex,1";
78         
79     
80         ArgHelper.OptionsInfo
81     getOptionInfo()
82         throws ArgHelper.IllegalOptionException
83     {
84         return( new ArgHelperOptionsInfo( OPTIONS_INFO ) );
85     }
86     
87     
88         String JavaDoc []
89     objectNamesToStrings( final ObjectName JavaDoc [] objectNames )
90     {
91         // sorting doesn't work on returned array, so convert to Strings first,then sort
92
final String JavaDoc [] resultStrs = new String JavaDoc [ objectNames.length ];
93         for( int i = 0; i < resultStrs.length; ++i )
94         {
95             resultStrs[ i ] = objectNames[ i ].toString();
96         }
97         
98         return( resultStrs );
99     }
100     
101         void
102     display( final Set JavaDoc names )
103     {
104         println( IteratorStringifier.stringify( names.iterator(), "\n" ) );
105     }
106     
107         Set JavaDoc
108     getCurrent()
109     {
110         return( (Set JavaDoc)envGet( ENV_CURRENT_SET ) );
111     }
112     
113     
114         Set JavaDoc
115     arrayToSet( final Object JavaDoc [] names )
116         throws Exception JavaDoc
117     {
118         final TreeSet JavaDoc theSet = new TreeSet JavaDoc();
119         
120         for( int i = 0; i < names.length; ++i )
121         {
122             theSet.add( names[ i ] );
123         }
124
125         return( theSet );
126     }
127     
128     private final static char BACKSLASH = '\\';
129     
130     /*
131         We support only '*" in a simplified form.
132      */

133         static String JavaDoc
134     convertToJavaRegex( String JavaDoc input )
135     {
136         String JavaDoc converted = input;
137         
138         if ( input != null )
139         {
140             // first run the tokenizer on it specifying no delimiters so as to eliminate any escape constructs
141
final String JavaDoc [] tokens = new TokenizerImpl( input, "", '\\', "*").getTokens();
142             assert( tokens.length == 1 );
143             // now any '\' or '*' characters are to be taken literally
144

145             final String JavaDoc unescapedInput = tokens[ 0 ];
146             
147             final int length = unescapedInput.length();
148             final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
149             
150             for( int i = 0; i < length; ++i )
151             {
152                 final char theChar = unescapedInput.charAt( i );
153                 
154                 if ( theChar == '.' )
155                 {
156                     buf.append( "[.]" );
157                 }
158                 else if ( theChar == '*' )
159                 {
160                     buf.append( ".*" );
161                 }
162                 else if ( theChar == BACKSLASH )
163                 {
164                     buf.append( "" + BACKSLASH + BACKSLASH );
165                 }
166                 else
167                 {
168                     buf.append( theChar );
169                 }
170             }
171             
172             converted = buf.toString();
173             
174         }
175         return( converted );
176     }
177
178         void
179     executeInternal()
180         throws Exception JavaDoc
181     {
182         String JavaDoc [] targets = getOperands();
183         
184         if ( targets.length == 0 )
185         {
186             targets = new String JavaDoc [] { "all" };
187         }
188         
189         final boolean add = getBoolean( "add", Boolean.FALSE ).booleanValue();
190         final boolean remove = getBoolean( "remove", Boolean.FALSE ).booleanValue();
191         final boolean displayCurrent = getBoolean( "current", Boolean.FALSE ).booleanValue();
192         final String JavaDoc regex = getString( "regex", null );
193         final String JavaDoc javaregex = getString( "java-regex", null );
194         
195         final Set JavaDoc currentSet = getCurrent();
196         
197         if ( displayCurrent )
198         {
199             if ( currentSet == null || currentSet.size() == 0 )
200             {
201                 println( "Nothing in current set." );
202             }
203             else
204             {
205                 display( currentSet );
206             }
207         }
208         else
209         {
210             establishProxy();
211             
212             final String JavaDoc actualRegex = (javaregex != null) ?
213                                 javaregex : convertToJavaRegex( regex );
214             
215             ObjectName JavaDoc [] objectNames = getProxy().mbeanFind( targets, actualRegex);
216             
217             final String JavaDoc [] objectStrings = objectNamesToStrings( objectNames );
218             Set JavaDoc resultSet = arrayToSet( objectStrings );
219             
220             if ( currentSet != null )
221             {
222                 if ( add )
223                 {
224                     currentSet.addAll( resultSet );
225                     resultSet = currentSet;
226                 }
227                 else if ( remove )
228                 {
229                     currentSet.removeAll( resultSet );
230                     resultSet = currentSet;
231                 }
232             }
233             
234             envPut( ENV_CURRENT_SET, resultSet, false );
235             
236             if ( resultSet.size() == 0 )
237             {
238                 println( "No objects match the targets " + SmartStringifier.toString( targets ) );
239             }
240             else
241             {
242                 
243                 display( resultSet );
244             }
245         }
246     }
247 }
248
249
250
251
252
253
Popular Tags