KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > server > lib > BladeInsertAdapterImpl


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.server.lib;
26
27 import org.objectweb.fractal.api.control.BindingController;
28 import org.objectweb.fractal.api.control.LifeCycleController;
29 import org.objectweb.util.monolog.Monolog;
30 import org.objectweb.util.monolog.api.Logger;
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.clif.supervisor.api.BladeState;
33 import org.objectweb.clif.storage.api.StorageProxyAdmin;
34 import org.objectweb.clif.storage.api.LifeCycleEvent;
35 import org.objectweb.clif.storage.api.AlarmEvent;
36 import org.objectweb.clif.supervisor.api.SupervisorInfo;
37 import org.objectweb.clif.server.api.BladeInsertResponse;
38 import org.objectweb.clif.server.api.BladeControl;
39 import org.objectweb.clif.datacollector.api.DataCollectorWrite;
40 import java.io.Serializable JavaDoc;
41
42 /**
43  * Implementation of a Blade Insert Adapter component (kind of an asynchronous wrapper to the
44  * Blade Insert component).
45  * @author Julien Buret
46  * @author Nicolas Droze
47  * @author Bruno Dillenseger
48  */

49 public class BladeInsertAdapterImpl
50     implements
51         BladeControl,
52         BladeInsertResponse,
53         BindingController,
54         LifeCycleController,
55         Runnable JavaDoc
56 {
57     private static Logger log = Monolog.getDefaultMonologFactory().getLogger(BladeInsertAdapterImpl.class.toString());
58     static final String JavaDoc[] interfaceNames = new String JavaDoc[] {
59         SupervisorInfo.SUPERVISOR_INFO,
60         BladeControl.BLADE_INSERT_CONTROL,
61         StorageProxyAdmin.STORAGEPROXY_ADMIN,
62         DataCollectorWrite.DATA_COLLECTOR_WRITE };
63     final int OP_IDLE = 0;
64     final int OP_INIT = 1;
65     final int OP_START = 2;
66     final int OP_STOP = 3;
67     final int OP_SUSPEND = 4;
68     final int OP_RESUME = 5;
69     final int OP_TERM = 6;
70
71     volatile int current_op = -1;
72     Serializable JavaDoc current_testId = null;
73     volatile Thread JavaDoc op_thread = null;
74
75     private SupervisorInfo sis;
76     private BladeControl bladeCtl;
77     private StorageProxyAdmin spa;
78     private DataCollectorWrite dcw;
79     private BladeState bladeState;
80     private String JavaDoc argument;
81     private String JavaDoc bladeId;
82
83
84     public BladeInsertAdapterImpl()
85     {
86     }
87
88
89     ////////////////////////
90
// interface Runnable //
91
////////////////////////
92

93
94     /**
95      * handles asynchronous calls on control operations, enforcing mutual exclusion
96      */

97     public void run()
98     {
99         synchronized (Thread.currentThread())
100         {
101             while (op_thread != null)
102             {
103                 switch (current_op)
104                 {
105                     case OP_IDLE:
106                         try
107                         {
108                             op_thread.wait();
109                         }
110                         catch (InterruptedException JavaDoc ex)
111                         {
112                             ex.printStackTrace(System.err);
113                         }
114                         break;
115                     case OP_INIT:
116                         if (bladeState == BladeState.DEPLOYED
117                             || bladeState == BladeState.COMPLETED
118                             || bladeState == BladeState.STOPPED
119                             || bladeState == BladeState.ABORTED)
120                         {
121                             do_init();
122                         }
123                         current_op = OP_IDLE;
124                         op_thread.notify();
125                         break;
126                     case OP_START:
127                         if (bladeState == BladeState.INITIALIZED)
128                         {
129                             do_start();
130                         }
131                         current_op = OP_IDLE;
132                         op_thread.notify();
133                         break;
134                     case OP_STOP:
135                         if (bladeState == BladeState.INITIALIZED
136                             || bladeState == BladeState.SUSPENDED
137                             || bladeState == BladeState.RUNNING)
138                         {
139                             do_stop();
140                         }
141                         current_op = OP_IDLE;
142                         op_thread.notify();
143                         break;
144                     case OP_SUSPEND:
145                         if (bladeState == BladeState.RUNNING)
146                         {
147                             do_suspend();
148                         }
149                         current_op = OP_IDLE;
150                         op_thread.notify();
151                         break;
152                     case OP_RESUME:
153                         if (bladeState == BladeState.SUSPENDED)
154                         {
155                             do_resume();
156                         }
157                         current_op = OP_IDLE;
158                         op_thread.notify();
159                         break;
160                     default:
161                         throw new Error JavaDoc("unexpected operation id: " + current_op);
162                 }
163             }
164             current_op = OP_TERM;
165             Thread.currentThread().notify();
166         }
167     }
168
169
170     ///////////////////////////////////
171
// interface LifeCycleController //
172
///////////////////////////////////
173

174
175     public String JavaDoc getFcState()
176     {
177         return op_thread != null ? LifeCycleController.STARTED : LifeCycleController.STOPPED;
178     }
179
180
181     public synchronized void startFc()
182     {
183         current_op = OP_IDLE;
184         bladeState = BladeState.DEPLOYED;
185         op_thread = new Thread JavaDoc(this, "Blade adapter control");
186         op_thread.start();
187     }
188
189
190     public synchronized void stopFc()
191     {
192         if (op_thread != null)
193         {
194             Object JavaDoc lock = op_thread;
195             synchronized (lock)
196             {
197                 op_thread = null;
198                 lock.notify();
199                 if (current_op != OP_TERM)
200                 {
201                     try
202                     {
203                         lock.wait();
204                     }
205                     catch (InterruptedException JavaDoc ex)
206                     {
207                         ex.printStackTrace(System.err);
208                     }
209                 }
210             }
211         }
212     }
213
214
215     /////////////////////////////////
216
// interface BindingController //
217
/////////////////////////////////
218

219
220     public Object JavaDoc lookupFc(String JavaDoc clientItfName)
221     {
222         if (clientItfName.equals(SupervisorInfo.SUPERVISOR_INFO))
223         {
224             return sis;
225         }
226         else if (clientItfName.equals(BladeControl.BLADE_INSERT_CONTROL))
227         {
228             return bladeCtl;
229         }
230         else if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN))
231         {
232             return spa;
233         }
234         else if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE))
235         {
236             return dcw;
237         }
238         else
239         {
240             return null;
241         }
242     }
243
244
245     public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
246     {
247         if (clientItfName.equals(SupervisorInfo.SUPERVISOR_INFO))
248         {
249             sis = (SupervisorInfo) serverItf;
250         }
251         else if (clientItfName.equals(BladeControl.BLADE_INSERT_CONTROL))
252         {
253             bladeCtl = (BladeControl) serverItf;
254         }
255         else if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN))
256         {
257             spa = (StorageProxyAdmin) serverItf;
258         }
259         else if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE))
260         {
261             dcw = (DataCollectorWrite) serverItf;
262         }
263     }
264
265
266     public void unbindFc(String JavaDoc clientItfName)
267     {
268         if (clientItfName.equals(SupervisorInfo.SUPERVISOR_INFO))
269         {
270             sis = null;
271         }
272         else if (clientItfName.equals(BladeControl.BLADE_INSERT_CONTROL))
273         {
274             bladeCtl = null;
275         }
276         else if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN))
277         {
278             spa = null;
279         }
280         else if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE))
281         {
282             dcw = null;
283         }
284     }
285
286
287     public String JavaDoc[] listFc()
288     {
289         return interfaceNames;
290     }
291
292
293     ////////////////////////////
294
// interface BladeControl //
295
////////////////////////////
296

