On Fri, Jul 16, 2004 at 01:44:38PM -0700, Dain Sundstrom wrote:
> I think is should be consistent with the other two, but this needs
> to be cleared with David Jencks first, as the connector code is the
> biggest (only) user of dynamic gbeans.
OK, here's a patch. It puts each attribute in the maps twice, once
upper-case and once lower-case so users should be able to put either
in their config files (I've tested it with ra.xml config-property and
it works).
Also added a class comment and I cache the targetClass so if something
goes wrong the error messages indicate which class they're talking
about. I found this to be helpful when I, ahem, cough, botched one of
my config files.
Should I file an improvement issue in Jira or will someone pick it up
from here?
Thanks,
Toby
Index: DynamicGBeanDelegate.java
===================================================================
RCS file:
/home/cvspublic/incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/gbean/DynamicGBeanDelegate.java,v
retrieving revision 1.7
diff -u -c -r1.7 DynamicGBeanDelegate.java
*** DynamicGBeanDelegate.java 27 May 2004 01:05:58 -0000 1.7
--- DynamicGBeanDelegate.java 16 Jul 2004 20:57:40 -0000
***************
*** 27,41 ****
/**
* @version $Revision: 1.7 $ $Date: 2004/05/27 01:05:58 $
*/
public class DynamicGBeanDelegate implements DynamicGBean {
protected final Map getters = new HashMap();
protected final Map setters = new HashMap();
protected final Map operations = new HashMap();
public void addAll(Object target) {
! Class targetClass = target.getClass();
Method[] methods = targetClass.getMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
--- 27,44 ----
/**
+ * Wraps an <code>Object</code> in a <code>DynamicGBean</code> facade.
+ *
* @version $Revision: 1.7 $ $Date: 2004/05/27 01:05:58 $
*/
public class DynamicGBeanDelegate implements DynamicGBean {
protected final Map getters = new HashMap();
protected final Map setters = new HashMap();
protected final Map operations = new HashMap();
+ private Class targetClass;
public void addAll(Object target) {
! this.targetClass = target.getClass();
Method[] methods = targetClass.getMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
***************
*** 52,62 ****
public void addGetter(Object target, Method method) {
String name = method.getName();
if (name.startsWith("get")) {
! addGetter(method.getName().substring(3), target, method);
} else if (name.startsWith("is")) {
! addGetter(method.getName().substring(2), target, method);
} else {
! throw new IllegalArgumentException("Method method name must start
with 'get' or 'is' " + method);
}
}
--- 55,65 ----
public void addGetter(Object target, Method method) {
String name = method.getName();
if (name.startsWith("get")) {
! addGetter(name.substring(3), target, method);
} else if (name.startsWith("is")) {
! addGetter(name.substring(2), target, method);
} else {
! throw new IllegalArgumentException("Method name must start with
'get' or 'is' " + method);
}
}
***************
*** 64,75 ****
if (!(method.getParameterTypes().length == 0 &&
method.getReturnType() != Void.TYPE)) {
throw new IllegalArgumentException("Method must take no
parameters and return a value " + method);
}
! getters.put(name, new Operation(target, method));
}
public void addSetter(Object target, Method method) {
if (!method.getName().startsWith("set")) {
! throw new IllegalArgumentException("Method method name must start
with 'set' " + method);
}
addSetter(method.getName().substring(3), target, method);
}
--- 67,81 ----
if (!(method.getParameterTypes().length == 0 &&
method.getReturnType() != Void.TYPE)) {
throw new IllegalArgumentException("Method must take no
parameters and return a value " + method);
}
! // we want to be user-friendly so we put the attribute name in
! // the Map in both lower-case and upper-case
! getters.put(name.substring(0,1).toUpperCase() + name.substring(1),
new Operation(target, method));
! getters.put(name.substring(0,1).toLowerCase() + name.substring(1),
new Operation(target, method));
}
public void addSetter(Object target, Method method) {
if (!method.getName().startsWith("set")) {
! throw new IllegalArgumentException("Method name must start with
'set' " + method);
}
addSetter(method.getName().substring(3), target, method);
}
***************
*** 78,84 ****
if (!(method.getParameterTypes().length == 1 &&
method.getReturnType() == Void.TYPE)) {
throw new IllegalArgumentException("Method must take one
parameter and not return anything " + method);
}
! setters.put(name, new Operation(target, method));
}
public void addOperation(Object target, Method method) {
--- 84,93 ----
if (!(method.getParameterTypes().length == 1 &&
method.getReturnType() == Void.TYPE)) {
throw new IllegalArgumentException("Method must take one
parameter and not return anything " + method);
}
! // we want to be user-friendly so we put the attribute name in
! // the Map in both lower-case and upper-case
! setters.put(name.substring(0,1).toUpperCase() + name.substring(1),
new Operation(target, method));
! setters.put(name.substring(0,1).toLowerCase() + name.substring(1),
new Operation(target, method));
}
public void addOperation(Object target, Method method) {
***************
*** 107,113 ****
public Object getAttribute(String name) throws Exception {
Operation operation = (Operation) getters.get(name);
if (operation == null) {
! throw new IllegalArgumentException("Unknown attribute " + name);
}
return operation.invoke(null);
}
--- 116,122 ----
public Object getAttribute(String name) throws Exception {
Operation operation = (Operation) getters.get(name);
if (operation == null) {
! throw new IllegalArgumentException(this.targetClass.getName() +
": no get() method for " + name);
}
return operation.invoke(null);
}
***************
*** 115,121 ****
public void setAttribute(String name, Object value) throws Exception {
Operation operation = (Operation) setters.get(name);
if (operation == null) {
! throw new IllegalArgumentException("Unknown attribute " + name);
}
operation.invoke(new Object[]{value});
}
--- 124,130 ----
public void setAttribute(String name, Object value) throws Exception {
Operation operation = (Operation) setters.get(name);
if (operation == null) {
! throw new IllegalArgumentException(this.targetClass.getName() +
": no get() method for " + name);
}
operation.invoke(new Object[]{value});
}
***************
*** 123,129 ****
public Object invoke(String name, Object[] arguments, String[] types)
throws Exception {
Operation operation = (Operation) operations.get(new
GOperationSignature(name, types));
if (operation == null) {
! throw new IllegalArgumentException("Unknown attribute " + name);
}
return operation.invoke(arguments);
}
--- 132,138 ----
public Object invoke(String name, Object[] arguments, String[] types)
throws Exception {
Operation operation = (Operation) operations.get(new
GOperationSignature(name, types));
if (operation == null) {
! throw new IllegalArgumentException(this.targetClass.getName() +
": no get() method for " + name);
}
return operation.invoke(arguments);
}