KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > custom > loading > MBeanAttributeSetter


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 package com.sun.enterprise.admin.mbeans.custom.loading;
26
27 import com.sun.enterprise.admin.mbeans.custom.CMBStrings;
28 import com.sun.enterprise.admin.mbeans.custom.CustomMBeanException;
29 import java.lang.reflect.Constructor JavaDoc;
30 import java.util.*;
31 import com.sun.enterprise.admin.common.constant.AdminConstants;
32 import javax.management.Attribute JavaDoc;
33 import javax.management.AttributeNotFoundException JavaDoc;
34 import javax.management.InstanceNotFoundException JavaDoc;
35 import javax.management.InvalidAttributeValueException JavaDoc;
36 import javax.management.MBeanAttributeInfo JavaDoc;
37 import javax.management.MBeanException JavaDoc;
38 import javax.management.MBeanServer JavaDoc;
39 import javax.management.ObjectName JavaDoc;
40 import javax.management.ReflectionException JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /** Class to set the attributes of given MBean taking into account the types of the
44  * attributes.
45  */

46 public class MBeanAttributeSetter {
47     private static final Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
48     private final MBeanServer JavaDoc mbs;
49     private final ObjectName JavaDoc on;
50     public MBeanAttributeSetter(final MBeanServer JavaDoc mbs, final ObjectName JavaDoc on) {
51         this.mbs = mbs;
52         this.on = on;
53     }
54     public void setIt(final String JavaDoc name, final String JavaDoc value) throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc,
55         InvalidAttributeValueException JavaDoc, MBeanException, ReflectionException JavaDoc {
56         final String JavaDoc at = getAttributeType(name);
57         final Object JavaDoc atv = getAttributeValue(at, name, value); // construct the attribute from "String" value
58
final Attribute JavaDoc atr = new Attribute JavaDoc(name, atv);
59         mbs.setAttribute(on, atr);
60     }
61     
62     ///// Private Methods /////
63
private String JavaDoc getAttributeType(final String JavaDoc name) { //gets it from MBeanInfo
64
String JavaDoc type = null;
65         List<String JavaDoc> attNames = new ArrayList<String JavaDoc>(); // just in case we need to log this info in the event of an error...
66
try {
67             MBeanAttributeInfo JavaDoc[] mais = mbs.getMBeanInfo(on).getAttributes();
68             for (MBeanAttributeInfo JavaDoc mai : mais) {
69                 final String JavaDoc an = mai.getName();
70                 attNames.add(an);
71                 if (an.equals(name)) {
72                     type = mai.getType();
73                     break;
74                 }
75             }
76         } catch (final Exception JavaDoc e) { //TODO
77
//even though one can land here, it is okay to squelch this exception
78
// Log it and move on
79
}
80         if(type == null){
81             // this is an error! They probably misspelled the Attribute Name.
82
// this code is NEVER called unless we are expecting to *find* the attribute
83
String JavaDoc names = new String JavaDoc();
84             for(int i = 0; i < attNames.size(); i++){
85                 if(i != 0)
86                     names += ", ";
87             
88                 names += attNames.get(i);
89             }
90             
91             String JavaDoc mesg = CMBStrings.get("cmb.badAttribute", name, names);
92             logger.warning(mesg);
93             throw new CustomMBeanException(mesg);
94         }
95         return ( type );
96     }
97     /** Method to construct an instance of a type from its String representation. Note that for
98      this method to succeed, the type class has to have such a constructor that
99      accepts a String and creates the instance of that type. Most of the primitive
100      wrappers have such a constructor. This requirement has to be satisfied because
101      the attributes of an MBean will be persisted to a config file and can only
102      be read back as Strings.
103      */

104     private Object JavaDoc getAttributeValue(final String JavaDoc type, final String JavaDoc name, final String JavaDoc value) {
105         Object JavaDoc valueObject = null;
106         try {
107             if (isPrimitive(type)) {
108                 //recursive call
109
return ( getAttributeValue(toWrapper(type), name, value) );
110             }
111             final Class JavaDoc c = Class.forName(type);
112             final Object JavaDoc[] params = new Object JavaDoc[]{value};
113             
114             // special case...
115
if(c.equals(Character JavaDoc.class)){
116                 if(value.length() != 1){
117                     String JavaDoc mesg = CMBStrings.get("cmb.badCharAttribute", name, value);
118                     logger.warning(mesg);
119                     throw new CustomMBeanException(mesg);
120                 }
121                 valueObject = new Character JavaDoc(value.charAt(0));
122             }
123             else{
124                 final Constructor JavaDoc ctor = c.getConstructor(new Class JavaDoc[]{java.lang.String JavaDoc.class});
125                 valueObject = ctor.newInstance(params);
126             }
127             
128         } catch (final Throwable JavaDoc t) {
129             final String JavaDoc msg = CMBStrings.get("attributeValueError", name, type, value);
130             throw new RuntimeException JavaDoc (msg, t);
131         }
132         return ( valueObject );
133     }
134     
135     private boolean isPrimitive(final String JavaDoc type) {
136         boolean primitive = int.class.getName().equals(type) ||
137                 char.class.getName().equals(type) ||
138                 short.class.getName().equals(type) ||
139                 byte.class.getName().equals(type) ||
140                 boolean.class.getName().equals(type) ||
141                 double.class.getName().equals(type) ||
142                 float.class.getName().equals(type) ||
143                             long.class.getName().equals(type);
144         return ( primitive );
145     }
146     private String JavaDoc toWrapper(final String JavaDoc primitive) {
147         return ( P2W.get(primitive) );
148         
149     }
150     private static final Map<String JavaDoc, String JavaDoc> P2W = new HashMap<String JavaDoc, String JavaDoc>();
151     static {
152         P2W.put(int.class.getName(), java.lang.Integer JavaDoc.class.getName());
153         P2W.put(char.class.getName(), java.lang.Character JavaDoc.class.getName());
154         P2W.put(short.class.getName(), java.lang.Short JavaDoc.class.getName());
155         P2W.put(byte.class.getName(), java.lang.Byte JavaDoc.class.getName());
156         P2W.put(boolean.class.getName(), java.lang.Boolean JavaDoc.class.getName());
157         P2W.put(double.class.getName(), java.lang.Double JavaDoc.class.getName());
158         P2W.put(float.class.getName(), java.lang.Float JavaDoc.class.getName());
159         P2W.put(long.class.getName(), java.lang.Long JavaDoc.class.getName());
160     }
161     ///// Private Methods /////
162
}
Popular Tags