1 23 24 29 30 31 package com.sun.cli.jmx.cmd; 32 33 import java.net.MalformedURLException ; 34 35 import javax.management.remote.JMXServiceURL ; 36 import javax.management.remote.JMXConnector ; 37 import javax.management.remote.JMXConnectorFactory ; 38 import javax.management.MBeanServerConnection ; 39 40 import com.sun.cli.jmx.support.AliasMgr; 41 import com.sun.cli.jmx.support.AliasMgrHashMapImpl; 42 import com.sun.cli.jmx.support.AliasMgrMBean; 43 import com.sun.cli.jmx.support.StandardAliasesIniter; 44 import com.sun.cli.jmx.support.CLISupport; 45 import com.sun.cli.jmx.support.CLISupportMBean; 46 47 import com.sun.cli.jmx.support.CLISupportMBeanProxy; 48 import com.sun.cli.util.stringifier.SmartStringifier; 49 import com.sun.cli.util.stringifier.Stringifier; 50 import com.sun.cli.util.stringifier.ArrayStringifier; 51 52 import com.sun.cli.jmx.spi.JMXConnectorProvider; 53 54 public abstract class JMXCmd extends CmdBase 55 { 56 final String [] ALL_TARGET = new String [] { "all" }; 57 58 public final static String ENV_PROXY = "PROXY"; 59 public final static String ENV_CONNECTION = "CONNECTION"; 60 public final static String ENV_TARGET = "TARGET"; 61 public final static String ENV_CONNECT_NAME_BASE = "CONNECT_"; 62 public final static String ENV_ALIAS_MGR = "ALIAS_MGR"; 63 public final static String ENV_PROVIDERS = "PROVIDERS"; 64 65 final static String DEFAULT_CONNECTION_NAME = "DEFAULT"; 66 67 private static ConnectionMgrImpl sConnectionMgr = null; 68 private static AliasMgrMBean sAliasMgr = createAliasMgr(); 69 70 71 JMXCmd( final CmdEnv env ) 72 { 73 super( env ); 74 75 env.put( ENV_ALIAS_MGR, sAliasMgr, false); 76 77 } 79 80 81 ConnectionMgr 82 getConnectionMgr( ) 83 throws Exception 84 { 85 initConnectionMgr(); 86 return( sConnectionMgr ); 87 } 88 89 void 90 initConnectionMgr( ) 91 throws IllegalAccessException , InstantiationException , ClassNotFoundException 92 { 93 if ( sConnectionMgr != null ) 94 return; 95 96 sConnectionMgr = new ConnectionMgrImpl( ); 97 98 final String providersString = ((String )envGet( ENV_PROVIDERS, "")).trim(); 99 100 if ( providersString != null && providersString.length() != 0 ) 101 { 102 final String [] providers = providersString.split( "," ); 103 104 for( int i = 0; i < providers.length; ++i ) 105 { 106 try 107 { 108 final Class theClass = Class.forName( providers[ i ] ); 109 110 sConnectionMgr.addProvider( theClass ); 111 println( "Added JMXConnectorProvider: " + providers[ i ] ); 112 } 113 catch( Exception e ) 114 { 115 printError( "WARNING: Can't add JMXConnectorProvider: " + providers[ i ] ); 116 } 117 } 118 } 119 } 120 121 122 static AliasMgrMBean 123 createAliasMgr() 124 { 125 final AliasMgrHashMapImpl aliasMgrImpl = new AliasMgrHashMapImpl(); 126 final AliasMgr aliasMgr = new AliasMgr( aliasMgrImpl ); 127 128 129 try 130 { 131 aliasMgrImpl.load( JMXAdminFileNames.getAliasesFile() ); 132 StandardAliasesIniter.init( aliasMgr ); 133 } 134 catch( Exception e ) 135 { 136 StandardAliasesIniter.init( aliasMgr ); 137 System.err.println( "can't load aliases file: " + e.getMessage()); 138 139 } 140 return( aliasMgr ); 141 } 142 143 144 private CLISupportMBean 145 getCLISupport( MBeanServerConnection conn ) 146 throws Exception 147 { 148 final CLISupportMBean cliSupport = new CLISupport( conn, sAliasMgr); 149 150 return( cliSupport ); 151 } 152 153 String 154 connectionNameToEnvName( String connectionName ) 155 { 156 return( ENV_CONNECT_NAME_BASE + connectionName ); 157 } 158 159 String 160 envNameToConnectionName( String envName ) 161 { 162 return( envName.substring( ENV_CONNECT_NAME_BASE.length(), envName.length() ) ); 163 } 164 165 CLISupportMBeanProxy 166 setProxy( String name, ConnectInfo connectInfo ) 167 throws Exception 168 { 169 JMXConnector jmxConnector = null; 170 171 try 172 { 173 jmxConnector = getConnectionMgr().connect( name, connectInfo ); 174 } 175 catch( java.io.IOException e ) 176 { 177 throw new Exception ( e.getMessage() + " (" + connectInfo.toString() + ")", e); 178 } 179 180 final MBeanServerConnection managedServer = jmxConnector.getMBeanServerConnection(); 181 182 final CLISupportMBean cliSupport = getCLISupport( managedServer ); 183 184 final CLISupportMBeanProxy proxy = new CLISupportMBeanProxy( sAliasMgr, cliSupport ); 185 186 envPut( connectionNameToEnvName( name ), connectInfo.toString(), true ); 187 envPut( ENV_PROXY, proxy, false ); 188 envPut( ENV_CONNECTION, managedServer, false ); 189 190 return( proxy ); 191 } 192 193 194 void 195 clearProxy( ) 196 { 197 envRemove( ENV_PROXY ); 198 } 199 200 CLISupportMBeanProxy 201 establishProxy( String name, ConnectInfo connectInfo ) 202 throws Exception 203 { 204 CLISupportMBeanProxy proxy = null; 205 206 proxy = setProxy( name, connectInfo ); 207 208 final String host = connectInfo.getParam( JMXConnectorProvider.HOST ); 209 final String port = connectInfo.getParam( JMXConnectorProvider.PORT ); 210 String protocol = connectInfo.getParam( JMXConnectorProvider.PROTOCOL ); 211 if ( protocol == null ) 212 { 213 protocol = "jmxmp"; 214 } 215 216 println( "Established connection to " + 217 host + ":" + port + " using " + protocol ); 218 219 return( proxy ); 220 } 221 222 223 CLISupportMBeanProxy 224 establishProxy( ConnectInfo connectInfo ) 225 throws Exception 226 { 227 final String envName = connectionNameToEnvName( DEFAULT_CONNECTION_NAME ); 228 229 final CLISupportMBeanProxy proxy = establishProxy( envName , connectInfo ); 230 231 return( proxy ); 232 } 233 234 void 235 establishProxy( ) 236 throws Exception 237 { 238 if ( envGet( ENV_PROXY ) == null ) 239 { 240 final String envName = connectionNameToEnvName( DEFAULT_CONNECTION_NAME ); 241 242 final String connectString = (String )envGet( envName ); 243 if ( connectString != null ) 244 { 245 establishProxy( new ConnectInfo( connectString ) ); 246 } 247 } 248 249 if ( envGet( ENV_PROXY ) == null ) 250 { 251 throw new java.io.IOException ( "can't connect" ); 252 } 253 } 254 255 CLISupportMBeanProxy 256 getProxy() 257 { 258 final CLISupportMBeanProxy proxy = (CLISupportMBeanProxy)envGet( ENV_PROXY ); 259 260 return( proxy ); 261 } 262 263 MBeanServerConnection 264 getConnection() 265 { 266 final MBeanServerConnection conn = (MBeanServerConnection )envGet( ENV_CONNECTION ); 267 268 return( conn ); 269 } 270 271 AliasMgrMBean 272 getAliasMgr() 273 { 274 final AliasMgrMBean proxy = (AliasMgrMBean)envGet( ENV_ALIAS_MGR ); 275 276 return( proxy ); 277 } 278 279 void 280 preExecute() 281 throws Exception 282 { 283 getConnectionMgr(); 284 super.preExecute(); 285 } 286 287 void 288 handleException( final Exception e ) 289 { 290 super.handleException( e ); 291 292 if ( e instanceof java.io.IOException ) 293 { 294 clearProxy(); 295 } 296 } 297 298 299 String [] 300 getEnvTargets() 301 { 302 final String targetsEnv = (String )envGet( ENV_TARGET ); 303 if ( targetsEnv == null ) 304 return( null ); 305 306 final String [] targets = (String []) 308 TargetPersister.DEFAULT.asObject( targetsEnv ); 309 310 return( targets ); 311 } 312 313 void 314 putEnvTargets( String [] targets ) 315 { 316 final String targetsStr = TargetPersister.DEFAULT.asString( targets ); 318 envPut( ENV_TARGET, targetsStr, true); 319 } 320 321 String [] 322 getTargets() 323 { 324 String [] targets = getOperands(); 325 326 if ( targets.length == 0 ) 327 { 328 targets = getEnvTargets(); 329 } 330 331 return( targets ); 332 } 333 334 335 final static class TargetPersister implements com.sun.cli.util.ValuePersister 336 { 337 final static TargetPersister DEFAULT = new TargetPersister(); 338 final static String TARGET_DELIM = " "; 339 340 public String 341 asString( Object value ) 342 { 343 return( ArrayStringifier.stringify( (String [])value, TARGET_DELIM ) ); 344 } 345 346 public Object 347 asObject( String value ) 348 { 349 return( value.split( TARGET_DELIM ) ); 350 } 351 } 352 }; 353 354 355 | Popular Tags |