KickJava   Java API By Example, From Geeks To Geeks.

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


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.BindingController;
29 import org.objectweb.fractal.api.control.IllegalBindingException;
30 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
31 import org.objectweb.fractal.api.control.LifeCycleController;
32 import org.objectweb.fractal.api.type.ComponentType;
33 import org.objectweb.fractal.api.type.InterfaceType;
34
35 import org.objectweb.fractal.julia.control.binding.ChainedIllegalBindingException;
36 import org.objectweb.fractal.julia.control.content.Util;
37
38 import java.util.List JavaDoc;
39
40 /**
41  * Provides basic type system related checks to a {@link LifeCycleController}.
42  * <br>
43  * <br>
44  * <b>Requirements</b>
45  * <ul>
46  * <li>the type of each direct and indirect sub component of this component
47  * (included) that has a {@link BindingController} interface must be an instance
48  * of {@link org.objectweb.fractal.api.type.ComponentType}.</li>
49  * </ul>
50  */

51
52 public abstract class TypeLifeCycleMixin implements LifeCycleController {
53
54   // -------------------------------------------------------------------------
55
// Private constructor
56
// -------------------------------------------------------------------------
57

58   private TypeLifeCycleMixin () {
59   }
60
61   // -------------------------------------------------------------------------
62
// Fields and methods added and overriden by the mixin class
63
// -------------------------------------------------------------------------
64

65   /**
66    * Checks the mandatory client interfaces of the component (and of all its sub
67    * components) and then calls the overriden method.
68    *
69    * @throws IllegalLifeCycleException if a problem occurs.
70    */

71
72   public void startFc () throws IllegalLifeCycleException {
73     // finds all the direct and indirect sub components of this component
74
Component thisComponent;
75     try {
76       thisComponent = (Component)_this_weaveableC.getFcInterface("component");
77     } catch (NoSuchInterfaceException e) {
78       throw new ChainedIllegalLifeCycleException(
79         e, _this_weaveableC, "Cannot start the component");
80     }
81     List JavaDoc allSubComponents = Util.getAllSubComponents(thisComponent);
82
83     // checks that the mandatory client interfaces of these components are bound
84
for (int i = 0; i < allSubComponents.size(); ++i) {
85       Component subComponent = (Component)allSubComponents.get(i);
86       try {
87         checkFcMandatoryInterfaces(subComponent);
88       } catch (IllegalBindingException e) {
89         throw new ChainedIllegalLifeCycleException(
90           e, _this_weaveableC, "Cannot start the component");
91       }
92     }
93
94     // calls the overriden method
95
_super_startFc();
96   }
97
98   /**
99    * Checks that all the mandatory client interface of the given component are
100    * bound.
101    *
102    * @param c a component.
103    * @throws IllegalBindingException if a mandatory client interface of the
104    * given component is not bound.
105    */

106
107   public void checkFcMandatoryInterfaces (final Component c)
108     throws IllegalBindingException
109   {
110     BindingController bc;
111     try {
112       bc = (BindingController)c.getFcInterface("binding-controller");
113     } catch (NoSuchInterfaceException e) {
114       return;
115     }
116     ComponentType compType = (ComponentType)c.getFcType();
117     String JavaDoc[] names = bc.listFc();
118     for (int i = 0; i < names.length; ++i) {
119       InterfaceType itfType;
120       try {
121         itfType = compType.getFcInterfaceType(names[i]);
122       } catch (NoSuchInterfaceException e) {
123         continue;
124       }
125       if (itfType.isFcClientItf() && !itfType.isFcOptionalItf()) {
126         Object JavaDoc sItf;
127         try {
128           sItf = bc.lookupFc(names[i]);
129         } catch (NoSuchInterfaceException e) {
130           continue;
131         }
132         if (sItf == null) {
133           throw new ChainedIllegalBindingException(
134             null,
135             c,
136             null,
137             names[i],
138             null,
139             "Mandatory client interface unbound");
140         }
141       }
142     }
143   }
144
145   // -------------------------------------------------------------------------
146
// Fields and methods required by the mixin class in the base class
147
// -------------------------------------------------------------------------
148

149   /**
150    * The <tt>weaveableC</tt> field required by this mixin. This field is
151    * supposed to reference the {@link Component} interface of the component to
152    * which this controller object belongs.
153    */

154
155   public Component _this_weaveableC;
156
157   /**
158    * The {@link LifeCycleController#startFc startFc} method overriden by this
159    * mixin.
160    *
161    * @throws IllegalLifeCycleException if a problem occurs.
162    */

163
164   public abstract void _super_startFc () throws IllegalLifeCycleException;
165 }
166
Popular Tags