KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > TestUtil


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 package com.sun.enterprise.management;
24
25 import java.util.Collection JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.SortedSet JavaDoc;
29 import java.util.TreeSet JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Collections JavaDoc;
32
33 import javax.management.ObjectName JavaDoc;
34
35 import com.sun.appserv.management.DomainRoot;
36 import com.sun.appserv.management.base.Util;
37 import com.sun.appserv.management.base.AMX;
38
39 import com.sun.appserv.management.client.ConnectionSource;
40 import com.sun.appserv.management.client.ProxyFactory;
41
42 import com.sun.appserv.management.util.misc.StringUtil;
43 import com.sun.appserv.management.util.misc.GSetUtil;
44 import com.sun.appserv.management.util.misc.ListUtil;
45 import com.sun.appserv.management.util.misc.ClassUtil;
46 import com.sun.appserv.management.util.misc.ExceptionUtil;
47 import com.sun.appserv.management.util.misc.TypeCast;
48
49 import com.sun.appserv.management.util.jmx.ObjectNameComparator;
50
51 import com.sun.enterprise.management.support.AMXDebugStuff;
52
53 /**
54     Observes various things as tests are run.
55  */

56 public final class TestUtil
57 {
58     private final DomainRoot mDomainRoot;
59     private final String JavaDoc NEWLINE;
60     
61         public
62     TestUtil( final DomainRoot domainRoot )
63     {
64         mDomainRoot = domainRoot;
65         NEWLINE = System.getProperty( "line.separator" );
66     }
67     
68         private void
69     trace( final Object JavaDoc o)
70     {
71         System.out.println( "" + o );
72     }
73     
74         public AMXDebugStuff
75     asAMXDebugStuff( final AMX amx )
76     {
77         final String JavaDoc[] attrNames = Util.getExtra( amx ).getAttributeNames();
78         
79         AMXDebugStuff result = null;
80         if ( GSetUtil.newUnmodifiableStringSet( attrNames ).contains( "AMXDebug" ) )
81         {
82             final ProxyFactory factory = Util.getExtra( amx ).getProxyFactory();
83                 
84             try
85             {
86                 final Class JavaDoc amxClass =
87                     ClassUtil.getClassFromName( Util.getExtra( amx ).getInterfaceName() );
88                 final Class JavaDoc[] interfaces = new Class JavaDoc[] { amxClass, AMXDebugStuff.class };
89                 
90                 final ObjectName JavaDoc objectName = Util.getObjectName( amx );
91                 
92                 return (AMXDebugStuff)
93                     factory.newProxyInstance( objectName, interfaces );
94             }
95             catch( Exception JavaDoc e )
96             {
97                 trace( ExceptionUtil.toString( e ) );
98                 throw new RuntimeException JavaDoc( e );
99             }
100         }
101         
102         return result;
103     }
104     
105     
106     /**
107         @return Set of j2eeTypes found in Set<AMX>
108      */

109         public Set JavaDoc<String JavaDoc>
110     getJ2EETypes( final Set JavaDoc<AMX> amxs )
111     {
112         final Set JavaDoc<String JavaDoc> registered = new HashSet JavaDoc<String JavaDoc>();
113         
114         for( final AMX amx : amxs )
115         {
116             registered.add( amx.getJ2EEType() );
117         }
118         
119         return registered;
120     }
121     
122     /**
123         @return Set of j2eeTypes for which no MBeans exist
124      */

125         public Set JavaDoc<String JavaDoc>
126     findRegisteredJ2EETypes()
127     {
128         return getJ2EETypes( mDomainRoot.getQueryMgr().queryAllSet() );
129     }
130     
131         public String JavaDoc
132     setToSortedString( final Set JavaDoc<String JavaDoc> s, final String JavaDoc delim )
133     {
134         final String JavaDoc[] a = GSetUtil.toStringArray( s );
135         Arrays.sort( a );
136        
137         return StringUtil.toString( NEWLINE, (Object JavaDoc[])a );
138     }
139     
140     
141         public static SortedSet JavaDoc<ObjectName JavaDoc>
142     newSortedSet( final ObjectName JavaDoc[] objectNames )
143     {
144         final SortedSet JavaDoc<ObjectName JavaDoc> s = new TreeSet JavaDoc<ObjectName JavaDoc>( ObjectNameComparator.INSTANCE );
145         
146         for( final ObjectName JavaDoc objectName : objectNames )
147         {
148             s.add( objectName );
149         }
150         
151         return s;
152     }
153     
154         public static SortedSet JavaDoc<ObjectName JavaDoc>
155     newSortedSet( final Collection JavaDoc<ObjectName JavaDoc> c )
156     {
157         final ObjectName JavaDoc[] objectNames = new ObjectName JavaDoc[ c.size() ];
158         c.toArray( objectNames );
159         
160         return newSortedSet( objectNames );
161     }
162
163     /**
164         As an optimization to speed up testing, we always get the Set of AMX
165         ObjectNames using Observer, which maintains such a list.
166      */

167         public SortedSet JavaDoc<ObjectName JavaDoc>
168     getAllObjectNames()
169     {
170         final Set JavaDoc<ObjectName JavaDoc> s =
171             Observer.getInstance().getCurrentlyRegisteredAMX();
172         
173         return newSortedSet( s );
174     }
175
176
177     /**
178         @return all AMX, sorted by ObjectName
179      */

180         public SortedSet JavaDoc<AMX>
181     getAllAMX()
182     {
183         final SortedSet JavaDoc<ObjectName JavaDoc> all = getAllObjectNames();
184         
185         final SortedSet JavaDoc<AMX> allAMX = new TreeSet JavaDoc<AMX>( new AMXComparator<AMX>() );
186         final ProxyFactory proxyFactory = Util.getExtra( mDomainRoot ).getProxyFactory();
187         for( final ObjectName JavaDoc objectName : all )
188         {
189             try
190             {
191                 final AMX amx = proxyFactory.getProxy( objectName, AMX.class );
192                 
193                 allAMX.add( amx );
194             }
195             catch( Exception JavaDoc e )
196             {
197                 trace( ExceptionUtil.toString( e ) );
198             }
199         }
200         
201         return allAMX;
202     }
203
204         public <T> SortedSet JavaDoc<T>
205     getAllAMX( final Class JavaDoc<T> theInterface )
206     {
207         final SortedSet JavaDoc<AMX> all = getAllAMX();
208         final TreeSet JavaDoc<AMX> allOfInterface = new TreeSet JavaDoc<AMX>( new AMXComparator<AMX>() );
209         
210         for( final AMX amx : all )
211         {
212             if ( theInterface.isAssignableFrom( amx.getClass() ) )
213             {
214                 allOfInterface.add( amx );
215             }
216         }
217         
218         return TypeCast.asSortedSet( allOfInterface );
219         
220     }
221     
222         public ObjectName JavaDoc[]
223     getAllAMXArray()
224     {
225         final SortedSet JavaDoc<ObjectName JavaDoc> s = getAllObjectNames();
226         final ObjectName JavaDoc[] objectNames = new ObjectName JavaDoc[ s.size() ];
227         s.toArray( objectNames );
228         
229         return( objectNames );
230     }
231
232         public Set JavaDoc<String JavaDoc>
233     getAvailJ2EETypes()
234     {
235         final SortedSet JavaDoc<ObjectName JavaDoc> allObjectNames = getAllObjectNames();
236         final Set JavaDoc<String JavaDoc> j2eeTypes = new HashSet JavaDoc<String JavaDoc>();
237         
238         for( final ObjectName JavaDoc objectName : allObjectNames )
239         {
240             final String JavaDoc value = Util.getJ2EEType( objectName );
241                 
242             j2eeTypes.add( value );
243         }
244         return( j2eeTypes );
245     }
246     
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
Popular Tags