KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > metadata > MBeanInfoConversion


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.metadata;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.management.Descriptor JavaDoc;
29 import javax.management.MBeanAttributeInfo JavaDoc;
30 import javax.management.MBeanConstructorInfo JavaDoc;
31 import javax.management.MBeanException JavaDoc;
32 import javax.management.MBeanInfo JavaDoc;
33 import javax.management.MBeanNotificationInfo JavaDoc;
34 import javax.management.MBeanOperationInfo JavaDoc;
35 import javax.management.MBeanParameterInfo JavaDoc;
36 import javax.management.modelmbean.DescriptorSupport JavaDoc;
37 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
38 import javax.management.modelmbean.ModelMBeanConstructorInfo JavaDoc;
39 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
40 import javax.management.modelmbean.ModelMBeanInfoSupport JavaDoc;
41 import javax.management.modelmbean.ModelMBeanNotificationInfo JavaDoc;
42 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
43 import org.jboss.mx.modelmbean.ModelMBeanConstants;
44 import org.jboss.mx.server.MethodMapper;
45
46 /**
47  * Routines for converting MBeanInfo to ModelMBeanInfoSupport and stripping
48  * ModelMBeanOperationInfos that are referred to in ModelMBeanAttributeInfos.
49  *
50  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
51  * @version $Revision: 37459 $
52  */

