{
beanInfo = Introspector.getBeanInfo(control.getClass());
}
catch (IntrospectionException ie)
{
throw new ControlException("Unable to locate BeanInfo", ie);
}
//
// Cast the encoding stream to an XMLEncoder (only encoding supported) and then set
// the stream owner to the bean being persisted
//
XMLEncoder xmlOut = (XMLEncoder)out;
Object owner = xmlOut.getOwner();
xmlOut.setOwner(control);
try
{
//
// The default implementation of property persistence will use BeanInfo to
// incrementally compare oldInstance property values to newInstance property values.
// Because the bean instance PropertyMap holds only the values that have been
// modified, this process can be optimized by directly writing out only the properties
// found in the map.
//
BeanPropertyMap beanMap = control.getPropertyMap();
PropertyDescriptor [] propDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyKey pk : beanMap.getPropertyKeys())
{
//
// Locate the PropertyDescriptor for the modified property, and use it to write
// the property value to the encoder stream
//
String propName = pk.getPropertyName();
boolean found = false;
for (int i = 0; i < propDescriptors.length; i++)
{
if (propName.equals(propDescriptors[i].getName()))
{
found = true;
// Only write the property if it is not flagged as transient
Object transientVal = propDescriptors[i].getValue("transient");
if (transientVal == null || transientVal.equals(Boolean.FALSE))
{
xmlOut.writeStatement(
new Statement(oldInstance,
propDescriptors[i].getWriteMethod().getName(),
new Object [] {beanMap.getProperty(pk)}));
}
}
}
if (found == false)
{
throw new ControlException("Unknown property in bean PropertyMap: " + pk);
}
}
//
// Get the bean context associated with the bean, and persist any nested controls
//
ControlBeanContext cbc = control.getControlBeanContext();
if (cbc.size() != 0)
{
xmlOut.setPersistenceDelegate(ControlBeanContext.class,
new ContextPersistenceDelegate());
Iterator nestedIter = cbc.iterator();
while (nestedIter.hasNext())
{
Object bean = nestedIter.next();
if (bean instanceof ControlBean)
{
xmlOut.writeStatement(
new Statement(cbc, "add", new Object [] { bean } ));
}
}
}
//
// Restore any listeners associated with the control
//
EventSetDescriptor [] eventSetDescriptors = beanInfo.getEventSetDescriptors();
for (int i = 0; i < eventSetDescriptors.length; i++)
{
EventSetDescriptor esd = eventSetDescriptors[i];
Method listenersMethod = esd.getGetListenerMethod();
String addListenerName = esd.getAddListenerMethod().getName();
if (listenersMethod != null)
{
//
// Get the list of listeners, and then add statements to incrementally
// add them in the same order
//
try
{
Object [] lstnrs = (Object [])listenersMethod.invoke(control,
new Object []{});
for (int j = 0; j < lstnrs.length; j++)
{
//
// If this is a generated EventAdaptor class, then set the delegate
// explicitly
//
if (lstnrs[j] instanceof EventAdaptor)
xmlOut.setPersistenceDelegate(lstnrs[j].getClass(),
new AdaptorPersistenceDelegate());
xmlOut.writeStatement(
new Statement(control, addListenerName, new Object [] {lstnrs[j]}));
}
}
catch (Exception iae)
{
throw new ControlException("Unable to initialize listeners", iae);
}
}
}
//