1 23 24 29 30 package com.sun.cli.jmx.cmd; 31 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.util.Arrays ; 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 o ) 50 { 51 System.out.println( SmartStringifier.toString( o ) ); 52 } 53 54 public 55 CmdMgr( ) 56 throws Exception 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 [] 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 96 { 97 final CmdFactoryIniter initer = new CmdFactoryIniter( mCmdFactory, CMDS ); 98 99 final ConfigureCmd.ClassList list = 101 new ConfigureCmd.ClassList( (String )mCmdEnv.get( CmdBase.ENV_COMMANDS ) ); 102 final java.util.Iterator iter = list.iterator(); 103 while ( iter.hasNext() ) 104 { 105 final String classname = (String )iter.next(); 106 107 initer.addMappings( ClassUtil.getClassFromName( classname ) ); 108 } 109 } 110 111 void 112 handleSingle( String [] args ) 113 throws Exception 114 { 115 final String OPTIONS = "connect,1"; 117 118 final ArgHelperImpl argHelper = new ArgHelperImpl( Arrays.asList( args ).listIterator(), 119 new ArgHelperOptionsInfo( OPTIONS )); 120 121 String lines = ""; 122 123 final String connectString = argHelper.getString( "connect", null ); 124 if ( connectString != null ) 125 { 126 CmdReader.processLine( "connect " + connectString, mCmdRunner ); 127 } 128 129 final String [] remainder = argHelper.getOperands(); 131 132 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 t ) 147 { 148 dm( "Exception from processing commands: " + t.getMessage() ); 149 t.printStackTrace(); 150 } 151 } 152 153 public void 154 run( final String [] args ) 155 throws Exception 156 { 157 final File file = JMXAdminFileNames.getPropsFile(); 158 mCmdEnv.load( file ); 159 160 initCmds(); 161 162 163 if ( args.length == 0 || (args.length == 1 && args[ 0 ].equals( "multi" ) ) ) 165 { 166 handleInteractive(); 167 } 168 else 169 { 170 handleSingle( args ); 171 } 172 173 Thread.sleep( 100 ); 176 mCmdEnv.store( file ); 177 } 178 } 179 180 | Popular Tags |