KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > control > activity > task > thread > AbstractThreadTask


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.thread;
26
27 import org.objectweb.dream.AbstractComponent;
28 import org.objectweb.dream.control.activity.scheduler.Scheduler;
29 import org.objectweb.dream.control.activity.scheduler.StoppedSchedulerException;
30 import org.objectweb.dream.control.activity.task.Task;
31 import org.objectweb.fractal.api.NoSuchInterfaceException;
32 import org.objectweb.fractal.api.control.IllegalBindingException;
33 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 /**
37  * Abstract class representing a thread task.
38  */

39 public abstract class AbstractThreadTask extends AbstractComponent
40     implements
41       Task
42 {
43
44   /** The scheduler to which the thread is bound. */
45   protected Scheduler scheduler = null;
46
47   /**
48    * Called by {@link #execute(Object) }method.
49    *
50    * @return <code>false</code> if the {@link #execute(Object) }method must
51    * return.
52    */

53   protected abstract boolean isExecuting();
54
55   /**
56    * Called by {@link #execute(Object)}method. Sets its execution state.
57    *
58    * @param b <code>false</code> if the {@link #execute(Object) }method must
59    * return.
60    */

61   protected abstract void setExecuting(boolean b);
62
63   // ---------------------------------------------------------------------------
64
// Implementation of the Task interface
65
// ---------------------------------------------------------------------------
66

67   /**
68    * @see Task#execute(Object)
69    */

70   public Object JavaDoc execute(Object JavaDoc hints) throws InterruptedException JavaDoc
71   {
72     try
73     {
74       while (isExecuting())
75       {
76         logger.log(BasicLevel.DEBUG, "Call to schedule");
77         if (((Integer JavaDoc) scheduler.schedule(null)).intValue() == -1)
78         {
79           setExecuting(false);
80         }
81       }
82     }
83     catch (StoppedSchedulerException e1)
84     {
85       logger.log(BasicLevel.DEBUG, "Scheduler stopped");
86     }
87     catch (InterruptedException JavaDoc e)
88     {
89       logger.log(BasicLevel.DEBUG, "Interrupted");
90     }
91     catch (Throwable JavaDoc e)
92     {
93       logger.log(BasicLevel.ERROR, "Exception catched", e);
94     }
95     logger.log(BasicLevel.DEBUG, "End of the execute method");
96     return null;
97   }
98
99   // ---------------------------------------------------------------------------
100
// Implementation of the BindingController interface
101
// ---------------------------------------------------------------------------
102

103   /**
104    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
105    * Object)
106    */

107   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
108       throws NoSuchInterfaceException, IllegalBindingException,
109       IllegalLifeCycleException
110   {
111     super.bindFc(clientItfName, serverItf);
112     if (clientItfName.equals(Scheduler.ITF_NAME))
113     {
114       scheduler = (Scheduler) serverItf;
115     }
116   }
117
118   /**
119    * @see org.objectweb.fractal.api.control.BindingController#unbindFc(String)
120    */

121   public synchronized void unbindFc(String JavaDoc clientItfName)
122       throws NoSuchInterfaceException, IllegalBindingException,
123       IllegalLifeCycleException
124   {
125     super.unbindFc(clientItfName);
126     if (clientItfName.equals(Scheduler.ITF_NAME))
127     {
128       scheduler = null;
129     }
130   }
131
132   /**
133    * @see org.objectweb.fractal.api.control.BindingController#listFc()
134    */

135   public synchronized String JavaDoc[] listFc()
136   {
137     return new String JavaDoc[]{Scheduler.ITF_NAME};
138   }
139
140 }
Popular Tags