001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jmeter.visualizers;
018:
019: import java.awt.Dimension;
020: import java.awt.FlowLayout;
021:
022: import javax.swing.ImageIcon;
023: import javax.swing.JPanel;
024: import javax.swing.JLabel;
025: import org.apache.jmeter.util.JMeterUtils;
026: import org.apache.jmeter.monitor.util.Stats;
027:
028: /**
029: * The purpose of ServerPanel is to display an unique server and its current
030: * status. The server label consist of the protocol, host and port. For example,
031: * a system with multiple Tomcat's running on different ports would be different
032: * ServerPanel.
033: */
034: public class ServerPanel extends JPanel implements MonitorGuiListener {
035:
036: private JLabel serverField;
037:
038: private JLabel timestampField;
039:
040: /**
041: * Preference size for the health icon
042: */
043: private Dimension prefsize = new Dimension(25, 75);
044:
045: private JLabel healthIcon;
046:
047: private JLabel loadIcon;
048:
049: /**
050: * Health Icons
051: */
052: public static final ImageIcon HEALTHY = JMeterUtils
053: .getImage("monitor-healthy.gif");
054:
055: public static final ImageIcon ACTIVE = JMeterUtils
056: .getImage("monitor-active.gif");
057:
058: public static final ImageIcon WARNING = JMeterUtils
059: .getImage("monitor-warning.gif");
060:
061: public static final ImageIcon DEAD = JMeterUtils
062: .getImage("monitor-dead.gif");
063:
064: /**
065: * Load Icons
066: */
067: public static final ImageIcon LOAD_1 = JMeterUtils
068: .getImage("monitor-load-1.gif");
069:
070: public static final ImageIcon LOAD_2 = JMeterUtils
071: .getImage("monitor-load-2.gif");
072:
073: public static final ImageIcon LOAD_3 = JMeterUtils
074: .getImage("monitor-load-3.gif");
075:
076: public static final ImageIcon LOAD_4 = JMeterUtils
077: .getImage("monitor-load-4.gif");
078:
079: public static final ImageIcon LOAD_5 = JMeterUtils
080: .getImage("monitor-load-5.gif");
081:
082: public static final ImageIcon LOAD_6 = JMeterUtils
083: .getImage("monitor-load-6.gif");
084:
085: public static final ImageIcon LOAD_7 = JMeterUtils
086: .getImage("monitor-load-7.gif");
087:
088: public static final ImageIcon LOAD_8 = JMeterUtils
089: .getImage("monitor-load-8.gif");
090:
091: public static final ImageIcon LOAD_9 = JMeterUtils
092: .getImage("monitor-load-9.gif");
093:
094: public static final ImageIcon LOAD_10 = JMeterUtils
095: .getImage("monitor-load-10.gif");
096:
097: // private MonitorModel DATA;
098:
099: /**
100: *
101: */
102: public ServerPanel(MonitorModel model) {
103: super ();
104: // DATA = model;
105: init(model);
106: }
107:
108: /**
109: *
110: * @deprecated Only for use in unit testing
111: */
112: public ServerPanel() {
113: // log.warn("Only for use in unit testing");
114: }
115:
116: /**
117: * Init will create the JLabel widgets for the host, health, load and
118: * timestamp.
119: *
120: * @param model
121: */
122: private void init(MonitorModel model) {
123: this .setLayout(new FlowLayout());
124: serverField = new JLabel(model.getURL());
125: this .add(serverField);
126: healthIcon = new JLabel(getHealthyImageIcon(model.getHealth()));
127: healthIcon.setPreferredSize(prefsize);
128: this .add(healthIcon);
129: loadIcon = new JLabel(getLoadImageIcon(model.getLoad()));
130: this .add(loadIcon);
131: timestampField = new JLabel(model.getTimestampString());
132: this .add(timestampField);
133: }
134:
135: /**
136: * Static method for getting the right ImageIcon for the health.
137: *
138: * @param health
139: * @return image for the status
140: */
141: public static ImageIcon getHealthyImageIcon(int health) {
142: ImageIcon i = null;
143: switch (health) {
144: case Stats.HEALTHY:
145: i = HEALTHY;
146: break;
147: case Stats.ACTIVE:
148: i = ACTIVE;
149: break;
150: case Stats.WARNING:
151: i = WARNING;
152: break;
153: case Stats.DEAD:
154: i = DEAD;
155: break;
156: }
157: return i;
158: }
159:
160: /**
161: * Static method looks up the right ImageIcon from the load value.
162: *
163: * @param load
164: * @return image for the load
165: */
166: public static ImageIcon getLoadImageIcon(int load) {
167: if (load <= 10) {
168: return LOAD_1;
169: } else if (load > 10 && load <= 20) {
170: return LOAD_2;
171: } else if (load > 20 && load <= 30) {
172: return LOAD_3;
173: } else if (load > 30 && load <= 40) {
174: return LOAD_4;
175: } else if (load > 40 && load <= 50) {
176: return LOAD_5;
177: } else if (load > 50 && load <= 60) {
178: return LOAD_6;
179: } else if (load > 60 && load <= 70) {
180: return LOAD_7;
181: } else if (load > 70 && load <= 80) {
182: return LOAD_8;
183: } else if (load > 80 && load <= 90) {
184: return LOAD_9;
185: } else {
186: return LOAD_10;
187: }
188: }
189:
190: /**
191: * Method will update the ServerPanel's health, load, and timestamp. For
192: * efficiency, it uses the static method to lookup the images.
193: */
194: public void updateGui(MonitorModel stat) {
195: // this.DATA = null;
196: // this.DATA = stat;
197: loadIcon.setIcon(getLoadImageIcon(stat.getLoad()));
198: healthIcon.setIcon(getHealthyImageIcon(stat.getHealth()));
199: timestampField.setText(stat.getTimestampString());
200: this .updateGui();
201: }
202:
203: /**
204: * update the gui
205: */
206: public void updateGui() {
207: this.repaint();
208: }
209: }
|