297
298     /**
299      * Sets blade argument. This argument will be set during next blade initialization.
300      */

301     public void setArgument(String JavaDoc argument)
302     {
303         this.argument = argument;
304     }
305
306
307     /**
308      * Sets the blade identifier.
309      */

310     public void setId(String JavaDoc id)
311     {
312         bladeId = id;
313         spa.init(id);
314     }
315
316
317     /**
318      * @return the blade identifier
319      */

320     public String JavaDoc getId()
321     {
322         return bladeId;
323     }
324
325
326     /**
327      * asynchronously initialize the blade
328      * @param testId should contain a unique test identifier
329      */

330     public synchronized void init(Serializable JavaDoc testId)
331     {
332         if (op_thread != null)
333         {
334             synchronized(op_thread)
335             {
336                 while (current_op != OP_IDLE)
337                 {
338                     try
339                     {
340                         op_thread.wait();
341                     }
342                     catch (InterruptedException JavaDoc ex)
343                     {
344                         ex.printStackTrace(System.err);
345                     }
346                 }
347                 current_op = OP_INIT;
348                 current_testId = testId;
349                 op_thread.notify();
350             }
351         }
352     }
353
354
355     public void do_init()
356     {
357         log.log(BasicLevel.DEBUG, "Init blade");
358         sis.setBladeState(bladeId, BladeState.INITIALIZING);
359         try
360         {
361             spa.newTest(current_testId);
362             dcw.init(current_testId, bladeId);
363             bladeCtl.setArgument(argument);
364             bladeCtl.setId(bladeId);
365             dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.INITIALIZING));
366             bladeCtl.init(current_testId);
367             bladeState = BladeState.INITIALIZED;
368             dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.INITIALIZED));
369         }
370         catch (Throwable JavaDoc ex)
371         {
372             sis.setBladeState(bladeId, BladeState.DEPLOYED);
373             sis.alarm(new AlarmEvent(
374                 System.currentTimeMillis(),
375                 bladeId,
376                 AlarmEvent.ERROR,
377                 ex));
378
379         }
380         System.gc();
381         log.log(BasicLevel.DEBUG, "Change blade state for: " + bladeState);
382         sis.setBladeState(bladeId, bladeState);
383     }
384
385
386     /**
387      * asynchronously starts the blade
388      */

