KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > isac > plugins > httpmatrix10 > SessionObject


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
24 package org.objectweb.clif.isac.plugins.httpmatrix10;
25
26 import java.util.Hashtable JavaDoc;
27
28 import org.apache.commons.httpclient.HostConfiguration;
29 import org.apache.commons.httpclient.HttpClient;
30 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
31 import org.objectweb.clif.isac.plugins.httpmatrix10.actions.MatrixSamples;
32 import org.objectweb.clif.isac.plugins.httpmatrix10.actions.MatrixTests;
33 import org.objectweb.clif.isac.plugins.httpmatrix10.actions.MatrixTimers;
34 import org.objectweb.clif.scenario.util.isac.exception.IsacRuntimeException;
35 import org.objectweb.clif.scenario.util.isac.plugin.SampleAction;
36 import org.objectweb.clif.scenario.util.isac.plugin.TestAction;
37 import org.objectweb.clif.scenario.util.isac.plugin.TimerAction;
38 import org.objectweb.clif.scenario.util.isac.util.SessionObjectAction;
39 import org.objectweb.clif.scenario.util.transitions.TransitionTable;
40 import org.objectweb.clif.storage.api.ActionEvent;
41
42 /**
43  * This class is the implementation of a session object for the http matrix
44  * plugin
45  *
46  * @author JC Meillaud
47  * @author A Peyrard
48  */

