1 23 24 29 30 package com.sun.cli.jmx.cmd; 31 32 import java.util.Arrays ; 33 import java.util.Set ; 34 import java.util.TreeSet ; 35 import java.util.Iterator ; 36 37 import javax.management.ObjectName ; 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 47 public class FindCmd extends JMXCmd 48 { 49 static final String 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 return( 0 ); 62 } 63 64 public String 65 getUsage() 66 { 67 return( CmdStrings.FIND_HELP.toString() ); 68 } 69 public static String [] 70 getNames( ) 71 { 72 return( new String [] { "find", "f" } ); 73 } 74 75 76 static private final String 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 [] 89 objectNamesToStrings( final ObjectName [] objectNames ) 90 { 91 final String [] resultStrs = new String [ 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 names ) 103 { 104 println( IteratorStringifier.stringify( names.iterator(), "\n" ) ); 105 } 106 107 Set 108 getCurrent() 109 { 110 return( (Set )envGet( ENV_CURRENT_SET ) ); 111 } 112 113 114 Set 115 arrayToSet( final Object [] names ) 116 throws Exception 117 { 118 final TreeSet theSet = new TreeSet (); 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 133 static String 134 convertToJavaRegex( String input ) 135 { 136 String converted = input; 137 138 if ( input != null ) 139 { 140 final String [] tokens = new TokenizerImpl( input, "", '\\', "*").getTokens(); 142 assert( tokens.length == 1 ); 143 145 final String unescapedInput = tokens[ 0 ]; 146 147 final int length = unescapedInput.length(); 148 final StringBuffer buf = new StringBuffer (); 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 181 { 182 String [] targets = getOperands(); 183 184 if ( targets.length == 0 ) 185 { 186 targets = new String [] { "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 regex = getString( "regex", null ); 193 final String javaregex = getString( "java-regex", null ); 194 195 final Set 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 actualRegex = (javaregex != null) ? 213 javaregex : convertToJavaRegex( regex ); 214 215 ObjectName [] objectNames = getProxy().mbeanFind( targets, actualRegex); 216 217 final String [] objectStrings = objectNamesToStrings( objectNames ); 218 Set 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 |