389     public synchronized void start()
390     {
391         if (op_thread != null)
392         {
393             synchronized(op_thread)
394             {
395                 while (current_op != OP_IDLE)
396                 {
397                     try
398                     {
399                         op_thread.wait();
400                     }
401                     catch (InterruptedException JavaDoc ex)
402                     {
403                         ex.printStackTrace(System.err);
404                     }
405                 }
406                 current_op = OP_START;
407                 op_thread.notify();
408             }
409         }
410     }
411
412
413     public void do_start()
414     {
415         log.log(BasicLevel.DEBUG, "Change injector state for: " + BladeState.STARTING);
416         sis.setBladeState(bladeId, BladeState.STARTING);
417         try
418         {
419             dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.STARTING));
420             bladeCtl.start();
421             bladeState = BladeState.RUNNING;
422             dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.RUNNING));
423         }
424         catch (Throwable JavaDoc e)
425         {
426             e.printStackTrace(System.err);
427         }
428         log.log(BasicLevel.DEBUG, "Change injector state for: " + bladeState);
429         sis.setBladeState(bladeId, bladeState);
430     }
431
432
433     /**
434      * Waits until the end of the activity
435      */

436     public synchronized void join()
437     {
438     //TODO: check mutual exclusion issues, enable Interruption by a STOP request
439
bladeCtl.join();
440     }
441
442
443     /**
444      * asynchronously stops the blade
445      */

