1 23 24 package org.objectweb.fractal.julia.control.lifecycle; 25 26 import org.objectweb.fractal.api.Component; 27 import org.objectweb.fractal.api.NoSuchInterfaceException; 28 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 29 import org.objectweb.fractal.api.factory.InstantiationException; 30 31 import org.objectweb.fractal.julia.Controller; 32 import org.objectweb.fractal.julia.InitializationContext; 33 import org.objectweb.fractal.julia.factory.ChainedInstantiationException; 34 35 53 54 public abstract class BasicLifeCycleControllerMixin 55 implements Controller, LifeCycleCoordinator 56 { 57 58 62 private BasicLifeCycleControllerMixin () { 63 } 64 65 69 73 74 public int fcState; 75 76 79 80 public int fcInvocationCounter; 81 82 86 87 public LifeCycleCoordinator fcCoordinator; 88 89 96 97 public void initFcController (final InitializationContext ic) 98 throws InstantiationException 99 { 100 try { 101 if (!(ic.getInterface("lifecycle-controller") instanceof LifeCycleCoordinator)) { 102 throw new Exception (); 103 } 104 } catch (Exception e) { 105 try { 106 ic.getInterface("/lifecycle-coordinator"); 107 } catch (Exception f) { 108 throw new ChainedInstantiationException( 109 f, null, "The component must provide a LifeCycleCoordinator interface"); 110 } 111 } 112 _super_initFcController(ic); 113 } 114 115 119 public String getFcState () { 120 return fcState == 0 ? STOPPED : STARTED; 121 } 122 123 public void startFc () throws IllegalLifeCycleException { 124 if (fcState != 2) { 125 _this_setFcState(true); 126 } 127 } 128 129 public void stopFc () throws IllegalLifeCycleException { 130 if (fcState == 2) { 131 _this_stopFc(new LifeCycleCoordinator[] { getFcCoordinator() }); 132 _this_setFcState(false); 133 } 134 } 135 136 140 public boolean setFcStarted () throws IllegalLifeCycleException { 141 synchronized (this) { 142 if (fcState == 2) { 143 return false; 144 } 145 fcState = 2; 146 150 notifyAll(); 151 return true; 152 } 153 } 154 155 public void setFcStopping (final LifeCycleCoordinator coordinator) 156 throws IllegalLifeCycleException 157 { 158 synchronized (this) { 159 fcState = 1; 160 fcCoordinator = coordinator; 161 if (fcInvocationCounter == 0) { 162 fcCoordinator.fcInactivated(getFcCoordinator()); 163 } 164 } 165 } 166 167 public boolean setFcStopped () throws IllegalLifeCycleException { 168 synchronized (this) { 169 if (fcState == 0) { 170 return false; 171 } 172 fcState = 0; 173 fcCoordinator = null; 174 return true; 175 } 176 } 177 178 182 193 194 public void incrementFcInvocationCounter () { 195 201 boolean ok; 202 do { 203 if (fcState == 0) { 204 ok = false; 205 } else if (fcState == 1) { 206 if (fcInvocationCounter == 0) { 207 ok = fcCoordinator.fcActivated(getFcCoordinator()); 208 } else { 209 ok = true; 210 } 211 } else { 212 ok = true; 213 } 214 if (!ok) { 215 try { 216 wait(); 217 } catch (final InterruptedException e) { 218 } 219 } 220 } while (!ok); 221 ++fcInvocationCounter; 222 223 } 224 225 231 232 public void decrementFcInvocationCounter () { 233 239 --fcInvocationCounter; 240 if (fcInvocationCounter == 0) { 241 fcCoordinator.fcInactivated(getFcCoordinator()); 242 } 243 244 } 245 246 250 256 257 public LifeCycleCoordinator getFcCoordinator () { 258 try { 259 return (LifeCycleCoordinator)_this_weaveableC. 260 getFcInterface("lifecycle-controller"); 261 } catch (Exception e) { 262 try { 263 return (LifeCycleCoordinator)_this_weaveableC. 264 getFcInterface("/lifecycle-coordinator"); 265 } catch (NoSuchInterfaceException f) { 266 throw new Error ("Internal error"); } 268 } 269 } 270 271 275 280 281 public Component _this_weaveableC; 282 283 293 294 public abstract void _this_setFcState (boolean started) 295 throws IllegalLifeCycleException; 296 297 306 307 public abstract void _this_stopFc (LifeCycleCoordinator[] components) 308 throws IllegalLifeCycleException; 309 310 318 319 public abstract void _super_initFcController (InitializationContext ic) 320 throws InstantiationException ; 321 } 322 | Popular Tags |