KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > base > GetSetAttributeTest


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.base;
24
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29
30
31 import javax.management.MBeanServerConnection JavaDoc;
32 import javax.management.MBeanInfo JavaDoc;
33 import javax.management.MBeanAttributeInfo JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35 import javax.management.Attribute JavaDoc;
36 import javax.management.AttributeList JavaDoc;
37
38 import com.sun.appserv.management.base.AMX;
39 import com.sun.appserv.management.base.Util;
40
41
42 import com.sun.appserv.management.util.misc.MapUtil;
43 import com.sun.appserv.management.util.misc.CollectionUtil;
44 import com.sun.appserv.management.util.misc.GSetUtil;
45 import com.sun.appserv.management.util.misc.StringUtil;
46 import com.sun.appserv.management.util.jmx.JMXUtil;
47
48 import com.sun.appserv.management.config.Enabled;
49
50 import com.sun.enterprise.management.AMXTestBase;
51
52
53 /**
54  */

55 public final class GetSetAttributeTest extends AMXTestBase
56 {
57     public GetSetAttributeTest()
58     {
59     }
60
61
62         private void
63     testGetSetAttributes(final AMX amx)
64         throws Exception JavaDoc
65     {
66         final ObjectName JavaDoc objectName = Util.getObjectName( amx );
67         final MBeanServerConnection JavaDoc conn = getMBeanServerConnection();
68         final MBeanInfo JavaDoc mbeanInfo = Util.getExtra( amx ).getMBeanInfo();
69         
70         final Map JavaDoc<String JavaDoc,MBeanAttributeInfo JavaDoc> attrInfos =
71             JMXUtil.attributeInfosToMap( mbeanInfo.getAttributes() );
72         final String JavaDoc[] attrNames = GSetUtil.toStringArray( attrInfos.keySet() );
73         final AttributeList JavaDoc values = conn.getAttributes( objectName, attrNames );
74         
75         final Map JavaDoc<String JavaDoc,Object JavaDoc> valuesMap = JMXUtil.attributeListToValueMap( values );
76             
77         final Set JavaDoc<String JavaDoc> getFailed = new HashSet JavaDoc<String JavaDoc>();
78         final Map JavaDoc<String JavaDoc,Object JavaDoc> setFailed = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
79         
80         for ( final MBeanAttributeInfo JavaDoc attrInfo : attrInfos.values() )
81         {
82             final String JavaDoc name = attrInfo.getName();
83             if ( ! valuesMap.keySet().contains( name ) )
84             {
85                 getFailed.add( name );
86                 continue;
87             }
88
89             if ( attrInfo.isReadable() )
90             {
91                 final Object JavaDoc value = valuesMap.get( name );
92
93                 if ( attrInfo.isWritable() )
94                 {
95                     // set it to the same value as before
96
try
97                     {
98                         final Attribute JavaDoc attr = new Attribute JavaDoc( name, value );
99                         conn.setAttribute( objectName, attr );
100                     }
101                     catch( Exception JavaDoc e )
102                     {
103                         setFailed.put( name, value );
104                     }
105                 }
106             }
107         }
108         
109         if ( getFailed.size() != 0 )
110         {
111             warning( "Could not get Attributes for " +
112                 StringUtil.quote( objectName ) + NEWLINE +
113                  CollectionUtil.toString( getFailed, NEWLINE ) );
114         }
115         
116         if ( setFailed.size() != 0 )
117         {
118             warning( "Could not identity-set Attributes for " +
119                 StringUtil.quote( objectName ) + NEWLINE +
120                  MapUtil.toString( setFailed, NEWLINE ) );
121         }
122     }
123     
124     
125         public void
126     testGetSetAttributes()
127         throws Exception JavaDoc
128     {
129         final Set JavaDoc<AMX> all = getAllAMX();
130         
131         for( final AMX amx : all )
132         {
133             testGetSetAttributes( amx );
134         }
135     }
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
Popular Tags