1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org |
4 |
* Copyright (C) 2000-2002 i-scream |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
*/ |
20 |
|
21 |
//---PACKAGE DECLARATION--- |
22 |
package uk.org.iscream.cms.conient; |
23 |
|
24 |
//---IMPORTS--- |
25 |
import uk.org.iscream.cms.util.*; |
26 |
import java.util.HashMap; |
27 |
import java.util.List; |
28 |
import java.util.Collections; |
29 |
import javax.swing.ImageIcon; |
30 |
import javax.swing.JPanel; |
31 |
import javax.swing.BorderFactory; |
32 |
import java.awt.CardLayout; |
33 |
import javax.swing.JSplitPane; |
34 |
import java.util.StringTokenizer; |
35 |
import javax.swing.SwingUtilities; |
36 |
import javax.swing.DefaultListModel; |
37 |
import javax.swing.JList; |
38 |
import javax.swing.JScrollPane; |
39 |
import java.awt.Dimension; |
40 |
import javax.swing.event.ListSelectionListener; |
41 |
import javax.swing.event.ListSelectionEvent; |
42 |
import javax.swing.ListSelectionModel; |
43 |
|
44 |
/** |
45 |
* This thread reads data from the DataReader |
46 |
* it then asks the appropriate HostDisplayPanel |
47 |
* to update its data. |
48 |
* |
49 |
* @author $Author: tdb $ |
50 |
* @version $Id: DataPanel.java,v 1.30 2003/02/05 19:35:04 tdb Exp $ |
51 |
*/ |
52 |
public class DataPanel extends JSplitPane implements Runnable { |
53 |
|
54 |
//---FINAL ATTRIBUTES--- |
55 |
|
56 |
/** |
57 |
* The current CVS revision of this class |
58 |
*/ |
59 |
public final String REVISION = "$Revision: 1.30 $"; |
60 |
|
61 |
//---STATIC METHODS--- |
62 |
|
63 |
//---CONSTRUCTORS--- |
64 |
|
65 |
/** |
66 |
* Constructs the data panel |
67 |
*/ |
68 |
public DataPanel() { |
69 |
super(JSplitPane.HORIZONTAL_SPLIT); |
70 |
|
71 |
JScrollPane left = new JScrollPane(_hostChooserList); |
72 |
JScrollPane right = new JScrollPane(_displayPane); |
73 |
|
74 |
setOneTouchExpandable(true); |
75 |
setLeftComponent(left); |
76 |
setRightComponent(right); |
77 |
setDividerLocation(150); |
78 |
_xmlPacketMaker = new XMLPacketMaker(); |
79 |
} |
80 |
|
81 |
//---PUBLIC METHODS--- |
82 |
|
83 |
/** |
84 |
* Starts the DataPanel running |
85 |
*/ |
86 |
public void run() { |
87 |
// setup the host list we will be using |
88 |
refreshHostList(); |
89 |
if(Configuration.getInstance().getProperty("displayQueueInformation").equals("1")) { |
90 |
if (_qFrame == null) { |
91 |
_qFrame = new QueueFrame(); |
92 |
} |
93 |
if (!_qFrame.isVisible()) { |
94 |
_qFrame.setVisible(true); |
95 |
} |
96 |
} |
97 |
|
98 |
try { |
99 |
while(_running) { |
100 |
|
101 |
String xml = (String) _dataQueue.get(_myQueue); |
102 |
// if we want to debug the packets |
103 |
if(Configuration.getInstance().getProperty("packetDump").equals("1")) { |
104 |
System.out.println("[PACKET DUMP]\n" + xml); |
105 |
} |
106 |
|
107 |
Conient.setQueueStatus(_dataQueue.queueSize(_myQueue), _dataQueue.elementCount()); |
108 |
if (xml == null) { |
109 |
// shouldn't really happen...but not sure |
110 |
//_status.insert("No XML to update...",0); |
111 |
} else { |
112 |
|
113 |
// Use XMLPacketMaker to make an XMLPacket object. |
114 |
|
115 |
XMLPacket packet = _xmlPacketMaker.createXMLPacket(xml); |
116 |
|
117 |
String packetType = packet.getParam("packet.attributes.type"); |
118 |
if (packetType.equals("heartbeat") || packetType.equals("data")) { |
119 |
String hostName = packet.getParam("packet.attributes.machine_name"); |
120 |
// if we're not using a fixed list |
121 |
if(!_usingConfiguredList) { |
122 |
// if we don't know about this host |
123 |
// add it. |
124 |
if(!_hostList.containsKey(hostName)) { |
125 |
addHostPanel(hostName); |
126 |
// if we want to remember new hosts (ie, in "discovery" mode) |
127 |
if(_config.getProperty("hostDiscoveryMode").equals("1")) { |
128 |
addToKnownHosts(hostName); |
129 |
} |
130 |
} |
131 |
} |
132 |
// one final check that we know about the host |
133 |
// if we don't by now, then the server must have sent something odd |
134 |
if(_hostList.containsKey(hostName)) { |
135 |
if (!((HostDisplayPanel) _hostList.get(hostName)).updateHost(packet)) { |
136 |
//throw new Exception(hostName + " sent an invalid data packet stopping data update!"); |
137 |
Conient.addMessage("WARNING{data panel}: " + hostName + " sent an invalid data or heartbeat packet"); |
138 |
} |
139 |
} else { |
140 |
Conient.addMessage("WARNING{data panel}: server sent data for an unexpected host - " + hostName); |
141 |
} |
142 |
} else if (packetType.equals("queueStat")) { |
143 |
// check to config to see if we want queueStat packets to be processed or not |
144 |
if(Configuration.getInstance().getProperty("displayQueueInformation").equals("1") && _qFrame != null) { |
145 |
if (_qFrame.isVisible()) _qFrame.update(packet); |
146 |
} |
147 |
} else { |
148 |
Conient.addMessage("WARNING{data panel}: and unknown packet type was received - " + packetType); |
149 |
} |
150 |
} |
151 |
} |
152 |
} catch (Exception e) { |
153 |
Conient.addMessage("ERROR{data panel}: +" + e); |
154 |
e.printStackTrace(); |
155 |
} |
156 |
} |
157 |
|
158 |
/** |
159 |
* This method allows other classes |
160 |
* to shutdown this data panel. |
161 |
*/ |
162 |
public void shutdown() { |
163 |
_running = false; |
164 |
} |
165 |
|
166 |
//---PRIVATE METHODS--- |
167 |
|
168 |
/** |
169 |
* Build the host list according to the configuration. |
170 |
* If we're set to use one, it populates the display. |
171 |
* If we're using one and its empty, or if we're not using |
172 |
* one, we set that we're not so that we use all host we detect. |
173 |
* |
174 |
* See the run method for information on host discovery if we're |
175 |
* getting all the hosts. |
176 |
*/ |
177 |
private void refreshHostList() { |
178 |
// reset the list |
179 |
_hostList = new HashMap(); |
180 |
// check we're using a set list |
181 |
if (_config.getProperty("useHostList").equals("1")) { |
182 |
// if we are get it and set up the display |
183 |
String hostList = _config.getProperty("hostList"); |
184 |
if (!hostList.equals("")) { |
185 |
StringTokenizer st = new StringTokenizer(hostList, ";"); |
186 |
while(st.hasMoreTokens()) { |
187 |
String host = st.nextToken(); |
188 |
addHostPanel(host); |
189 |
} |
190 |
_usingConfiguredList = true; |
191 |
// if we've got no list setup, we set that we're taking all |
192 |
// hosts that might come in. |
193 |
} else { |
194 |
_usingConfiguredList = false; |
195 |
} |
196 |
// we're not using a list |
197 |
} else { |
198 |
_usingConfiguredList = false; |
199 |
} |
200 |
} |
201 |
|
202 |
/** |
203 |
* Adds a new Host to the hostList and adds its |
204 |
* display to the tabbed pane. |
205 |
* Ensures the host list is in alphabetical order. |
206 |
* |
207 |
* @param host the host to add |
208 |
*/ |
209 |
private void addHostPanel(String host) { |
210 |
HostDisplayPanel hostPanel = new HostDisplayPanel(host); |
211 |
SwingSafeAddCard task = new SwingSafeAddCard(_displayPane, hostPanel, host); |
212 |
SwingUtilities.invokeLater(task); |
213 |
_displayPane.add(hostPanel, host); |
214 |
_hostList.put(host, hostPanel); |
215 |
|
216 |
// we need to reconstruct the displayed host list, so its |
217 |
// sorted. |
218 |
|
219 |
_hostChooserList.setListData((new java.util.TreeSet(_hostList.keySet())).toArray()); |
220 |
|
221 |
Conient.addMessage("New Host added: " + host); |
222 |
} |
223 |
|
224 |
/** |
225 |
* If we are in "discovery" mode, we want to keep a list |
226 |
* of all new hosts we find, so we can add it to our list. |
227 |
* This method simply adds the host to the end of the |
228 |
* "knownHostList" property. |
229 |
* |
230 |
* @param host the host to add |
231 |
*/ |
232 |
private void addToKnownHosts(String host) { |
233 |
String knownHosts = _config.getProperty("knownHostsList"); |
234 |
if (knownHosts.indexOf(host) == -1) { |
235 |
_config.setProperty("knownHostsList", knownHosts + host + ";"); |
236 |
} |
237 |
} |
238 |
|
239 |
/** |
240 |
* Removes all the tabs on display |
241 |
* Used to tidy up when a new data |
242 |
* channel is opened. |
243 |
*/ |
244 |
public void cleanUpTabs() { |
245 |
int currloc = getDividerLocation(); |
246 |
_displayPaneLayout = new CardLayout(); |
247 |
_displayPane = new JPanel(_displayPaneLayout); |
248 |
JScrollPane right = new JScrollPane(_displayPane); |
249 |
setRightComponent(right); |
250 |
setDividerLocation(currloc); |
251 |
} |
252 |
|
253 |
//---ACCESSOR/MUTATOR METHODS--- |
254 |
|
255 |
/** |
256 |
* Assigns the queue that this panel |
257 |
* will use to obtain data |
258 |
* |
259 |
* @param queue the queue |
260 |
*/ |
261 |
public void setQueue(Queue queue) { |
262 |
_dataQueue = queue; |
263 |
_myQueue = _dataQueue.getQueue(); |
264 |
} |
265 |
|
266 |
//---ATTRIBUTES--- |
267 |
|
268 |
/** |
269 |
* The state of this thread. |
270 |
*/ |
271 |
boolean _running = true; |
272 |
|
273 |
/** |
274 |
* This card layout is used for the _displayPane |
275 |
* it allows the chooser list to select which |
276 |
* host to display |
277 |
*/ |
278 |
private CardLayout _displayPaneLayout = new CardLayout(); |
279 |
|
280 |
/** |
281 |
* The panel where HostDisplayPanel's |
282 |
* are placed, it uses a CardLayout |
283 |
*/ |
284 |
private JPanel _displayPane = new JPanel(_displayPaneLayout); |
285 |
|
286 |
/** |
287 |
* The host list allows the user to select which |
288 |
* host pane is topmost |
289 |
*/ |
290 |
private JList _hostChooserList = new JList(); |
291 |
{ |
292 |
_hostChooserList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
293 |
_hostChooserList.addListSelectionListener(new ListSelectionListener() { |
294 |
public void valueChanged(ListSelectionEvent e) { |
295 |
int selection = _hostChooserList.getSelectedIndex(); |
296 |
if (selection > -1) { |
297 |
_displayPaneLayout.show(_displayPane, (String) _hostChooserList.getSelectedValue()); |
298 |
} |
299 |
} |
300 |
}); |
301 |
} |
302 |
/** |
303 |
* The queue new data will be read from |
304 |
*/ |
305 |
private Queue _dataQueue; |
306 |
|
307 |
/** |
308 |
* Our queue number |
309 |
*/ |
310 |
int _myQueue; |
311 |
|
312 |
/** |
313 |
* An icon to represent a host |
314 |
*/ |
315 |
private ImageIcon _serverIcon = new ImageIcon(getClass().getResource("/resources/server.gif")); |
316 |
|
317 |
/** |
318 |
* A frame to display Queue information |
319 |
* may not always be used - loaded according to config |
320 |
*/ |
321 |
private QueueFrame _qFrame = null; |
322 |
|
323 |
/** |
324 |
* A reference to the configuraton object |
325 |
*/ |
326 |
private Configuration _config = Configuration.getInstance(); |
327 |
|
328 |
/** |
329 |
* Contains a list of hosts that the data panel will use |
330 |
* or build during its operation. |
331 |
*/ |
332 |
private HashMap _hostList = null; |
333 |
|
334 |
/** |
335 |
* If we should be using a configured list or just accepting |
336 |
* all the hosts we get |
337 |
*/ |
338 |
boolean _usingConfiguredList = false; |
339 |
|
340 |
private XMLPacketMaker _xmlPacketMaker; |
341 |
} |