KickJava   Java API By Example, From Geeks To Geeks.

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


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.activity.task;
26
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.objectweb.fractal.api.NoSuchInterfaceException;
32 import org.objectweb.fractal.api.control.BindingController;
33 import org.objectweb.fractal.api.factory.InstantiationException;
34 import org.objectweb.fractal.julia.InitializationContext;
35 import org.objectweb.util.monolog.api.BasicLevel;
36 import org.objectweb.util.monolog.api.Logger;
37
38 /**
39  * Provides a basic implementation of the {@link TaskManagerController}
40  * interface. <br>
41  * <br>
42  * <b>Requirements </b>
43  * <ul>
44  * <li>this mixin requires the {@link LoggableTaskMixin }mixin.</li>
45  * <li>this mixin requires the
46  * {@link org.objectweb.fractal.julia.control.binding.UseBindingControllerMixin}
47  * mixin.</li>
48  * </ul>
49  */

50 public abstract class BasicTaskControllerMixin implements TaskController
51 {
52
53   /** A map containing registration hints for eack task. */
54   protected Map JavaDoc taskRegistrationHints;
55   /** A map containing control interface for each registered task. */
56   protected Map JavaDoc taskControls;
57
58   // ---------------------------------------------------------------------------
59
// private constructor
60
// ---------------------------------------------------------------------------
61

62   private BasicTaskControllerMixin()
63   {
64   }
65
66   /**
67    * @see org.objectweb.fractal.julia.Controller#initFcController(InitializationContext)
68    */

69   public void initFcController(final InitializationContext ic)
70       throws InstantiationException JavaDoc
71   {
72     _super_initFcController(ic);
73
74     taskRegistrationHints = Collections.synchronizedMap(new HashMap JavaDoc());
75     taskControls = Collections.synchronizedMap(new HashMap JavaDoc());
76   }
77
78   // ---------------------------------------------------------------------------
79
// Implementation of the TaskManagerController interface
80
// ---------------------------------------------------------------------------
81

82   /**
83    * @see TaskController#getTasks()
84    */

85   public Task[] getTasks()
86   {
87     Task[] tasks = new Task[taskRegistrationHints.keySet().size()];
88     return (Task[]) taskRegistrationHints.keySet().toArray(tasks);
89   }
90
91   /**
92    * @see TaskController#addTask(Task, Map)
93    */

94   public void addTask(Task task, Map JavaDoc hints) throws IllegalTaskException
95   {
96     if (taskRegistrationHints.containsKey(task))
97     {
98       throw new IllegalTaskException(task,
99           "The given task has already been added in the task controller", null);
100     }
101     taskRegistrationHints.put(task, hints);
102   }
103
104   /**
105    * @see TaskController#getTaskControl(Task)
106    */

107   public Object JavaDoc getTaskControl(Task task) throws NoSuchTaskException
108   {
109     if (!taskRegistrationHints.containsKey(task))
110     {
111       throw new NoSuchTaskException(task);
112     }
113     return taskControls.get(task);
114   }
115
116   /**
117    * @see TaskController#removeTask(Task)
118    */

119   public void removeTask(Task task) throws NoSuchTaskException,
120       IllegalTaskException
121   {
122     if (!taskRegistrationHints.containsKey(task))
123     {
124       throw new NoSuchTaskException(task);
125     }
126     _this_weaveableTCLogger.log(BasicLevel.DEBUG, "remove a task");
127     try
128     {
129       taskRegistrationHints.remove(task);
130     }
131     catch (Exception JavaDoc e)
132     {
133       throw new IllegalTaskException(task, "Unable to unregister task", e);
134     }
135     _this_weaveableTCLogger.log(BasicLevel.DEBUG, "task removed");
136   }
137
138   // ---------------------------------------------------------------------------
139
// Utility methods
140
// ---------------------------------------------------------------------------
141

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

160   /**
161    * The <tt>weaveableTCLogger</tt> field required by this mixin. This field
162    * is supposed to reference the logger of this controller.
163    */

164   public Logger _this_weaveableTCLogger;
165
166   /**
167    * The <tt>weaveableBC</tt> field required by this mixin. This field is
168    * supposed to reference the
169    * {@link org.objectweb.fractal.api.control.BindingController}interface of
170    * the component to which this controller object belongs.
171    */

172   public BindingController _this_weaveableBC;
173
174   /**
175    * The
176    * {@link org.objectweb.fractal.julia.Controller#initFcController(InitializationContext)}
177    * method overriden by this mixin.
178    *
179    * @see org.objectweb.fractal.julia.Controller#initFcController(InitializationContext)
180    */

181   public abstract void _super_initFcController(InitializationContext ic)
182       throws InstantiationException JavaDoc;
183 }
Popular Tags