KickJava   Java API By Example, From Geeks To Geeks.

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


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

29  
30 package com.sun.cli.jmx.cmd;
31
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 import javax.management.MBeanServerConnection JavaDoc;
38
39 import com.sun.cli.util.stringifier.ArrayStringifier;
40 import com.sun.cli.util.StringValuePersister;
41 import com.sun.cli.jmx.support.CLISupportMBeanProxy;
42 import com.sun.cli.util.stringifier.IteratorStringifier;
43 import com.sun.cli.util.ClassUtil;
44
45 import com.sun.cli.jmx.spi.JMXConnectorProvider;
46
47 import com.sun.cli.jmx.spi.JMXConnectorProviderInfo;
48
49
50 /*
51     Invokes the 'list' commmand on all specified targets
52  */

53 public class ConnectCmd extends JMXCmd
54 {
55         public
56     ConnectCmd( final CmdEnv env )
57     {
58         super( env );
59     }
60     
61         int
62     getNumRequiredOperands()
63     {
64         // require 1, by default
65
return( 0 );
66     }
67     
68         static JMXConnectorProviderInfo
69     getProviderInfo( final Class JavaDoc theClass )
70         throws Exception JavaDoc
71     {
72         return( JMXConnectorProviderInfo.InfoGetter.getInfo( theClass ) );
73     }
74     
75         public String JavaDoc
76     getUsage()
77     {
78         final String JavaDoc usage = CmdStrings.CONNECT_HELP.toString();
79         
80         String JavaDoc extra = null;
81             
82         try
83         {
84             extra = "\n\nConnector usage:\n";
85         
86             final JMXConnectorProvider [] providers = getConnectionMgr().getProviders();
87             
88             
89             for ( int i = 0; i < providers.length; ++i )
90             {
91                 extra = extra + providers[ i ].getClass().getName() + ":\n";
92                 
93                 final JMXConnectorProviderInfo info = getProviderInfo( providers[ i ].getClass() );
94                 
95                 if ( info != null )
96                 {
97                     extra = extra + info.getUsage() + "\n";
98                 }
99                 else
100                 {
101                     extra = extra + "<none available>";
102                 }
103                 
104                 extra = extra + "\n";
105             }
106         }
107         catch( Exception JavaDoc e )
108         {
109         }
110         
111         return( usage + extra );
112     }
113     
114     
115     static private final String JavaDoc OPTIONS_INFO =
116     "list host,1 port,1 user,1 password-file,1 protocol,1 options,1";
117         
118         ArgHelper.OptionsInfo
119     getOptionInfo()
120         throws ArgHelper.IllegalOptionException
121     {
122         return( new ArgHelperOptionsInfo( OPTIONS_INFO ) );
123     }
124
125     
126         void
127     establishProxy()
128         throws Exception JavaDoc
129     {
130         // defeat default behavior; this is the 'connect' command after all
131
}
132     
133         ConnectInfo
134     getDefaultConnectInfo()
135         throws com.sun.cli.jmx.support.ArgParserException
136     {
137         final String JavaDoc envName = connectionNameToEnvName( DEFAULT_CONNECTION_NAME );
138         
139         final String JavaDoc defaultString = (String JavaDoc)envGet( envName );
140         
141         final ConnectInfo connectInfo = defaultString == null ?
142             null : new ConnectInfo( defaultString );
143         
144         return( connectInfo );
145     }
146     
147     final static String JavaDoc NAME = "connect";
148     final static String JavaDoc NAME_ABBREV = "c";
149     
150         public static String JavaDoc []
151     getNames( )
152     {
153         return( new String JavaDoc [] { NAME, NAME_ABBREV } );
154     }
155         
156     
157         boolean
158     isValidConnectionName( String JavaDoc name )
159     {
160         return( name.indexOf( ':' ) < 0 && name.indexOf( '@' ) < 0 );
161     }
162     
163     
164     
165         boolean
166     isNamedConnection( String JavaDoc name )
167     {
168         final String JavaDoc key = connectionNameToEnvName( name );
169         
170         final String JavaDoc value = (String JavaDoc)envGet( key );
171         
172         return( value != null );
173     }
174     
175         void
176     handleNamedConnect( String JavaDoc name )
177         throws Exception JavaDoc
178     {
179         // see if it's a name for an existing connection
180
final String JavaDoc connectString = (String JavaDoc)envGet( connectionNameToEnvName( name ) );
181         if ( connectString != null )
182         {
183             final ConnectInfo connectInfo = new ConnectInfo( connectString );
184             
185             if ( connectInfo.equals( getConnectionMgr().getConnectInfo( name ) ) )
186             {
187                 println( "Connection already active: " + connectInfo.toString() );
188                 setProxy( name, connectInfo );
189             }
190             else
191             {
192                 setProxy( name, connectInfo );
193                 
194                 println( "Connection " + name +
195                     " (" + connectInfo.toString() + ") is now the active connection" );
196             }
197             envPut( connectionNameToEnvName( DEFAULT_CONNECTION_NAME ), connectInfo.toString(), true);
198         }
199         else
200         {
201             printError( "ERROR: No such named connection: " + name );
202         }
203     }
204     
205         void
206     handleConnect( final String JavaDoc name, final ConnectInfo connectInfo )
207         throws Exception JavaDoc
208     {
209         envRemove( connectionNameToEnvName( name ) );
210         envPut( connectionNameToEnvName( name ), connectInfo.toString(), true);
211         handleNamedConnect( name );
212     }
213     
214         static void
215     maybePut( Map JavaDoc m, String JavaDoc key, Object JavaDoc value )
216     {
217         if ( value != null )
218         {
219             m.put( key, value );
220         }
221     }
222     
223         void
224     listNamedConnections()
225         throws Exception JavaDoc
226     {
227         println( "Named connections:" );
228         
229         final Iterator JavaDoc iter = getEnvKeys( ENV_CONNECT_NAME_BASE + ".+" ).iterator();
230         
231         while ( iter.hasNext() )
232         {
233             final String JavaDoc envName = (String JavaDoc)iter.next();
234             final String JavaDoc value = (String JavaDoc)envGet( envName );
235             
236             println( envNameToConnectionName( envName ) + ": " + new ConnectInfo( value ) );
237         }
238     }
239     
240     
241         void
242     listActiveConnections()
243         throws Exception JavaDoc
244     {
245         println( "Active connections:" );
246         
247         final ConnectionMgr mgr = getConnectionMgr();
248         final Iterator JavaDoc iter = mgr.getNames().iterator();
249         
250         while ( iter.hasNext() )
251         {
252             final String JavaDoc name = (String JavaDoc)iter.next();
253             
254             println( name + ": " + mgr.getConnectInfo( name ) );
255         }
256     }
257     
258     
259         void
260     executeInternal()
261         throws Exception JavaDoc
262     {
263         final String JavaDoc [] operands = getOperands();
264         
265         // at least one argument
266
if ( operands.length > 1 )
267         {
268             throw new IllegalArgumentException JavaDoc( "requires 0 or 1 operands" );
269         }
270         
271         if ( getBoolean( "list", null ) != null )
272         {
273             listNamedConnections();
274             println( "" );
275             listActiveConnections();
276         }
277         else if ( countOptions() == 0 )
278         {
279             if ( operands.length == 0 )
280             {
281                 // connect to default connection
282
final ConnectInfo connectInfo = getDefaultConnectInfo();
283                 if ( connectInfo != null )
284                 {
285                     handleNamedConnect( DEFAULT_CONNECTION_NAME );
286                 }
287                 else
288                 {
289                     printError( "No default connection available. Please specify a connect string.\n" );
290                     printUsage();
291                 }
292             }
293             else
294             {
295                 // named connection
296
handleNamedConnect( operands[ 0 ] );
297             }
298         }
299         else
300         {
301             final String JavaDoc host = getString( "host", "localhost" );
302             final String JavaDoc port = getString( "port", null );
303             final String JavaDoc protocol = getString( "protocol", "jmxmp" );
304             final String JavaDoc user = getString( "user", null );
305             final String JavaDoc passwordFile = getString( "password-file", null );
306             final String JavaDoc options = getString( "options", null );
307             
308             final HashMap JavaDoc params = new HashMap JavaDoc();
309             maybePut( params, JMXConnectorProvider.HOST, host );
310             maybePut( params, JMXConnectorProvider.PORT, port );
311             maybePut( params, JMXConnectorProvider.PROTOCOL, protocol );
312             maybePut( params, JMXConnectorProvider.USER, user );
313             maybePut( params, JMXConnectorProvider.PASSWORD_FILE, passwordFile );
314         
315         
316             final String JavaDoc connectionName = (operands.length > 0) ?
317                                     operands[ 0 ] : DEFAULT_CONNECTION_NAME;
318             
319             handleConnect( connectionName, new ConnectInfo( params ) );
320         }
321     }
322     
323 }
324
325
326
327
328
329
330
Popular Tags