53 public class MBeanInfoConversion
54    implements ModelMBeanConstants
55 {
56    /**
57     * Convert regular MBeanInfo into ModelMBeanInfo.
58     *
59     * @param info MBeanInfo to convert (such as the Standard MBean info)
60     */

61    public static ModelMBeanInfoSupport JavaDoc toModelMBeanInfo(MBeanInfo JavaDoc info)
62    {
63       return toModelMBeanInfo(info, true);
64    }
65    
66    /**
67     * Convert regular MBeanInfo to ModelMBeanInfo.
68     *
69     * @param info MBeanInfo to convert (such a the Standard MBean info)
70     * @param createAttributeOperationMapping setting this boolean to
71     * <tt>true</tt> will automatically create the attribute operation
72     * mapping for Model MBean managemenet attributes. Based on the
73     * Standard MBean attribute naming conventions, the Model MBean
74     * attribute descriptors are mapped to appropriate management
75     * operations with the <tt>getMethod</tt> and <tt>setMethod</tt>
76     * descriptor fields.
77     */

78    public static ModelMBeanInfoSupport JavaDoc toModelMBeanInfo(MBeanInfo JavaDoc info, boolean createAttributeOperationMapping)
79    {
80       if (info instanceof ModelMBeanInfoSupport JavaDoc)
81          return (ModelMBeanInfoSupport JavaDoc)info;
82          
83       if (info instanceof ModelMBeanInfo JavaDoc)
84          return new ModelMBeanInfoSupport JavaDoc((ModelMBeanInfo JavaDoc)info);
85       
86       // create attributes
87
MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
88       ModelMBeanAttributeInfo JavaDoc[] mmbAttributes = new ModelMBeanAttributeInfo JavaDoc[attributes.length];
89       List JavaDoc accessorOperations = new ArrayList JavaDoc();
90       
91       for (int i = 0; i < attributes.length; ++i)
92       {
93          // add basic info
94
ModelMBeanAttributeInfo JavaDoc attrInfo = new ModelMBeanAttributeInfo JavaDoc(
95             attributes[i].getName(),
96             attributes[i].getType(),
97             attributes[i].getDescription(),
98             attributes[i].isReadable(),
99             attributes[i].isWritable(),
100             attributes[i].isIs()
101          );
102          
103          // by default, conversion metadata should not try to cache attributes
104
Descriptor JavaDoc d = attrInfo.getDescriptor();
105          d.setField(CURRENCY_TIME_LIMIT, CACHE_NEVER);
106          attrInfo.setDescriptor(d);
107          
108          mmbAttributes[i] = attrInfo;
109
110          // if we're doing attribute operation mapping, find the accessor methods
111
// from the Standard MBean interface, and create the 'setter' and 'getter'
112
// management operations for them. Map the Model MBean attributes to
113
// these operations.
114
if (createAttributeOperationMapping)
115          {
116             String JavaDoc getterOperationName = null;
117             String JavaDoc setterOperationName = null;
118             Descriptor JavaDoc getterDescriptor = null;
119             Descriptor JavaDoc setterDescriptor = null;
120             
121             // figure out the getter type
122
if (attributes[i].isReadable())
123             {
124                if (attributes[i].isIs())
125                   getterOperationName = "is" + attributes[i].getName();
126                else
127                   getterOperationName = "get" + attributes[i].getName();
128                   
129                // create a descriptor for 'getter' mgmt operation
130
getterDescriptor = new DescriptorSupport JavaDoc();
131                getterDescriptor.setField(NAME, getterOperationName);
132                getterDescriptor.setField(DESCRIPTOR_TYPE, OPERATION_DESCRIPTOR);
133                getterDescriptor.setField(ROLE, ROLE_GETTER);
134                
135                // create the new management operation
136
ModelMBeanOperationInfo JavaDoc opInfo = new ModelMBeanOperationInfo JavaDoc(
137                      getterOperationName,
138                      "Read accessor operation for '" + attributes[i].getName() + "' attribute.",
139                      new MBeanParameterInfo JavaDoc[0], // void signature
140
attributes[i].getType(), // return type
141
MBeanOperationInfo.INFO, // impact
142
getterDescriptor
143                );
144
145                // modify the attributes descriptor to map the read operation
146
// to the above created management operation
147
Descriptor JavaDoc attrDescriptor = mmbAttributes[i].getDescriptor();
148                attrDescriptor.setField(GET_METHOD, getterOperationName);
149                mmbAttributes[i].setDescriptor(attrDescriptor);
150                
151                accessorOperations.add(opInfo);
152             }
153             
154             // figure out the setter
155
if (attributes[i].isWritable())
156             {
157                setterOperationName = "set" + attributes[i].getName();
158                
159                // create a descriptor for 'setter' mgmt operation
160
setterDescriptor = new DescriptorSupport JavaDoc();
161                setterDescriptor.setField(NAME, setterOperationName);
162                setterDescriptor.setField(DESCRIPTOR_TYPE, OPERATION_DESCRIPTOR);
163                setterDescriptor.setField(ROLE, ROLE_SETTER);
164                
165                // create the new management operation
166
ModelMBeanOperationInfo JavaDoc opInfo = new ModelMBeanOperationInfo JavaDoc(
167                      setterOperationName,
168                      "Write accessor operation for '" + attributes[i].getName() + "' attribute.",
169                      
170                      new MBeanParameterInfo JavaDoc[] {
171                         new MBeanParameterInfo JavaDoc("value", attributes[i].getType(), "Attribute's value.")
172                      },
173                      
174                      Void.TYPE.getName(),
175                      MBeanOperationInfo.ACTION,
176                      setterDescriptor
177                );
178                
179                // modify the attributes descriptor to map the read operation
180
// to the above created management operation
181
Descriptor JavaDoc attrDescriptor = mmbAttributes[i].getDescriptor();
182                attrDescriptor.setField(SET_METHOD, setterOperationName);
183                mmbAttributes[i].setDescriptor(attrDescriptor);
184                
185                accessorOperations.add(opInfo);
186             }
187          }
188       }
189
190       // deal with the basic manaement operations (non-getter and setter types)
191
MBeanOperationInfo JavaDoc[] operations = info.getOperations();
192       ModelMBeanOperationInfo JavaDoc[] mmbOperations = new ModelMBeanOperationInfo JavaDoc[operations.length + accessorOperations.size()];
193
194       for (int i = 0; i < operations.length; ++i)
195       {
196          mmbOperations[i] = new ModelMBeanOperationInfo JavaDoc(
197             operations[i].getName(),
198             operations[i].getDescription(),
199             operations[i].getSignature(),
200             operations[i].getReturnType(),
201             operations[i].getImpact()
202          );
203       }
204       
205       for (int i = operations.length; i < mmbOperations.length; ++i)
206          mmbOperations[i] = (ModelMBeanOperationInfo JavaDoc)accessorOperations.get(i - operations.length);
207
208       // the constructors...
209
MBeanConstructorInfo JavaDoc[] constructors = info.getConstructors();
210       ModelMBeanConstructorInfo JavaDoc[] mmbConstructors = new ModelMBeanConstructorInfo JavaDoc[constructors.length];
211
212       for (int i = 0; i < constructors.length; ++i)
213       {
214          mmbConstructors[i] = new ModelMBeanConstructorInfo JavaDoc(
215             constructors[i].getName(),
216             constructors[i].getDescription(),
217             constructors[i].getSignature()
218          );
219       }
220
221       // and finally the notifications
222

223       // FIXME: we are assuming here that the Model MBean implementation adds the
224
// default generic and attribute change notifications to the metadata.
225
// I think we could explicitly add them here as well, can't see it
226
// do any harm. [JPL]
227
MBeanNotificationInfo JavaDoc[] notifications = info.getNotifications();
228       ModelMBeanNotificationInfo JavaDoc[] mmbNotifications = new ModelMBeanNotificationInfo JavaDoc[notifications.length];
229
230       for (int i = 0; i < notifications.length; ++i)
231       {
232          mmbNotifications[i] = new ModelMBeanNotificationInfo JavaDoc(
233             notifications[i].getNotifTypes(),
234             notifications[i].getName(),
235             notifications[i].getDescription()
236          );
237       }
238
239       return new ModelMBeanInfoSupport JavaDoc(info.getClassName(), info.getDescription(),
240                                        mmbAttributes, mmbConstructors, mmbOperations, mmbNotifications);
241    }
242
243    /**
244     * Returns a ModelMBeanInfoSupport where ModelMBeanOperationInfos that are
245     * referred to by ModelMBeanAttributeInfo getMethod or setMethod descriptor
246     * fields are stripped out. If the stripAllRoles parameter is true
247     * then all the referred-to operations will be stripped. Otherwise only
248     * referred-to operations with a role of "getter" or "setter" will be stripped.
249     */

250     // why mbeanexception?
251
public static ModelMBeanInfoSupport JavaDoc stripAttributeOperations(ModelMBeanInfo JavaDoc info, boolean stripAllRoles) throws MBeanException JavaDoc
252    {
253       HashMap JavaDoc opsMap = new HashMap JavaDoc();
254       ModelMBeanOperationInfo JavaDoc[] operations = (ModelMBeanOperationInfo JavaDoc[]) info.getOperations();
255
256       for (int i = 0; i < operations.length; i++)
257       {
258          opsMap.put(MethodMapper.operationSignature(operations[i]), operations[i]);
259       }
260
261       ModelMBeanAttributeInfo JavaDoc[] attributes = (ModelMBeanAttributeInfo JavaDoc[]) info.getAttributes();
262
263       for (int i = 0; i < attributes.length; i++)
264       {
265          if (attributes[i].isReadable() && (attributes[i].getDescriptor().getFieldValue("getMethod") != null))
266          {
267             String JavaDoc key = MethodMapper.getterSignature(attributes[i]);
268             ModelMBeanOperationInfo JavaDoc opinfo = (ModelMBeanOperationInfo JavaDoc) opsMap.get(key);
269             String JavaDoc role = (String JavaDoc) opinfo.getDescriptor().getFieldValue("role");
270             if ("getter".equals(role) || stripAllRoles)
271             {
272                opsMap.remove(key);
273             }
274          }
275
276          if (attributes[i].isWritable() && (attributes[i].getDescriptor().getFieldValue("setMethod") != null))
277          {
278             String JavaDoc key = MethodMapper.setterSignature(attributes[i]);
279             ModelMBeanOperationInfo JavaDoc opinfo = (ModelMBeanOperationInfo JavaDoc) opsMap.get(key);
280             
281             String JavaDoc role = (String JavaDoc) opinfo.getDescriptor().getFieldValue("role");
282             if ("setter".equals(role) || stripAllRoles)
283             {
284                opsMap.remove(key);
285             }
286          }
287       }
288
289       operations = new ModelMBeanOperationInfo JavaDoc[opsMap.size()];
290       int position = 0;
291       for (Iterator JavaDoc iterator = opsMap.values().iterator(); iterator.hasNext(); position++)
292       {
293          operations[position] = (ModelMBeanOperationInfo JavaDoc) iterator.next();
294       }
295
296       return new ModelMBeanInfoSupport JavaDoc(
297             info.getClassName(), info.getDescription(),
298             (ModelMBeanAttributeInfo JavaDoc[]) info.getAttributes(),
299             (ModelMBeanConstructorInfo JavaDoc[]) info.getConstructors(),
300             operations,
301             (ModelMBeanNotificationInfo JavaDoc[]) info.getNotifications(),
302             info.getMBeanDescriptor()
303       );
304    }
305 }
306
Popular Tags