KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > QueryMgrImpl


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.support;
24
25 import java.util.Set JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.io.IOException JavaDoc;
32
33
34 import javax.management.ObjectName JavaDoc;
35 import javax.management.MBeanInfo JavaDoc;
36 import javax.management.JMException JavaDoc;
37
38 import javax.management.MBeanServer JavaDoc;
39 import javax.management.MBeanServerConnection JavaDoc;
40 import javax.management.AttributeNotFoundException JavaDoc;
41 import javax.management.InstanceNotFoundException JavaDoc;
42 import javax.management.ReflectionException JavaDoc;
43 import javax.management.MBeanException JavaDoc;
44
45 import com.sun.appserv.management.base.AMX;
46 import com.sun.appserv.management.base.AMXAttributes;
47 import com.sun.appserv.management.base.Util;
48 import com.sun.appserv.management.base.QueryMgr;
49 import com.sun.appserv.management.base.XTypesMapper;
50 import com.sun.appserv.management.base.XTypes;
51 import com.sun.appserv.management.j2ee.J2EETypes;
52 import com.sun.appserv.management.j2ee.J2EETypesMapper;
53 import com.sun.appserv.management.base.QueryMgr;
54 import com.sun.enterprise.management.support.ObjectNames;
55
56 import com.sun.appserv.management.util.jmx.ObjectNameQueryImpl;
57 import com.sun.appserv.management.util.jmx.JMXUtil;
58 import com.sun.appserv.management.util.misc.RegexUtil;
59 import com.sun.appserv.management.util.misc.ArrayConversion;
60
61 import com.sun.appserv.management.util.misc.ClassUtil;
62 import com.sun.appserv.management.util.misc.GSetUtil;
63 import com.sun.appserv.management.util.misc.ListUtil;
64 import com.sun.appserv.management.util.stringifier.SmartStringifier;
65
66
67 /**
68  */

