KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > GBeanInfo


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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.IOException JavaDoc;
21 import java.io.ObjectInputStream JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import org.apache.geronimo.kernel.management.NotificationType;
36
37 /**
38  * Describes a GBean. This class should never be constructed directly. Insted use GBeanInfoBuilder.
39  *
40  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
41  */

42 public final class GBeanInfo implements Serializable JavaDoc {
43     private static final long serialVersionUID = -6198804067155550221L;
44     
45     public static final int PRIORITY_CLASSLOADER = 1;
46     public static final int PRIORITY_NORMAL = 5;
47
48     private static final Set JavaDoc DEFAULT_NOTIFICATIONS = Collections.unmodifiableSet(new HashSet JavaDoc(Arrays.asList(NotificationType.TYPES)));
49
50     /**
51      * Static helper to try to get the GBeanInfo from the class supplied.
52      *
53      * @param className name of the class to get the GBeanInfo from
54      * @param classLoader the class loader use to load the specifiec class
55      * @return GBeanInfo generated by supplied class
56      * @throws InvalidConfigurationException
57      * if there is a problem getting the GBeanInfo from the class
58      */

59     public static GBeanInfo getGBeanInfo(String JavaDoc className, ClassLoader JavaDoc classLoader) throws InvalidConfigurationException {
60         Class JavaDoc clazz;
61         try {
62             clazz = classLoader.loadClass(className);
63         } catch (ClassNotFoundException JavaDoc e) {
64             throw new InvalidConfigurationException("Could not load class " + className, e);
65         } catch (NoClassDefFoundError JavaDoc e) {
66             throw new InvalidConfigurationException("Could not load class " + className, e);
67         }
68         Method JavaDoc method;
69         try {
70             method = clazz.getDeclaredMethod("getGBeanInfo", new Class JavaDoc[]{});
71         } catch (NoSuchMethodException JavaDoc e) {
72             try {
73                 // try to get the info from ${className}GBean
74
clazz = classLoader.loadClass(className + "GBean");
75                 method = clazz.getDeclaredMethod("getGBeanInfo", new Class JavaDoc[]{});
76             } catch (Exception JavaDoc ignored) {
77                 throw new InvalidConfigurationException("Class does not have a getGBeanInfo() method: " + className);
78             }
79         } catch (NoClassDefFoundError JavaDoc e) {
80             throw new InvalidConfigurationException("Could not find getGBeanInfo method on " + className, e);
81         }
82         try {
83             return (GBeanInfo) method.invoke(null, new Object JavaDoc[]{});
84         } catch (Exception JavaDoc e) {
85             throw new InvalidConfigurationException("Could not get GBeanInfo from class: " + className, e);
86         }
87     }
88
89     private final String JavaDoc sourceClass;
90     private final String JavaDoc name;
91     private final String JavaDoc className;
92     private final String JavaDoc j2eeType;
93     private final Set JavaDoc attributes;
94     private final Map JavaDoc attributesByName;
95     private final GConstructorInfo constructor;
96     private final Set JavaDoc operations;
97     private final Set JavaDoc notifications;
98     private final Set JavaDoc references;
99     private final Map JavaDoc referencesByName;
100     private final Set JavaDoc interfaces;
101     private int priority;
102
103     /**
104      * @deprecated use GBeanInfoBuilder
105      */

106     public GBeanInfo(String JavaDoc name, String JavaDoc className, String JavaDoc j2eeType, Collection JavaDoc attributes, GConstructorInfo constructor, Collection JavaDoc operations, Set JavaDoc references, Set JavaDoc interfaces) {
107         this(null, name, className, j2eeType, attributes, constructor, operations, references, interfaces, DEFAULT_NOTIFICATIONS, PRIORITY_NORMAL);
108     }
109
110     /**
111      * @deprecated use GBeanInfoBuilder
112      */

113     public GBeanInfo(String JavaDoc className, String JavaDoc j2eeType, Collection JavaDoc attributes, GConstructorInfo constructor, Collection JavaDoc operations, Set JavaDoc references, Set JavaDoc interfaces) {
114         this(null, className, className, j2eeType, attributes, constructor, operations, references, interfaces, DEFAULT_NOTIFICATIONS, PRIORITY_NORMAL);
115     }
116
117     /**
118      * @deprecated use GBeanInfoBuilder
119      */

120     public GBeanInfo(String JavaDoc className, String JavaDoc j2eeType, Collection JavaDoc attributes, GConstructorInfo constructor, Collection JavaDoc operations, Set JavaDoc references, Set JavaDoc interfaces, Set JavaDoc notifications) {
121         this(null, className, className, j2eeType, attributes, constructor, operations, references, interfaces, notifications, PRIORITY_NORMAL);
122     }
123
124     /**
125      * @deprecated use GBeanInfoBuilder
126      */

127     public GBeanInfo(String JavaDoc name, String JavaDoc className, String JavaDoc j2eeType, Collection JavaDoc attributes, GConstructorInfo constructor, Collection JavaDoc operations, Set JavaDoc references, Set JavaDoc interfaces, Set JavaDoc notifications) {
128         this(null, name, className, j2eeType, attributes, constructor, operations, references, interfaces, notifications, PRIORITY_NORMAL);
129     }
130
131     GBeanInfo(String JavaDoc sourceClass, String JavaDoc name, String JavaDoc className, String JavaDoc j2eeType, Collection JavaDoc attributes, GConstructorInfo constructor, Collection JavaDoc operations, Set JavaDoc references, Set JavaDoc interfaces, int priority) {
132         this(sourceClass, name, className, j2eeType, attributes, constructor, operations, references, interfaces, DEFAULT_NOTIFICATIONS, priority);
133     }
134
135     GBeanInfo(String JavaDoc sourceClass, String JavaDoc name, String JavaDoc className, String JavaDoc j2eeType, Collection JavaDoc attributes, GConstructorInfo constructor, Collection JavaDoc operations, Set JavaDoc references, Set JavaDoc interfaces, Set JavaDoc notifications, int priority) {
136         this.sourceClass = sourceClass;
137         this.name = name;
138         this.className = className;
139         this.j2eeType = j2eeType;
140         if (attributes == null) {
141             this.attributes = Collections.EMPTY_SET;
142             this.attributesByName = Collections.EMPTY_MAP;
143         } else {
144             Map JavaDoc map = new HashMap JavaDoc();
145             for (Iterator JavaDoc iterator = attributes.iterator(); iterator.hasNext();) {
146                 GAttributeInfo attribute = (GAttributeInfo) iterator.next();
147                 map.put(attribute.getName(), attribute);
148             }
149             this.attributesByName = Collections.unmodifiableMap(map);
150             this.attributes = Collections.unmodifiableSet(new HashSet JavaDoc(map.values()));
151         }
152         if (constructor == null) {
153             this.constructor = new GConstructorInfo(Collections.EMPTY_LIST);
154         } else {
155             this.constructor = constructor;
156         }
157         if (operations == null) {
158             this.operations = Collections.EMPTY_SET;
159         } else {
160             this.operations = Collections.unmodifiableSet(new HashSet JavaDoc(operations));
161         }
162         if (references == null) {
163             this.references = Collections.EMPTY_SET;
164             this.referencesByName = Collections.EMPTY_MAP;
165         } else {
166             Map JavaDoc map = new HashMap JavaDoc();
167             for (Iterator JavaDoc iterator = references.iterator(); iterator.hasNext();) {
168                 GReferenceInfo reference = (GReferenceInfo) iterator.next();
169                 map.put(reference.getName(), reference);
170             }
171             this.referencesByName = Collections.unmodifiableMap(map);
172             this.references = Collections.unmodifiableSet(new HashSet JavaDoc(references));
173         }
174         if (interfaces == null) {
175             this.interfaces = Collections.EMPTY_SET;
176         } else {
177             this.interfaces = Collections.unmodifiableSet(new HashSet JavaDoc(interfaces));
178         }
179         if (notifications == null) {
180             this.notifications = Collections.EMPTY_SET;
181         } else {
182             this.notifications = Collections.unmodifiableSet(new HashSet JavaDoc(notifications));
183         }
184         this.priority = priority;
185     }
186
187     /**
188      * Gets the source class from which this GBeanInfo can be retrieved using GBeanInfo.getGBeanInfo(className, classLoader).
189      * A null source class means the gbean info was dynamically generated.
190      *
191      * @return the source of this GBeanInfo, or null if it was dynamically generated
192      */

193     public String JavaDoc getSourceClass() {
194         return sourceClass;
195     }
196
197     public String JavaDoc getName() {
198         return name;
199     }
200
201     public String JavaDoc getClassName() {
202         return className;
203     }
204
205     public String JavaDoc getJ2eeType() {
206         return j2eeType;
207     }
208
209     /**
210      * Gets the info for the specified attribute, or null if there is no such
211      * attribute. Note that the attribute may have a getter or setter or both;
212      * being an attribute does not imply that both methods are available.
213      */

214     public GAttributeInfo getAttribute(String JavaDoc name) {
215         return (GAttributeInfo) attributesByName.get(name);
216     }
217
218     /**
219      * Returns a Set where the elements are type GAttributeInfo
220      */

221     public Set JavaDoc getAttributes() {
222         return attributes;
223     }
224
225     /**
226      * Returns a list where the elements are type GAttributeInfo
227      */

228     public List JavaDoc getPersistentAttributes() {
229         List JavaDoc attrs = new ArrayList JavaDoc();
230         for (Iterator JavaDoc i = attributes.iterator(); i.hasNext();) {
231             GAttributeInfo info = (GAttributeInfo) i.next();
232             if (info.isPersistent()) {
233                 attrs.add(info);
234             }
235         }
236         return attrs;
237     }
238
239     /**
240      * Returns a list where the elements are type GAttributeInfo
241      */

242     public List JavaDoc getManageableAttributes() {
243         List JavaDoc attrs = new ArrayList JavaDoc();
244         for (Iterator JavaDoc i = attributes.iterator(); i.hasNext();) {
245             GAttributeInfo info = (GAttributeInfo) i.next();
246             if (info.isManageable()) {
247                 attrs.add(info);
248             }
249         }
250         return attrs;
251     }
252
253     public GConstructorInfo getConstructor() {
254         return constructor;
255     }
256
257     public Set JavaDoc getOperations() {
258         return operations;
259     }
260
261     public Set JavaDoc getNotifications() {
262         return notifications;
263     }
264
265     public Set JavaDoc getReferences() {
266         return references;
267     }
268
269     public GReferenceInfo getReference(String JavaDoc name) {
270         return (GReferenceInfo) referencesByName.get(name);
271     }
272
273     public Set JavaDoc getInterfaces() {
274         return interfaces;
275     }
276
277     public int getPriority() {
278         return priority;
279     }
280
281     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
282         priority = GBeanInfo.PRIORITY_NORMAL;
283         in.defaultReadObject();
284     }
285     
286     public String JavaDoc toString() {
287         StringBuffer JavaDoc result = new StringBuffer JavaDoc("[GBeanInfo:");
288         result.append(" id=").append(super.toString());
289         result.append(" sourceClass=").append(sourceClass);
290         result.append(" name=").append(name);
291         for (Iterator JavaDoc iterator = attributes.iterator(); iterator.hasNext();) {
292             GAttributeInfo geronimoAttributeInfo = (GAttributeInfo) iterator.next();
293             result.append("\n attribute: ").append(geronimoAttributeInfo);
294         }
295         for (Iterator JavaDoc iterator = operations.iterator(); iterator.hasNext();) {
296             GOperationInfo geronimoOperationInfo = (GOperationInfo) iterator.next();
297             result.append("\n operation: ").append(geronimoOperationInfo);
298         }
299         for (Iterator JavaDoc iterator = references.iterator(); iterator.hasNext();) {
300             GReferenceInfo referenceInfo = (GReferenceInfo) iterator.next();
301             result.append("\n reference: ").append(referenceInfo);
302         }
303         result.append("]");
304         return result.toString();
305     }
306 }
Popular Tags