Using OGNL you can register a custom PropertyAccessor to make your object's dynamic properties appear as if they are true properties with getters and setters:Is it possible to have a page or component with dynamic properties?
In our applications, almost 50% (maybe more) of code is in getters and setters. I’m wondering about the possibility to integrate Jakarta DynaBeans to Tapestry in order to eliminate these getters and setters.
Does it make sense?
I’m not sure about features overlap between ognl and dynabeans,
Foo.java:
-------------------------
public class Foo
{
private Map variables = new HashMap()
private int count = 0;
public int getCount()
{
return count;
}
public void setCount(int value)
{
count = value;
}
public Object getVariableValue(String key)
{
return variables.get(key);
}
public void setVariableValue(String key, Object value)
{
variables.put(key, value);
}
}
FooPropertyAccessor.java:
-------------------------
import java.util.*;
import ognl.ObjectPropertyAccessor;
import ognl.OgnlException;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;
public class FooPropertyAccessor
{
private ObjectPropertyAccessor defaultAccessor;
/*===================================================================
Private methods
===================================================================*/
{
try {
defaultAccessor = (ObjectPropertyAccessor)OgnlRuntime.getPropertyAccessor(Object.class);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/*===================================================================
PropertyAccessor interface
===================================================================*/
public Object getProperty(Map context, Object target, Object name) throws OgnlException
{
Object result = null;
if ((result = defaultAccessor.getPossibleProperty(context, target, name.toString())) == OgnlRuntime.NotFound) {
try {
result = ((Foo)target).getVariableValue(name.toString());
} catch (OgnlException ex) {
throw ex;
} catch (Exception ex) {
throw new OgnlException("getting property '" + name + "'", ex);
}
}
return result;
}
public void setProperty(Map context, Object target, Object name, Object value) throws OgnlException
{
if (defaultAccessor.setPossibleProperty(context, target, name.toString(), value) == OgnlRuntime.NotFound) {
try {
((Foo)target).setVariableValue(name.toString(), value);
} catch (OgnlException ex) {
throw ex;
} catch (Exception ex) {
throw new OgnlException("setting property '" + name + "'", ex);
}
}
}
}
Now foo.xxx will get the variable value from getVariableValue("xxx") and foo.count will call the getCount() method (that's the defaultAccessor). You could potentially make a page or component abstract superclass with this pattern and subclass all of your components from that (or register as many property accessors as necessary).
Presto! Dynamic properties.
- Drew
-- +---------------------------------+ < Drew Davidson | OGNL Technology > +---------------------------------+ | Email: [EMAIL PROTECTED] / | Web: http://www.ognl.org / | Vox: (520) 531-1966 < | Fax: (520) 531-1965 \ | Mobile: (520) 405-2967 \ +---------------------------------+
