KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > control > lifecycle > SimpleLifeCycleControllerMixin


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.lifecycle;
25
26 import org.objectweb.fractal.api.Component;
27 import org.objectweb.fractal.api.NoSuchInterfaceException;
28 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
29 import org.objectweb.fractal.api.factory.InstantiationException;
30 import org.objectweb.fractal.julia.InitializationContext;
31 import org.objectweb.fractal.julia.control.lifecycle.ChainedIllegalLifeCycleException;
32 import org.objectweb.fractal.julia.control.lifecycle.LifeCycleCoordinator;
33 import org.objectweb.fractal.julia.factory.ChainedInstantiationException;
34 import org.objectweb.util.monolog.api.BasicLevel;
35 import org.objectweb.util.monolog.api.Logger;
36
37 /**
38  * Basic Dream life cycle controller. Component with this simple lifecycle
39  * implementation can't be stopped alone (ie :{@link #stopFc() }throws a
40  * {@link IllegalLifeCycleException }is the component is not already in the
41  * stop state.
42  * <p>
43  * <p>
44  * <b>Requirements </b>
45  * <ul>
46  * <li>This mixin must be used with the
47  * {@link org.objectweb.fractal.julia.UseComponentMixin }mixin.</li>
48  * <li>This mixin must be used with the
49  * {@link org.objectweb.fractal.julia.control.lifecycle.BasicLifeCycleCoordinatorMixin}
50  * lifecycle coordinator implementation.</li>
51  * </ul>
52  */

