1 23 24 package org.objectweb.fractal.julia.control.lifecycle; 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.ContentController; 30 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 31 import org.objectweb.fractal.api.type.InterfaceType; 32 33 import org.objectweb.fractal.julia.control.binding.Util; 34 35 import java.util.HashSet ; 36 import java.util.List ; 37 import java.util.Set ; 38 import java.util.ArrayList ; 39 40 57 58 public abstract class OptimizedLifeCycleControllerMixin 59 implements LifeCycleCoordinator 60 { 61 62 66 private OptimizedLifeCycleControllerMixin () { 67 } 68 69 73 76 77 public boolean fcStarted; 78 79 83 public String getFcState () { 84 return fcStarted ? STARTED : STOPPED; 85 } 86 87 public void startFc () throws IllegalLifeCycleException { 88 Component id; 89 try { 90 id = (Component)_this_weaveableC.getFcInterface("component"); 91 } catch (NoSuchInterfaceException e) { 92 throw new ChainedIllegalLifeCycleException( 93 e, _this_weaveableC, "Cannot start component"); 94 } 95 LifeCycleCoordinator[] clccs = getFcLifeCycleControllers(id); 96 for (int i = 0; i < clccs.length; ++i) { 97 clccs[i].setFcStarted(); 98 } 99 _this_setFcState(true); 100 } 101 102 public void stopFc () throws IllegalLifeCycleException { 103 Component id; 104 try { 105 id = (Component)_this_weaveableC.getFcInterface("component"); 106 } catch (NoSuchInterfaceException e) { 107 throw new ChainedIllegalLifeCycleException( 108 e, _this_weaveableC, "Cannot stop component"); 109 } 110 LifeCycleCoordinator[] clccs = getFcLifeCycleControllers(id); 111 _this_stopFc(clccs); 112 _this_setFcState(false); 113 } 114 115 119 public boolean setFcStarted () { 120 if (!fcStarted) { 121 fcStarted = true; 122 return true; 123 } 124 return false; 125 } 126 127 public void setFcStopping (final LifeCycleCoordinator coordinator) 128 throws IllegalLifeCycleException 129 { 130 throw new Error ("Internal error"); 133 } 134 135 public boolean setFcStopped () { 136 if (fcStarted) { 137 fcStarted = false; 138 return true; 139 } 140 return false; 141 } 142 143 147 158 159 public LifeCycleCoordinator[] getFcLifeCycleControllers ( 160 final Component id) throws IllegalLifeCycleException 161 { 162 List clccList = getFcInternalLifeCycleControllers(); 163 Object [] sItfs = id.getFcInterfaces(); 164 Set visited = new HashSet (); 165 for (int i = 0; i < sItfs.length; ++i) { 166 Interface sItf = (Interface)sItfs[i]; 167 if (!((InterfaceType)sItf.getFcItfType()).isFcClientItf()) { 168 getSExtLifeCycleControllers(sItf, clccList, visited); 169 } 170 } 171 LifeCycleCoordinator[] clccs; 172 clccs = new LifeCycleCoordinator[clccList.size()]; 173 return (LifeCycleCoordinator[])clccList.toArray(clccs); 174 } 175 176 186 187 private void getSExtLifeCycleControllers ( 188 final Interface serverItf, 189 final List clccList, 190 final Set visited) throws IllegalLifeCycleException 191 { 192 Object [] comps; 193 try { 194 comps = Util.getFcPotentialClientsOf(serverItf).toArray(); 195 } catch (Exception e) { 196 throw new ChainedIllegalLifeCycleException( 197 e, 198 serverItf.getFcItfOwner(), 199 "Cannot get the LifeCycleCoordinator interfaces"); 200 } 201 for (int i = 0; i < comps.length; ++i) { 202 Component comp = (Component)comps[i]; 203 Interface[] clientItfs; 204 try { 205 List l = Util.getFcClientItfsBoundTo(comp, serverItf); 206 clientItfs = (Interface[])l.toArray(new Interface[l.size()]); 207 } catch (Exception e) { 208 throw new ChainedIllegalLifeCycleException( 209 e, 210 serverItf.getFcItfOwner(), 211 "Cannot get the LifeCycleCoordinator interfaces"); 212 } 213 for (int j = 0; j < clientItfs.length; ++j) { 214 getCExtLifeCycleControllers(clientItfs[j], clccList, visited); 215 } 216 } 217 } 218 219 229 230 private void getCExtLifeCycleControllers ( 231 final Interface clientItf, 232 final List clccList, 233 final Set visited) throws IllegalLifeCycleException 234 { 235 Component component = clientItf.getFcItfOwner(); 236 ContentController cc = null; 237 try { 238 cc = (ContentController)component.getFcInterface("content-controller"); 239 } catch (NoSuchInterfaceException e) { 240 } 241 if (cc != null) { 242 Interface itf; 243 String name = clientItf.getFcItfName(); 244 try { 245 if (!clientItf.isFcInternalItf()) { 246 itf = (Interface)cc.getFcInternalInterface(name); 247 } else { 248 itf = (Interface)component.getFcInterface(name); 249 } 250 } catch (NoSuchInterfaceException e) { 251 throw new ChainedIllegalLifeCycleException( 252 e, 253 component, 254 "Cannot find the LifeCycleCoordinator interfaces"); 255 } 256 if (!visited.contains(itf)) { 257 visited.add(itf); 258 getSExtLifeCycleControllers(itf, clccList, visited); 259 } 260 } else if (!visited.contains(clientItf)) { 261 visited.add(clientItf); 262 Component c = clientItf.getFcItfOwner(); 263 LifeCycleCoordinator lcc; 264 try { 265 lcc = (LifeCycleCoordinator)c.getFcInterface("lifecycle-controller"); 266 } catch (Exception e) { 267 try { 268 lcc = (LifeCycleCoordinator)c.getFcInterface("/lifecycle-coordinator"); 269 } catch (NoSuchInterfaceException f) { 270 throw new ChainedIllegalLifeCycleException( 271 f, component, "Primitive client without a LifeCycleCoordinator"); 272 } 273 } 274 if (!clccList.contains(lcc)) { 275 clccList.add(lcc); 276 } 277 } 278 } 279 280 public List getFcInternalLifeCycleControllers () 281 throws IllegalLifeCycleException 282 { 283 Component thisComponent; 285 try { 286 thisComponent = (Component)_this_weaveableC.getFcInterface("component"); 287 } catch (NoSuchInterfaceException e) { 288 throw new ChainedIllegalLifeCycleException( 289 e, 290 _this_weaveableC, 291 "The OptimizedLifeCycleControllerMixin requires " + 292 "components to provide the Component interface"); 293 } 294 List allSubComponents = org.objectweb.fractal.julia.control.content.Util. 295 getAllSubComponents(thisComponent); 296 297 List result = new ArrayList (); 298 for (int i = 0; i < allSubComponents.size(); ++i) { 299 Component c = (Component)allSubComponents.get(i); 300 try { 301 c.getFcInterface("content-controller"); 302 } catch (NoSuchInterfaceException e) { 303 try { 304 result.add( 306 (LifeCycleCoordinator)c.getFcInterface("lifecycle-controller")); 307 } catch (Exception f) { 308 try { 309 result.add(c.getFcInterface("/lifecycle-coordinator")); 310 } catch (NoSuchInterfaceException ignored) { 311 } 312 } 313 } 314 } 315 return result; 316 } 317 318 322 327 328 public Component _this_weaveableC; 329 330 340 341 public abstract void _this_setFcState (boolean started) 342 throws IllegalLifeCycleException; 343 344 353 354 public abstract void _this_stopFc (LifeCycleCoordinator[] components) 355 throws IllegalLifeCycleException; 356 } 357 | Popular Tags |