KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > samplers > RemoteListenerWrapper


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/samplers/RemoteListenerWrapper.java,v 1.11 2004/02/13 01:22:16 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.samplers;
20
21 import java.io.Serializable JavaDoc;
22 import java.rmi.RemoteException JavaDoc;
23
24 import org.apache.jmeter.engine.event.LoopIterationEvent;
25 import org.apache.jmeter.engine.util.NoThreadClone;
26 import org.apache.jmeter.testelement.AbstractTestElement;
27 import org.apache.jmeter.testelement.TestListener;
28 import org.apache.jorphan.logging.LoggingManager;
29 import org.apache.log.Logger;
30 import org.apache.jmeter.util.JMeterUtils;
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 /**
36  * @author unascribed
37  *
38  * Lars-Erik Helander provided the idea (and original implementation)
39  * for the caching functionality (sampleStore).
40  *
41  * @version $Revision: 1.11 $ Updated on: $Date: 2004/02/13 01:22:16 $
42  */

43 public class RemoteListenerWrapper
44     extends AbstractTestElement
45     implements SampleListener, TestListener, Serializable JavaDoc, NoThreadClone
46 {
47     transient private static Logger log = LoggingManager.getLoggerForClass();
48     private RemoteSampleListener listener = null;
49     
50     private boolean holdSamples; //Hold samples to end of test?
51
private List JavaDoc sampleStore; // Samples stored here
52

53     private void setUpStore(){
54         holdSamples = JMeterUtils.getPropDefault("hold_samples",false);
55         if (holdSamples){
56             sampleStore = new ArrayList JavaDoc();
57             log.info("Using Sample store for this test run");
58         }
59     }
60
61     public RemoteListenerWrapper(RemoteSampleListener l)
62     {
63         listener = l;
64     }
65
66
67     public RemoteListenerWrapper() //TODO: not used - make private?
68
{
69     }
70
71     public void testStarted()
72     {
73         log.info("Test Started()");
74         setUpStore();
75         try
76         {
77            listener.testStarted();
78         }
79         catch (Throwable JavaDoc ex)
80         {
81             log.warn("testStarted()", ex);
82         }
83
84     }
85     public void testEnded()
86     {
87         log.info("Test ended()");
88         try
89         {
90             if (holdSamples){
91                 synchronized(sampleStore){
92                     Iterator JavaDoc i = sampleStore.iterator();
93                     while (i.hasNext()) {
94                       SampleEvent se = (SampleEvent) i.next();
95                       listener.sampleOccurred(se);
96                     }
97                 }
98             }
99             listener.testEnded();
100             sampleStore = null;
101         }
102         catch (Throwable JavaDoc ex)
103         {
104             log.warn("testEnded()", ex);
105         }
106     }
107     public void testStarted(String JavaDoc host)
108     {
109         log.info("Test Started on "+host); // should this be debug?
110
setUpStore();
111         try
112         {
113             listener.testStarted(host);
114         }
115         catch (Throwable JavaDoc ex)
116         {
117             log.error("testStarted(host)", ex);
118         }
119     }
120     public void testEnded(String JavaDoc host)
121     {
122         log.info("Test Ended on " + host); // should this be debug?
123
try
124         {
125             if (holdSamples){
126                 Iterator JavaDoc i = sampleStore.iterator();
127                 while (i.hasNext()) {
128                   SampleEvent se = (SampleEvent) i.next();
129                   listener.sampleOccurred(se);
130                 }
131             }
132             listener.testEnded(host);
133             sampleStore = null;
134         }
135         catch (Throwable JavaDoc ex)
136         {
137             log.error("testEnded(host)", ex);
138         }
139     }
140
141     public void sampleOccurred(SampleEvent e)
142     {
143         log.debug("Sample occurred");
144         try
145         {
146           if (holdSamples) {
147             synchronized(sampleStore)
148             {
149                 sampleStore.add(e);
150             }
151           } else {
152             listener.sampleOccurred(e);
153           }
154         }
155         catch (RemoteException JavaDoc err)
156         {
157             log.error("sampleOccurred", err);
158         }
159     }
160
161 // Note that sampleStarted() and sampleStopped() is not made to appear
162
// in synch with sampleOccured() when replaying held samples.
163
// For now this is not critical since sampleStarted() and sampleStopped()
164
// is not used, but it may become an issue in the future. Then these
165
// events must also be stored so that replay of all events may occur and
166
// in the right order. Each stored event must then be tagged with something
167
// that lets you distinguish between occured, started and ended.
168

169     public void sampleStarted(SampleEvent e)
170     {
171         log.debug("Sample started");
172         try
173         {
174             listener.sampleStarted(e);
175         }
176         catch (RemoteException JavaDoc err)
177         {
178             log.error("sampleStarted", err);
179         }
180     }
181     public void sampleStopped(SampleEvent e)
182     {
183         log.debug("Sample stopped");
184         try
185         {
186             listener.sampleStopped(e);
187         }
188         catch (RemoteException JavaDoc err)
189         {
190             log.error("sampleStopped", err);
191         }
192     }
193     /* (non-Javadoc)
194      * @see TestListener#testIterationStart(LoopIterationEvent)
195      */

196     public void testIterationStart(LoopIterationEvent event)
197     {
198     }
199
200 }
Popular Tags