1 24 25 package org.objectweb.dream.control.activity.task; 26 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.objectweb.dream.control.activity.scheduler.ForwarderSchedulerImpl; 31 import org.objectweb.dream.control.activity.scheduler.Scheduler; 32 import org.objectweb.dream.control.activity.task.thread.ThreadPoolController; 33 import org.objectweb.dream.control.activity.task.thread.ThreadTask; 34 import org.objectweb.dream.util.Error; 35 import org.objectweb.fractal.api.Component; 36 import org.objectweb.fractal.api.Interface; 37 import org.objectweb.fractal.api.NoSuchInterfaceException; 38 import org.objectweb.fractal.api.control.ContentController; 39 import org.objectweb.fractal.api.control.IllegalBindingException; 40 import org.objectweb.fractal.api.control.IllegalContentException; 41 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 42 import org.objectweb.fractal.api.factory.InstantiationException; 43 import org.objectweb.fractal.julia.InitializationContext; 44 import org.objectweb.fractal.util.Fractal; 45 import org.objectweb.util.monolog.api.BasicLevel; 46 import org.objectweb.util.monolog.api.Logger; 47 48 71 72 public abstract class ThreadPerTaskTaskManagerControllerMixin 73 implements 74 TaskManagerController, 75 TaskStoppedListener 76 { 77 78 79 protected Map tasks; 80 81 protected Map interruptedTasks; 82 83 87 private ThreadPerTaskTaskManagerControllerMixin() 88 { 89 } 90 91 95 98 public void initFcController(final InitializationContext ic) 99 throws InstantiationException 100 { 101 102 tasks = new HashMap (); 103 interruptedTasks = new HashMap (); 104 105 _super_initFcController(ic); 106 } 107 108 112 115 public synchronized Object registerTask(Task task, Map hints) 116 throws IllegalTaskException 117 { 118 if (tasks.containsKey(task)) 119 { 120 throw new IllegalTaskException(task, "The task is already registred", 121 null); 122 } 123 124 String controllerDesc = (String ) hints.get("taskControllerDesc"); 126 Component componentTask; 127 try 128 { 129 componentTask = Util.createTask(task, (controllerDesc == null) 130 ? "taskPrimitive" 131 : controllerDesc); 132 } 133 catch (InstantiationException e) 134 { 135 throw new IllegalTaskException(null, 136 "Unable to instantiate task component", e); 137 } 138 try 139 { 140 _this_weaveableCC.addFcSubComponent(componentTask); 141 } 142 catch (IllegalContentException e) 143 { 144 throw new IllegalTaskException(task, 145 "Unable to add task component in activity manager", e); 146 } 147 catch (IllegalLifeCycleException e) 148 { 149 throw new IllegalTaskException(task, 150 "Unable to add task component in activity manager", e); 151 } 152 String taskName = null; 153 try 154 { 155 taskName = Fractal.getNameController(componentTask).getFcName(); 156 } 157 catch (NoSuchInterfaceException e1) 158 { 159 } 161 162 Component thread; 164 ThreadPoolController returnObject = null; 165 String s = (String ) hints.get("thread"); 166 if (s != null && "pool".equals(s)) 167 { 168 _this_weaveableTMCLogger.log(BasicLevel.DEBUG, "Create thread pool task"); 169 170 try 171 { 172 thread = Util.createThreadPoolTask((taskName == null) 173 ? "threadPoolTask" 174 : taskName + "-threadPoolTask"); 175 } 176 catch (InstantiationException e) 177 { 178 throw new IllegalTaskException(task, 179 "Unable to create thread pool task component in activity manager", 180 e); 181 } 182 183 try 184 { 185 returnObject = (ThreadPoolController) thread 186 .getFcInterface("thread-pool-controller"); 187 } 188 catch (NoSuchInterfaceException e) 189 { 190 Error.bug(_this_weaveableTMCLogger, e); 191 } 192 Integer capacity = (Integer ) hints.get("threadPool.capacity"); 194 if (capacity != null) 195 { 196 returnObject.setCapacity(capacity.intValue()); 197 } 198 } 199 else 200 { 201 _this_weaveableTMCLogger 202 .log(BasicLevel.DEBUG, "Create basic thread task"); 203 try 204 { 205 thread = Util.createTask(new ThreadTask(), "threadPrimitive"); 206 } 207 catch (InstantiationException e) 208 { 209 throw new IllegalTaskException(task, 210 "Unable to create thread task component in activity manager", e); 211 } 212 returnObject = null; 213 } 214 215 if (taskName != null) 216 { 217 try 218 { 219 Fractal.getNameController(thread).setFcName(taskName + "-thread"); 220 } 221 catch (NoSuchInterfaceException e2) 222 { 223 } 225 } 226 227 try 229 { 230 _this_weaveableCC.addFcSubComponent(thread); 231 } 232 catch (IllegalContentException e) 233 { 234 throw new IllegalTaskException(task, 235 "Unable to add thread task component in activity manager", e); 236 } 237 catch (IllegalLifeCycleException e) 238 { 239 throw new IllegalTaskException(task, 240 "Unable to add thread task component in activity manager", e); 241 } 242 243 Component forwarderScheduler; 245 try 246 { 247 forwarderScheduler = Util.createScheduler(new ForwarderSchedulerImpl(), 248 (taskName == null) ? "scheduler" : taskName + "-scheduler"); 249 } 250 catch (InstantiationException e) 251 { 252 throw new IllegalTaskException(task, 253 "Unable to create scheduler component in activity manager", e); 254 } 255 if (taskName != null) 256 { 257 try 258 { 259 Fractal.getNameController(forwarderScheduler).setFcName( 260 taskName + "-scheduler"); 261 } 262 catch (NoSuchInterfaceException e2) 263 { 264 } 266 } 267 268 try 269 { 270 _this_weaveableCC.addFcSubComponent(forwarderScheduler); 271 } 272 catch (IllegalContentException e) 273 { 274 throw new IllegalTaskException(task, 275 "Unable to add scheduler component in activity manager", e); 276 } 277 catch (IllegalLifeCycleException e) 278 { 279 throw new IllegalTaskException(task, 280 "Unable to add scheduler component in activity manager", e); 281 } 282 283 try 285 { 286 Fractal.getBindingController(thread).bindFc(Scheduler.ITF_NAME, 287 forwarderScheduler.getFcInterface(Scheduler.ITF_NAME)); 288 Fractal.getBindingController(forwarderScheduler).bindFc(Task.ITF_NAME, 290 componentTask.getFcInterface(Task.ITF_NAME)); 291 } 292 catch (NoSuchInterfaceException e) 293 { 294 Error.bug(_this_weaveableTMCLogger, e); 295 } 296 catch (IllegalBindingException e) 297 { 298 Error.bug(_this_weaveableTMCLogger, e); 299 } 300 catch (IllegalLifeCycleException e) 301 { 302 Error.bug(_this_weaveableTMCLogger, e); 303 } 304 try 305 { 306 Fractal.getLifeCycleController(thread).startFc(); 308 } 309 catch (IllegalLifeCycleException e) 310 { 311 throw new IllegalTaskException(task, 312 "unable to start thread task component", e); 313 } 314 catch (NoSuchInterfaceException e) 315 { 316 Error.bug(_this_weaveableTMCLogger, e); 317 } 318 319 tasks.put(task, new Component[]{componentTask, forwarderScheduler, thread}); 320 321 return returnObject; 322 323 } 324 325 328 public synchronized void unregisterTask(Task task) 329 throws IllegalTaskException 330 { 331 if (!tasks.containsKey(task)) 332 { 333 throw new IllegalTaskException(task, "The task is not registered", null); 334 } 335 Component[] components = (Component[]) tasks.get(task); 338 Component componentTask = components[0]; 339 Component forwarderScheduler = components[1]; 340 Component thread = components[2]; 341 removeTask(task, componentTask, forwarderScheduler, thread); 342 tasks.remove(task); 343 } 344 345 348 public synchronized void interruptTask(Task task, TaskStoppedListener listener) 349 throws IllegalTaskException 350 { 351 if (!tasks.containsKey(task)) 352 { 353 throw new IllegalTaskException(task, "The task is not registered", null); 354 } 355 Component[] components = (Component[]) tasks.get(task); 358 Component thread = components[2]; 359 Object [] taskStatus = new Object []{task, listener, components[0], 360 components[1]}; 361 interruptedTasks.put(thread, taskStatus); 362 try 363 { 364 ((TaskLifeCycleController) Fractal.getLifeCycleController(thread)) 365 .asyncStop(this); 366 } 367 catch (NoSuchInterfaceException e) 368 { 369 Error.bug(_this_weaveableTMCLogger, e); 371 } 372 } 373 374 377 public Task[] getTasks() 378 { 379 if (tasks == null) 380 { 381 return new Task[0]; 382 } 383 return (Task[]) tasks.keySet().toArray(new Task[tasks.size()]); 384 } 385 386 390 393 public void taskStopped(Task thread) 394 { 395 Task task; 396 TaskStoppedListener listener; 397 synchronized (this) 398 { 399 Component threadComponent = ((Interface) thread).getFcItfOwner(); 400 Object [] taskStatus = (Object []) interruptedTasks.get(threadComponent); 401 task = (Task) taskStatus[0]; 402 listener = (TaskStoppedListener) taskStatus[1]; 403 Component componentTask = (Component) taskStatus[2]; 404 Component forwarderScheduler = (Component) taskStatus[3]; 405 try 406 { 407 removeTask(task, componentTask, forwarderScheduler, threadComponent); 408 } 409 catch (IllegalTaskException e) 410 { 411 Error.bug(_this_weaveableTMCLogger, e); 412 } 413 interruptedTasks.remove(threadComponent); 414 } 415 listener.taskStopped(task); 416 } 417 418 422 431 public void removeTask(Task task, Component componentTask, 432 Component forwarderScheduler, Component thread) 433 throws IllegalTaskException 434 { 435 try 436 { 437 Fractal.getLifeCycleController(thread).stopFc(); 439 } 440 catch (IllegalLifeCycleException e) 441 { 442 throw new IllegalTaskException(task, 443 "unable to stop thread task component", e); 444 } 445 catch (NoSuchInterfaceException e) 446 { 447 Error.bug(_this_weaveableTMCLogger, e); 448 } 449 try 451 { 452 Fractal.getBindingController(thread).unbindFc(Scheduler.ITF_NAME); 453 _this_weaveableCC.removeFcSubComponent(thread); 454 Fractal.getBindingController(forwarderScheduler).unbindFc(Task.ITF_NAME); 455 _this_weaveableCC.removeFcSubComponent(forwarderScheduler); 456 _this_weaveableCC.removeFcSubComponent(componentTask); 458 } 459 catch (NoSuchInterfaceException e) 460 { 461 Error.bug(_this_weaveableTMCLogger, e); 462 } 463 catch (IllegalBindingException e) 464 { 465 Error.bug(_this_weaveableTMCLogger, e); 466 } 467 catch (IllegalLifeCycleException e) 468 { 469 Error.bug(_this_weaveableTMCLogger, e); 470 } 471 catch (IllegalContentException e) 472 { 473 Error.bug(_this_weaveableTMCLogger, e); 474 } 475 } 476 477 481 485 public Logger _this_weaveableTMCLogger; 486 487 491 public ContentController _this_weaveableCC; 492 493 499 public abstract void _super_initFcController(InitializationContext ic) 500 throws InstantiationException ; 501 } | Popular Tags |