1 23 24 29 30 package com.sun.cli.jmx.test; 31 32 import java.lang.reflect.Array ; 35 import java.util.Arrays ; 36 37 import javax.management.ObjectName ; 40 import javax.management.StandardMBean ; 41 import javax.management.MBeanServer ; 42 import javax.management.MBeanServerConnection ; 43 import javax.management.MBeanServerInvocationHandler ; 44 import javax.management.MBeanServerFactory ; 45 import javax.management.MalformedObjectNameException ; 46 import javax.management.remote.JMXServiceURL ; 47 import javax.management.remote.jmxmp.JMXMPConnectorServer; 48 import javax.management.Attribute ; 49 50 52 import com.sun.cli.jmx.support.AliasMgr; 53 import com.sun.cli.jmx.support.AliasMgrMBean; 54 import com.sun.cli.jmx.support.AliasMgrHashMapImpl; 55 import com.sun.cli.jmx.support.CLISupport; 56 import com.sun.cli.jmx.support.CLISupportMBeanProxy; 57 58 import com.sun.cli.jmx.support.CLISupportStrings; 59 import com.sun.cli.jmx.support.CLISupport; 60 import com.sun.cli.jmx.support.AliasMgrHashMapImpl; 61 62 import com.sun.cli.jmx.test.CLISupportTester; 63 import com.sun.cli.jmx.test.CLISupportTestee; 64 import com.sun.cli.jmx.support.StringifierRegistryIniter; 65 import com.sun.cli.jmx.support.StandardAliasesIniter; 66 import com.sun.cli.util.stringifier.SmartStringifier; 67 68 69 import com.sun.cli.jmx.cmd.ArgHelperImpl; 70 import com.sun.cli.jmx.cmd.ArgHelperOptionsInfo; 71 72 import com.sun.enterprise.jmx.kstat.kstatMgr; 73 74 76 77 public class TestServer 78 { 79 MBeanServer mServer; 80 final boolean mTestInProcess; 81 82 private MBeanServer 83 createAgent( ) 84 { 85 final MBeanServer server = MBeanServerFactory.createMBeanServer( "Test" ); 86 87 return( server ); 88 } 89 90 91 private void 92 registerConnectors( int connectorPort ) 93 { 94 try 96 { 97 final JMXServiceURL url = new JMXServiceURL ( "service:jmx:jmxmp://localhost:" + connectorPort ); 98 final JMXMPConnectorServer connector = new JMXMPConnectorServer( url, null, mServer ); 99 100 final String name = ":name=JMXMPConnectorServer,type=connector,port=" + connectorPort; 101 registerMBean( mServer, connector, name); 102 connector.start(); 103 } 104 catch( Exception e ) 105 { 106 System.out.println("\tCould not create the StandardConnector"); 107 e.printStackTrace(); 108 } 109 110 } 111 112 private void 113 registerAdapters( int adapterPort ) 114 { 115 130 } 131 132 133 private static void 134 p( Object arg ) 135 { 136 System.out.println( arg.toString() ); 137 } 138 139 140 private void 141 registerMBean( MBeanServer conn, Object mbean, String name ) 142 throws Exception 143 { 144 conn.registerMBean( mbean, new ObjectName ( name ) ); 145 p( "registered object: " + name ); 146 } 147 148 public interface DottedNameTesterMBean 149 { 150 public String getString(); 151 public void setString( String value ); 152 153 public Boolean getBoolean(); 154 public void setBoolean( Boolean value ); 155 } 156 157 private static class DottedNameTester implements DottedNameTesterMBean 158 { 159 String mString; 160 Boolean mBoolean; 161 Integer mInteger; 162 163 public 164 DottedNameTester() 165 { 166 super(); 167 168 mString = "string"; 169 mBoolean = Boolean.FALSE; 170 mInteger = new Integer ( 0 ); 171 172 } 173 174 public String 175 getString() 176 { 177 return( mString ); 178 } 179 180 public void 181 setString( String s) 182 { 183 mString = s; 184 } 185 186 187 public Boolean 188 getBoolean() 189 { 190 return( mBoolean ); 191 } 192 193 public void 194 setBoolean( Boolean b) 195 { 196 mBoolean = b; 197 } 198 } 199 200 265 266 private void 267 AddMBeans( MBeanServer conn ) 268 throws Exception 269 { 270 final AliasMgrHashMapImpl aliasImpl = new AliasMgrHashMapImpl(); 272 try 273 { 274 aliasImpl.load( new java.io.File ( AliasMgrHashMapImpl.DEFAULT_FILENAME ) ); 275 } 276 catch( Exception e ) 277 { 278 } 280 final AliasMgr aliasMgr = new AliasMgr( aliasImpl ); 281 282 StandardAliasesIniter.init( aliasMgr ); 283 284 registerMBean( conn, aliasMgr , CLISupportStrings.ALIAS_MGR_TARGET ); 285 286 287 289 final CLISupport cliSupport = new CLISupport( conn, aliasMgr); 290 291 registerMBean( conn, cliSupport, CLISupportStrings.CLI_SUPPORT_TARGET ); 292 293 295 registerMBean( conn, new CLISupportTestee( ), CLISupportStrings.CLI_SUPPORT_TESTEE_TARGET ); 296 297 registerMBean( conn, new CLISupportSimpleTestee( ), CLISupportStrings.CLI_SIMPLE_TESTEE_TARGET ); 298 299 registerMBean( conn, new kstatMgr( ), "kstat:name=kstat-mgr,type=kstat-mgr" ); 300 301 } 302 303 private 304 TestServer( final int port, final boolean testInProcess ) throws Exception 305 { 306 mTestInProcess = testInProcess; 307 308 new StringifierRegistryIniter(); 309 310 mServer = createAgent( ); 311 registerConnectors( port ); 312 registerAdapters( 8082 ); 313 314 AddMBeans( mServer ); 315 } 316 317 public static void 318 main(String args[]) 319 { 320 try 321 { 322 final ArgHelperOptionsInfo optionInfo = new ArgHelperOptionsInfo( ); 323 optionInfo.addOptions( "port,1 testInProcess" ); 324 325 final ArgHelperImpl argHelper = new ArgHelperImpl( Arrays.asList( args ).listIterator(), optionInfo); 326 327 final Integer port = argHelper.getInteger( "--port" ); 328 if ( port == null ) 329 { 330 System.out.println( "USAGE: TestServer --port=<port-number> [--testInProcess]" ); 331 System.exit( 1 ); 332 } 333 final Boolean testInProcess = argHelper.getBoolean( "testInProcess", Boolean.FALSE ); 334 335 final TestServer server = new TestServer( port.intValue(), testInProcess.booleanValue() ); 336 337 if ( testInProcess.booleanValue() ) 338 { 339 final AliasMgrHashMapImpl aliasMgrImpl = new AliasMgrHashMapImpl(); 340 aliasMgrImpl.load( new java.io.File ( AliasMgrHashMapImpl.DEFAULT_FILENAME ) ); 341 final AliasMgr aliasMgr = new AliasMgr( aliasMgrImpl ); 342 final CLISupport cliSupport = new CLISupport( server.mServer, aliasMgr ); 343 344 final CLISupportMBeanProxy proxy = new CLISupportMBeanProxy( aliasMgr, cliSupport ) ; 345 346 final CLISupportTester tester = new CLISupportTester( server.mServer, proxy ); 347 tester.Run(); 348 } 349 350 p( "Server is running." ); 351 } 352 catch( Exception e ) 353 { 354 p( e ); 355 } 356 } 357 }; 358 359 360 | Popular Tags |