KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.objectweb.dream.control.activity.scheduler.Scheduler;
28 import org.objectweb.dream.control.activity.task.thread.ThreadPoolTask;
29 import org.objectweb.dream.util.Error;
30 import org.objectweb.fractal.api.Component;
31 import org.objectweb.fractal.api.NoSuchInterfaceException;
32 import org.objectweb.fractal.api.control.NameController;
33 import org.objectweb.fractal.api.factory.GenericFactory;
34 import org.objectweb.fractal.api.factory.InstantiationException;
35 import org.objectweb.fractal.api.type.ComponentType;
36 import org.objectweb.fractal.api.type.InterfaceType;
37 import org.objectweb.fractal.api.type.TypeFactory;
38 import org.objectweb.fractal.util.Fractal;
39
40 /**
41  * Provides utility functions to handle tasks.
42  */

43 public final class Util
44 {
45
46   private Util()
47   {
48   }
49
50   /**
51    * Creates a task.
52    *
53    * @param task an object implementing the <code>Task</code> interface.
54    * @param controllerDesc the name of the controller description.
55    * @return a component implementing the <code>Task</code> interface.
56    * @throws InstantiationException if an error occurs while creating the task.
57    */

58   public static Component createTask(Task task, String JavaDoc controllerDesc)
59       throws InstantiationException JavaDoc
60   {
61     Component bootstrap = org.objectweb.fractal.api.Fractal
62         .getBootstrapComponent();
63     TypeFactory typeFactory = null;
64     GenericFactory genericFactory = null;
65     try
66     {
67       genericFactory = Fractal.getGenericFactory(bootstrap);
68       typeFactory = Fractal.getTypeFactory(bootstrap);
69     }
70     catch (NoSuchInterfaceException e1)
71     {
72       Error.bug(null, e1);
73     }
74
75     // create interface type
76
// The task server interface type
77
InterfaceType taskItfType = typeFactory.createFcItfType(Task.ITF_NAME,
78         Task.class.getName(), false, false, false);
79
80     // The scheduler client interface type
81
InterfaceType schedulerItfType = typeFactory.createFcItfType(
82         Scheduler.ITF_NAME, Scheduler.class.getName(), true, true, false);
83
84     // create component type
85
ComponentType taskType = typeFactory.createFcType(new InterfaceType[]{
86         taskItfType, schedulerItfType});
87
88     // instantiate components
89
Component component = genericFactory.newFcInstance(taskType,
90         controllerDesc, task);
91
92     if (task instanceof NameController)
93     {
94       try
95       {
96         Fractal.getNameController(component).setFcName(
97             ((NameController) task).getFcName());
98       }
99       catch (NoSuchInterfaceException e)
100       {
101         // ignore
102
}
103     }
104
105     return component;
106   }
107
108   /**
109    * Creates a scheduler.
110    *
111    * @param scheduler the object implementing the <code>Scheduler</code>
112    * interface.
113    * @param taskName the name of the thread pool task to create. May be
114    * <code>null</code>.
115    * @return a component implementing the <code>Scheduler</code> interface.
116    * @throws InstantiationException if an error occurs while creating the
117    * scheduler.
118    */

119   public static Component createScheduler(Scheduler scheduler, String JavaDoc taskName)
120       throws InstantiationException JavaDoc
121   {
122     Component bootstrap = org.objectweb.fractal.api.Fractal
123         .getBootstrapComponent();
124     TypeFactory typeFactory = null;
125     GenericFactory genericFactory = null;
126     try
127     {
128       genericFactory = Fractal.getGenericFactory(bootstrap);
129       typeFactory = Fractal.getTypeFactory(bootstrap);
130     }
131     catch (NoSuchInterfaceException e1)
132     {
133       Error.bug(null, e1);
134     }
135
136     // create interface type
137
// The Scheduler server interface
138
InterfaceType schedulerItfType = typeFactory.createFcItfType(
139         Scheduler.ITF_NAME, Scheduler.class.getName(), false, false, false);
140
141     // The task client collection interface
142
InterfaceType taskItfType = typeFactory.createFcItfType(Task.ITF_NAME,
143         Task.class.getName(), true, false, true);
144
145     // create component type
146
ComponentType taskType = typeFactory.createFcType(new InterfaceType[]{
147         schedulerItfType, taskItfType});
148
149     // instantiate components
150
Component component = genericFactory.newFcInstance(taskType,
151         "taskPrimitive", scheduler);
152
153     if (taskName != null)
154     {
155       try
156       {
157         Fractal.getNameController(component).setFcName(taskName);
158       }
159       catch (NoSuchInterfaceException e)
160       {
161         // ignore.
162
}
163     }
164
165     return component;
166
167   }
168
169   /**
170    * Creates a thread pool task.
171    *
172    * @param taskName the name of the thread pool task to create. May be
173    * <code>null</code>.
174    * @return a component interface of a thread pool task.
175    * @throws InstantiationException if an error occurs while creating the task.
176    */

177   public static Component createThreadPoolTask(String JavaDoc taskName)
178       throws InstantiationException JavaDoc
179   {
180     Component bootstrap = org.objectweb.fractal.api.Fractal
181         .getBootstrapComponent();
182     TypeFactory typeFactory = null;
183     GenericFactory genericFactory = null;
184     try
185     {
186       genericFactory = Fractal.getGenericFactory(bootstrap);
187       typeFactory = Fractal.getTypeFactory(bootstrap);
188     }
189     catch (NoSuchInterfaceException e1)
190     {
191       Error.bug(null, e1);
192     }
193
194     // create interface type
195
// The task server interface type
196
InterfaceType taskItfType = typeFactory.createFcItfType(Task.ITF_NAME,
197         Task.class.getName(), false, false, false);
198
199     // The scheduler client interface type
200
InterfaceType schedulerItfType = typeFactory.createFcItfType(
201         Scheduler.ITF_NAME, Scheduler.class.getName(), true, true, false);
202
203     // create component type
204
ComponentType taskType = typeFactory.createFcType(new InterfaceType[]{
205         taskItfType, schedulerItfType});
206
207     // instantiate components
208
Component component = genericFactory.newFcInstance(taskType,
209         "threadPoolPrimitive", new ThreadPoolTask());
210
211     if (taskName != null)
212     {
213       try
214       {
215         Fractal.getNameController(component).setFcName(taskName);
216       }
217       catch (NoSuchInterfaceException e)
218       {
219         // ignore.
220
}
221     }
222
223     return component;
224   }
225 }
Popular Tags