KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > dottedname > valueaccessor > AttributeValueAccessor


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/mbeans/src/java/com/sun/enterprise/admin/dottedname/valueaccessor/AttributeValueAccessor.java,v 1.3 2005/12/25 03:42:06 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:42:06 $
28  */

29  
30
31 package com.sun.enterprise.admin.dottedname.valueaccessor;
32
33 import java.util.Set JavaDoc;
34 import java.util.HashSet JavaDoc;
35
36 import javax.management.MBeanServerConnection JavaDoc;
37 import javax.management.IntrospectionException JavaDoc;
38 import javax.management.ReflectionException JavaDoc;
39 import javax.management.InstanceNotFoundException JavaDoc;
40 import javax.management.AttributeNotFoundException JavaDoc;
41 import javax.management.InvalidAttributeValueException JavaDoc;
42 import javax.management.MBeanException JavaDoc;
43 import javax.management.AttributeList JavaDoc;
44 import javax.management.MBeanInfo JavaDoc;
45 import javax.management.MBeanAttributeInfo JavaDoc;
46 import javax.management.ObjectName JavaDoc;
47 import javax.management.Attribute JavaDoc;
48
49
50 import com.sun.enterprise.admin.dottedname.DottedNameStrings;
51
52 import com.sun.enterprise.admin.util.ClassUtil;
53
54 public class AttributeValueAccessor extends ValueAccessorBase
55 {
56         public
57     AttributeValueAccessor( final MBeanServerConnection JavaDoc conn )
58     {
59         super( conn );
60     }
61         
62     /*
63         Return a Set of String of the names of all attributes within the MBean
64      */

65         public static Set JavaDoc
66     getAllAttributeNames( final MBeanServerConnection JavaDoc conn, final ObjectName JavaDoc objectName )
67         throws java.io.IOException JavaDoc, ReflectionException JavaDoc, InstanceNotFoundException JavaDoc, IntrospectionException JavaDoc
68     {
69         final Set JavaDoc allNames = new HashSet JavaDoc();
70     
71         // add the Attribute names
72
final MBeanInfo JavaDoc info = conn.getMBeanInfo( objectName );
73         final MBeanAttributeInfo JavaDoc [] attrsInfo = info.getAttributes();
74         if ( attrsInfo != null )
75         {
76             for( int i = 0; i < attrsInfo.length; ++i )
77             {
78                 allNames.add( attrsInfo[ i ].getName() );
79             }
80         }
81         
82         return( allNames );
83     }
84     
85     
86         public Attribute JavaDoc
87     getValue( final ObjectName JavaDoc objectName, final String JavaDoc valueName )
88         throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
89         ReflectionException JavaDoc, java.io.IOException JavaDoc
90     {
91         final Object JavaDoc value = getMBS().getAttribute( objectName, valueName);
92         
93         return( new Attribute JavaDoc( valueName, value ) );
94     }
95     
96         Class JavaDoc
97     getAttributeClass( final ObjectName JavaDoc objectName, final String JavaDoc attributeName )
98         throws IntrospectionException JavaDoc, java.io.IOException JavaDoc, ReflectionException JavaDoc,
99                 InstanceNotFoundException JavaDoc, ClassNotFoundException JavaDoc
100     {
101         final MBeanInfo JavaDoc info = getMBS().getMBeanInfo( objectName );
102         final MBeanAttributeInfo JavaDoc [] attrsInfo = info.getAttributes();
103         Class JavaDoc theClass = null;
104         
105         for( int i = 0; i < attrsInfo.length; ++i )
106         {
107             final String JavaDoc testName = attrsInfo[ i ].getName();
108             
109             if ( testName.equals( attributeName ) )
110             {
111                 theClass = ClassUtil.getClassFromName( attrsInfo[ i ].getType() );
112                 break;
113             }
114         }
115         return( theClass );
116     }
117     
118         public Attribute JavaDoc
119     setValue( final ObjectName JavaDoc objectName, final Attribute JavaDoc attr )
120         throws Exception JavaDoc
121     {
122         if ( attr.getValue() == null )
123         {
124             final String JavaDoc msg = DottedNameStrings.getString(
125                     DottedNameStrings.ILLEGAL_TO_SET_NULL_KEY,
126                     attr.getName( ) );
127                     
128             throw new IllegalArgumentException JavaDoc( msg );
129         }
130
131         Attribute JavaDoc actualAttr = null;
132         
133         Object JavaDoc value = attr.getValue();
134         if ( value instanceof String JavaDoc )
135         {
136             final Class JavaDoc attrClass = getAttributeClass( objectName, attr.getName() );
137             if ( attrClass == null )
138             {
139                 final String JavaDoc msg = DottedNameStrings.getString(
140                         DottedNameStrings.ATTRIBUTE_NOT_FOUND_KEY,
141                         attr.getName( ) );
142                     
143                 throw new AttributeNotFoundException JavaDoc( attr.getName() );
144             }
145             
146             value = coerceToClass( attrClass, (String JavaDoc)value );
147             
148             actualAttr = new Attribute JavaDoc( attr.getName(), value );
149         }
150         else
151         {
152             // if not a String, then pass on through
153
actualAttr = attr;
154         }
155         
156         getMBS().setAttribute( objectName, actualAttr);
157         
158         return( actualAttr );
159     }
160 }
161
162
Popular Tags