KickJava   Java API By Example, From Geeks To Geeks.

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


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.Interface;
28 import org.objectweb.fractal.api.NoSuchInterfaceException;
29 import org.objectweb.fractal.api.control.ContentController;
30 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
31 import org.objectweb.fractal.api.type.InterfaceType;
32
33 import org.objectweb.fractal.julia.control.binding.Util;
34
35 import java.util.HashSet JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.ArrayList JavaDoc;
39
40 /**
41  * Provides an optimized implementation of the {@link
42  * org.objectweb.fractal.api.control.LifeCycleController} interface. This
43  * implementation does not need interceptors. This life cycle controller
44  * works by stopping all the direct and indirect primitive sub components that
45  * have a {@link LifeCycleCoordinator} interface, and the primitive client
46  * components of these components (these clients <i>must</i> also have a {@link
47  * LifeCycleCoordinator} interface). These components are stopped
48  * simultaneously, by using the coordinator interface provided by this
49  * component.
50  * <br>
51  * <br>
52  * <b>Requirements</b>
53  * <ul>
54  * <li>TODO.</li>
55  * </ul>
56  */

57
58 public abstract class OptimizedLifeCycleControllerMixin
59   implements LifeCycleCoordinator
60 {
61
62   // -------------------------------------------------------------------------
63
// Private constructor
64
// -------------------------------------------------------------------------
65

66   private OptimizedLifeCycleControllerMixin () {
67   }
68
69   // -------------------------------------------------------------------------
70
// Fields and methods added and overriden by the mixin class
71
// -------------------------------------------------------------------------
72

73   /**
74    * Indicates if this component is started or not.
75    */

76
77   public boolean fcStarted;
78
79   // -------------------------------------------------------------------------
80
// Implementation of the LifeCycleController interface
81
// -------------------------------------------------------------------------
82

83   public String JavaDoc getFcState () {
84     return fcStarted ? STARTED : STOPPED;
85   }
86
87   public void startFc () throws IllegalLifeCycleException {
88     Component id;
89     try {
90       id = (Component)_this_weaveableC.getFcInterface("component");
91     } catch (NoSuchInterfaceException e) {
92       throw new ChainedIllegalLifeCycleException(
93         e, _this_weaveableC, "Cannot start component");
94     }
95     LifeCycleCoordinator[] clccs = getFcLifeCycleControllers(id);
96     for (int i = 0; i < clccs.length; ++i) {
97       clccs[i].setFcStarted();
98     }
99     _this_setFcState(true);
100   }
101
102   public void stopFc () throws IllegalLifeCycleException {
103     Component id;
104     try {
105       id = (Component)_this_weaveableC.getFcInterface("component");
106     } catch (NoSuchInterfaceException e) {
107       throw new ChainedIllegalLifeCycleException(
108         e, _this_weaveableC, "Cannot stop component");
109     }
110     LifeCycleCoordinator[] clccs = getFcLifeCycleControllers(id);
111     _this_stopFc(clccs);
112     _this_setFcState(false);
113   }
114
115   // -------------------------------------------------------------------------
116
// Implementation of the CoordinatorLifeCycleController interface
117
// -------------------------------------------------------------------------
118

119   public boolean setFcStarted () {
120     if (!fcStarted) {
121       fcStarted = true;
122       return true;
123     }
124     return false;
125   }
126
127   public void setFcStopping (final LifeCycleCoordinator coordinator)
128     throws IllegalLifeCycleException
129   {
130     // this method should never be called,
131
// as this component is stopped by stopping its sub components
132
throw new Error JavaDoc("Internal error");
133   }
134
135   public boolean setFcStopped () {
136     if (fcStarted) {
137       fcStarted = false;
138       return true;
139     }
140     return false;
141   }
142
143   // -------------------------------------------------------------------------
144
// Utility methods
145
// -------------------------------------------------------------------------
146

147   /**
148    * Returns the components that must be stopped in order to stop the given
149    * component. These components are the direct or indirect primitive sub
150    * components of this component that provide a {@link LifeCycleCoordinator}
151    * interface, as well as the primitive client components of these components.
152    *
153    * @param id a composite component.
154    * @return the components that must be stopped in order to stop the given
155    * component.
156    * @throws IllegalLifeCycleException if a problem occurs.
157    */

158
159   public LifeCycleCoordinator[] getFcLifeCycleControllers (
160     final Component id) throws IllegalLifeCycleException
161   {
162     List JavaDoc clccList = getFcInternalLifeCycleControllers();
163     Object JavaDoc[] sItfs = id.getFcInterfaces();
164     Set JavaDoc visited = new HashSet JavaDoc();
165     for (int i = 0; i < sItfs.length; ++i) {
166       Interface sItf = (Interface)sItfs[i];
167       if (!((InterfaceType)sItf.getFcItfType()).isFcClientItf()) {
168         getSExtLifeCycleControllers(sItf, clccList, visited);
169       }
170     }
171     LifeCycleCoordinator[] clccs;
172     clccs = new LifeCycleCoordinator[clccList.size()];
173     return (LifeCycleCoordinator[])clccList.toArray(clccs);
174   }
175
176   /**
177    * Finds the primitive client components that are bound to the given server
178    * interface.
179    *
180    * @param serverItf a server interface.
181    * @param clccList where to put the {@link LifeCycleCoordinator}
182    * interfaces of the primitive client components that are found.
183    * @param visited the already visited interfaces.
184    * @throws IllegalLifeCycleException if a problem occurs.
185    */

186
187   private void getSExtLifeCycleControllers (
188     final Interface serverItf,
189     final List JavaDoc clccList,
190     final Set JavaDoc visited) throws IllegalLifeCycleException
191   {
192     Object JavaDoc[] comps;
193     try {
194       comps = Util.getFcPotentialClientsOf(serverItf).toArray();
195     } catch (Exception JavaDoc e) {
196       throw new ChainedIllegalLifeCycleException(
197         e,
198         serverItf.getFcItfOwner(),
199         "Cannot get the LifeCycleCoordinator interfaces");
200     }
201     for (int i = 0; i < comps.length; ++i) {
202       Component comp = (Component)comps[i];
203       Interface[] clientItfs;
204       try {
205         List JavaDoc l = Util.getFcClientItfsBoundTo(comp, serverItf);
206         clientItfs = (Interface[])l.toArray(new Interface[l.size()]);
207       } catch (Exception JavaDoc e) {
208         throw new ChainedIllegalLifeCycleException(
209           e,
210           serverItf.getFcItfOwner(),
211           "Cannot get the LifeCycleCoordinator interfaces");
212       }
213       for (int j = 0; j < clientItfs.length; ++j) {
214         getCExtLifeCycleControllers(clientItfs[j], clccList, visited);
215       }
216     }
217   }
218
219   /**
220    * Finds the primitive client components that are bound to the given client
221    * interface.
222    *
223    * @param clientItf a client interface.
224    * @param clccList where to put the {@link LifeCycleCoordinator} interfaces of
225    * the primitive client components that are found.
226    * @param visited the already visited interfaces.
227    * @throws IllegalLifeCycleException if a problem occurs.
228    */

229
230   private void getCExtLifeCycleControllers (
231     final Interface clientItf,
232     final List JavaDoc clccList,
233     final Set JavaDoc visited) throws IllegalLifeCycleException
234   {
235     Component component = clientItf.getFcItfOwner();
236     ContentController cc = null;
237     try {
238       cc = (ContentController)component.getFcInterface("content-controller");
239     } catch (NoSuchInterfaceException e) {
240     }
241     if (cc != null) {
242       Interface itf;
243       String JavaDoc name = clientItf.getFcItfName();
244       try {
245         if (!clientItf.isFcInternalItf()) {
246           itf = (Interface)cc.getFcInternalInterface(name);
247         } else {
248           itf = (Interface)component.getFcInterface(name);
249         }
250       } catch (NoSuchInterfaceException e) {
251         throw new ChainedIllegalLifeCycleException(
252           e,
253           component,
254           "Cannot find the LifeCycleCoordinator interfaces");
255       }
256       if (!visited.contains(itf)) {
257         visited.add(itf);
258         getSExtLifeCycleControllers(itf, clccList, visited);
259       }
260     } else if (!visited.contains(clientItf)) {
261       visited.add(clientItf);
262       Component c = clientItf.getFcItfOwner();
263       LifeCycleCoordinator lcc;
264       try {
265         lcc = (LifeCycleCoordinator)c.getFcInterface("lifecycle-controller");
266       } catch (Exception JavaDoc e) {
267         try {
268           lcc = (LifeCycleCoordinator)c.getFcInterface("/lifecycle-coordinator");
269         } catch (NoSuchInterfaceException f) {
270           throw new ChainedIllegalLifeCycleException(
271             f, component, "Primitive client without a LifeCycleCoordinator");
272         }
273       }
274       if (!clccList.contains(lcc)) {
275         clccList.add(lcc);
276       }
277     }
278   }
279
280   public List JavaDoc getFcInternalLifeCycleControllers ()
281     throws IllegalLifeCycleException
282   {
283     // finds all the direct and indirect sub components of this component
284
Component thisComponent;
285     try {
286       thisComponent = (Component)_this_weaveableC.getFcInterface("component");
287     } catch (NoSuchInterfaceException e) {
288       throw new ChainedIllegalLifeCycleException(
289         e,
290         _this_weaveableC,
291         "The OptimizedLifeCycleControllerMixin requires " +
292         "components to provide the Component interface");
293     }
294     List JavaDoc allSubComponents = org.objectweb.fractal.julia.control.content.Util.
295       getAllSubComponents(thisComponent);
296
297     List JavaDoc result = new ArrayList JavaDoc();
298     for (int i = 0; i < allSubComponents.size(); ++i) {
299       Component c = (Component)allSubComponents.get(i);
300       try {
301         c.getFcInterface("content-controller");
302       } catch (NoSuchInterfaceException e) {
303         try {
304           // do not remove the cast!
305
result.add(
306             (LifeCycleCoordinator)c.getFcInterface("lifecycle-controller"));
307         } catch (Exception JavaDoc f) {
308           try {
309             result.add(c.getFcInterface("/lifecycle-coordinator"));
310           } catch (NoSuchInterfaceException ignored) {
311           }
312         }
313       }
314     }
315     return result;
316   }
317
318   // -------------------------------------------------------------------------
319
// Fields and methods required by the mixin class in the base class
320
// -------------------------------------------------------------------------
321

322   /**
323    * The <tt>weaveableC</tt> field required by this mixin. This field is
324    * supposed to reference the {@link Component} interface of the component to
325    * which this controller object belongs.
326    */

327
328   public Component _this_weaveableC;
329
330   /**
331    * The <tt>setFcState</tt> method required by this mixin. This method is
332    * supposed to work as this {@link BasicLifeCycleCoordinatorMixin#setFcState
333    * setFcState} method.
334    *
335    * @param started <tt>true</tt> to set the lifecycle state of the components
336    * to {@link #STARTED STARTED}, or <tt>false</tt> to set this state to
337    * {@link #STOPPED STOPPED}.
338    * @throws IllegalLifeCycleException if a problem occurs.
339    */

340
341   public abstract void _this_setFcState (boolean started)
342     throws IllegalLifeCycleException;
343
344   /**
345    * The <tt>stopFc</tt> method required by this mixin. This method is
346    * supposed to work as this {@link BasicLifeCycleCoordinatorMixin#stopFc
347    * stopFc} method.
348    *
349    * @param components the {@link LifeCycleCoordinator} interface of the
350    * components to be stopped.
351    * @throws IllegalLifeCycleException if a problem occurs.
352    */

353
354   public abstract void _this_stopFc (LifeCycleCoordinator[] components)
355     throws IllegalLifeCycleException;
356 }
357
Popular Tags