FileDocCategorySizeDatePackage
ConnectCmd.javaAPI DocGlassfish v2 API8763Fri May 04 22:24:50 BST 2007com.sun.cli.jmx.cmd

ConnectCmd

public class ConnectCmd extends JMXCmd

Fields Summary
private static final String
OPTIONS_INFO
static final String
NAME
static final String
NAME_ABBREV
Constructors Summary
public ConnectCmd(CmdEnv env)

		super( env );
	
Methods Summary
voidestablishProxy()

		// defeat default behavior; this is the 'connect' command after all
	
voidexecuteInternal()

		final String [] operands	= getOperands();
		
		// at least one argument
		if ( operands.length > 1 )
		{
			throw new IllegalArgumentException( "requires 0 or 1 operands" );
		}
		
		if ( getBoolean( "list", null ) != null )
		{
			listNamedConnections();
			println( "" );
			listActiveConnections();
		}
		else if ( countOptions() == 0 )
		{
			if ( operands.length == 0 )
			{
				// connect to default connection
				final ConnectInfo	connectInfo	= getDefaultConnectInfo();
				if ( connectInfo != null )
				{
					handleNamedConnect( DEFAULT_CONNECTION_NAME );
				}
				else
				{
					printError( "No default connection available.  Please specify a connect string.\n" );
					printUsage();
				}
			}
			else
			{
				// named connection
				handleNamedConnect( operands[ 0 ] );
			}
		}
		else
		{
			final String	host			= getString( "host", "localhost" );
			final String	port			= getString( "port", null );
			final String	protocol		= getString( "protocol", "jmxmp" );
			final String	user			= getString( "user", null );
			final String	passwordFile	= getString( "password-file", null );
			final String	options			= getString( "options", null );
			
			final HashMap	params	= new HashMap();
			maybePut( params, JMXConnectorProvider.HOST, host );
			maybePut( params, JMXConnectorProvider.PORT, port );
			maybePut( params, JMXConnectorProvider.PROTOCOL, protocol );
			maybePut( params, JMXConnectorProvider.USER, user );
			maybePut( params, JMXConnectorProvider.PASSWORD_FILE, passwordFile );
		
		
			final String	connectionName	= (operands.length > 0) ?
									operands[ 0 ] : DEFAULT_CONNECTION_NAME;
			
			handleConnect( connectionName, new ConnectInfo( params ) );
		}
	
ConnectInfogetDefaultConnectInfo()

		final String	envName	= connectionNameToEnvName( DEFAULT_CONNECTION_NAME );
		
		final String	defaultString	= (String)envGet( envName );
		
		final ConnectInfo connectInfo	= defaultString == null ?
			null : new ConnectInfo( defaultString );
		
		return( connectInfo );
	
public static java.lang.String[]getNames()

	
		   
	 
	
		return( new String [] { NAME, NAME_ABBREV } );
	
intgetNumRequiredOperands()

		// require 1, by default
		return( 0 );
	
ArgHelper.OptionsInfogetOptionInfo()

		
		
	
		 
	
		return( new ArgHelperOptionsInfo( OPTIONS_INFO ) );
	
static com.sun.cli.jmx.spi.JMXConnectorProviderInfogetProviderInfo(java.lang.Class theClass)

		return( JMXConnectorProviderInfo.InfoGetter.getInfo( theClass ) );
	
public java.lang.StringgetUsage()

		final String usage	= CmdStrings.CONNECT_HELP.toString();
		
		String extra	= null;
			
		try
		{
			extra	= "\n\nConnector usage:\n";
		
			final JMXConnectorProvider []	providers	= getConnectionMgr().getProviders();
			
			
			for ( int i = 0; i < providers.length; ++i )
			{
				extra	= extra + providers[ i ].getClass().getName() + ":\n";
				
				final JMXConnectorProviderInfo	info	= getProviderInfo( providers[ i ].getClass() );
				
				if ( info != null )
				{
					extra	= extra + info.getUsage() + "\n";
				}
				else
				{
					extra	= extra + "<none available>";
				}
				
				extra	= extra + "\n";
			}
		}
		catch( Exception e )
		{
		}
		
		return( usage + extra  );
	
voidhandleConnect(java.lang.String name, ConnectInfo connectInfo)

		envRemove( connectionNameToEnvName( name ) );
		envPut( connectionNameToEnvName( name ), connectInfo.toString(), true);
		handleNamedConnect( name );
	
voidhandleNamedConnect(java.lang.String name)

		// see if it's a name for an existing connection
		final String connectString = (String)envGet( connectionNameToEnvName( name ) );
		if ( connectString != null )
		{
			final ConnectInfo	connectInfo	= new ConnectInfo( connectString );
			
			if ( connectInfo.equals( getConnectionMgr().getConnectInfo( name ) ) )
			{
				println( "Connection already active: " + connectInfo.toString() );
				setProxy( name, connectInfo );
			}
			else
			{
				setProxy( name, connectInfo );
				
				println( "Connection " + name +
					" (" + connectInfo.toString() + ") is now the active connection" );
			}
			envPut( connectionNameToEnvName( DEFAULT_CONNECTION_NAME ), connectInfo.toString(), true);
		}
		else
		{
			printError( "ERROR: No such named connection: " + name );
		}
	
booleanisNamedConnection(java.lang.String name)

		final String key	= connectionNameToEnvName( name );
		
		final String value	= (String)envGet( key );
		
		return( value != null );
	
booleanisValidConnectionName(java.lang.String name)

		return( name.indexOf( ':" ) < 0 && name.indexOf( '@" ) < 0 );
	
voidlistActiveConnections()

		println( "Active connections:" );
		
		final ConnectionMgr	mgr		= getConnectionMgr();
		final Iterator		iter	= mgr.getNames().iterator();
		
		while ( iter.hasNext() )
		{
			final String	name	= (String)iter.next();
			
			println( name + ": " + mgr.getConnectInfo( name ) );
		}
	
voidlistNamedConnections()

		println( "Named connections:" );
		
		final Iterator	iter	= getEnvKeys( ENV_CONNECT_NAME_BASE + ".+" ).iterator();
		
		while ( iter.hasNext() )
		{
			final String	envName	= (String)iter.next();
			final String	value	= (String)envGet( envName );
			
			println( envNameToConnectionName( envName ) + ": " + new ConnectInfo( value ) );
		}
	
static voidmaybePut(java.util.Map m, java.lang.String key, java.lang.Object value)

		if ( value != null )
		{
			m.put( key, value );
		}