KickJava   Java API By Example, From Geeks To Geeks.

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


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

29  
30 package com.sun.cli.jmx.cmd;
31
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.Arrays JavaDoc;
35
36 import com.sun.cli.util.stringifier.ArrayStringifier;
37 import com.sun.cli.util.stringifier.SmartStringifier;
38 import com.sun.cli.util.LineReaderImpl;
39 import com.sun.cli.util.ClassUtil;
40 import com.sun.cli.jmx.cmd.CmdFactoryIniter;
41
42 public final class CmdMgr
43 {
44     final CmdEnvImpl mCmdEnv;
45     final CmdFactory mCmdFactory;
46     final CmdRunner mCmdRunner;
47     
48         private static void
49     dm( Object JavaDoc o )
50     {
51         System.out.println( SmartStringifier.toString( o ) );
52     }
53     
54         public
55     CmdMgr( )
56         throws Exception JavaDoc
57     {
58         mCmdEnv = new CmdEnvImpl();
59         mCmdFactory = new CmdFactory( );
60         mCmdRunner = new CmdRunnerImpl( mCmdFactory, mCmdEnv );
61         
62         mCmdEnv.put( CmdBase.ENV_CMD_RUNNER, mCmdRunner, false);
63         mCmdEnv.put( CmdBase.ENV_CMD_FACTORY, mCmdFactory, false);
64     }
65
66
67     
68         
69     private final static Class JavaDoc [] CMDS =
70     {
71         GetCmd.class,
72         SetCmd.class,
73         FindCmd.class,
74         InspectCmd.class,
75         CreateCmd.class,
76         DeleteCmd.class,
77         ListenCmd.class,
78         InvokeCmd.class,
79         CreateAliasCmd.class,
80         DeleteAliasCmd.class,
81         ResolveAliasCmd.class,
82         ListAliasesCmd.class,
83         TargetCmd.class,
84         ConnectCmd.class,
85         HelpCmd.class,
86         SourceCmd.class,
87         CountCmd.class,
88         DomainsCmd.class,
89         ConfigureCmd.class,
90         SetenvCmd.class,
91     };
92         
93         private void
94     initCmds()
95         throws Exception JavaDoc
96     {
97         final CmdFactoryIniter initer = new CmdFactoryIniter( mCmdFactory, CMDS );
98         
99         // initialize all non-built-in commands
100
final ConfigureCmd.ClassList list =
101                 new ConfigureCmd.ClassList( (String JavaDoc)mCmdEnv.get( CmdBase.ENV_COMMANDS ) );
102         final java.util.Iterator JavaDoc iter = list.iterator();
103         while ( iter.hasNext() )
104         {
105             final String JavaDoc classname = (String JavaDoc)iter.next();
106             
107             initer.addMappings( ClassUtil.getClassFromName( classname ) );
108         }
109     }
110     
111         void
112     handleSingle( String JavaDoc [] args )
113         throws Exception JavaDoc
114     {
115         // one-off command mode. Only allowed meta option is a connect string "--connect"
116
final String JavaDoc OPTIONS = "connect,1";
117                     
118         final ArgHelperImpl argHelper = new ArgHelperImpl( Arrays.asList( args ).listIterator(),
119                                 new ArgHelperOptionsInfo( OPTIONS ));
120         
121         String JavaDoc lines = "";
122         
123         final String JavaDoc connectString = argHelper.getString( "connect", null );
124         if ( connectString != null )
125         {
126             CmdReader.processLine( "connect " + connectString, mCmdRunner );
127         }
128         
129         // now execute the remainder of the line
130
final String JavaDoc [] remainder = argHelper.getOperands();
131         
132         // the first operand is the command name
133
mCmdRunner.execute( remainder[ 0 ], remainder );
134     }
135         
136         void
137     handleInteractive( )
138     {
139         final LineReaderImpl lineReader = new LineReaderImpl( System.in );
140         final CmdReader reader = new CmdReader();
141         
142         try
143         {
144             reader.goInteractive( lineReader, mCmdRunner );
145         }
146         catch( Throwable JavaDoc t )
147         {
148             dm( "Exception from processing commands: " + t.getMessage() );
149             t.printStackTrace();
150         }
151     }
152     
153         public void
154     run( final String JavaDoc [] args )
155         throws Exception JavaDoc
156     {
157         final File JavaDoc file = JMXAdminFileNames.getPropsFile();
158         mCmdEnv.load( file );
159         
160         initCmds();
161         
162             
163         // if there are no arguments, start up in interactive mode
164
if ( args.length == 0 || (args.length == 1 && args[ 0 ].equals( "multi" ) ) )
165         {
166             handleInteractive();
167         }
168         else
169         {
170             handleSingle( args );
171         }
172         
173         // CAUTION: if a ctrl-c is received, the JVM seems to kill us half-way through
174
// storing mCmdEnv, which ends up destroying it. Pause briefly to avoid this (hack).
175
Thread.sleep( 100 );
176         mCmdEnv.store( file );
177     }
178 }
179
180
Popular Tags