KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > isac > plugins > timers > ConstantTimer


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
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.isac.plugins.timers;
24
25
26 import java.util.Hashtable JavaDoc;
27 import org.objectweb.clif.scenario.util.isac.exception.IsacRuntimeException;
28 import org.objectweb.clif.scenario.util.isac.plugin.TimerAction;
29 import org.objectweb.clif.scenario.util.isac.plugin.TestAction;
30 import org.objectweb.clif.scenario.util.isac.plugin.SampleAction;
31 import org.objectweb.clif.scenario.util.isac.util.SessionObjectAction;
32 import org.objectweb.clif.storage.api.ActionEvent;
33
34
35 /**
36  * This class is the implementation of the ConstantTimer session object.
37  * Includes time-out facilities.
38  *
39  * @author Bruno Dillenseger
40  */

41 public class ConstantTimer implements SampleAction, TestAction, TimerAction, SessionObjectAction
42 {
43     // plug-in parameter: duration parameter
44
static public final String JavaDoc DURATION_ARG = "duration_arg";
45
46     // tests identifiers
47
static public final int RUNNING_TEST = 0;
48     static public final int PASSED_TEST = 1;
49
50     // pseudo-samples identifiers
51
static public final int RESET_SAMPLE = 0;
52     static public final int SET_SAMPLE = 1;
53
54     // timer default duration
55
protected long defaultDuration;
56     // current timer duration in ms
57
protected long duration;
58     // time origin for current time out
59
protected long startTime;
60
61
62     /**
63      * Build a new SessionObject for this plugin
64      * @param params The table containing all the parameters
65      */

66     public ConstantTimer(Hashtable JavaDoc params)
67     {
68         startTime = -1;
69         String JavaDoc value = (String JavaDoc)params.get(DURATION_ARG);
70         if (value != null && value.length() > 0)
71         {
72             duration = defaultDuration = Integer.parseInt(value);
73         }
74         else
75         {
76             duration = defaultDuration = 0;
77         }
78     }
79
80
81     /**
82      * This constructor is used to clone the specified session object
83      *
84      * @param toClone
85      * The session object to clone
86      */

87     private ConstantTimer(ConstantTimer toClone)
88     {
89         startTime = -1;
90         defaultDuration = toClone.defaultDuration;
91         duration = toClone.defaultDuration;
92     }
93
94
95     ////////////////////////////
96
// SampleAction interface //
97
////////////////////////////
98

99
100     public ActionEvent doSample(int number, Hashtable JavaDoc params, ActionEvent dummy)
101     {
102         switch (number)
103         {
104             case RESET_SAMPLE:
105                 startTime = System.currentTimeMillis();
106                 break;
107             case SET_SAMPLE:
108                 startTime = System.currentTimeMillis();
109                 duration = Integer.parseInt((String JavaDoc)params.get(DURATION_ARG));
110                 break;
111         }
112         return null;
113     }
114
115
116     ///////////////////////////
117
// TestAction interface //
118
///////////////////////////
119

120
121     public boolean doTest(int number, Hashtable JavaDoc params)
122         throws IsacRuntimeException
123     {
124         switch (number)
125         {
126             case RUNNING_TEST:
127                 return duration > System.currentTimeMillis() - startTime;
128             case PASSED_TEST:
129                 return duration <= System.currentTimeMillis() - startTime;
130         }
131         throw new Error JavaDoc("Fatal error in ISAC's ConstantTimer plug-in: unknown test identifier " + number);
132     }
133
134
135     ///////////////////////////
136
// TimerAction interface //
137
///////////////////////////
138

139
140     public long doTimer(int number, Hashtable JavaDoc params)
141         throws IsacRuntimeException
142     {
143         String JavaDoc durationStr = (String JavaDoc)params.get(DURATION_ARG);
144         if (durationStr != null && durationStr.length() > 0)
145         {
146             try
147             {
148                 return Integer.parseInt(durationStr);
149             }
150             catch (Exception JavaDoc ex)
151             {
152                 throw new IsacRuntimeException("Can't get timer duration", ex);
153             }
154         }
155         else
156         {
157             return duration;
158         }
159     }
160
161
162     ///////////////////////////////////
163
// SessionObjectAction interface //
164
///////////////////////////////////
165

166
167     public Object JavaDoc createNewSessionObject()
168     {
169         return new ConstantTimer(this);
170     }
171
172
173     public void close()
174     {
175     }
176
177
178     public void reset()
179     {
180         startTime = System.currentTimeMillis();
181         duration = defaultDuration;
182     }
183 }
184
Popular Tags