KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > mxbean > CompositeTypeMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.mxbean;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Type JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.management.openmbean.CompositeType JavaDoc;
30 import javax.management.openmbean.OpenType JavaDoc;
31 import javax.management.openmbean.SimpleType JavaDoc;
32
33 import org.jboss.util.UnexpectedThrowable;
34
35 /**
36  * CompositeTypeMetaData.
37  *
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @version $Revision: 1.1 $
40  */

41 public class CompositeTypeMetaData
42 {
43    /** The clazz */
44    private Class JavaDoc<?> clazz;
45    
46    /** The composite type */
47    private CompositeType JavaDoc compositeType;
48    
49    /** The items */
50    private List JavaDoc<ItemMetaData> items = new ArrayList JavaDoc<ItemMetaData>();
51    
52    /**
53     * Create a new CompositeTypeMetaData.
54     *
55     * @param clazz the class
56     */

57    public CompositeTypeMetaData(Class JavaDoc<?> clazz)
58    {
59       this.clazz = clazz;
60    }
61    
62    /**
63     * Get the composite type
64     *
65     * @return the composite type
66     */

67    public CompositeType JavaDoc getCompositeType()
68    {
69       if (compositeType == null)
70       {
71          String JavaDoc typeName = clazz.getName();
72          if (items.size() == 0)
73             return generateAnObject(typeName);
74             
75          String JavaDoc[] itemNames = new String JavaDoc[items.size()];
76          OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[items.size()];
77          for (int i = 0; i < items.size(); ++i)
78          {
79             ItemMetaData item = items.get(i);
80             itemNames[i] = item.getName();
81             itemTypes[i] = item.getOpenType();
82          }
83          try
84          {
85             compositeType = new CompositeType JavaDoc(typeName, typeName, itemNames, itemNames, itemTypes);
86          }
87          catch (Throwable JavaDoc t)
88          {
89             throw new UnexpectedThrowable("Error creating composite type for: " + typeName, t);
90          }
91       }
92       return compositeType;
93    }
94
95    /**
96     * Generate the metadata
97     */

98    public void generate()
99    {
100       if (Object JavaDoc.class.equals(clazz))
101       {
102          compositeType = generateObject();
103          return;
104       }
105       if (Class JavaDoc.class.equals(clazz))
106       {
107          compositeType = generateClass();
108          return;
109       }
110       if (ClassLoader JavaDoc.class.equals(clazz))
111       {
112          compositeType = generateClassLoader();
113          return;
114       }
115
116       Method JavaDoc[] methods = clazz.getMethods();
117       for (Method JavaDoc method : methods)
118       {
119          Type JavaDoc returnType = method.getGenericReturnType();
120          Class JavaDoc<?> declaring = method.getDeclaringClass();
121          if (Object JavaDoc.class.equals(declaring) == false)
122          {
123             String JavaDoc key = MXBeanUtils.getCompositeDataKey(method);
124             if (key != null)
125                items.add(new ItemMetaData(key, returnType));
126          }
127       }
128    }
129
130    /**
131     * Generate the composite data for an object
132     *
133     * @return the composite type
134     */

135    public static CompositeType JavaDoc generateObject()
136    {
137       return generateAnObject(Object JavaDoc.class.getName());
138    }
139
140    /**
141     * Generate the composite data for an object
142     *
143     * @param name the class name
144     * @return the composite type
145     */

146    private static CompositeType JavaDoc generateAnObject(String JavaDoc name)
147    {
148       String JavaDoc[] itemNames = { "class" };
149       OpenType JavaDoc[] openTypes = { generateClass() };
150       return safeCreateCompositeType(name, itemNames, openTypes);
151    }
152
153    /**
154     * Generate the composite data for a class
155     *
156     * @return the composite type
157     */

158    public static CompositeType JavaDoc generateClass()
159    {
160       String JavaDoc name = Object JavaDoc.class.getName();
161       String JavaDoc[] itemNames = { "name" };
162       OpenType JavaDoc[] openTypes = { SimpleType.STRING };
163       return safeCreateCompositeType(name, itemNames, openTypes);
164    }
165
166    /**
167     * Generate the composite data for a classloader
168     *
169     * @return the composite type
170     */

171    public static CompositeType JavaDoc generateClassLoader()
172    {
173       String JavaDoc name = Object JavaDoc.class.getName();
174       String JavaDoc[] itemNames = { "name" };
175       OpenType JavaDoc[] openTypes = { SimpleType.STRING };
176       return safeCreateCompositeType(name, itemNames, openTypes);
177    }
178
179    /**
180     * Safely create a composite type
181     *
182     * @param name the name
183     * @param itemNames the item names
184     * @param openTypes the open types
185     * @return the composite type
186     */

187    private static CompositeType JavaDoc safeCreateCompositeType(String JavaDoc name, String JavaDoc[] itemNames, OpenType JavaDoc[] openTypes)
188    {
189       try
190       {
191          return new CompositeType JavaDoc(name, name, itemNames, itemNames, openTypes);
192       }
193       catch (Exception JavaDoc e)
194       {
195          throw new UnexpectedThrowable(e);
196       }
197    }
198 }
199
Popular Tags