49 public class SessionObject implements SampleAction, TimerAction, TestAction, SessionObjectAction {
50     // logger
51
// public static Category cat = Category.getInstance("SessionObject") ;
52
// sample constantes definition
53
public static final int SAMPLE_DO_NEXT = 0;
54
55     // timer constantes definition
56
public static final int TIMER_WAITING_TIME = 0;
57
58     // test constantes definition
59
public static final int TEST_HAS_NEXT = 1;
60
61     // test constantes definition
62
public static final int TEST_HAS_PREVIOUS = 0;
63
64
65     // default waiting time
66
private static final double DEFAULT_WAITING_TIME = 5;
67     // default nb hits before ending, this value : '0' means that the process will
68
// never stop on this link
69
private static final int DEFAULT_NB_HITS = 0 ;
70
71     // session object params name
72
public static final String JavaDoc FILE_MATRIX = "filematrix" ;
73     public static final String JavaDoc FILE_ACTIONS = "fileactions";
74     public static final String JavaDoc HOST = "host" ;
75     public static final String JavaDoc PORT = "port" ;
76
77     // matrix description
78
private TransitionTable transitionTable ;
79
80     // http client will be used to execute the http methods
81
private HttpClient httpClient;
82
83     // host and port for the proxy
84
private String JavaDoc host ;
85     private String JavaDoc port ;
86
87     // finish state
88
private boolean finishState ;
89
90     /**
91      * Constructor
92      *
93      * @param params
94      * The table containing all the so params
95      */

96     public SessionObject(Hashtable JavaDoc params) {
97         // init the http client
98
this.httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
99         // init port and host
100
this.port = (String JavaDoc)params.get(PORT);
101         this.host = (String JavaDoc)params.get(HOST);
102         // if the proxy has been configured set it to
103
// the connection manager
104
try {
105             if (!host.equals("") && !port.equals("")) {
106                 this.setProxy(host, (new Integer JavaDoc(port)).intValue());
107             }
108         } catch (NullPointerException JavaDoc ne) {
109             System.err.println("Unable to set the proxy...") ;
110         } catch (NumberFormatException JavaDoc nfe) {
111             System.err.println("Unable to set the proxy, the port seams to be malformed");
112         }
113
114         // get the matrix file
115
String JavaDoc fileName = (String JavaDoc)params.get(FILE_MATRIX);
116         // get the actions file
117
String JavaDoc fileActions = (String JavaDoc)params.get(FILE_ACTIONS);
118         // load the transition table
119
this.transitionTable = new TransitionTable(fileName, fileActions, false, false);
120         // init the finish state
121
this.finishState = false;
122     }
123
124     /**
125      * Constructor used in cloneObject method
126      * @param so The session object to clone
127      */

128     private SessionObject(SessionObject so) {
129         // init the http client
130
// The httpClient will be shared between all the session object
131
this.httpClient = so.getHttpClient() ;
132         // init the matrix description
133
this.transitionTable = so.getTransitionTable().createNewTransitionTable() ;
134         // init finish state
135
this.finishState = false;
136     }
137
138     /**
139      * @see org.objectweb.clif.scenario.asec.plugin.SampleAction#doSample(int,
140      * java.lang.Object[])
141      */

142     public ActionEvent doSample(int number, Hashtable JavaDoc params, ActionEvent report) {
143         // switch between the existing sample method
144
switch (number) {
145         case SAMPLE_DO_NEXT:
146             return MatrixSamples.doNext(this, params, report);
147         default:
148             throw new IsacRuntimeException(
149                     "Unable to find this sample in matrix plugin : " + number);
150         }
151     }
152
153     /**
154      * @see org.objectweb.clif.scenario.asec.plugin.TimerAction#doTimer(int,
155      * java.lang.Object[])
156      */

157     public long doTimer(int number, Hashtable JavaDoc params) {
158         // switch between the existing timer method
159
switch (number) {
160         case TIMER_WAITING_TIME:
161             return MatrixTimers.waitingTime(this);
162         default:
163             throw new IsacRuntimeException(
164                     "Unable to find this timer in matrix plugin : " + number);
165         }
166     }
167
168     /**
169      * @see org.objectweb.clif.scenario.asec.plugin.TestAction#doTest(int,
170      * java.lang.Object[])
171      */

172     public boolean doTest(int number, Hashtable JavaDoc params) {
173         // switch between the existing test method
174
switch (number) {
175         case TEST_HAS_NEXT:
176             return MatrixTests.hasNext(this);
177         case TEST_HAS_PREVIOUS:
178             return MatrixTests.hasNext(this);
179         default:
180             throw new IsacRuntimeException(
181                     "Unable to find this test in matrix plugin : " + number);
182         }
183     }
184
185     /**
186      * Method which configure the proxy
187      *
188      * @param host
189      * The hostname of the proxy server
190      * @param port
191      * The port name to go through the proxy
192      */

193     private void setProxy(String JavaDoc host, int port) {
194         // configure the proxy
195
HostConfiguration hostConfiguration = new HostConfiguration();
196         hostConfiguration.setProxy(host, port);
197         // add it to the connection manager
198
this.httpClient.setHostConfiguration(hostConfiguration);
199     }
200
201     /////////////////////////////////////////
202
// SessionObjectAction implementation
203
/////////////////////////////////////////
204

205     /**
206      * @see org.objectweb.clif.scenario.util.isac.util.SessionObjectAction#createNewSessionObject()
207      */

208     public Object JavaDoc createNewSessionObject() {
209         return new SessionObject(this) ;
210     }
211
212     /**
213      * @see org.objectweb.clif.scenario.util.isac.util.SessionObjectAction#close()
214      */

215     public void close() {
216         // nothing to close
217
}
218
219     /**
220      * @see org.objectweb.clif.scenario.util.isac.util.SessionObjectAction#reset()
221      */

222     public void reset() {
223         // reset the states stored in transition table
224
this.transitionTable.resetPreviousState();
225         // reset finish state
226
this.finishState = false;
227     }
228
229     ///////////////////////////////////////////////////
230
// Attribute getters and setters
231
///////////////////////////////////////////////////
232

233     /**
234      * @return Returns the httpClient.
235      */

236     public HttpClient getHttpClient() {
237         return httpClient;
238     }
239
240     /**
241      * @param httpClient
242      * The httpClient to set.
243      */

244     public void setHttpClient(HttpClient httpClient) {
245         this.httpClient = httpClient;
246     }
247
248     /**
249      * @return Returns the transitionTable.
250      */

251     public TransitionTable getTransitionTable() {
252         return transitionTable;
253     }
254
255     /**
256      * @return Returns the host.
257      */

258     public String JavaDoc getHost() {
259         return host;
260     }
261     /**
262      * @return Returns the port.
263      */

264     public String JavaDoc getPort() {
265         return port;
266     }
267
268     /**
269      * @return Returns the finishState.
270      */

271     public boolean isFinishState() {
272         return finishState;
273     }
274     /**
275      * @param finishState The finishState to set.
276      */

277     public void setFinishState(boolean finishState) {
278         this.finishState = finishState;
279     }
280     ///////////////////////////////////////
281
// Debug Part
282
///////////////////////////////////////
283

284 // // this method is only used in debug mode
285
// private static void waitTime(int milli) {
286
// try {
287
// System.out.print("waiting_time=" + milli);
288
// for (int i = 0; i < milli; i++) {
289
// // each 100 millisec wait 100 millisec
290
// if (i % 100 == 0) {
291
// System.out.print(".");
292
// System.out.flush();
293
// Thread.sleep(100);
294
// }
295
// }
296
// } catch (Exception e) {
297
// System.out.println("Exception occurs during waiting time...");
298
// }
299
// }
300

301 // // test method main
302
// public static void main(String[] args) {
303
// // check if a filename has been set into parameters
304
// if (args.length == 0) {
305
// System.out.println("usage : java SessionObject matrixFileName");
306
// }
307
// // proxy configuration
308
// String host = "";
309
// String port = "";
310
// if (args.length == 3) {
311
// System.out.println("ask for proxy configuration...");
312
// host = args[1];
313
// port = args[2];
314
// }
315
// // init a new Session object
316
// SessionObject sessionObject = new SessionObject(args[0], host, port);
317
// // System.out.println(sessionObject.getMatrixDescription()) ;
318
// System.out.println("Session object intialized");
319
//
320
// // do n loop
321
// int n = 25;
322
// for (int i = 0; i < 10; i++) {
323
// ActionEvent report = sessionObject.doSample(SAMPLE_DO_NEXT, null);
324
// System.out.println(report);
325
// long waitingTime = sessionObject.doTimer(TIMER_WAITING_TIME, null);
326
// waitTime(waitingTime);
327
// boolean test = sessionObject.doTest(TEST_HAS_NEXT, null);
328
// System.out.println("continue=" + test);
329
// if (!test)
330
// break;
331
// }
332
// }
333
}
334
Popular Tags