KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > datacollector > lib > AbstractDataCollector


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2003,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.datacollector.lib;
25
26 import org.objectweb.fractal.api.control.BindingController;
27 import org.objectweb.fractal.api.control.LifeCycleController;
28 import org.objectweb.clif.storage.api.StorageWrite;
29 import org.objectweb.clif.storage.api.LifeCycleEvent;
30 import org.objectweb.clif.storage.api.AlarmEvent;
31 import org.objectweb.clif.storage.api.BladeEvent;
32 import org.objectweb.clif.storage.api.ActionEvent;
33 import org.objectweb.clif.storage.api.ProbeEvent;
34 import org.objectweb.clif.datacollector.api.DataCollectorWrite;
35 import org.objectweb.clif.datacollector.api.DataCollectorAdmin;
36 import java.io.Serializable JavaDoc;
37 import java.util.SortedSet JavaDoc;
38 import java.util.TreeSet JavaDoc;
39 import java.util.Collections JavaDoc;
40
41
42 /**
43  * Abstract implementation of a Data Collector component.
44  * @author Bruno Dillenseger
45  */

46 abstract public class AbstractDataCollector
47     implements
48         DataCollectorWrite,
49         DataCollectorAdmin,
50         BindingController,
51         LifeCycleController
52 {
53     static protected long DELAY_MS = 10000;
54     static final String JavaDoc[] interfaceNames = new String JavaDoc[] { StorageWrite.STORAGE_WRITE };
55
56
57     protected StorageWrite sws;
58     SortedSet JavaDoc eventQ = Collections.synchronizedSortedSet(new TreeSet JavaDoc());
59     DelayedWriter writer;
60     boolean stopped;
61     Object JavaDoc state_lock = new Object JavaDoc();
62
63
64     public AbstractDataCollector()
65     {
66     }
67
68
69     ///////////////////////////////////
70
// interface LifeCycleController //
71
///////////////////////////////////
72

73
74     public void startFc()
75     {
76         synchronized (state_lock)
77         {
78             stopped = false;
79         }
80     }
81
82
83     public void stopFc()
84     {
85         terminate();
86     }
87
88
89     public String JavaDoc getFcState()
90     {
91         synchronized (state_lock)
92         {
93             return stopped ? LifeCycleController.STOPPED : LifeCycleController.STARTED;
94         }
95     }
96
97
98     /////////////////////////////////
99
// interface BindingController //
100
/////////////////////////////////
101

102
103     public Object JavaDoc lookupFc(String JavaDoc clientItfName)
104     {
105         if (clientItfName.equals(StorageWrite.STORAGE_WRITE))
106         {
107             return sws;
108         }
109         else
110         {
111             return null;
112         }
113     }
114
115
116     public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
117     {
118         if (clientItfName.equals(StorageWrite.STORAGE_WRITE))
119         {
120             sws = (StorageWrite) serverItf;
121         }
122     }
123
124
125     public void unbindFc(String JavaDoc clientItfName)
126     {
127         if (clientItfName.equals(StorageWrite.STORAGE_WRITE))
128         {
129             sws = null;
130         }
131     }
132
133
134     public String JavaDoc[] listFc()
135     {
136         return sws == null ? new String JavaDoc[0] : interfaceNames;
137     }
138
139
140     //////////////////////////////////
141
// interface DataCollectorWrite //
142
//////////////////////////////////
143

144
145     /**
146      * @param testId test identifier
147      * @param bladeId blade identifier
148      */

149     public void init(Serializable JavaDoc testId, String JavaDoc bladeId)
150     {
151         stopped = false;
152         writer = new DelayedWriter();
153         writer.start();
154     }
155
156
157     /**
158      * Flushes remaining events, and returns. This might take some time (several seconds)
159      * @see #DELAY_MS
160      */

161     public void terminate()
162     {
163         synchronized (state_lock)
164         {
165             if (writer != null)
166             {
167                 stopped = true;
168                 try
169                 {
170                     state_lock.wait();
171                 }
172                 catch (InterruptedException JavaDoc ex)
173                 {
174                     ex.printStackTrace(System.err);
175                 }
176             }
177         }
178     }
179
180
181     /**
182      * Add a new lifecycle event - simply transmit it to the Storage Proxy component.
183      * @param event new lifecycle event
184      */

185     public void add(LifeCycleEvent event)
186     {
187         eventQ.add(event);
188     }
189
190
191     /**
192      * Add a new measure - simply transmit it to the Storage Proxy component.
193      * @param action the new action event
194      */

195     public void add(ActionEvent action)
196     {
197         eventQ.add(action);
198     }
199
200
201     /**
202      * Add a new alarm - simply transmit it to the Storage Proxy component.
203      * @param alarm new alarm event
204      */

205     public void add(AlarmEvent alarm)
206     {
207         eventQ.add(alarm);
208     }
209
210
211     /**
212      * Add a new measure - simply transmit it to the Storage Proxy component.
213      * @param measure the new measure
214      */

215     public void add(ProbeEvent measure)
216     {
217         if (measure != null)
218         {
219             eventQ.add(measure);
220         }
221     }
222
223
224     /////////////////////////////////////////////////////////////////////
225
// inner classes for delayed background writing of incoming events //
226
/////////////////////////////////////////////////////////////////////
227

228
229     class DelayedWriter extends Thread JavaDoc
230     {
231         public DelayedWriter()
232         {
233             super("Delayed writer");
234         }
235
236
237         public void run()
238         {
239             BladeEvent firstEvent;
240             while (! (stopped && eventQ.isEmpty()))
241             {
242                 if (eventQ.isEmpty())
243                 {
244                     try
245                     {
246                         sleep(DELAY_MS);
247                     }
248                     catch (InterruptedException JavaDoc ex)
249                     {
250                     }
251                 }
252                 else if ((firstEvent = (BladeEvent)eventQ.first()).getDate() + DELAY_MS < System.currentTimeMillis())
253                 {
254                     eventQ.remove(firstEvent);
255                     if (sws != null)
256                     {
257                         sws.write(firstEvent);
258                     }
259                 }
260                 else
261                 {
262                     try
263                     {
264                         sleep(DELAY_MS - System.currentTimeMillis() + firstEvent.getDate());
265                     }
266                     catch (Exception JavaDoc ex)
267                     {
268                     }
269                 }
270             }
271             synchronized (state_lock)
272             {
273                 writer = null;
274                 state_lock.notify();
275             }
276         }
277     }
278 }
279
Popular Tags