KickJava   Java API By Example, From Geeks To Geeks.

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


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

29  
30 package com.sun.cli.jmx.cmd;
31
32
33 import com.sun.cli.jmx.support.ResultsForGetSet;
34 import com.sun.cli.jmx.support.CLISupportMBeanProxy;
35 import com.sun.cli.jmx.support.InvokeResult;
36 import com.sun.cli.util.stringifier.*;
37
38 public class InvokeCmd extends JMXCmd
39 {
40         public
41     InvokeCmd( final CmdEnv env )
42     {
43         super( env );
44     }
45     
46         int
47     getNumRequiredOperands()
48     {
49         // require 1, by default
50
return( 0 );
51     }
52     
53         public String JavaDoc
54     getUsage()
55     {
56         return( CmdStrings.INVOKE_HELP.toString() );
57     }
58     
59     public static String JavaDoc FORMAL_NAME = "invoke";
60     
61         public static String JavaDoc []
62     getNames( )
63     {
64         return( new String JavaDoc [] { FORMAL_NAME, CmdFactory.DEFAULT_CMD_NAME} );
65     }
66     
67     /*
68         Allow two forms of invocation:
69         doIt:[args] [targets]
70         alias.doIt:[args]
71      */

72         void
73     executeInternal()
74         throws Exception JavaDoc
75     {
76         final String JavaDoc cmdString = getCmdNameAsInvoked();
77         String JavaDoc [] targets = getTargets();
78         
79         final int colonPos = cmdString.indexOf( ":" );
80         
81         if ( cmdString.equals( FORMAL_NAME ) )
82         {
83             printUsage();
84             return;
85         }
86         else if ( colonPos < 0 )
87         {
88             printError( CmdStrings.NO_SUCH_COMMAND + cmdString );
89             return;
90         }
91         
92         
93         String JavaDoc argList = null;
94         String JavaDoc methodName = null;
95         final int dotIndex = cmdString.indexOf( '.' );
96         if ( dotIndex > 0 && dotIndex < colonPos )
97         {
98             requireNumOperands( 0 );
99             targets = new String JavaDoc [] { cmdString.substring( 0, dotIndex ) };
100             methodName = cmdString.substring( dotIndex + 1, colonPos );
101         }
102         else
103         {
104             
105             methodName = cmdString.substring( 0, colonPos );
106         }
107         
108         if ( colonPos + 1 != cmdString.length() )
109         {
110             argList = cmdString.substring( colonPos + 1, cmdString.length() );
111         }
112         else
113         {
114             argList = null;
115         }
116         
117         
118         //p( "INVOKE: " + methodName + "(" + argList + ") on " + ArrayStringifier.stringify( targets, " " ) );
119

120         establishProxy();
121
122         final InvokeResult [] results = invoke( methodName, argList, targets );
123         
124         getProxy().mbeanInvoke( methodName, argList, targets );
125         
126         if ( results.length == 0 )
127         {
128             println( "Invocation failed: no targets found that match the expression" );
129         }
130         else
131         {
132             displayResults( results );
133         }
134     }
135     
136         void
137     displayResults( final InvokeResult [] results )
138     {
139         println( new SmartStringifier( "\n", false ).stringify( results ) );
140     }
141     
142         InvokeResult []
143     invoke( String JavaDoc operationName, final String JavaDoc argList, final String JavaDoc [] targets )
144         throws Exception JavaDoc
145     {
146         final InvokeResult [] results = getProxy().mbeanInvoke( operationName, argList, targets );
147         
148         return( results );
149     }
150 }
151
152
153
154
Popular Tags