KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > AbstractComponent


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): Matthieu Leclercq
22  * Contributor(s): Vivien Quema
23  */

24
25 package org.objectweb.dream;
26
27 import java.util.Hashtable JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.objectweb.dream.control.logger.Loggable;
31 import org.objectweb.dream.control.logger.LoggerControllerRegister;
32 import org.objectweb.dream.util.NullLogger;
33 import org.objectweb.fractal.api.Component;
34 import org.objectweb.fractal.api.NoSuchInterfaceException;
35 import org.objectweb.fractal.api.control.BindingController;
36 import org.objectweb.fractal.api.control.IllegalBindingException;
37 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
38 import org.objectweb.fractal.api.control.LifeCycleController;
39 import org.objectweb.fractal.julia.control.binding.ChainedIllegalBindingException;
40 import org.objectweb.util.monolog.api.BasicLevel;
41 import org.objectweb.util.monolog.api.Logger;
42
43 /**
44  * Base implementation of Dream components. This class is provided for
45  * convenience. It is not mandatory for a component implementation to be a
46  * subclasse of this class. <br>
47  * This class provides implementation of <code>BindingController</code>
48  * methods. Bindings are stored in a {@link java.util.Map}. When a component
49  * has client interfaces, it is recommended to override (at least) the
50  * {@link BindingController#bindFc(String, Object) bindFc }method.
51  */

