KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HelloWorldSessionObject


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
25 import org.objectweb.clif.scenario.util.isac.plugin.SampleAction;
26 import org.objectweb.clif.scenario.util.isac.util.SessionObjectAction;
27 import org.objectweb.clif.scenario.util.isac.exception.IsacRuntimeException;
28 import org.objectweb.clif.storage.api.ActionEvent;
29 import java.util.Hashtable JavaDoc;
30
31
32 /**
33  *
34  * @author Bruno Dillenseger
35  */

36 public class HelloWorldSessionObject implements SampleAction, SessionObjectAction
37 {
38     // samples identifier
39
static protected final int SAY_HELLO = 0;
40     static protected final int SAY_WORLD = 1;
41     static protected final int SAY_NEXT_WORD = 2;
42     static protected final int SAY_USER_DEFINED = 3;
43
44     // name of the params
45
static protected final String JavaDoc USER_DEFINED_ARG = "user_string_arg";
46     static protected final String JavaDoc HELLO_ARG = "hello_arg";
47     static protected final String JavaDoc WORLD_ARG = "world_arg";
48
49     boolean wordSwitch = true;
50     String JavaDoc hello = "Hello";
51     String JavaDoc world = "World";
52
53     /**
54      * Build a new SessionObject for this plugin
55      *
56      * @param params
57      * The table containing all the so params
58      */

59     public HelloWorldSessionObject(Hashtable JavaDoc params)
60     {
61         String JavaDoc value = (String JavaDoc)params.get(HELLO_ARG);
62         if (value != null && value.length() > 0)
63         {
64             hello = value;
65         }
66         value = (String JavaDoc)params.get(WORLD_ARG);
67         if (value != null && value.length() > 0)
68         {
69             world = value;
70         }
71     }
72
73
74     /**
75      * This constructor is used to clone the specified session object
76      *
77      * @param toClone
78      * The session object to clone
79      */

80     private HelloWorldSessionObject(HelloWorldSessionObject toClone)
81     {
82         hello = toClone.hello;
83         world = toClone.world;
84         wordSwitch = toClone.wordSwitch;
85     }
86
87
88     ////////////////////////////
89
// SampleAction interface //
90
////////////////////////////
91

92
93     public ActionEvent doSample(int number, Hashtable JavaDoc params, ActionEvent report)
94         throws IsacRuntimeException
95     {
96         synchronized(System.out)
97         {
98             long startingTime = System.currentTimeMillis();
99             switch (number)
100             {
101                 case SAY_HELLO:
102                     report.result = hello;
103                     report.type = "HELLO";
104                     break;
105                 case SAY_WORLD:
106                     report.result = world;
107                     report.type = "WORLD";
108                     break;
109                 case SAY_NEXT_WORD:
110                     report.result = wordSwitch ? hello : world;
111                     wordSwitch = ! wordSwitch;
112                     report.type = "NEXT_WORD";
113                     break;
114                 case SAY_USER_DEFINED:
115                     report.type = "USER_DEFINED";
116                     report.result = (String JavaDoc)params.get(USER_DEFINED_ARG);
117                     break;
118                 default:
119                     report.comment = "no such sample number: " + number;
120                     report.type = "ERROR";
121                     report.successful = false;
122             }
123             if (report.isSuccessful())
124             {
125                 System.out.println(report.result);
126                 System.out.flush();
127                 report.duration = (int)(System.currentTimeMillis() - startingTime);
128             }
129             report.setDate(startingTime);
130         }
131         return report;
132     }
133
134
135     ///////////////////////////////////
136
// SessionObjectAction interface //
137
///////////////////////////////////
138

139
140     public Object JavaDoc createNewSessionObject()
141     {
142         return new HelloWorldSessionObject(this);
143     }
144
145
146     public void close()
147     {
148     }
149
150
151     public void reset()
152     {
153     }
154 }
155
Popular Tags