1 package org.apache.beehive.controls.runtime.bean; 2 19 20 import java.beans.BeanInfo ; 21 import java.beans.DefaultPersistenceDelegate ; 22 import java.beans.Encoder ; 23 import java.beans.EventSetDescriptor ; 24 import java.beans.Expression ; 25 import java.beans.IntrospectionException ; 26 import java.beans.Introspector ; 27 import java.beans.PersistenceDelegate ; 28 import java.beans.PropertyDescriptor ; 29 import java.beans.Statement ; 30 import java.beans.XMLEncoder ; 31 import java.lang.reflect.Field ; 32 import java.lang.reflect.Method ; 33 import java.util.Iterator ; 34 import java.util.Set ; 35 36 import org.apache.beehive.controls.api.ControlException; 37 import org.apache.beehive.controls.api.properties.AnnotatedElementMap; 38 import org.apache.beehive.controls.api.properties.BeanPropertyMap; 39 import org.apache.beehive.controls.api.properties.PropertyKey; 40 import org.apache.beehive.controls.api.properties.PropertyMap; 41 42 63 public class BeanPersistenceDelegate extends DefaultPersistenceDelegate 64 { 65 71 class FieldPersistenceDelegate extends PersistenceDelegate 72 { 73 protected Expression instantiate(Object oldInstance, Encoder out) 74 { 75 Field f = (Field )oldInstance; 76 return new Expression (oldInstance, f.getDeclaringClass(), "getDeclaredField", 77 new Object []{f.getName()}); 78 } 79 } 80 81 84 protected Expression instantiate(Object oldInstance, Encoder out) 85 { 86 XMLEncoder xmlOut = (XMLEncoder )out; 87 ControlBean control = (ControlBean)oldInstance; 88 89 ControlBeanContext cbc = null; 94 if (xmlOut.getOwner() != null) 95 cbc = ((ControlBean)xmlOut.getOwner()).getControlBeanContext(); 96 97 AnnotatedElementMap aem = null; 105 PropertyMap pMap = control.getPropertyMap(); 106 while (pMap != null) 107 { 108 if (pMap instanceof AnnotatedElementMap) 109 { 110 aem = (AnnotatedElementMap)pMap; 111 112 if (aem.getAnnotatedElement() instanceof Class ) 118 aem = null; 119 120 xmlOut.setPersistenceDelegate(AnnotatedElementMap.class, 121 new AnnotatedElementMapPersistenceDelegate()); 122 123 break; 124 } 125 126 pMap = pMap.getDelegateMap(); 127 } 128 129 130 return new Expression (control, control.getClass(), "new", 138 new Object [] {cbc, control.getLocalID(), aem}); 139 } 140 141 144 protected void initialize(Class <?> type, Object oldInstance, Object newInstance, Encoder out) 145 { 146 ControlBean control = (ControlBean)oldInstance; 150 BeanInfo beanInfo; 151 try 152 { 153 beanInfo = Introspector.getBeanInfo(control.getClass()); 154 } 155 catch (IntrospectionException ie) 156 { 157 throw new ControlException("Unable to locate BeanInfo", ie); 158 } 159 160 XMLEncoder xmlOut = (XMLEncoder )out; 165 Object owner = xmlOut.getOwner(); 166 xmlOut.setOwner(control); 167 try 168 { 169 170 BeanPropertyMap beanMap = control.getPropertyMap(); 178 PropertyDescriptor [] propDescriptors = beanInfo.getPropertyDescriptors(); 179 for (PropertyKey pk : beanMap.getPropertyKeys()) 180 { 181 String propName = pk.getPropertyName(); 186 boolean found = false; 187 for (int i = 0; i < propDescriptors.length; i++) 188 { 189 if (propName.equals(propDescriptors[i].getName())) 190 { 191 found = true; 192 193 Object transientVal = propDescriptors[i].getValue("transient"); 195 if (transientVal == null || transientVal.equals(Boolean.FALSE)) 196 { 197 xmlOut.writeStatement( 198 new Statement (oldInstance, 199 propDescriptors[i].getWriteMethod().getName(), 200 new Object [] {beanMap.getProperty(pk)})); 201 } 202 } 203 } 204 if (found == false) 205 { 206 throw new ControlException("Unknown property in bean PropertyMap: " + pk); 207 } 208 } 209 210 ControlBeanContext cbc = control.getControlBeanContext(); 214 if (cbc.size() != 0) 215 { 216 xmlOut.setPersistenceDelegate(ControlBeanContext.class, 217 new ContextPersistenceDelegate()); 218 219 Iterator nestedIter = cbc.iterator(); 220 while (nestedIter.hasNext()) 221 { 222 Object bean = nestedIter.next(); 223 if (bean instanceof ControlBean) 224 { 225 xmlOut.writeStatement( 226 new Statement (cbc, "add", new Object [] { bean } )); 227 } 228 } 229 } 230 231 EventSetDescriptor [] eventSetDescriptors = beanInfo.getEventSetDescriptors(); 235 for (int i = 0; i < eventSetDescriptors.length; i++) 236 { 237 EventSetDescriptor esd = eventSetDescriptors[i]; 238 Method listenersMethod = esd.getGetListenerMethod(); 239 String addListenerName = esd.getAddListenerMethod().getName(); 240 if (listenersMethod != null) 241 { 242 try 247 { 248 Object [] lstnrs = (Object [])listenersMethod.invoke(control, 249 new Object []{}); 250 for (int j = 0; j < lstnrs.length; j++) 251 { 252 if (lstnrs[j] instanceof EventAdaptor) 257 xmlOut.setPersistenceDelegate(lstnrs[j].getClass(), 258 new AdaptorPersistenceDelegate()); 259 xmlOut.writeStatement( 260 new Statement (control, addListenerName, new Object [] {lstnrs[j]})); 261 } 262 } 263 catch (Exception iae) 264 { 265 throw new ControlException("Unable to initialize listeners", iae); 266 } 267 } 268 } 269 270 Object impl = control.getImplementation(); 275 if (impl != null) 276 { 277 278 Class implClass = impl.getClass(); 283 if (xmlOut.getPersistenceDelegate(implClass) instanceof DefaultPersistenceDelegate ) 284 xmlOut.setPersistenceDelegate(implClass, new ImplPersistenceDelegate()); 285 286 xmlOut.writeStatement( 292 new Statement (impl, "toString", null)); 293 } 294 } 295 finally 296 { 297 xmlOut.setOwner(owner); 299 } 300 } 301 302 305 public void writeObject(Object oldInstance, Encoder out) 306 { 307 out.setPersistenceDelegate(Field .class, new FieldPersistenceDelegate()); 310 super.writeObject(oldInstance, out); 311 } 312 } 313 | Popular Tags |