446     public synchronized void stop()
447     {
448         if (op_thread != null)
449         {
450             synchronized(op_thread)
451             {
452                 while (current_op != OP_IDLE)
453                 {
454                     try
455                     {
456                         op_thread.wait();
457                     }
458                     catch (InterruptedException JavaDoc ex)
459                     {
460                         ex.printStackTrace(System.err);
461                     }
462                 }
463                 current_op = OP_STOP;
464                 op_thread.notify();
465             }
466         }
467     }
468
469
470     protected void do_stop()
471     {
472         log.log(BasicLevel.DEBUG, "Change injector state for: " + BladeState.STOPPING);
473         sis.setBladeState(bladeId, BladeState.STOPPING);
474         try
475         {
476             dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.STOPPING));
477             bladeCtl.stop();
478             bladeState = BladeState.STOPPED;
479             dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.STOPPED));
480             dcw.terminate();
481             spa.closeTest();
482         }
483         catch (Throwable JavaDoc e)
484         {
485             e.printStackTrace(System.err);
486         }
487         log.log(BasicLevel.DEBUG, "Change injector state for: " + bladeState);
488         sis.setBladeState(bladeId, bladeState);
489     }
490
491
492     /**
493      * asynchronously suspends the blade
494      */

495     public synchronized void suspend()
496     {
497         if (op_thread != null)
498         {
499             synchronized(op_thread)
500             {
501                 while (current_op != OP_IDLE)
502                 {
503                     try
504                     {
505                         op_thread.wait();
506                     }
507                     catch (InterruptedException JavaDoc ex)
508                     {
509                         ex.printStackTrace(System.err);
510                     }
511                 }
512                 current_op = OP_SUSPEND;
513                 op_thread.notify();
514             }
515         }
516     }
517
518
519     protected void do_suspend()
520     {
521         log.log(BasicLevel.DEBUG, "Change blade " + bladeId + " state to " + BladeState.SUSPENDING);
522         sis.setBladeState(bladeId, BladeState.SUSPENDING);
523         try
524         {
525             dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.SUSPENDING));
526             bladeCtl.suspend();
527             bladeState = BladeState.SUSPENDED;
528             dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.SUSPENDED));
529         }
530         catch (Throwable JavaDoc e)
531         {
532             e.printStackTrace(System.err);
533         }
534         log.log(BasicLevel.DEBUG, "Change injector state for: " + bladeState);
535         sis.setBladeState(bladeId, bladeState);
536     }
537
538
539     /**
540      * asynchronously resumes the blade
541      */

542     public synchronized void resume()
543     {
544         if (op_thread != null)
545         {
546             synchronized(op_thread)
547             {
548                 while (current_op != OP_IDLE)
549                 {
550                     try
551                     {
552                         op_thread.wait();
553                     }
554                     catch (InterruptedException JavaDoc ex)
555                     {
556                         ex.printStackTrace(System.err);
557                     }
558                 }
559                 current_op = OP_RESUME;
560                 op_thread.notify();
561             }
562         }
563     }
564
565
566     protected void do_resume()
567     {
568         log.log(BasicLevel.DEBUG, "Change injector state for: " + BladeState.RESUMING);
569         sis.setBladeState(bladeId, BladeState.RESUMING);
570         try
571         {
572             dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.RESUMING));
573             bladeCtl.resume();
574             bladeState = BladeState.RUNNING;
575             dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.RUNNING));
576         }
577         catch (Throwable JavaDoc e)
578         {
579             e.printStackTrace(System.err);
580         }
581         log.log(BasicLevel.DEBUG, "Change injector state for: " + bladeState);
582         sis.setBladeState(bladeId, bladeState);
583     }
584
585
586     ///////////////////////////////////
587
// interface BladeInsertResponse //
588
///////////////////////////////////
589

590
591     public void aborted()
592     {
593         bladeState = BladeState.ABORTED;
594         log.log(BasicLevel.DEBUG, "Change blade state for: " + bladeState);
595         dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.ABORTED));
596         dcw.terminate();
597         spa.closeTest();
598         sis.setBladeState(bladeId, bladeState);
599     }
600
601
602     public void completed()
603     {
604         bladeState = BladeState.COMPLETED;
605         log.log(BasicLevel.DEBUG, "Change blade state for: " + bladeState);
606         dcw.add(new LifeCycleEvent(System.currentTimeMillis(), bladeId, BladeState.COMPLETED));
607         dcw.terminate();
608         spa.closeTest();
609         sis.setBladeState(bladeId, bladeState);
610     }
611 }
612
Popular Tags