KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > test > TestServer


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

29  
30 package com.sun.cli.jmx.test;
31
32 // java imports
33
//
34
import java.lang.reflect.Array JavaDoc;
35 import java.util.Arrays JavaDoc;
36
37 // RI imports
38
//
39
import javax.management.ObjectName JavaDoc;
40 import javax.management.StandardMBean JavaDoc;
41 import javax.management.MBeanServer JavaDoc;
42 import javax.management.MBeanServerConnection JavaDoc;
43 import javax.management.MBeanServerInvocationHandler JavaDoc;
44 import javax.management.MBeanServerFactory JavaDoc;
45 import javax.management.MalformedObjectNameException JavaDoc;
46 import javax.management.remote.JMXServiceURL JavaDoc;
47 import javax.management.remote.jmxmp.JMXMPConnectorServer;
48 import javax.management.Attribute JavaDoc;
49
50 //import com.sun.jdmk.comm.HtmlAdaptorServer;
51

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 //import com.sun.jdmk.comm.HtmlAdaptorServer;
75

76
77 public class TestServer
78 {
79     MBeanServer JavaDoc mServer;
80     final boolean mTestInProcess;
81     
82         private MBeanServer JavaDoc
83     createAgent( )
84     {
85         final MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer( "Test" );
86         
87         return( server );
88     }
89     
90     
91         private void
92     registerConnectors( int connectorPort )
93     {
94         // create the StandardConnector
95
try
96         {
97             final JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc( "service:jmx:jmxmp://localhost:" + connectorPort );
98             final JMXMPConnectorServer connector = new JMXMPConnectorServer( url, null, mServer );
99             
100             final String JavaDoc name = ":name=JMXMPConnectorServer,type=connector,port=" + connectorPort;
101             registerMBean( mServer, connector, name);
102             connector.start();
103         }
104         catch( Exception JavaDoc 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         /*
116         // CREATE and START a new HTML adaptor
117         final HtmlAdaptorServer html = new HtmlAdaptorServer();
118         try
119         {
120             final String name = ":name=html,type=adapter,port=" + adapterPort;
121             registerMBean( mServer, html, name);
122             html.start();
123         }
124         catch(Exception e)
125         {
126             System.out.println("\tCould not create the HTML adaptor.");
127             e.printStackTrace();
128         }
129         */

130     }
131     
132
133         private static void
134     p( Object JavaDoc arg )
135     {
136         System.out.println( arg.toString() );
137     }
138
139
140         private void
141     registerMBean( MBeanServer JavaDoc conn, Object JavaDoc mbean, String JavaDoc name )
142         throws Exception JavaDoc
143     {
144         conn.registerMBean( mbean, new ObjectName JavaDoc( name ) );
145         p( "registered object: " + name );
146     }
147     
148     public interface DottedNameTesterMBean
149     {
150         public String JavaDoc getString();
151         public void setString( String JavaDoc value );
152         
153         public Boolean JavaDoc getBoolean();
154         public void setBoolean( Boolean JavaDoc value );
155     }
156     
157     private static class DottedNameTester implements DottedNameTesterMBean
158     {
159         String JavaDoc mString;
160         Boolean JavaDoc mBoolean;
161         Integer JavaDoc mInteger;
162         
163             public
164         DottedNameTester()
165         {
166             super();
167             
168             mString = "string";
169             mBoolean = Boolean.FALSE;
170             mInteger = new Integer JavaDoc( 0 );
171             
172         }
173         
174             public String JavaDoc
175         getString()
176         {
177             return( mString );
178         }
179         
180             public void
181         setString( String JavaDoc s)
182         {
183             mString = s;
184         }
185         
186         
187             public Boolean JavaDoc
188         getBoolean()
189         {
190             return( mBoolean );
191         }
192         
193             public void
194         setBoolean( Boolean JavaDoc b)
195         {
196             mBoolean = b;
197         }
198     }
199     
200 /*
201         com.sun.cli.jmx.cmd.DottedNameRegistryMBean
202     getDottedNameRegistry( MBeanServer conn )
203         throws MalformedObjectNameException
204     {
205         // associate it with its dotted name
206         final ObjectName registryName =
207             new ObjectName( com.sun.cli.jmx.cmd.DottedNameRegistryMBean.SUGGESTED_OBJECT_NAME );
208         com.sun.cli.jmx.cmd.DottedNameRegistryMBean registry = (com.sun.cli.jmx.cmd.DottedNameRegistryMBean)
209             MBeanServerInvocationHandler.newProxyInstance( conn,
210                                     registryName,
211                                     com.sun.cli.jmx.cmd.DottedNameRegistryMBean.class, false );
212         
213         return( registry );
214     }
215     
216     static private int sCounter = 0;
217         void
218     registerDottedNameTestee(
219         MBeanServer conn,
220         Object impl,
221         int id)
222         throws Exception
223     {
224         final String objectName = "test:id=" + id;
225         final String dottedName = "test." + id;
226         
227         registerMBean( conn, impl, objectName );
228         
229         getDottedNameRegistry( conn ).add( dottedName, new ObjectName( objectName ) );
230     }
231
232
233         void
234     registerDottedNameTestees( MBeanServer conn )
235         throws Exception
236     {
237         for( int i = 0; i < 10; ++i )
238         {
239             registerDottedNameTestee( conn, new DottedNameTester(), sCounter++ );
240         }
241     }
242
243         private void
244     registerDottedNameTesters( MBeanServer conn )
245         throws Exception
246     {
247         registerMBean( conn, new com.sun.cli.jmx.cmd.DottedNameRegistryMBeanImpl(),
248             com.sun.cli.jmx.cmd.DottedNameRegistryMBean.SUGGESTED_OBJECT_NAME );
249             
250         registerMBean( conn, new com.sun.cli.jmx.cmd.DottedNameResolverMBeanImpl(),
251             com.sun.cli.jmx.cmd.DottedNameResolverMBeanImpl.SUGGESTED_OBJECT_NAME );
252             
253         registerMBean( conn, new com.sun.cli.jmx.cmd.DottedNameGetSetMBeanImpl(),
254             com.sun.cli.jmx.cmd.DottedNameGetSetMBeanImpl.SUGGESTED_OBJECT_NAME );
255         
256         registerDottedNameTestees( conn );
257         
258         
259         final java.util.Iterator iter = getDottedNameRegistry( conn ).allDottedNames().iterator();
260         p( "Dotted names from registry:" );
261         p( SmartStringifier.toString( iter ) );
262     }
263
264 */

265     
266         private void
267     AddMBeans( MBeanServer JavaDoc conn )
268         throws Exception JavaDoc
269     {
270         // setup alias mgr
271
final AliasMgrHashMapImpl aliasImpl = new AliasMgrHashMapImpl();
272         try
273         {
274             aliasImpl.load( new java.io.File JavaDoc( AliasMgrHashMapImpl.DEFAULT_FILENAME ) );
275         }
276         catch( Exception JavaDoc e )
277         {
278             // ignore
279
}
280         final AliasMgr aliasMgr = new AliasMgr( aliasImpl );
281             
282         StandardAliasesIniter.init( aliasMgr );
283         
284         registerMBean( conn, aliasMgr , CLISupportStrings.ALIAS_MGR_TARGET );
285         
286         
287         // setup CLI support, using alias mgr via a proxy (don't use directly)
288

289         final CLISupport cliSupport = new CLISupport( conn, aliasMgr);
290         
291         registerMBean( conn, cliSupport, CLISupportStrings.CLI_SUPPORT_TARGET );
292         
293         // register our testees
294

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 JavaDoc
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc( 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 JavaDoc e )
353         {
354             p( e );
355         }
356     }
357 };
358
359
360
Popular Tags