53 public abstract class SimpleLifeCycleControllerMixin
54     implements
55       LifeCycleCoordinator
56 {
57
58   /**
59    * The lifecycle state of the component. Zero means stopped, one means
60    * stopping and two means started.
61    */

62   public int weaveableFcState;
63
64   // ---------------------------------------------------------------------------
65
// Fields and methods added and overriden by the mixin class
66
// ---------------------------------------------------------------------------
67

68   /**
69    * @see org.objectweb.fractal.julia.Controller#initFcController(InitializationContext)
70    */

71   public void initFcController(final InitializationContext ic)
72       throws InstantiationException JavaDoc
73   {
74     try
75     {
76       if (!(ic.getInterface("lifecycle-controller") instanceof LifeCycleCoordinator))
77       {
78         throw new Exception JavaDoc();
79       }
80     }
81     catch (Exception JavaDoc e)
82     {
83       try
84       {
85         ic.getInterface("/lifecycle-coordinator");
86       }
87       catch (Exception JavaDoc f)
88       {
89         throw new ChainedInstantiationException(f, null,
90             "The component must provide a LifeCycleCoordinator interface");
91       }
92     }
93
94     _super_initFcController(ic);
95   }
96
97   // ---------------------------------------------------------------------------
98
// Implementation of the LifeCycleController interface
99
// ---------------------------------------------------------------------------
100

101   /**
102    * @see org.objectweb.fractal.api.control.LifeCycleController#getFcState()
103    */

104   public String JavaDoc getFcState()
105   {
106     return weaveableFcState == 0 ? STOPPED : STARTED;
107   }
108
109   /**
110    * @see org.objectweb.fractal.api.control.LifeCycleController#startFc()
111    */

112   public void startFc() throws IllegalLifeCycleException
113   {
114     if (weaveableFcState != 2)
115     {
116       _this_weaveableLCCLogger.log(BasicLevel.DEBUG, "Start component");
117       _this_setFcState(true);
118     }
119   }
120
121   /**
122    * Throws a {@link IllegalLifeCycleException }if the component is in the
123    * {@link org.objectweb.fractal.api.control.LifeCycleController#STARTED}
124    * state.
125    *
126    * @see org.objectweb.fractal.api.control.LifeCycleController#stopFc()
127    */

128   public void stopFc() throws IllegalLifeCycleException
129   {
130     if (weaveableFcState == 2)
131     {
132       throw new ChainedIllegalLifeCycleException(null, _this_weaveableC,
133           "This component can't be stopped alone");
134     }
135   }
136
137   // ---------------------------------------------------------------------------
138
// Implementation of the LifeCycleCoordinator interface
139
// ---------------------------------------------------------------------------
140

141   /**
142    * @see LifeCycleCoordinator#setFcStarted()
143    */

144   public boolean setFcStarted() throws IllegalLifeCycleException
145   {
146     synchronized (this)
147     {
148       if (weaveableFcState == 2)
149       {
150         return false;
151       }
152       _this_weaveableLCCLogger.log(BasicLevel.DEBUG, "Component started");
153       weaveableFcState = 2;
154       return true;
155     }
156   }
157
158   /**
159    * Calls immediatly {@link LifeCycleCoordinator#fcInactivated }.
160    *
161    * @see LifeCycleCoordinator#setFcStopping(LifeCycleCoordinator)
162    */

163   public void setFcStopping(LifeCycleCoordinator coordinator)
164       throws IllegalLifeCycleException
165   {
166     synchronized (this)
167     {
168       _this_weaveableLCCLogger.log(BasicLevel.DEBUG, "Stopping component");
169       weaveableFcState = 1;
170       // immediatly inactive.
171
isInactivated(coordinator);
172     }
173   }
174
175   /**
176    * @see LifeCycleCoordinator#setFcStopped()
177    */

178   public boolean setFcStopped() throws IllegalLifeCycleException
179   {
180     synchronized (this)
181     {
182       if (weaveableFcState == 0)
183       {
184         return false;
185       }
186       _this_weaveableLCCLogger.log(BasicLevel.DEBUG, "Component stopped");
187       weaveableFcState = 0;
188       return true;
189     }
190   }
191
192   /**
193    * Returns <code>true</code> if the component is inactive. This
194    * implementation always returns <code>true</code>. Other mixins can
195    * ovverride this method.
196    *
197    * @return <code>true</code> if the component is inactive.
198    */

199   public boolean checkInactivity()
200   {
201     return true;
202   }
203
204   /**
205    * If {@link #checkInactivity }returns <code>true</code>, calls
206    * {@link LifeCycleCoordinator#fcInactivated }with the given
207    * <code>coordinator</code> and returns <code>true</code>.
208    *
209    * @param coordinator the coordinator passed to
210    * {@link LifeCycleCoordinator#fcInactivated }
211    * @return the value of {@link #checkInactivity}
212    */

213   public boolean isInactivated(LifeCycleCoordinator coordinator)
214   {
215     if (checkInactivity())
216     {
217       _this_weaveableLCCLogger.log(BasicLevel.DEBUG, "Component inactivated");
218       coordinator.fcInactivated(getFcCoordinator());
219       return true;
220     }
221     return false;
222   }
223
224   // -------------------------------------------------------------------------
225
// Utility methods
226
// -------------------------------------------------------------------------
227

228   /**
229    * Returns the {@link LifeCycleCoordinator }interface of this component.
230    *
231    * @return the {@link LifeCycleCoordinator }interface of the component to
232    * which this controller object belongs.
233    */

234   public LifeCycleCoordinator getFcCoordinator()
235   {
236     try
237     {
238       return (LifeCycleCoordinator) _this_weaveableC
239           .getFcInterface("lifecycle-controller");
240     }
241     catch (Exception JavaDoc e)
242     {
243       try
244       {
245         return (LifeCycleCoordinator) _this_weaveableC
246             .getFcInterface("/lifecycle-coordinator");
247       }
248       catch (NoSuchInterfaceException f)
249       {
250         throw new Error JavaDoc("Internal error"); // should not happen
251
}
252     }
253   }
254
255   // -------------------------------------------------------------------------
256
// Fields and methods required by the mixin class in the base class
257
// -------------------------------------------------------------------------
258

259   /**
260    * The <tt>weaveableC</tt> field required by this mixin. This field is
261    * supposed to reference the {@link Component}interface of the component to
262    * which this controller object belongs.
263    */

264   public Component _this_weaveableC;
265
266   /**
267    * The <tt>weaveableLCCLogger</tt> field required by this mixin. This field
268    * is supposed to reference the {@link Logger }of this controller.
269    */

270   public Logger _this_weaveableLCCLogger;
271
272   /**
273    * The <tt>setFcState</tt> method required by this mixin. This method is
274    * supposed to work as this
275    * {@link org.objectweb.fractal.julia.control.lifecycle.BasicLifeCycleCoordinatorMixin#setFcState
276    * setFcState} method.
277    *
278    * @param started <tt>true</tt> to set the lifecycle state of the components
279    * to {@link #STARTED STARTED}, or <tt>false</tt> to set this
280    * state to {@link #STOPPED STOPPED}.
281    * @throws IllegalLifeCycleException if a problem occurs.
282    */

283   public abstract void _this_setFcState(boolean started)
284       throws IllegalLifeCycleException;
285
286   /**
287    * The
288    * {@link org.objectweb.fractal.julia.Controller#initFcController initFcController}
289    * method overriden by this mixin.
290    *
291    * @param ic information about the component to which this controller object
292    * belongs.
293    * @throws InstantiationException if the initialization fails.
294    */

295   public abstract void _super_initFcController(InitializationContext ic)
296       throws InstantiationException JavaDoc;
297 }
Popular Tags