KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > control > lifecycle > ActivityLifeCycleMixin


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: dream@objectweb.org
20  *
21  * Initial developer(s): Vivien Quema
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.control.lifecycle;
26
27 import org.objectweb.dream.control.activity.task.IllegalTaskException;
28 import org.objectweb.dream.control.activity.task.NoSuchTaskException;
29 import org.objectweb.dream.control.activity.task.Task;
30 import org.objectweb.dream.control.activity.task.TaskActivationController;
31 import org.objectweb.dream.control.activity.task.TaskController;
32 import org.objectweb.dream.control.activity.task.TaskStoppedListener;
33 import org.objectweb.dream.util.Error;
34 import org.objectweb.fractal.api.Component;
35 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
36 import org.objectweb.fractal.julia.control.lifecycle.ChainedIllegalLifeCycleException;
37 import org.objectweb.fractal.julia.control.lifecycle.LifeCycleCoordinator;
38 import org.objectweb.util.monolog.api.BasicLevel;
39 import org.objectweb.util.monolog.api.Logger;
40
41 /**
42  * This mixin can be applied to a lifecycle controller in order to start the
43  * activities of the component to which the controller belongs.
44  * <p>
45  * <p>
46  * <b>Requirements </b>
47  * <ul>
48  * <li>This mixin must be used with the
49  * {@link org.objectweb.fractal.julia.UseComponentMixin }mixin.</li>
50  * <li>This mixin must be used with the
51  * {@link org.objectweb.dream.control.activity.task.UseTaskControllerMixin}
52  * mixin.</li>
53  * <li>This mixin must be used with the
54  * {@link org.objectweb.dream.control.activity.task.UseTaskActivationControllerMixin}
55  * mixin.</li>
56  * <li>This mixin must be used with the {@link LoggableLifeCycleMixin }mixin.
57  * </li>
58  * <li>This mixin must be used with the
59  * {@link org.objectweb.fractal.julia.control.lifecycle.BasicLifeCycleControllerMixin}
60  * lifecycle controller implementation.</li>
61  * </ul>
62  */

