KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > control > activity > task > BasicTaskActivationMixin


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

23
24 package org.objectweb.dream.control.activity.task;
25
26 import java.util.Map JavaDoc;
27
28 import org.objectweb.fractal.api.NoSuchInterfaceException;
29 import org.objectweb.fractal.api.control.BindingController;
30 import org.objectweb.util.monolog.api.BasicLevel;
31 import org.objectweb.util.monolog.api.Logger;
32
33 /**
34  * Basic Implementation of the {@link TaskActivationController}. When a task is
35  * activated, it registers it in the {@link TaskManagerController }this
36  * component is bound to. This mixin must be used with a {@link TaskController }
37  * implementation mixin like {@link BasicTaskControllerMixin}.<br>
38  * <br>
39  * <b>Requirements </b>
40  * <ul>
41  * <li>this mixin requires the {@link LoggableTaskMixin }mixin.</li>
42  * <li>this mixin requires the
43  * {@link org.objectweb.fractal.julia.control.binding.UseBindingControllerMixin}
44  * mixin.</li>
45  * </ul>
46  */

47 public abstract class BasicTaskActivationMixin
48     implements
49       TaskActivationController
50 {
51
52   // ---------------------------------------------------------------------------
53
// Implementation of the TaskActivationController interface
54
// ---------------------------------------------------------------------------
55

56   /**
57    * @see TaskActivationController#activateTask(Task)
58    */

59   public void activateTask(Task task) throws NoSuchTaskException,
60       IllegalTaskException
61   {
62     if (!_this_taskRegistrationHints.containsKey(task))
63     {
64       throw new NoSuchTaskException(task);
65     }
66     if (_this_taskControls.containsKey(task))
67     {
68       return;
69     }
70     _this_weaveableTCLogger.log(BasicLevel.DEBUG, "register a task");
71     Map JavaDoc hints = (Map JavaDoc) _this_taskRegistrationHints.get(task);
72     TaskManagerController tmc = getTaskManagerBinding();
73     Object JavaDoc taskControl;
74     try
75     {
76       taskControl = tmc.registerTask(task, hints);
77     }
78     catch (Exception JavaDoc e)
79     {
80       throw new IllegalTaskException(task, "Unable to register component", e);
81     }
82
83     _this_taskControls.put(task, taskControl);
84     _this_weaveableTCLogger.log(BasicLevel.DEBUG, "task registered");
85   }
86
87   /**
88    * @see TaskActivationController#deactivateTask(Task)
89    */

90   public void deactivateTask(Task task) throws NoSuchTaskException,
91       IllegalTaskException
92   {
93     if (!_this_taskRegistrationHints.containsKey(task))
94     {
95       throw new NoSuchTaskException(task);
96     }
97     if (!_this_taskControls.containsKey(task))
98     {
99       return;
100     }
101     _this_weaveableTCLogger.log(BasicLevel.DEBUG, "deactivate a task");
102     try
103     {
104       getTaskManagerBinding().unregisterTask(task);
105     }
106     catch (Exception JavaDoc e)
107     {
108       throw new IllegalTaskException(task, "Unable to unregister component", e);
109     }
110     _this_taskControls.remove(task);
111     _this_weaveableTCLogger.log(BasicLevel.DEBUG, "task deactivated");
112   }
113
114   /**
115    * @see TaskActivationController#deactivateTask(Task, TaskStoppedListener)
116    */

117   public void deactivateTask(Task task, TaskStoppedListener taskStoppedListener)
118       throws NoSuchTaskException, IllegalTaskException
119   {
120     if (!_this_taskRegistrationHints.containsKey(task))
121     {
122       throw new NoSuchTaskException(task);
123     }
124     if (!_this_taskControls.containsKey(task))
125     {
126       return;
127     }
128     _this_weaveableTCLogger.log(BasicLevel.DEBUG, "deactivate a task");
129     try
130     {
131       getTaskManagerBinding().interruptTask(
132           task,
133           new TaskActivationStoppedListener(taskStoppedListener,
134               _this_taskControls));
135     }
136     catch (Exception JavaDoc e)
137     {
138       throw new IllegalTaskException(task, "Unable to unregister component", e);
139     }
140     _this_taskControls.remove(task);
141     _this_weaveableTCLogger.log(BasicLevel.DEBUG, "task deactivated");
142   }
143
144   // ---------------------------------------------------------------------------
145
// Utility methods
146
// ---------------------------------------------------------------------------
147

148   private TaskManagerController getTaskManagerBinding()
149       throws IllegalTaskException
150   {
151     try
152     {
153       return (TaskManagerController) _this_weaveableBC.lookupFc("task-manager");
154     }
155     catch (NoSuchInterfaceException e)
156     {
157       throw new IllegalTaskException(null,
158           "This component has no task-manager client interface", null);
159     }
160   }
161
162   // ---------------------------------------------------------------------------
163
// Fields and methods required by the mixin class in the base class
164
// ---------------------------------------------------------------------------
165

166   /**
167    * The <code>weaveableTCLogger</code> field required by this mixin. This
168    * field is supposed to reference the logger of this controller.
169    */

170   public Logger _this_weaveableTCLogger;
171
172   /**
173    * The <code>taskRegistrationHints</code> field required by this mixin. This
174    * field is supposed to reference the map containing hints passed when task
175    * are added in the task controller
176    *
177    * @see TaskController#addTask(Task, Map)
178    */

179   public Map JavaDoc _this_taskRegistrationHints;
180
181   /**
182    * The <code>taskControls</code> field required by this mixin. This field is
183    * supposed to reference the map containing control object returned by
184    * {@link TaskController#getTaskControl(Task)}.
185    */

186   public Map JavaDoc _this_taskControls;
187
188   /**
189    * The <tt>weaveableBC</tt> field required by this mixin. This field is
190    * supposed to reference the
191    * {@link org.objectweb.fractal.api.control.BindingController}interface of
192    * the component to which this controller object belongs.
193    */

194   public BindingController _this_weaveableBC;
195
196 }
Popular Tags