KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > visualizers > MonitorAccumModel


1 // $Header: /home/cvs/jakarta-jmeter/src/monitor/components/org/apache/jmeter/visualizers/MonitorAccumModel.java,v 1.6 2004/03/20 20:36:37 sebb Exp $
2
/*
3  * Copyright 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 package org.apache.jmeter.visualizers;
18
19 import java.io.Serializable JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.LinkedList JavaDoc;
27
28 import org.apache.jmeter.samplers.Clearable;
29 import org.apache.jmeter.samplers.SampleResult;
30 import org.apache.jmeter.monitor.model.ObjectFactory;
31 import org.apache.jmeter.monitor.model.Status;
32 import org.apache.jmeter.monitor.util.Stats;
33 import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult;
34
35 public class MonitorAccumModel implements Clearable, Serializable JavaDoc
36 {
37
38     private HashMap JavaDoc MAP;
39     //private MonitorModel CURRENT;
40
private List JavaDoc listeners;
41     /**
42      * By default, we set the default to 800
43      */

44     private int DEFAULT_BUFFER = 800;
45     
46     /**
47      *
48      */

49     public MonitorAccumModel()
50     {
51         MAP = new HashMap JavaDoc();
52         listeners = new LinkedList JavaDoc();
53     }
54     
55     public int getBufferSize(){
56         return DEFAULT_BUFFER;
57     }
58     
59     public void setBufferSize(int buffer){
60         DEFAULT_BUFFER = buffer;
61     }
62     
63     /**
64      * Method will look up the server in the map. The
65      * MonitorModel will be added to an existing list,
66      * or a new one will be created.
67      * @param model
68      */

69     public void addSample(MonitorModel model){
70         //this.CURRENT = model;
71
if (MAP.containsKey(model.getURL())){
72             List JavaDoc newlist = updateArray(model,(List JavaDoc)MAP.get(model.getURL()));
73             MAP.put(model.getURL(),newlist);
74         } else {
75             List JavaDoc samples = Collections.synchronizedList(new LinkedList JavaDoc());
76             samples.add(model);
77             MAP.put(model.getURL(),samples);
78         }
79     }
80
81     /**
82      * We want to keep only 240 entries for each server,
83      * so we handle the object array ourselves.
84      * @param model
85      */

86     private List JavaDoc updateArray(MonitorModel model, List JavaDoc list){
87         if (list.size() < DEFAULT_BUFFER){
88             list.add(model);
89         } else {
90             list.add(model);
91             list.remove(0);
92         }
93         return list;
94     }
95     
96     /**
97      * Get all MonitorModels matching the URL.
98      * @param url
99      * @return list
100      */

101     public List JavaDoc getAllSamples(String JavaDoc url){
102         if (!MAP.containsKey(url)){
103             return Collections.synchronizedList(new LinkedList JavaDoc());
104         } else {
105             return (List JavaDoc)MAP.get(url);
106         }
107     }
108     
109     /**
110      * Get the MonitorModel matching the url.
111      * @param url
112      * @return list
113      */

114     public MonitorModel getSample(String JavaDoc url){
115         if (MAP.containsKey(url)){
116             ArrayList JavaDoc list = (ArrayList JavaDoc)MAP.get(url);
117             return (MonitorModel)list.get(0);
118         } else {
119             return null;
120         }
121     }
122     
123     /**
124      * Method will try to parse the response data. If the
125      * request was a monitor request, but the response was
126      * incomplete, bad or the server refused the connection,
127      * we will set the server's health to "dead". If the
128      * request was not a monitor sample, the method will
129      * ignore it.
130      * @param sample
131      */

132     public void addSample(SampleResult sample){
133         URL JavaDoc surl = null;
134         if (sample instanceof HTTPSampleResult){
135             surl = ((HTTPSampleResult)sample).getURL();
136         }
137         //String rescontent = new String(sample.getResponseData());
138
if (sample.getResponseCode().equals("200") &&
139             ((HTTPSampleResult)sample).isMonitor()){
140             ObjectFactory of = ObjectFactory.getInstance();
141             Status st = of.parseBytes(sample.getResponseData());
142             if (st != null){
143                 MonitorStats stat =
144                     new MonitorStats(Stats.calculateStatus(st),
145                         Stats.calculateLoad(st),
146                         0,
147                         Stats.calculateMemoryLoad(st),
148                         Stats.calculateThreadLoad(st),
149                         surl.getHost(),
150                         String.valueOf(surl.getPort()),
151                         surl.getProtocol(),
152                         System.currentTimeMillis());
153                 MonitorModel mo = new MonitorModel(stat);
154                 this.addSample(mo);
155                 notifyListeners(mo);
156             } else {
157                 noResponse(surl);
158             }
159         } else if (((HTTPSampleResult)sample).isMonitor() ){
160             noResponse(surl);
161         }
162     }
163
164     /**
165      * If there is no response from the server, we
166      * create a new MonitorStats object with the
167      * current timestamp and health "dead".
168      * @param url
169      */

170     public void noResponse(URL JavaDoc url){
171         notifyListeners(createNewMonitorModel(url));
172     }
173     
174     /**
175      * Method will return a new MonitorModel object
176      * with the given URL. This is used when the server
177      * fails to respond fully, or is dead.
178      * @param url
179      * @return new MonitorModel
180      */

181     public MonitorModel createNewMonitorModel(URL JavaDoc url){
182         MonitorStats stat = new MonitorStats(Stats.DEAD,
183             0,
184             0,
185             0,
186             0,
187             url.getHost(),
188             String.valueOf(url.getPort()),
189             url.getProtocol(),
190             System.currentTimeMillis());
191         MonitorModel mo = new MonitorModel(stat);
192         return mo;
193     }
194     
195     /**
196      * Clears everything except the listener. Do not
197      * clear the listeners. If we clear listeners,
198      * subsequent "run" will not notify the gui of
199      * data changes.
200      */

201     public void clear(){
202         Iterator JavaDoc itr = this.MAP.keySet().iterator();
203         while (itr.hasNext()){
204             List JavaDoc lt = (List JavaDoc)this.MAP.get(itr.next());
205             lt.clear();
206         }
207         this.MAP.clear();
208     }
209     
210     /**
211      * notify the listeners with the MonitorModel object.
212      * @param model
213      */

214     public void notifyListeners(MonitorModel model)
215     {
216         for (int idx=0; idx < listeners.size(); idx++){
217             MonitorListener ml = (MonitorListener)listeners.get(idx);
218             ml.addSample(model);
219         }
220     }
221     
222     /**
223      * Add a listener. When samples are added, the class will
224      * notify the listener of the change.
225      * @param listener
226      */

227     public void addListener(MonitorListener listener){
228         listeners.add(listener);
229     }
230 }
231
Popular Tags