KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > control > lifecycle > BasicLifeCycleCoordinatorMixin


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
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: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.julia.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
30 import org.objectweb.fractal.julia.control.content.Util;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34
35 /**
36  * Provides an abstract implementation of the {@link LifeCycleCoordinator}
37  * interface.
38  * <br>
39  * <br>
40  * <b>Requirements</b>
41  * <ul>
42  * <li>the component to which this controller object belongs must provide the
43  * {@link Component} interface.</li>
44  * </ul>
45  */

46
47 public abstract class BasicLifeCycleCoordinatorMixin
48   implements LifeCycleCoordinator
49 {
50
51   // -------------------------------------------------------------------------
52
// Private constructor
53
// -------------------------------------------------------------------------
54

55   private BasicLifeCycleCoordinatorMixin () {
56   }
57
58   // -------------------------------------------------------------------------
59
// Fields and methods added and overriden by the mixin class
60
// -------------------------------------------------------------------------
61

62   /**
63    * The components that are currently active.
64    */

65
66   public List JavaDoc fcActive;
67
68   /**
69    * Sets the lifecycle state of this component and of all its direct and
70    * indirect sub components that have a {@link LifeCycleCoordinator} interface.
71    *
72    * @param started <tt>true</tt> to set the lifecycle state of the components
73    * to {@link #STARTED STARTED}, or <tt>false</tt> to set this state to
74    * {@link #STOPPED STOPPED}.
75    * @throws IllegalLifeCycleException if a problem occurs.
76    */

77
78   public void setFcState (final boolean started)
79     throws IllegalLifeCycleException
80   {
81     // finds all the direct and indirect sub components of this component
82
Component thisComponent;
83     try {
84       thisComponent = (Component)_this_weaveableC.getFcInterface("component");
85     } catch (NoSuchInterfaceException e) {
86       throw new ChainedIllegalLifeCycleException(
87         e, _this_weaveableC, "Cannot set the lifecycle state");
88     }
89     List JavaDoc allSubComponents = Util.getAllSubComponents(thisComponent);
90
91     // sets the state of these components
92
for (int i = 0; i < allSubComponents.size(); ++i) {
93       Component c = (Component)allSubComponents.get(i);
94       LifeCycleCoordinator lc;
95       try {
96         lc = (LifeCycleCoordinator)c.getFcInterface("lifecycle-controller");
97       } catch (Exception JavaDoc e) {
98         try {
99           lc = (LifeCycleCoordinator)c.getFcInterface("/lifecycle-coordinator");
100         } catch (NoSuchInterfaceException f) {
101           continue;
102         }
103       }
104       if (started) {
105         lc.setFcStarted();
106       } else {
107         lc.setFcStopped();
108       }
109     }
110   }
111
112   public boolean fcActivated (final LifeCycleCoordinator component) {
113     synchronized (fcActive) {
114       // a component can become active iff another component is already active
115
if (fcActive.size() > 0) {
116         if (!fcActive.contains(component)) {
117           fcActive.add(component);
118         }
119         return true;
120       }
121       return false;
122     }
123   }
124
125   public void fcInactivated (final LifeCycleCoordinator component) {
126     synchronized (fcActive) {
127       fcActive.remove(component);
128       // notifies the thread that may be blocked in stopFc
129
fcActive.notifyAll();
130     }
131   }
132
133   /**
134    * Stops the given components simultaneously. This method sets the state of
135    * the components to "<tt>STOPPING</tt>", waits until all the components
136    * are simultaneoulsy inactive (their state is known thanks to the {@link
137    * #fcActivated fcActivated} and {@link #fcInactivated fcInactivated} callback
138    * methods), and then sets the state of the components to {@link #STOPPED
139    * STOPPED}.
140    *
141    * @param components the {@link LifeCycleCoordinator} interface of the
142    * components to be stopped.
143    * @throws IllegalLifeCycleException if a problem occurs.
144    */

145
146   public void stopFc (final LifeCycleCoordinator[] components)
147     throws IllegalLifeCycleException
148   {
149     // initializes the fcActive list
150
fcActive = new ArrayList JavaDoc();
151     for (int i = 0; i < components.length; ++i) {
152       if (components[i].getFcState().equals(STARTED)) {
153         fcActive.add(components[i]);
154       }
155     }
156     // sets the state of the components to STOPPING
157
LifeCycleCoordinator c;
158     try {
159       c = (LifeCycleCoordinator)_this_weaveableC.
160         getFcInterface("lifecycle-controller");
161     } catch (Exception JavaDoc e) {
162       try {
163         c = (LifeCycleCoordinator)_this_weaveableC.
164           getFcInterface("/lifecycle-coordinator");
165       } catch (NoSuchInterfaceException f) {
166         throw new ChainedIllegalLifeCycleException(
167           f, _this_weaveableC, "Cannot stop components");
168       }
169     }
170     for (int i = 0; i < components.length; ++i) {
171       if (components[i].getFcState().equals(STARTED)) {
172         components[i].setFcStopping(c);
173       }
174     }
175     // waits until all the components are simultaneously inactive
176
synchronized (fcActive) {
177       while (fcActive.size() > 0) {
178         try {
179           fcActive.wait();
180         } catch (InterruptedException JavaDoc e) {
181         }
182       }
183     }
184     // sets the state of the components to STOPPED
185
for (int i = 0; i < components.length; ++i) {
186       if (components[i].getFcState().equals(STARTED)) {
187         components[i].setFcStopped();
188       }
189     }
190     fcActive = null;
191   }
192
193   // -------------------------------------------------------------------------
194
// Fields and methods required by the mixin class in the base class
195
// -------------------------------------------------------------------------
196

197   /**
198    * The <tt>weaveableC</tt> field required by this mixin. This field is
199    * supposed to reference the {@link Component} interface of the component to
200    * which this controller object belongs.
201    */

202
203   public Component _this_weaveableC;
204 }
205
Popular Tags