1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.gbean;
19
20 import java.io.Serializable;
21 import java.lang.reflect.Method;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.Map;
30 import java.util.HashMap;
31 import java.util.Arrays;
32
33 import org.apache.geronimo.kernel.management.NotificationType;
34
35 /**
36 * Describes a GBean. This class should never be constructed directly. Insted use GBeanInfoBuilder.
37 *
38 * @version $Rev: 471683 $ $Date: 2006-11-06 02:28:54 -0800 (Mon, 06 Nov 2006) $
39 */
40 public final class GBeanInfo implements Serializable {
41
42 public static final int PRIORITY_CLASSLOADER = 1;
43 public static final int PRIORITY_NORMAL = 5;
44
45 private static final Set DEFAULT_NOTIFICATIONS = Collections.unmodifiableSet(new HashSet(Arrays.asList(NotificationType.TYPES)));
46
47 /**
48 * Static helper to try to get the GBeanInfo from the class supplied.
49 *
50 * @param className name of the class to get the GBeanInfo from
51 * @param classLoader the class loader use to load the specifiec class
52 * @return GBeanInfo generated by supplied class
53 * @throws InvalidConfigurationException
54 * if there is a problem getting the GBeanInfo from the class
55 */
56 public static GBeanInfo getGBeanInfo(String className, ClassLoader classLoader) throws InvalidConfigurationException {
57 Class clazz;
58 try {
59 clazz = classLoader.loadClass(className);
60 } catch (ClassNotFoundException e) {
61 throw new InvalidConfigurationException("Could not load class " + className, e);
62 } catch (NoClassDefFoundError e) {
63 throw new InvalidConfigurationException("Could not load class " + className, e);
64 }
65 Method method;
66 try {
67 method = clazz.getDeclaredMethod("getGBeanInfo", new Class[]{});
68 } catch (NoSuchMethodException e) {
69 try {
70
71 clazz = classLoader.loadClass(className + "GBean");
72 method = clazz.getDeclaredMethod("getGBeanInfo", new Class[]{});
73 } catch (Exception ignored) {
74 throw new InvalidConfigurationException("Class does not have a getGBeanInfo() method: " + className);
75 }
76 } catch (NoClassDefFoundError e) {
77 throw new InvalidConfigurationException("Could not find getGBeanInfo method on " + className, e);
78 }
79 try {
80 return (GBeanInfo) method.invoke(null, new Object[]{});
81 } catch (Exception e) {
82 throw new InvalidConfigurationException("Could not get GBeanInfo from class: " + className, e);
83 }
84 }
85
86 private final String sourceClass;
87 private final String name;
88 private final String className;
89 private final String j2eeType;
90 private final Set attributes;
91 private final Map attributesByName;
92 private final GConstructorInfo constructor;
93 private final Set operations;
94 private final Set notifications;
95 private final Set references;
96 private final Map referencesByName;
97 private final Set interfaces;
98 private final int priority;
99
100 /**
101 * @deprecated use GBeanInfoBuilder
102 */
103 public GBeanInfo(String name, String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces) {
104 this(null, name, className, j2eeType, attributes, constructor, operations, references, interfaces, DEFAULT_NOTIFICATIONS, PRIORITY_NORMAL);
105 }
106
107 /**
108 * @deprecated use GBeanInfoBuilder
109 */
110 public GBeanInfo(String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces) {
111 this(null, className, className, j2eeType, attributes, constructor, operations, references, interfaces, DEFAULT_NOTIFICATIONS, PRIORITY_NORMAL);
112 }
113
114 /**
115 * @deprecated use GBeanInfoBuilder
116 */
117 public GBeanInfo(String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces, Set notifications) {
118 this(null, className, className, j2eeType, attributes, constructor, operations, references, interfaces, notifications, PRIORITY_NORMAL);
119 }
120
121 /**
122 * @deprecated use GBeanInfoBuilder
123 */
124 public GBeanInfo(String name, String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces, Set notifications) {
125 this(null, name, className, j2eeType, attributes, constructor, operations, references, interfaces, notifications, PRIORITY_NORMAL);
126 }
127
128 GBeanInfo(String sourceClass, String name, String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces, int priority) {
129 this(sourceClass, name, className, j2eeType, attributes, constructor, operations, references, interfaces, DEFAULT_NOTIFICATIONS, priority);
130 }
131
132 GBeanInfo(String sourceClass, String name, String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces, Set notifications, int priority) {
133 this.sourceClass = sourceClass;
134 this.name = name;
135 this.className = className;
136 this.j2eeType = j2eeType;
137 if (attributes == null) {
138 this.attributes = Collections.EMPTY_SET;
139 this.attributesByName = Collections.EMPTY_MAP;
140 } else {
141 Map map = new HashMap();
142 for (Iterator iterator = attributes.iterator(); iterator.hasNext();) {
143 GAttributeInfo attribute = (GAttributeInfo) iterator.next();
144 map.put(attribute.getName(), attribute);
145 }
146 this.attributesByName = Collections.unmodifiableMap(map);
147 this.attributes = Collections.unmodifiableSet(new HashSet(map.values()));
148 }
149 if (constructor == null) {
150 this.constructor = new GConstructorInfo(Collections.EMPTY_LIST);
151 } else {
152 this.constructor = constructor;
153 }
154 if (operations == null) {
155 this.operations = Collections.EMPTY_SET;
156 } else {
157 this.operations = Collections.unmodifiableSet(new HashSet(operations));
158 }
159 if (references == null) {
160 this.references = Collections.EMPTY_SET;
161 this.referencesByName = Collections.EMPTY_MAP;
162 } else {
163 Map map = new HashMap();
164 for (Iterator iterator = references.iterator(); iterator.hasNext();) {
165 GReferenceInfo reference = (GReferenceInfo) iterator.next();
166 map.put(reference.getName(), reference);
167 }
168 this.referencesByName = Collections.unmodifiableMap(map);
169 this.references = Collections.unmodifiableSet(new HashSet(references));
170 }
171 if (interfaces == null) {
172 this.interfaces = Collections.EMPTY_SET;
173 } else {
174 this.interfaces = Collections.unmodifiableSet(new HashSet(interfaces));
175 }
176 if (notifications == null) {
177 this.notifications = Collections.EMPTY_SET;
178 } else {
179 this.notifications = Collections.unmodifiableSet(new HashSet(notifications));
180 }
181 this.priority = priority;
182 }
183
184 /**
185 * Gets the source class from which this GBeanInfo can be retrieved using GBeanInfo.getGBeanInfo(className, classLoader).
186 * A null source class means the gbean info was dynamically generated.
187 *
188 * @return the source of this GBeanInfo, or null if it was dynamically generated
189 */
190 public String getSourceClass() {
191 return sourceClass;
192 }
193
194 public String getName() {
195 return name;
196 }
197
198 public String getClassName() {
199 return className;
200 }
201
202 public String getJ2eeType() {
203 return j2eeType;
204 }
205
206 /**
207 * Gets the info for the specified attribute, or null if there is no such
208 * attribute. Note that the attribute may have a getter or setter or both;
209 * being an attribute does not imply that both methods are available.
210 */
211 public GAttributeInfo getAttribute(String name) {
212 return (GAttributeInfo) attributesByName.get(name);
213 }
214
215 /**
216 * Returns a Set where the elements are type GAttributeInfo
217 */
218 public Set getAttributes() {
219 return attributes;
220 }
221
222 /**
223 * Returns a list where the elements are type GAttributeInfo
224 */
225 public List getPersistentAttributes() {
226 List attrs = new ArrayList();
227 for (Iterator i = attributes.iterator(); i.hasNext();) {
228 GAttributeInfo info = (GAttributeInfo) i.next();
229 if (info.isPersistent()) {
230 attrs.add(info);
231 }
232 }
233 return attrs;
234 }
235
236 /**
237 * Returns a list where the elements are type GAttributeInfo
238 */
239 public List getManageableAttributes() {
240 List attrs = new ArrayList();
241 for (Iterator i = attributes.iterator(); i.hasNext();) {
242 GAttributeInfo info = (GAttributeInfo) i.next();
243 if (info.isManageable()) {
244 attrs.add(info);
245 }
246 }
247 return attrs;
248 }
249
250 public GConstructorInfo getConstructor() {
251 return constructor;
252 }
253
254 public Set getOperations() {
255 return operations;
256 }
257
258 public Set getNotifications() {
259 return notifications;
260 }
261
262 public Set getReferences() {
263 return references;
264 }
265
266 public GReferenceInfo getReference(String name) {
267 return (GReferenceInfo) referencesByName.get(name);
268 }
269
270 public Set getInterfaces() {
271 return interfaces;
272 }
273
274 public int getPriority() {
275 return priority;
276 }
277
278 public String toString() {
279 StringBuffer result = new StringBuffer("[GBeanInfo:");
280 result.append(" id=").append(super.toString());
281 result.append(" sourceClass=").append(sourceClass);
282 result.append(" name=").append(name);
283 for (Iterator iterator = attributes.iterator(); iterator.hasNext();) {
284 GAttributeInfo geronimoAttributeInfo = (GAttributeInfo) iterator.next();
285 result.append("\n attribute: ").append(geronimoAttributeInfo);
286 }
287 for (Iterator iterator = operations.iterator(); iterator.hasNext();) {
288 GOperationInfo geronimoOperationInfo = (GOperationInfo) iterator.next();
289 result.append("\n operation: ").append(geronimoOperationInfo);
290 }
291 for (Iterator iterator = references.iterator(); iterator.hasNext();) {
292 GReferenceInfo referenceInfo = (GReferenceInfo) iterator.next();
293 result.append("\n reference: ").append(referenceInfo);
294 }
295 result.append("]");
296 return result.toString();
297 }
298 }