KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > probe > util > AbstractInsert


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2004 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 * CLIF $Name: $
20 *
21 * Contact: clif@objectweb.org
22 */

23
24 package org.objectweb.clif.probe.util;
25
26
27 /**
28  * This abstract implementation of a blade insert component manages a single thread,
29  * doing a periodic task (probing) with a given period (in milliseconds) and during
30  * an given actual time. It provides a strict interpretation of the blade lifecycle
31  * with regard to the ActivityControl interface.
32  * @author Bruno Dillenseger
33  */

34 abstract public class AbstractInsert
35     extends AbstractDumbInsert
36 {
37
38
39     //////////////////////////////////////////////
40
// implementation of interface BladeControl //
41
//////////////////////////////////////////////
42

43
44     public void start()
45     {
46         synchronized(activity_lock)
47         {
48             started = true;
49             activity_lock.notify();
50         }
51     }
52
53
54     public void stop()
55     {
56         synchronized (activity_lock)
57         {
58             stopped = true;
59             poller.interrupt();
60         }
61         if (suspended)
62         {
63             resume();
64         }
65         synchronized (activity_lock)
66         {
67             if (started && ! terminated)
68             {
69                 try
70                 {
71                     activity_lock.wait();
72                 }
73                 catch (InterruptedException JavaDoc ex)
74                 {
75                 }
76             }
77         }
78     }
79
80
81     public void suspend()
82     {
83         synchronized (activity_lock)
84         {
85             suspended = true;
86             poller.interrupt();
87         }
88     }
89
90
91     public void resume()
92     {
93         synchronized (activity_lock)
94         {
95             suspended = false;
96             activity_lock.notify();
97         }
98     }
99
100
101     public void join()
102     {
103         synchronized (activity_lock)
104         {
105             if (!terminated)
106             {
107                 try
108                 {
109                     activity_lock.wait();
110                 }
111                 catch (InterruptedException JavaDoc ex)
112                 {
113                 }
114             }
115         }
116     }
117
118
119     ////////////////////////
120
// Runnable interface //
121
////////////////////////
122

123
124     public void run()
125     {
126         try
127         {
128             synchronized (activity_lock)
129             {
130                 if (! started)
131                 {
132                     activity_lock.wait();
133                 }
134                 baseTime_ms = System.currentTimeMillis();
135             }
136         }
137         catch (InterruptedException JavaDoc ex)
138         {
139         }
140         while (
141             ! stopped
142             && arg_duration_ms > System.currentTimeMillis() - baseTime_ms)
143         {
144             try
145             {
146                 synchronized (activity_lock)
147                 {
148                     if (suspended)
149                     {
150                         baseTime_ms = System.currentTimeMillis() - baseTime_ms;
151                         activity_lock.wait();
152                         baseTime_ms = System.currentTimeMillis() - baseTime_ms;
153                     }
154                 }
155                 synchronized (activity_lock)
156                 {
157                     if (!stopped
158                         && arg_duration_ms > System.currentTimeMillis() - baseTime_ms)
159                     {
160                         try
161                         {
162                             dcw.add(doProbe());
163                         }
164                         catch (Exception JavaDoc ex)
165                         {
166                         }
167                     }
168                 }
169                 long time = System.currentTimeMillis();
170                 if (arg_duration_ms > time - baseTime_ms + arg_period_ms)
171                 {
172                     Thread.currentThread().sleep(arg_period_ms);
173                 }
174                 else
175                 {
176                     Thread.currentThread().sleep(
177                         arg_duration_ms
178                         - time + baseTime_ms);
179                 }
180             }
181             catch (InterruptedException JavaDoc ex)
182             {
183             }
184         }
185         synchronized (activity_lock)
186         {
187             terminated = true;
188             if (! stopped)
189             {
190                 bir.completed();
191             }
192             else
193             {
194                 activity_lock.notify();
195             }
196         }
197     }
198 }
199
Popular Tags