KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > supervisor > lib > SupervisorImpl


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2003,2004 France Telecom R&D
4 * Copyright (C) 2003 INRIA
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * CLIF $Name: $
21 *
22 * Contact: clif@objectweb.org
23 */

24
25 package org.objectweb.clif.supervisor.lib;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Observable JavaDoc;
31 import java.io.Serializable JavaDoc;
32 import org.objectweb.clif.datacollector.api.DataCollectorAdmin;
33 import org.objectweb.clif.storage.api.StorageAdmin;
34 import org.objectweb.clif.storage.api.AlarmEvent;
35 import org.objectweb.clif.supervisor.api.SupervisorInfo;
36 import org.objectweb.clif.supervisor.api.TestControl;
37 import org.objectweb.clif.supervisor.api.BladeState;
38 import org.objectweb.clif.server.api.BladeControl;
39 import org.objectweb.fractal.api.NoSuchInterfaceException;
40 import org.objectweb.fractal.api.Interface;
41 import org.objectweb.fractal.api.control.*;
42 import org.objectweb.util.monolog.api.Logger;
43 import org.objectweb.util.monolog.api.BasicLevel;
44 import org.objectweb.util.monolog.Monolog;
45
46
47 /**
48  * Supervisor implementation, used to deploy and control test plans among CLIF servers.
49  * It relies on a FractalRMI registry to find Fractal seeds where CLIF servers may be deployed
50  * (the same registry used by Fractal seeds to register themselves).
51  * This class extends the Observable class in order to provide feedback information about the state
52  * of the test plan's blades and the occurrence of alarms.
53  * @see AlarmEvent
54  * @see BladeObservation
55  * @author Julien Buret
56  * @author Nicolas Droze
57  * @author Bruno Dillenseger
58  */