52 public abstract class AbstractComponent
53     implements
54       LifeCycleController,
55       BindingController,
56       Loggable
57 {
58
59   /** the {@link Component }controller interface reference of this component */
60   public Component weaveableC;
61
62   /** the functional logger of this component */
63   protected Logger logger = NullLogger.NULL_LOGGER;
64
65   /**
66    * the controller logger associated with the life cycle controller of this
67    * component
68    */

69   protected Logger lifeCycleLogger = NullLogger.NULL_LOGGER;
70
71   /**
72    * the controller logger associated with the binding controller of this
73    * component
74    */

75   protected Logger bindingLogger = NullLogger.NULL_LOGGER;
76
77   /** this hashtable contain all bindings of the component. */
78   private Map JavaDoc bindings;
79
80   /**
81    * this attribut give the state of Dream Component. <br>
82    * the fcSstate value is {@link LifeCycleController#STARTED}
83    * {@link LifeCycleController#STOPPED}
84    */

85   protected String JavaDoc fcState;
86
87   /** The {@link Component}description object associated with this component */
88   protected Component componentDesc;
89
90   /** A boolean that indicates whether this is the first start or not. */
91   protected boolean firstStart = true;
92
93   /**
94    * Constructor method create the hashtable of bindings and initialize fcState
95    * to LifeCycleController.STOPPED.
96    */

97   public AbstractComponent()
98   {
99     bindings = new Hashtable JavaDoc();
100     fcState = LifeCycleController.STOPPED;
101   }
102
103   // ---------------------------------------------------------------------------
104
// Methods extending the component lifecycle
105
// ---------------------------------------------------------------------------
106

107   /**
108    * Initializes this component. This method is called during initialization of
109    * the component controllers. <br>
110    * This method can be overriden by sub classes as follows:
111    *
112    * <pre>
113    *
114    * protected void initComponent(Component componentItf)
115    * {
116    * super.initComponent(componentItf);
117    * // initialisation code here ...
118    * }
119    *
120    *
121    * </pre>
122    *
123    * @param componentItf the {@link Component }interface of this component.
124    * @throws InitializationException if an error occurs.
125    */

126   protected void initComponent(Component componentItf)
127       throws InitializationException
128   {
129     try
130     {
131       LoggerControllerRegister lcr = (LoggerControllerRegister) weaveableC
132           .getFcInterface("/logger-controller-register");
133       lcr.register(null, this);
134       lcr.register("life-cycle", this);
135       lcr.register("binding", this);
136       logger.log(BasicLevel.DEBUG, "Component initialized.");
137     }
138     catch (NoSuchInterfaceException e)
139     {
140       // ignore
141
}
142   }
143
144   /**
145    * This method is called the first time the component is started.
146    *
147    * @param componentItf the {@link Component }interface of this component.
148    */

149   protected void beforeFirstStart(Component componentItf)
150       throws IllegalLifeCycleException
151   {
152
153   }
154
155   // ---------------------------------------------------------------------------
156
// Implementation of the Loggable interface
157
// ---------------------------------------------------------------------------
158

159   /**
160    * @see Loggable#setLogger(String, Logger)
161    */

162   public void setLogger(String JavaDoc name, Logger l)
163   {
164     if (name == null)
165     {
166       logger = l;
167     }
168     else if (name.equals("life-cycle"))
169     {
170       lifeCycleLogger = l;
171     }
172     else if (name.equals("binding"))
173     {
174       bindingLogger = l;
175     }
176   }
177
178   // ---------------------------------------------------------------------------
179
// Implementation of the LifeCycleController interface
180
// ---------------------------------------------------------------------------
181

182   /**
183    * @see LifeCycleController#getFcState()
184    */

185   public String JavaDoc getFcState()
186   {
187     if (lifeCycleLogger.isLoggable(BasicLevel.DEBUG))
188     {
189       lifeCycleLogger.log(BasicLevel.DEBUG, "getFcState: " + fcState);
190     }
191     return fcState;
192   }
193
194   /**
195    * @see LifeCycleController#stopFc()
196    */

197   public void stopFc() throws IllegalLifeCycleException
198   {
199     fcState = LifeCycleController.STOPPED;
200     lifeCycleLogger.log(BasicLevel.DEBUG, "stopped");
201   }
202
203   /**
204    * @see LifeCycleController#startFc()
205    */

206   public void startFc() throws IllegalLifeCycleException
207   {
208     if (firstStart)
209     {
210       beforeFirstStart(weaveableC);
211       firstStart = false;
212     }
213     fcState = LifeCycleController.STARTED;
214     lifeCycleLogger.log(BasicLevel.DEBUG, "started");
215   }
216
217   // ---------------------------------------------------------------------------
218
// Implementation of the BindingController interface
219
// ---------------------------------------------------------------------------
220

221   /**
222    * @see BindingController#lookupFc(String)
223    */

224   public Object JavaDoc lookupFc(String JavaDoc clientItfName) throws NoSuchInterfaceException
225   {
226     synchronized (bindings)
227     {
228       if (bindingLogger.isLoggable(BasicLevel.DEBUG))
229       {
230         bindingLogger.log(BasicLevel.DEBUG, "lookupFc on interface \""
231             + clientItfName + "\"");
232       }
233       return bindings.get(clientItfName);
234     }
235   }
236
237   /**
238    * @see BindingController#bindFc(String, Object)
239    */

240   public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
241       throws NoSuchInterfaceException, IllegalBindingException,
242       IllegalLifeCycleException
243   {
244     synchronized (bindings)
245     {
246       if (bindingLogger.isLoggable(BasicLevel.DEBUG))
247       {
248         bindingLogger.log(BasicLevel.DEBUG, "bindFc on interface \""
249             + clientItfName + "\"");
250       }
251       if (clientItfName.equals("component"))
252       {
253         weaveableC = (Component) serverItf;
254         try
255         {
256           initComponent(weaveableC);
257         }
258         catch (InitializationException e)
259         {
260           logger.log(BasicLevel.ERROR,
261               "An error occured while initializing the component", e);
262           throw new ChainedIllegalBindingException(e, weaveableC, null,
263               "component", null,
264               "An error occured while initializing component");
265         }
266       }
267       else
268       {
269         bindings.put(clientItfName, serverItf);
270       }
271     }
272   }
273
274   /**
275    * @see BindingController#unbindFc(String)
276    */

277   public void unbindFc(String JavaDoc clientItfName) throws NoSuchInterfaceException,
278       IllegalBindingException, IllegalLifeCycleException
279   {
280     synchronized (bindings)
281     {
282       if (bindingLogger.isLoggable(BasicLevel.DEBUG))
283       {
284         bindingLogger.log(BasicLevel.DEBUG, "unbindIf on interface \""
285             + clientItfName + "\"");
286       }
287       bindings.remove(clientItfName);
288     }
289   }
290 }
Popular Tags