1 23 24 package org.objectweb.fractal.julia.control.binding; 25 26 import org.objectweb.fractal.api.Component; 27 import org.objectweb.fractal.api.Interface; 28 import org.objectweb.fractal.api.NoSuchInterfaceException; 29 import org.objectweb.fractal.api.control.BindingController; 30 import org.objectweb.fractal.api.control.ContentController; 31 import org.objectweb.fractal.api.control.IllegalBindingException; 32 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 33 import org.objectweb.fractal.api.type.InterfaceType; 34 35 import org.objectweb.fractal.julia.ComponentInterface; 36 import org.objectweb.fractal.julia.Interceptor; 37 38 import java.util.HashSet ; 39 import java.util.List ; 40 import java.util.Set ; 41 import java.util.ArrayList ; 42 43 57 58 public abstract class OptimizedCompositeBindingMixin 59 implements BindingController 60 { 61 62 66 private OptimizedCompositeBindingMixin () { 67 } 68 69 73 87 88 public void bindFc ( 89 final InterfaceType clientItfType, 90 final String clientItfName, 91 final Object serverItf) 92 throws 93 NoSuchInterfaceException, 94 IllegalBindingException, 95 IllegalLifeCycleException 96 { 97 _super_bindFc(clientItfType, clientItfName, serverItf); 98 setFcShortcuts(clientItfType, clientItfName, serverItf); 99 } 100 101 114 115 public void unbindFc ( 116 final InterfaceType clientItfType, 117 final String clientItfName) 118 throws 119 NoSuchInterfaceException, 120 IllegalBindingException, 121 IllegalLifeCycleException 122 { 123 _super_unbindFc(clientItfType, clientItfName); 124 setFcShortcuts(clientItfType, clientItfName, null); 125 } 126 127 131 142 143 public void setFcShortcuts ( 144 final InterfaceType clientItfType, 145 final String clientItfName, 146 final Object serverItf) 147 throws 148 NoSuchInterfaceException, 149 IllegalBindingException 150 { 151 Interface clientItf; 152 if (clientItfType.isFcClientItf()) { 153 clientItf = 154 (Interface)_this_weaveableC.getFcInterface(clientItfName); 155 } else { 156 clientItf = 157 (Interface)_this_weaveableCC.getFcInternalInterface(clientItfName); 158 } 159 160 Set visited = new HashSet (); 161 List shortcutItfs = new ArrayList (); 162 List shortcutImpls = new ArrayList (); 163 164 Object itf = serverItf; 166 while (true) { 167 ComponentInterface citf; 168 if (itf instanceof ComponentInterface) { 169 citf = (ComponentInterface)itf; 170 } else { 171 break; 172 } 173 if (citf.getFcItfImpl() instanceof Interceptor) { 174 break; 175 } 176 try { 177 Component owner = citf.getFcItfOwner(); 178 owner.getFcInterface("content-controller"); 179 BindingController bc = 180 (BindingController)owner.getFcInterface("binding-controller"); 181 itf = bc.lookupFc(citf.getFcItfName()); 182 } catch (NoSuchInterfaceException e) { 183 break; 184 } 185 } 186 Object impl; 187 if (itf == null) { 188 impl = null; 189 } else { 190 if (itf instanceof ComponentInterface) { 191 impl = ((ComponentInterface)itf).getFcItfImpl(); 192 } else { 193 impl = itf; 195 } 196 } 197 198 try { 200 setFcClientShortcuts(clientItf, impl, visited, shortcutItfs, shortcutImpls); 201 } catch (Exception e) { 202 Interface sItf; 203 sItf = serverItf instanceof Interface ? (Interface)serverItf : null; 204 throw new ChainedIllegalBindingException( 205 e, 206 clientItf.getFcItfOwner(), 207 sItf == null ? null : sItf.getFcItfOwner(), 208 clientItf.getFcItfName(), 209 sItf == null ? null : sItf.getFcItfName(), 210 "Cannot create shortcut links"); 211 } 212 213 for (int i = 0; i < shortcutItfs.size(); ++i) { 215 Interface shortcutItf = (Interface)shortcutItfs.get(i); 216 Object shortcutImpl = shortcutImpls.get(i); 217 Component c = shortcutItf.getFcItfOwner(); 218 ContentController cc; 219 try { 220 cc = (ContentController)c.getFcInterface("content-controller"); 221 } catch (NoSuchInterfaceException e) { 222 cc = null; 223 } 224 225 if (cc != null) { 226 ComponentInterface cShortcutItf = (ComponentInterface)shortcutItf; 227 if (cShortcutItf.hasFcInterceptor()) { 228 Object newImpl = cShortcutItf.getFcItfImpl(); 229 ((Interceptor)newImpl).setFcItfDelegate(shortcutImpl); 230 } else { 231 cShortcutItf.setFcItfImpl(shortcutImpl); 232 } 233 } else { 234 BindingController bc; 235 try { 236 bc = (BindingController)c.getFcInterface("binding-controller"); 237 } catch (NoSuchInterfaceException e) { 238 continue; 239 } 240 try { 241 String itfName = shortcutItf.getFcItfName(); 242 Object o = bc.lookupFc(itfName); 243 if (o != null) { 245 bc.unbindFc(itfName); 246 bc.bindFc(itfName, o); 247 } 248 } catch (Exception e) { 249 Interface sItf; 250 sItf = serverItf instanceof Interface ? (Interface)serverItf : null; 251 throw new ChainedIllegalBindingException( 252 e, 253 clientItf.getFcItfOwner(), 254 sItf == null ? null : sItf.getFcItfOwner(), 255 clientItf.getFcItfName(), 256 sItf == null ? null : sItf.getFcItfName(), 257 "Cannot create shortcut links"); 258 } 259 } 260 } 261 } 262 263 276 277 public void setFcClientShortcuts ( 278 final Interface clientItf, 279 final Object impl, 280 final Set visited, 281 final List shortcutItfs, 282 final List shortcutImpls) throws Exception 283 { 284 Component clientComp = clientItf.getFcItfOwner(); 285 ContentController cc; 286 try { 287 cc = (ContentController)clientComp.getFcInterface("content-controller"); 288 } catch (NoSuchInterfaceException e) { 289 cc = null; 290 } 291 292 if (cc != null) { 293 ComponentInterface itf; 294 try { 295 String name = clientItf.getFcItfName(); 296 if (clientItf.isFcInternalItf()) { 297 itf = (ComponentInterface)clientComp.getFcInterface(name); 300 } else { 301 itf = (ComponentInterface)cc.getFcInternalInterface(name); 304 } 305 } catch (NoSuchInterfaceException e) { 306 throw new Exception ("Cannot create shortcut links"); 307 } catch (ClassCastException e) { 308 throw new Exception ("Cannot create shortcut links"); 309 } 310 311 if (!visited.contains(itf)) { 312 visited.add(itf); 313 shortcutItfs.add(itf); 314 shortcutImpls.add(impl); 315 if (itf.hasFcInterceptor()) { 316 Object newImpl = itf.getFcItfImpl(); 317 setFcServerShortcuts(itf, newImpl, visited, shortcutItfs, shortcutImpls); 318 } else { 319 setFcServerShortcuts(itf, impl, visited, shortcutItfs, shortcutImpls); 320 } 321 } 322 } else if (!visited.contains(clientItf)) { 323 visited.add(clientItf); 324 shortcutItfs.add(clientItf); 325 shortcutImpls.add(impl); 326 } 327 } 328 329 342 343 public void setFcServerShortcuts ( 344 final Interface serverItf, 345 final Object impl, 346 final Set visited, 347 final List shortcutItfs, 348 final List shortcutImpls) throws Exception 349 { 350 Object [] comps = Util.getFcPotentialClientsOf(serverItf).toArray(); 351 for (int i = 0; i < comps.length; ++i) { 352 Component comp = (Component)comps[i]; 353 List clientItfs = Util.getFcClientItfsBoundTo(comp, serverItf); 354 for (int j = 0; j < clientItfs.size(); ++j) { 355 setFcClientShortcuts( 356 (Interface)clientItfs.get(j), 357 impl, 358 visited, 359 shortcutItfs, 360 shortcutImpls); 361 } 362 } 363 } 364 365 369 374 375 public Component _this_weaveableC; 376 377 382 383 public ContentController _this_weaveableCC; 384 385 399 400 public abstract void _super_bindFc ( 401 InterfaceType clientItfType, 402 String clientItfName, 403 Object serverItf) 404 throws 405 NoSuchInterfaceException, 406 IllegalBindingException, 407 IllegalLifeCycleException; 408 409 422 423 public abstract void _super_unbindFc ( 424 InterfaceType clientItfType, 425 String clientItfName) 426 throws 427 NoSuchInterfaceException, 428 IllegalBindingException, 429 IllegalLifeCycleException; 430 } 431 | Popular Tags |