69 public class QueryMgrImpl extends AMXNonConfigImplBase
70     // implements QueryMgr
71
{
72     private static J2EETypesMapper sJ2EETypesMapper = null;
73     
74         public
75     QueryMgrImpl()
76     {
77     }
78     
79         public final String JavaDoc
80     getGroup()
81     {
82         return( AMX.GROUP_UTILITY );
83     }
84
85         private static Set JavaDoc<ObjectName JavaDoc>
86     checked( final Set JavaDoc<ObjectName JavaDoc> s )
87     {
88         return Collections.checkedSet(s, ObjectName JavaDoc.class);
89     }
90     
91     /**
92      */

93         public static Set JavaDoc<ObjectName JavaDoc>
94     queryPropsObjectNameSet(
95         final MBeanServerConnection JavaDoc conn,
96         final String JavaDoc jmxDomainName,
97         final String JavaDoc props )
98         throws IOException JavaDoc
99     {
100         final ObjectName JavaDoc pattern =
101                 Util.newObjectNamePattern( jmxDomainName, props );
102         
103         final Set JavaDoc<ObjectName JavaDoc> names = JMXUtil.queryNames( conn, pattern, null );
104         
105         return( checked(names) );
106     }
107     
108     /**
109      */

110         public static Set JavaDoc<ObjectName JavaDoc>
111     queryPropsObjectNameSet(
112         final MBeanServer JavaDoc server,
113         final String JavaDoc jmxDomainName,
114         final String JavaDoc props )
115     {
116         final ObjectName JavaDoc pattern =
117                 Util.newObjectNamePattern( jmxDomainName, props );
118         
119         final Set JavaDoc<ObjectName JavaDoc> names = JMXUtil.queryNames( server, pattern, null );
120         
121         return( checked(names) );
122     }
123     
124         public Set JavaDoc<ObjectName JavaDoc>
125     queryPropsObjectNameSet( final String JavaDoc props )
126     {
127         final String JavaDoc myDomain = getObjectName().getDomain();
128         
129         final Set JavaDoc<ObjectName JavaDoc> names =
130             queryPropsObjectNameSet( getMBeanServer(), myDomain, props );
131         
132         return( checked(names) );
133     }
134     
135         public ObjectName JavaDoc
136     querySingletonJ2EETypeObjectName( final String JavaDoc j2eeType )
137     {
138         return( querySingletonJ2EETypeObjectName( getMBeanServer(), getObjectName().getDomain(), j2eeType ) );
139     }
140     
141         public static ObjectName JavaDoc
142     querySingletonJ2EETypeObjectName(
143         final MBeanServer JavaDoc server,
144         final String JavaDoc domainName,
145         final String JavaDoc j2eeTypeValue )
146     {
147         try
148         {
149             final ObjectName JavaDoc objectName =
150                                 querySingletonJ2EETypeObjectName(
151                                     (MBeanServerConnection JavaDoc)server,
152                                     domainName,
153                                     j2eeTypeValue );
154             return( objectName );
155         }
156         catch( IOException JavaDoc e )
157         {
158             assert( false );
159             throw new RuntimeException JavaDoc( e );
160         }
161     }
162     
163         public static ObjectName JavaDoc
164     querySingletonJ2EETypeObjectName(
165         final MBeanServerConnection JavaDoc conn,
166         final String JavaDoc domainName,
167         final String JavaDoc j2eeTypeValue )
168         throws IOException JavaDoc
169     {
170         final Set JavaDoc<ObjectName JavaDoc> names =
171             queryJ2EETypeObjectNameSet( conn, domainName, j2eeTypeValue );
172         if ( names.size() != 1 )
173         {
174             throw new IllegalArgumentException JavaDoc(
175                 "request was for a single Object name of type: " + j2eeTypeValue +
176                 " but found several: " + SmartStringifier.toString( names ) );
177         }
178         
179         final ObjectName JavaDoc objectName = (ObjectName JavaDoc)GSetUtil.getSingleton( names );
180         
181         return( objectName );
182     }
183     
184
185     /**
186      */

187         public static Set JavaDoc<ObjectName JavaDoc>
188     queryJ2EETypeObjectNameSet(
189         final MBeanServerConnection JavaDoc conn,
190         final String JavaDoc domainName,
191         final String JavaDoc j2eeTypeValue )
192         throws IOException JavaDoc
193     {
194         final String JavaDoc prop = Util.makeJ2EETypeProp( j2eeTypeValue ) ;
195         final ObjectName JavaDoc pat = Util.newObjectNamePattern( domainName, prop );
196         
197         final Set JavaDoc<ObjectName JavaDoc> names = queryPatternObjectNameSet( conn, pat );
198         
199         return( checked(names) );
200     }
201     
202     /**
203      */

204         public Set JavaDoc<ObjectName JavaDoc>
205     queryJ2EETypesObjectNameSet( final Set JavaDoc<String JavaDoc> j2eeTypes )
206         throws IOException JavaDoc
207     {
208         final String JavaDoc[] values = GSetUtil.toStringArray( j2eeTypes );
209         final String JavaDoc[] keys = new String JavaDoc[ j2eeTypes.size() ];
210         
211         for( int i = 0; i < keys.length; ++i )
212         {
213             keys[ i ] = AMX.J2EE_TYPE_KEY;
214         }
215         
216         return queryWildObjectNameSet( keys, values );
217     }
218     
219     //---------------------------------------
220

221     
222     /**
223         Get the ObjectName of an MBean having the specified J2EEType. If
224         there is more than one such MBean, an IllegalArgumentException
225         is thrown. If no object is found, then null is returned.
226         
227         @throws IllegalArgumentException if more than one object is found or null if none found
228      */

229         public ObjectName JavaDoc
230     querySingletonJ2EEType( final String JavaDoc j2eeTypeValue )
231     {
232         final Set JavaDoc<ObjectName JavaDoc> names = queryJ2EETypeObjectNameSet( j2eeTypeValue );
233         if ( names.size() > 1 )
234         {
235             trace( "QueryMgrImpl.getJ2EETypeObjectName: expected 1, got " + names.size() );
236             throw new IllegalArgumentException JavaDoc( j2eeTypeValue );
237         }
238         
239         final ObjectName JavaDoc objectName = names.size() == 0 ?
240                             null :GSetUtil.getSingleton( names );
241         
242         return( objectName );
243     }
244     
245     /**
246      */

247         public Set JavaDoc<ObjectName JavaDoc>
248     queryJ2EETypeObjectNameSet( final String JavaDoc j2eeTypeValue )
249     {
250         final String JavaDoc prop = Util.makeJ2EETypeProp( j2eeTypeValue ) ;
251         final ObjectName JavaDoc pat = Util.newObjectNamePattern( getJMXDomain(), prop );
252         
253         final Set JavaDoc<ObjectName JavaDoc> names = queryPropsObjectNameSet( getMBeanServer(),
254                                 getJMXDomain(), prop );
255         
256         return( names );
257     }
258     
259
260     /**
261         Get all MBeans having the specified name.
262         
263         @param nameValue the value of the "name" property to match
264      */

265         public Set JavaDoc<ObjectName JavaDoc>
266     queryJ2EENameObjectNameSet( final String JavaDoc nameValue )
267     {
268         final String JavaDoc prop = Util.makeNameProp( nameValue );
269         final Set JavaDoc<ObjectName JavaDoc> names = queryPropsObjectNameSet( getMBeanServer(),
270                                     getJMXDomain(), prop );
271                                                     
272         return( names );
273     }
274     
275
276     /**
277      */

278         public static Set JavaDoc<ObjectName JavaDoc>
279     queryPatternObjectNameSet(
280         final MBeanServerConnection JavaDoc conn,
281         final ObjectName JavaDoc pattern )
282         throws IOException JavaDoc
283     {
284         final Set JavaDoc<ObjectName JavaDoc> names = JMXUtil.queryNames( conn, pattern, null );
285         
286         return checked(names);
287     }
288     
289     
290     /**
291      */

292         public static Set JavaDoc<ObjectName JavaDoc>
293     queryPatternObjectNameSet(
294         final MBeanServer JavaDoc server,
295         final ObjectName JavaDoc pattern )
296     {
297         final Set JavaDoc<ObjectName JavaDoc> names = JMXUtil.queryNames( server, pattern, null );
298         
299         return checked(names);
300     }
301     /**
302      */

303         public Set JavaDoc<ObjectName JavaDoc>
304     queryPatternObjectNameSet( final ObjectName JavaDoc pattern )
305     {
306         return( queryPatternObjectNameSet( getMBeanServer(), pattern ) );
307     }
308     
309     /**
310      */

311         public Set JavaDoc<ObjectName JavaDoc>
312     queryPatternObjectNameSet( String JavaDoc domain, String JavaDoc props )
313     {
314         final String JavaDoc myDomain = getObjectName().getDomain();
315         
316         final ObjectName JavaDoc pattern = Util.newObjectNamePattern(
317             domain == null ? myDomain : domain, props );
318         return( queryPatternObjectNameSet( getMBeanServer(), pattern ) );
319     }
320     
321     
322     /**
323      */

324         public String JavaDoc[]
325     queryJ2EETypeNames( final String JavaDoc j2eeType )
326     {
327         final String JavaDoc prop = Util.makeJ2EETypeProp( j2eeType );
328         
329         final Set JavaDoc<ObjectName JavaDoc> objectNameSet = queryPropsObjectNameSet( prop );
330         final ObjectName JavaDoc[] objectNames = JMXUtil.objectNameSetToArray( objectNameSet );
331         
332         final String JavaDoc[] nameKeyValues =
333             JMXUtil.getKeyProperty( NAME_KEY, objectNames );
334         
335         return( nameKeyValues );
336     }
337     
338     
339     /**
340         @return Set<ObjectName> containing all items that have the matching j2eeType and name
341      */

342         public Set JavaDoc<ObjectName JavaDoc>
343     queryJ2EETypeNameObjectNameSet(
344         final String JavaDoc j2eeType,
345         final String JavaDoc name )
346     {
347         final String JavaDoc props = Util.makeRequiredProps( j2eeType, name );
348         return queryPatternObjectNameSet( Util.newObjectNamePattern( getObjectName().getDomain(), props ) );
349     }
350     
351     
352         private static String JavaDoc[]
353     convertToRegex( String JavaDoc[] wildExprs )
354     {
355         String JavaDoc[] regexExprs = null;
356         
357         if ( wildExprs != null )
358         {
359             regexExprs = new String JavaDoc[ wildExprs.length ];
360                                 
361             for( int i = 0; i < wildExprs.length; ++i )
362             {
363                 final String JavaDoc expr = wildExprs[ i ];
364                 
365                 final String JavaDoc regex = expr == null ?
366                                     null : RegexUtil.wildcardToJavaRegex( expr );
367                 
368                 regexExprs[ i ] = regex;
369             }
370         }
371         return( regexExprs );
372     }
373     
374     /**
375      */

376         private Set JavaDoc<ObjectName JavaDoc>
377     matchWild(
378         final Set JavaDoc<ObjectName JavaDoc> candidates,
379         final String JavaDoc[] wildKeys,
380         final String JavaDoc[] wildValues)
381     {
382         final String JavaDoc[] regexNames = convertToRegex( wildKeys );
383         final String JavaDoc[] regexValues = convertToRegex( wildValues );
384         
385         final ObjectNameQueryImpl query = new ObjectNameQueryImpl();
386         final Set JavaDoc<ObjectName JavaDoc> resultSet = query.matchAll( candidates, regexNames, regexValues );
387         
388         return( resultSet );
389     }
390     
391     
392     /**
393      */

394         public Set JavaDoc<ObjectName JavaDoc>
395     queryWildObjectNameSet(
396         final String JavaDoc[] wildKeys,
397         final String JavaDoc[] wildValues )
398     {
399         final Set JavaDoc<ObjectName JavaDoc> candidates = queryAllObjectNameSet();
400         
401         return( matchWild( candidates, wildKeys, wildValues ) );
402     }
403     
404
405     /**
406         Return a Set containing all AMX ObjectNames.
407         
408         @return a Set of all ObjectNames
409      */

410         public Set JavaDoc<ObjectName JavaDoc>
411     queryAllObjectNameSet()
412     {
413         final ObjectName JavaDoc pat = Util.newObjectNamePattern( getJMXDomain(), "" );
414         
415         return( queryPatternObjectNameSet( pat ) );
416     }
417     
418     /**
419         Match an interface classname against the value of an Attribute.
420         
421         @param objectNames the set to search
422         @param interfaceName the name of the interface to find
423         @param attributeName the name of the Attribute to search
424      */

425         private Set JavaDoc<ObjectName JavaDoc>
426     queryInterfaceObjectNameSet(
427         final Set JavaDoc<ObjectName JavaDoc> objectNames,
428         final String JavaDoc interfaceName,
429         final String JavaDoc attributeName)
430     {
431         final Set JavaDoc<ObjectName JavaDoc> result = new HashSet JavaDoc<ObjectName JavaDoc>();
432         
433         final MBeanServer JavaDoc server = getMBeanServer();
434         for( final ObjectName JavaDoc objectName : objectNames )
435         {
436             try
437             {
438                 if ( server.getAttribute( objectName, attributeName ).equals( interfaceName ) )
439                 {
440                     result.add( objectName );
441                 }
442             }
443             catch( JMException JavaDoc e )
444             {
445                 // ignore
446
}
447         }
448         
449         return checked(result);
450     }
451
452     
453     /**
454      */

455         public Set JavaDoc<ObjectName JavaDoc>
456     queryInterfaceObjectNameSet(
457         final String JavaDoc searchInterfaceName,
458         final Set JavaDoc<ObjectName JavaDoc> candidates )
459         throws ClassNotFoundException JavaDoc
460     {
461         final Set JavaDoc<ObjectName JavaDoc> result = new HashSet JavaDoc<ObjectName JavaDoc>();
462         final Set JavaDoc<ObjectName JavaDoc> iter = candidates == null ?
463                                     queryAllObjectNameSet() : candidates;
464         
465         final Class JavaDoc<?> searchInterface = ClassUtil.getClassFromName( searchInterfaceName );
466         
467         final MBeanServer JavaDoc server = getMBeanServer();
468         for( final ObjectName JavaDoc objectName : iter )
469         {
470             try
471             {
472                 final String JavaDoc interfaceName = (String JavaDoc)
473                     server.getAttribute( objectName, AMXAttributes.ATTR_INTERFACE_NAME );
474                 
475                 final Class JavaDoc c =
476                     ClassUtil.getClassFromName( interfaceName );
477                     
478                 if ( searchInterface.isAssignableFrom( c ) )
479                 {
480                     result.add( objectName );
481                 }
482             }
483             catch( JMException JavaDoc e )
484             {
485                 // ignore
486
}
487         }
488         
489         return( checked(result) );
490     }
491     
492
493     
494     /**
495         Return the Class associated with a given type or null if none.
496      */

497         private static synchronized Class JavaDoc
498     interfaceForJ2EEType( final String JavaDoc j2eeType )
499     {
500         Class JavaDoc theClass = XTypesMapper.getInstance().getInterfaceForType( j2eeType );
501         
502         if ( theClass == null )
503         {
504             theClass = J2EETypesMapper.getInstance().getInterfaceForType( j2eeType );
505         }
506         
507         return( theClass );
508     }
509     
510     
511 }
512
513
514
515
516
517
518
519
520
521
522
523
Popular Tags