59
60
61 public class SupervisorImpl
62     extends
63         Observable JavaDoc
64     implements
65         TestControl,
66         SupervisorInfo,
67         BindingController,
68         LifeCycleController
69 {
70     static private Logger log = Monolog.getDefaultMonologFactory().getLogger(SupervisorImpl.class.toString());
71
72     /** identifier for current test */
73     private Serializable JavaDoc currentTestId = null;
74
75     /** contains BladeControl interfaces, indexed by their blade identifier */
76     private Map JavaDoc bladesById;
77
78     /** current Supervisor component state */
79     private String JavaDoc fcState = LifeCycleController.STOPPED;
80
81
82     ////////////////////////////////
83
// fields for client bindings //
84
////////////////////////////////
85

86
87     /** contains BladeControl Fractal interfaces indexed by their names */
88     private Map JavaDoc bladesItf = new HashMap JavaDoc();
89
90     /** contains DataCollectorAdmin Fractal interfaces indexed by their names */
91     private Map JavaDoc collectorsItf = new HashMap JavaDoc();
92
93     /** Fractal interface for storage component administration */
94     private StorageAdmin storageItf;
95
96     /** Fractal interface for Console component information interface */
97     private SupervisorInfo infoItf;
98
99     /** keeps a cache of all bound client interfaces names (null value means invalidation) */
100     private String JavaDoc[] interfaceNamesCache = null;
101
102
103     ///////////////////////////////////
104
// interface LifeCycleController //
105
///////////////////////////////////
106

107
108     public void startFc()
109     {
110         fcState = LifeCycleController.STARTED;
111     }
112
113
114     public void stopFc()
115     {
116         bladesById = null;
117         fcState = LifeCycleController.STOPPED;
118     }
119
120
121     public String JavaDoc getFcState()
122     {
123         return fcState;
124     }
125
126
127     /////////////////////////////////
128
// interface BindingController //
129
/////////////////////////////////
130

131
132     public Object JavaDoc lookupFc(String JavaDoc clientItfName)
133     {
134         if (clientItfName.equals(StorageAdmin.STORAGE_ADMIN))
135         {
136             return storageItf;
137         }
138         else if (clientItfName.startsWith(DataCollectorAdmin.DATA_COLLECTOR_ADMIN))
139         {
140             return collectorsItf.get(clientItfName);
141         }
142         else if (clientItfName.startsWith(BladeControl.BLADE_CONTROL))
143         {
144             return bladesItf.get(clientItfName);
145         }
146         else if (clientItfName.equals(SupervisorInfo.SUPERVISOR_INFO))
147         {
148             return infoItf;
149         }
150         else
151         {
152             return null;
153         }
154     }
155
156
157     public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
158     {
159         if (clientItfName.equals(StorageAdmin.STORAGE_ADMIN))
160         {
161             storageItf = (StorageAdmin) serverItf;
162             interfaceNamesCache = null;
163         }
164         else if (clientItfName.startsWith(DataCollectorAdmin.DATA_COLLECTOR_ADMIN))
165         {
166             collectorsItf.put(clientItfName, serverItf);
167             interfaceNamesCache = null;
168         }
169         else if (clientItfName.startsWith(BladeControl.BLADE_CONTROL))
170         {
171             bladesItf.put(clientItfName, serverItf);
172             interfaceNamesCache = null;
173         }
174         else if (clientItfName.equals(SupervisorInfo.SUPERVISOR_INFO))
175         {
176             infoItf = (SupervisorInfo) serverItf;
177             interfaceNamesCache = null;
178         }
179     }
180
181
182     public synchronized void unbindFc(String JavaDoc clientItfName)
183     {
184         if (clientItfName.equals(StorageAdmin.STORAGE_ADMIN))
185         {
186             storageItf = null;
187             interfaceNamesCache = null;
188         }
189         else if (clientItfName.startsWith(DataCollectorAdmin.DATA_COLLECTOR_ADMIN))
190         {
191             collectorsItf.remove(clientItfName);
192             interfaceNamesCache = null;
193         }
194         else if (clientItfName.startsWith(BladeControl.BLADE_CONTROL))
195         {
196             bladesItf.remove(clientItfName);
197             interfaceNamesCache = null;
198         }
199     }
200
201
202     public synchronized String JavaDoc[] listFc()
203     {
204         if (interfaceNamesCache == null)
205         {
206             int i = 0;
207             interfaceNamesCache = new String JavaDoc[
208                 (storageItf == null ? 0 : 1)
209                 + (infoItf == null ? 0 : 1)
210                 + bladesItf.size()
211                 + collectorsItf.size()];
212             if (storageItf != null)
213             {
214                 interfaceNamesCache[i++] = StorageAdmin.STORAGE_ADMIN;
215             }
216             if (infoItf != null)
217             {
218                 interfaceNamesCache[i++] = SupervisorInfo.SUPERVISOR_INFO;
219             }
220             i = fillInterfaceArray(i, interfaceNamesCache, bladesItf.keySet().iterator());
221             i = fillInterfaceArray(i, interfaceNamesCache, collectorsItf.keySet().iterator());
222         }
223         return interfaceNamesCache;
224     }
225
226
227     /**
228      * utility method used by listFc()
229      */

230     private int fillInterfaceArray(int i, String JavaDoc[] array, Iterator JavaDoc values)
231     {
232         while (values.hasNext())
233         {
234             array[i++] = (String JavaDoc)values.next();
235         }
236         return i;
237     }
238
239
240     ///////////////////////////
241
// interface TestControl //
242
///////////////////////////
243

244
245     /**
246      * Retrieve the execution statistics of a specific blade.
247      * @param bladeId The blade identifier to get the report from
248      * @return An array containing execution statistics for the given blade
249      */

250     public long[] getStats(String JavaDoc bladeId)
251     {
252         Interface blade = (Interface)bladesById.get(bladeId);
253         if (blade != null)
254         {
255             Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
256             try
257             {
258                 return ((DataCollectorAdmin)blade.getFcItfOwner().getFcInterface(
259                     DataCollectorAdmin.DATA_COLLECTOR_ADMIN)).getStat();
260             }
261             catch (NoSuchInterfaceException ex)
262             {
263                 log.log(BasicLevel.ERROR, "Blade " + bladeId + " does not have a DataCollectorAdmin interface.");
264             }
265         }
266         return null;
267     }
268
269
270     public String JavaDoc[] getStatLabels(String JavaDoc bladeId)
271     {
272         Interface blade = (Interface)bladesById.get(bladeId);
273         if (blade != null)
274         {
275             try
276             {
277                 return ((DataCollectorAdmin)blade.getFcItfOwner().getFcInterface(
278                     DataCollectorAdmin.DATA_COLLECTOR_ADMIN)).getLabels();
279             }
280             catch (NoSuchInterfaceException ex)
281             {
282                 log.log(BasicLevel.ERROR, "Blade " + bladeId + " does not have a DataCollectorAdmin interface.");
283             }
284         }
285         return null;
286     }
287
288
289     /**
290      * Collects latest test data
291      */

292     public void collect()
293     {
294         storageItf.collect();
295     }
296
297
298     /**
299      * Initialize the test
300      */

301     public void init(Serializable JavaDoc testDef)
302     {
303         currentTestId = (Serializable JavaDoc)((Object JavaDoc[])testDef)[0];
304         Map JavaDoc testPlan = (Map JavaDoc)((Object JavaDoc[])testDef)[1];
305         storageItf.newTest(
306             currentTestId,
307             testPlan);
308         boolean firstInit = bladesById == null;
309         if (firstInit)
310         {
311             bladesById = new HashMap JavaDoc(bladesItf.size());
312         }
313         try
314         {
315             Iterator JavaDoc iter = bladesItf.values().iterator();
316             while (iter.hasNext())
317             {
318                 BladeControl bladeCtl = (BladeControl)iter.next();
319                 if (firstInit)
320                 {
321                     bladesById.put(bladeCtl.getId(), bladeCtl);
322                 }
323                 bladeCtl.init(currentTestId);
324             }
325         }
326         catch (Exception JavaDoc e)
327         {
328             log.log(BasicLevel.ERROR, "SupervisorImpl.init() Error ", e);
329         }
330     }
331
332
333     /**
334      * Starts the test plan (i.e. all blades)
335      */

336     public void start()
337     {
338         log.log(BasicLevel.DEBUG, "SupervisorImpl.start()");
339         Iterator JavaDoc it = bladesItf.values().iterator();
340         while (it.hasNext())
341         {
342             ((BladeControl)it.next()).start();
343         }
344     }
345
346
347     /**
348      * Final stop of the test
349      */

350     public void stop()
351     {
352         log.log(BasicLevel.DEBUG, "SupervisorImpl.stop()");
353         Iterator JavaDoc it = bladesItf.values().iterator();
354         while (it.hasNext())
355         {
356             ((BladeControl)it.next()).stop();
357         }
358     }
359
360
361     /**
362      * Suspend the test
363      */

364     public void suspend()
365     {
366         log.log(BasicLevel.DEBUG, "SupervisorImpl.suspend()");
367         Iterator JavaDoc it = bladesItf.values().iterator();
368         while (it.hasNext())
369         {
370             ((BladeControl)it.next()).suspend();
371         }
372     }
373
374
375     /**
376      * Resume the test (if suspended)
377      */

378     public void resume()
379     {
380         log.log(BasicLevel.DEBUG, "SupervisorImpl.resume()");
381         Iterator JavaDoc it = bladesItf.values().iterator();
382         while (it.hasNext())
383         {
384             ((BladeControl) it.next()).resume();
385         }
386     }
387
388
389     /**
390      * Waits for the end of the test
391      */

392     public void join()
393     {
394         log.log(BasicLevel.DEBUG, "SupervisorImpl.join()");
395         Iterator JavaDoc it = bladesItf.values().iterator();
396         while (it.hasNext())
397         {
398             ((BladeControl) it.next()).join();
399         }
400     }
401
402
403     //////////////////////////////
404
// interface SupervisorInfo //
405
//////////////////////////////
406

407
408     /**
409      * Forwards the alarm event to observers
410      */

411     public void alarm(AlarmEvent alarm)
412     {
413         setChanged();
414         notifyObservers(alarm);
415     }
416
417
418     /**
419      * Inform that the state of a host has changed.
420      * @param id the scenario globally unique identifier object
421      * @param state The new state of the blade
422      */

423     public void setBladeState(String JavaDoc id, BladeState state)
424     {
425         setChanged();
426         notifyObservers(new BladeObservation(id, state));
427     }
428 }
429
Popular Tags