63 public abstract class ActivityLifeCycleMixin implements TaskStoppedListener
64 {
65
66   protected int fcNbStoppingTask;
67   protected LifeCycleCoordinator fcActivityCoordinator;
68
69   // ---------------------------------------------------------------------------
70
// Private constructor
71
// ---------------------------------------------------------------------------
72

73   private ActivityLifeCycleMixin()
74   {
75   }
76
77   // ---------------------------------------------------------------------------
78
// Fields and methods added and overriden by the mixin class
79
// ---------------------------------------------------------------------------
80

81   /**
82    * Calls super implementation and starts internal activities in the started
83    * state.
84    *
85    * @see LifeCycleCoordinator#setFcStarted()
86    */

87   public synchronized boolean setFcStarted() throws IllegalLifeCycleException
88   {
89     if (!_super_setFcStarted())
90     {
91       return false;
92     }
93
94     // restart tasks
95
Task[] tasks = _this_weaveableTC.getTasks();
96     for (int i = 0; i < tasks.length; i++)
97     {
98       if (_this_weaveableLCCLogger.isLoggable(BasicLevel.DEBUG))
99       {
100         _this_weaveableLCCLogger.log(BasicLevel.DEBUG, "activate task "
101             + tasks[i]);
102       }
103       try
104       {
105         _this_weaveableTAC.activateTask(tasks[i]);
106       }
107       catch (NoSuchTaskException e)
108       {
109         // cannot happend
110
Error.bug(_this_weaveableLCCLogger, e);
111       }
112       catch (IllegalTaskException e)
113       {
114         throw new ChainedIllegalLifeCycleException(e, _this_weaveableC,
115             "Error while registering task");
116       }
117     }
118     return true;
119   }
120
121   /**
122    * Calls super implementation and looks for this component's coordinator
123    * controller.
124    *
125    * @see LifeCycleCoordinator#setFcStopping(LifeCycleCoordinator)
126    */

127   public void setFcStopping(final LifeCycleCoordinator coordinator)
128       throws IllegalLifeCycleException
129   {
130     Task[] tasks = _this_weaveableTC.getTasks();
131     synchronized (this)
132     {
133       fcNbStoppingTask += tasks.length;
134
135       fcActivityCoordinator = coordinator;
136       for (int i = 0; i < tasks.length; i++)
137       {
138         if (_this_weaveableLCCLogger.isLoggable(BasicLevel.DEBUG))
139         {
140           _this_weaveableLCCLogger.log(BasicLevel.DEBUG, "interrupt task "
141               + tasks[i]);
142         }
143         try
144         {
145           _this_weaveableTAC.deactivateTask(tasks[i], this);
146         }
147         catch (NoSuchTaskException e)
148         {
149           // cannot happend
150
Error.bug(_this_weaveableLCCLogger, e);
151         }
152         catch (IllegalTaskException e)
153         {
154           throw new ChainedIllegalLifeCycleException(e, _this_weaveableC,
155               "Error while unregistering task");
156         }
157       }
158     }
159     _super_setFcStopping(coordinator);
160   }
161
162   /**
163    * Returns <code>true</code> If the component has no executing task and
164    * super implementation returns <code>true</code>.
165    *
166    * @return <code>true</code> If the component is inactive
167    */

168   public boolean checkInactivity()
169   {
170     return fcNbStoppingTask == 0 && _super_checkInactivity();
171   }
172
173   //---------------------------------------------------------------------------
174
// Implementation of TaskStoppedListener interface
175
//---------------------------------------------------------------------------
176

177   /**
178    * @see TaskStoppedListener#taskStopped(Task)
179    */

180   public void taskStopped(Task task)
181   {
182     synchronized (this)
183     {
184       if (_this_weaveableLCCLogger.isLoggable(BasicLevel.DEBUG))
185       {
186         _this_weaveableLCCLogger.log(BasicLevel.DEBUG, "task stopped" + task);
187       }
188       fcNbStoppingTask--;
189       _this_isInactivated(fcActivityCoordinator);
190     }
191   }
192
193   // ---------------------------------------------------------------------------
194
// Fields and methods required by the mixin class in the base class
195
// ---------------------------------------------------------------------------
196

197   /**
198    * The <tt>weaveableC</tt> field required by this mixin. This field is
199    * supposed to reference the {@link Component}interface of the component to
200    * which this controller object belongs.
201    */

202   public Component _this_weaveableC;
203
204   /**
205    * The <tt>weaveableLCCLogger</tt> field required by this mixin. This field
206    * is supposed to reference the {@link Logger }of this controller.
207    */

208   public Logger _this_weaveableLCCLogger;
209
210   /**
211    * The <tt>weaveableTC</tt> field required by this mixin. This field is
212    * supposed to reference the {@link TaskController }interface of the
213    * component to which this controller object belongs.
214    */

215   public TaskController _this_weaveableTC;
216
217   /**
218    * The <code>weaveableTAC</code> field required by this mixin. This field is
219    * supposed to reference the {@link TaskActivationController }interface of
220    * the component to which this controller object belongs.
221    */

222   public TaskActivationController _this_weaveableTAC;
223
224   /**
225    * The {@link LifeCycleCoordinator#setFcStarted() }method overriden by this
226    * mixin.
227    *
228    * @see org.objectweb.fractal.julia.control.lifecycle.BasicLifeCycleControllerMixin
229    */

230   public abstract boolean _super_setFcStarted()
231       throws IllegalLifeCycleException;
232
233   /**
234    * The {@link LifeCycleCoordinator#setFcStopping(LifeCycleCoordinator) }
235    * method overriden by this mixin.
236    *
237    * @see org.objectweb.fractal.julia.control.lifecycle.BasicLifeCycleControllerMixin
238    */

239   public abstract void _super_setFcStopping(
240       final LifeCycleCoordinator coordinator) throws IllegalLifeCycleException;
241
242   /**
243    * The <code>checkInactivity</code> method overriden by this mixin. This
244    * method is supposed to work as this
245    * {@link SimpleLifeCycleControllerMixin#checkInactivity() }method.
246    *
247    * @see SimpleLifeCycleControllerMixin#checkInactivity()
248    */

249   public abstract boolean _super_checkInactivity();
250
251   /**
252    * The <code>isInactivated</code> method used by this mixin. This method is
253    * supposed to work as this
254    * {@link SimpleLifeCycleControllerMixin#isInactivated }method.
255    *
256    * @see SimpleLifeCycleControllerMixin#isInactivated
257    */

258   public abstract boolean _this_isInactivated(LifeCycleCoordinator coordinator);
259 }
Popular Tags