1 17 18 package org.apache.geronimo.gbean; 19 20 import java.io.IOException ; 21 import java.io.ObjectInputStream ; 22 import java.io.Serializable ; 23 import java.lang.reflect.Method ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import org.apache.geronimo.kernel.management.NotificationType; 36 37 42 public final class GBeanInfo implements Serializable { 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 DEFAULT_NOTIFICATIONS = Collections.unmodifiableSet(new HashSet (Arrays.asList(NotificationType.TYPES))); 49 50 59 public static GBeanInfo getGBeanInfo(String className, ClassLoader classLoader) throws InvalidConfigurationException { 60 Class clazz; 61 try { 62 clazz = classLoader.loadClass(className); 63 } catch (ClassNotFoundException e) { 64 throw new InvalidConfigurationException("Could not load class " + className, e); 65 } catch (NoClassDefFoundError e) { 66 throw new InvalidConfigurationException("Could not load class " + className, e); 67 } 68 Method method; 69 try { 70 method = clazz.getDeclaredMethod("getGBeanInfo", new Class []{}); 71 } catch (NoSuchMethodException e) { 72 try { 73 clazz = classLoader.loadClass(className + "GBean"); 75 method = clazz.getDeclaredMethod("getGBeanInfo", new Class []{}); 76 } catch (Exception ignored) { 77 throw new InvalidConfigurationException("Class does not have a getGBeanInfo() method: " + className); 78 } 79 } catch (NoClassDefFoundError e) { 80 throw new InvalidConfigurationException("Could not find getGBeanInfo method on " + className, e); 81 } 82 try { 83 return (GBeanInfo) method.invoke(null, new Object []{}); 84 } catch (Exception e) { 85 throw new InvalidConfigurationException("Could not get GBeanInfo from class: " + className, e); 86 } 87 } 88 89 private final String sourceClass; 90 private final String name; 91 private final String className; 92 private final String j2eeType; 93 private final Set attributes; 94 private final Map attributesByName; 95 private final GConstructorInfo constructor; 96 private final Set operations; 97 private final Set notifications; 98 private final Set references; 99 private final Map referencesByName; 100 private final Set interfaces; 101 private int priority; 102 103 106 public GBeanInfo(String name, String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces) { 107 this(null, name, className, j2eeType, attributes, constructor, operations, references, interfaces, DEFAULT_NOTIFICATIONS, PRIORITY_NORMAL); 108 } 109 110 113 public GBeanInfo(String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces) { 114 this(null, className, className, j2eeType, attributes, constructor, operations, references, interfaces, DEFAULT_NOTIFICATIONS, PRIORITY_NORMAL); 115 } 116 117 120 public GBeanInfo(String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces, Set notifications) { 121 this(null, className, className, j2eeType, attributes, constructor, operations, references, interfaces, notifications, PRIORITY_NORMAL); 122 } 123 124 127 public GBeanInfo(String name, String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces, Set notifications) { 128 this(null, name, className, j2eeType, attributes, constructor, operations, references, interfaces, notifications, PRIORITY_NORMAL); 129 } 130 131 GBeanInfo(String sourceClass, String name, String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces, int priority) { 132 this(sourceClass, name, className, j2eeType, attributes, constructor, operations, references, interfaces, DEFAULT_NOTIFICATIONS, priority); 133 } 134 135 GBeanInfo(String sourceClass, String name, String className, String j2eeType, Collection attributes, GConstructorInfo constructor, Collection operations, Set references, Set interfaces, Set 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 map = new HashMap (); 145 for (Iterator 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 (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 (operations)); 161 } 162 if (references == null) { 163 this.references = Collections.EMPTY_SET; 164 this.referencesByName = Collections.EMPTY_MAP; 165 } else { 166 Map map = new HashMap (); 167 for (Iterator 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 (references)); 173 } 174 if (interfaces == null) { 175 this.interfaces = Collections.EMPTY_SET; 176 } else { 177 this.interfaces = Collections.unmodifiableSet(new HashSet (interfaces)); 178 } 179 if (notifications == null) { 180 this.notifications = Collections.EMPTY_SET; 181 } else { 182 this.notifications = Collections.unmodifiableSet(new HashSet (notifications)); 183 } 184 this.priority = priority; 185 } 186 187 193 public String getSourceClass() { 194 return sourceClass; 195 } 196 197 public String getName() { 198 return name; 199 } 200 201 public String getClassName() { 202 return className; 203 } 204 205 public String getJ2eeType() { 206 return j2eeType; 207 } 208 209 214 public GAttributeInfo getAttribute(String name) { 215 return (GAttributeInfo) attributesByName.get(name); 216 } 217 218 221 public Set getAttributes() { 222 return attributes; 223 } 224 225 228 public List getPersistentAttributes() { 229 List attrs = new ArrayList (); 230 for (Iterator 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 242 public List getManageableAttributes() { 243 List attrs = new ArrayList (); 244 for (Iterator 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 getOperations() { 258 return operations; 259 } 260 261 public Set getNotifications() { 262 return notifications; 263 } 264 265 public Set getReferences() { 266 return references; 267 } 268 269 public GReferenceInfo getReference(String name) { 270 return (GReferenceInfo) referencesByName.get(name); 271 } 272 273 public Set getInterfaces() { 274 return interfaces; 275 } 276 277 public int getPriority() { 278 return priority; 279 } 280 281 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 282 priority = GBeanInfo.PRIORITY_NORMAL; 283 in.defaultReadObject(); 284 } 285 286 public String toString() { 287 StringBuffer result = new StringBuffer ("[GBeanInfo:"); 288 result.append(" id=").append(super.toString()); 289 result.append(" sourceClass=").append(sourceClass); 290 result.append(" name=").append(name); 291 for (Iterator iterator = attributes.iterator(); iterator.hasNext();) { 292 GAttributeInfo geronimoAttributeInfo = (GAttributeInfo) iterator.next(); 293 result.append("\n attribute: ").append(geronimoAttributeInfo); 294 } 295 for (Iterator iterator = operations.iterator(); iterator.hasNext();) { 296 GOperationInfo geronimoOperationInfo = (GOperationInfo) iterator.next(); 297 result.append("\n operation: ").append(geronimoOperationInfo); 298 } 299 for (Iterator 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 |