KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > helper > Helper


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.appserv.management.helper;
24
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29
30 import com.sun.appserv.management.base.Util;
31 import com.sun.appserv.management.base.AMX;
32 import com.sun.appserv.management.DomainRoot;
33 import com.sun.appserv.management.base.QueryMgr;
34 import com.sun.appserv.management.base.BulkAccess;
35
36 /**
37     Base class for Helpers, useable alone as well.
38  */

39 public class Helper
40 {
41     protected final DomainRoot mDomainRoot;
42     protected final QueryMgr mQueryMgr;
43     protected final BulkAccess mBulkAccess;
44     
45         public
46     Helper( final AMX proxy )
47     {
48         mDomainRoot = proxy.getDomainRoot();
49         mQueryMgr = mDomainRoot.getQueryMgr();
50         mBulkAccess = mDomainRoot.getBulkAccess();
51     }
52     
53         public DomainRoot
54     getDomainRoot()
55     {
56         return( mDomainRoot );
57     }
58     
59         protected <T extends AMX> Set JavaDoc<T>
60     propsQuery( final String JavaDoc props )
61     {
62         final Set JavaDoc<T> results = mQueryMgr.queryPropsSet( props );
63         return( results );
64     }
65     
66         protected <T extends AMX> Set JavaDoc<T>
67     propsQuery(
68         final String JavaDoc props1,
69         final String JavaDoc props2 )
70     {
71         final String JavaDoc props = Util.concatenateProps( props1, props2 );
72         
73         return( propsQuery( props ) );
74     }
75     
76     /**
77         Filter ObjectNames based on the value of a particular Attribute. The value
78         may be null or anything else. This is essentially a crude form of using
79         the QueryMgr. A value which is a Class object succeeds if the result is
80         an object whose class is assignable to the specfied class. Typically this
81         is used to detect a thrown Exception.
82         <p>
83         For example, to select all MBeans which have a [bB]oolean Attribute named "Enabled",
84         which is set to true, call:
85         <pre>filterByAttributeValue( objectNameSet, "Enabled", Boolean.TRUE)</pre>
86         <p>
87         The query for the Attribute value is performed as a bulk operation; thus this
88         routine may be used with confidence that it is fast.
89         
90         
91         @param objectNameSet Set of ObjectName
92         @param attributeName
93         @param valueToMatch an Object whose value must be null, or equals() to the result
94         @return Set of ObjectName which have Enabled flag matching
95      */

96         public Set JavaDoc<ObjectName JavaDoc>
97     filterByAttributeValue(
98         final Set JavaDoc<ObjectName JavaDoc> objectNameSet,
99         final String JavaDoc attributeName,
100         final Object JavaDoc valueToMatch )
101     {
102         final ObjectName JavaDoc[] objectNames = new ObjectName JavaDoc[ objectNameSet.size() ];
103         objectNameSet.toArray( objectNames );
104         
105         final Object JavaDoc[] values = mBulkAccess.bulkGetAttribute( objectNames, attributeName );
106         
107         final Set JavaDoc<ObjectName JavaDoc> filtered = new HashSet JavaDoc<ObjectName JavaDoc>();
108         for( int i = 0; i < values.length; ++i )
109         {
110             final Object JavaDoc idxValue = values[ i ];
111             
112             boolean matches = false;
113             
114             if ( valueToMatch == null && idxValue == null )
115             {
116                 matches = true;
117             }
118             else if ( valueToMatch instanceof Class JavaDoc &&
119                     ((Class JavaDoc<?>)valueToMatch).isAssignableFrom( idxValue.getClass() ) )
120             {
121                 matches = true;
122             }
123             else if ( valueToMatch != null && valueToMatch.equals( idxValue ) )
124             {
125                 matches = true;
126             }
127             else
128             {
129                 // no match
130
}
131             
132             if ( matches )
133             {
134                 filtered.add( objectNames[ i ] );
135             }
136         }
137         
138         return( filtered );
139     }
140     
141 }
142
143
144
Popular Tags