KickJava   Java API By Example, From Geeks To Geeks.

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


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

29  
30
31 package com.sun.cli.jmx.cmd;
32
33 import java.net.MalformedURLException JavaDoc;
34
35 import javax.management.remote.JMXServiceURL JavaDoc;
36 import javax.management.remote.JMXConnector JavaDoc;
37 import javax.management.remote.JMXConnectorFactory JavaDoc;
38 import javax.management.MBeanServerConnection JavaDoc;
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 JavaDoc [] ALL_TARGET = new String JavaDoc [] { "all" };
57     
58     public final static String JavaDoc ENV_PROXY = "PROXY";
59     public final static String JavaDoc ENV_CONNECTION = "CONNECTION";
60     public final static String JavaDoc ENV_TARGET = "TARGET";
61     public final static String JavaDoc ENV_CONNECT_NAME_BASE = "CONNECT_";
62     public final static String JavaDoc ENV_ALIAS_MGR = "ALIAS_MGR";
63     public final static String JavaDoc ENV_PROVIDERS = "PROVIDERS";
64     
65     final static String JavaDoc 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         // force initialization
78
}
79     
80     
81         ConnectionMgr
82     getConnectionMgr( )
83         throws Exception JavaDoc
84     {
85         initConnectionMgr();
86         return( sConnectionMgr );
87     }
88     
89         void
90     initConnectionMgr( )
91         throws IllegalAccessException JavaDoc, InstantiationException JavaDoc, ClassNotFoundException JavaDoc
92     {
93         if ( sConnectionMgr != null )
94             return;
95             
96         sConnectionMgr = new ConnectionMgrImpl( );
97         
98         final String JavaDoc providersString = ((String JavaDoc)envGet( ENV_PROVIDERS, "")).trim();
99         
100         if ( providersString != null && providersString.length() != 0 )
101         {
102             final String JavaDoc [] providers = providersString.split( "," );
103             
104             for( int i = 0; i < providers.length; ++i )
105             {
106                 try
107                 {
108                     final Class JavaDoc theClass = Class.forName( providers[ i ] );
109                 
110                     sConnectionMgr.addProvider( theClass );
111                     println( "Added JMXConnectorProvider: " + providers[ i ] );
112                 }
113                 catch( Exception JavaDoc 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 JavaDoc 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 JavaDoc conn )
146         throws Exception JavaDoc
147     {
148         final CLISupportMBean cliSupport = new CLISupport( conn, sAliasMgr);
149         
150         return( cliSupport );
151     }
152     
153         String JavaDoc
154     connectionNameToEnvName( String JavaDoc connectionName )
155     {
156         return( ENV_CONNECT_NAME_BASE + connectionName );
157     }
158     
159         String JavaDoc
160     envNameToConnectionName( String JavaDoc envName )
161     {
162         return( envName.substring( ENV_CONNECT_NAME_BASE.length(), envName.length() ) );
163     }
164     
165         CLISupportMBeanProxy
166     setProxy( String JavaDoc name, ConnectInfo connectInfo )
167         throws Exception JavaDoc
168     {
169         JMXConnector JavaDoc jmxConnector = null;
170         
171         try
172         {
173             jmxConnector = getConnectionMgr().connect( name, connectInfo );
174         }
175         catch( java.io.IOException JavaDoc e )
176         {
177             throw new Exception JavaDoc( e.getMessage() + " (" + connectInfo.toString() + ")", e);
178         }
179         
180         final MBeanServerConnection JavaDoc 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 JavaDoc name, ConnectInfo connectInfo )
202         throws Exception JavaDoc
203     {
204         CLISupportMBeanProxy proxy = null;
205         
206         proxy = setProxy( name, connectInfo );
207         
208         final String JavaDoc host = connectInfo.getParam( JMXConnectorProvider.HOST );
209         final String JavaDoc port = connectInfo.getParam( JMXConnectorProvider.PORT );
210         String JavaDoc 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 JavaDoc
226     {
227         final String JavaDoc 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 JavaDoc
237     {
238         if ( envGet( ENV_PROXY ) == null )
239         {
240             final String JavaDoc envName = connectionNameToEnvName( DEFAULT_CONNECTION_NAME );
241         
242             final String JavaDoc connectString = (String JavaDoc)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 JavaDoc( "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 JavaDoc
264     getConnection()
265     {
266         final MBeanServerConnection JavaDoc conn = (MBeanServerConnection JavaDoc)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 JavaDoc
282     {
283         getConnectionMgr();
284         super.preExecute();
285     }
286     
287         void
288     handleException( final Exception JavaDoc e )
289     {
290         super.handleException( e );
291     
292         if ( e instanceof java.io.IOException JavaDoc )
293         {
294             clearProxy();
295         }
296     }
297     
298     
299         String JavaDoc []
300     getEnvTargets()
301     {
302         final String JavaDoc targetsEnv = (String JavaDoc)envGet( ENV_TARGET );
303         if ( targetsEnv == null )
304             return( null );
305         
306         // convert to String []
307
final String JavaDoc [] targets = (String JavaDoc [])
308                             TargetPersister.DEFAULT.asObject( targetsEnv );
309         
310         return( targets );
311     }
312     
313         void
314     putEnvTargets( String JavaDoc [] targets )
315     {
316         // put targets into environment
317
final String JavaDoc targetsStr = TargetPersister.DEFAULT.asString( targets );
318         envPut( ENV_TARGET, targetsStr, true);
319     }
320     
321         String JavaDoc []
322     getTargets()
323     {
324         String JavaDoc [] 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 JavaDoc TARGET_DELIM = " ";
339         
340             public String JavaDoc
341         asString( Object JavaDoc value )
342         {
343             return( ArrayStringifier.stringify( (String JavaDoc [])value, TARGET_DELIM ) );
344         }
345         
346             public Object JavaDoc
347         asObject( String JavaDoc value )
348         {
349             return( value.split( TARGET_DELIM ) );
350         }
351     }
352 };
353
354
355
Popular Tags