01: package org.osbl.item;
02:
03: import java.util.*;
04: import java.beans.*;
05: import java.lang.reflect.Method;
06:
07: public class BeanAuthorizationCollector extends AbstractItemCollector {
08: Class beanClass;
09: List<String> items;
10:
11: public BeanAuthorizationCollector(Class beanClass) {
12: this .beanClass = beanClass;
13: }
14:
15: public Collection<String> getItems() {
16: if (items == null) {
17: try {
18: BeanInfo info = Introspector.getBeanInfo(beanClass,
19: Object.class);
20: PropertyDescriptor[] descriptors = info
21: .getPropertyDescriptors();
22:
23: items = new ArrayList<String>(descriptors.length + 1);
24: StringBuilder builder = new StringBuilder(beanClass
25: .getName());
26: String actions = "[rw]";
27: items.add(build(builder, actions));
28:
29: for (int i = 0; i < descriptors.length; i++) {
30: PropertyDescriptor descriptor = descriptors[i];
31: Method getter = descriptor.getReadMethod();
32: Method setter = descriptor.getWriteMethod();
33:
34: if (getter == null && setter == null)
35: continue;
36: else if (getter != null && setter != null)
37: actions = "[rw]";
38: else if (getter != null)
39: actions = "[r]";
40: else
41: // if (setter != null)
42: actions = "[w]";
43:
44: items.add(build(builder, ".", descriptor.getName(),
45: actions));
46: }
47: } catch (Exception e) {
48: throw new RuntimeException(e);
49: }
50: }
51: return items;
52: }
53:
54: public boolean equals(Object o) {
55: if (this == o)
56: return true;
57: if (o == null || getClass() != o.getClass())
58: return false;
59:
60: BeanAuthorizationCollector that = (BeanAuthorizationCollector) o;
61:
62: if (!beanClass.equals(that.beanClass))
63: return false;
64:
65: return true;
66: }
67:
68: public int hashCode() {
69: return beanClass.hashCode();
70: }
71: }
|