Source code: org/altara/mars/MarsModel.java
1 /* MARS Network Monitoring Engine
2 Copyright (C) 1999 Brian H. Trammell
3 Copyright (C) 2002 Leapfrog Research & Development, LLC
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, it is available at
17 http:///www.gnu.org/copyleft/gpl.html, or by writing to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20 */
21
22 package org.altara.mars;
23
24 import java.util.*;
25 import java.net.*;
26 import java.io.*;
27 import org.jdom.*;
28 import org.jdom.input.*;
29 import org.jdom.output.*;
30 import javax.swing.tree.*;
31
32 /** Represents the whole of the data model used by the
33 MARS monitoring engine. MarsModel is the root of the data model
34 tree containing hosts and services. MarsModel contains both
35 configuration and current service status information.
36 */
37
38 public class MarsModel implements java.io.Serializable, TreeNode {
39
40 public static final Namespace NAMESPACE = Namespace.getNamespace("mars",
41 "http://www.altara.org/mars/xmlns/model/");
42
43 private SortedMap hosts;
44 private LinkedList hostListCache;
45 private HashSet mmListeners;
46
47 public MarsModel() {
48 hosts = new TreeMap();
49 updateHostListCache();
50 mmListeners = new HashSet();
51 }
52
53 public void addHost(Host host) {
54 hosts.put(host.getName(),host);
55 updateHostListCache();
56 hostListChanged();
57 }
58
59 public Host getHost(String name) {
60 return (Host)hosts.get(name);
61 }
62
63 public Iterator getHostNames() {
64 return hosts.keySet().iterator();
65 }
66
67 public Iterator getHosts() {
68 return hosts.values().iterator();
69 }
70
71 public void removeHost(String name) {
72 hosts.remove(name);
73 updateHostListCache();
74 hostListChanged();
75 }
76
77 public void removeHost(Host host) {
78 removeHost(host.getName());
79 }
80
81 public Set getServiceSet() {
82 HashSet out = new HashSet();
83 Iterator hostIter = getHosts();
84 while (hostIter.hasNext()) {
85 Host host = (Host)hostIter.next();
86 Iterator serviceIter = host.getServices();
87 while (serviceIter.hasNext()) {
88 out.add(serviceIter.next());
89 }
90 }
91 return out;
92 }
93
94 private void updateHostListCache() {
95 hostListCache = new LinkedList();
96 hostListCache.addAll(hosts.values());
97 }
98
99 /*------------------------------------------------------------
100 Event Distribution
101 ------------------------------------------------------------*/
102
103 public void addMarsModelListener(MarsModelListener mml) {
104 mmListeners.add(mml);
105 }
106
107 public void removeMarsModelListener(MarsModelListener mml) {
108 mmListeners.remove(mml);
109 }
110
111 public void clearMarsModelListeners() {
112 mmListeners.clear();
113 }
114
115 public void hostChanged(Host host) {
116 // ignore irrelevant hosts
117 if (host.getModel() != this) return;
118 // notify all listeners
119 Iterator mmlIter = mmListeners.iterator();
120 while (mmlIter.hasNext()) {
121 ((MarsModelListener)mmlIter.next()).hostChanged(host);
122 }
123 }
124
125 public void serviceChanged(Service service) {
126 // ignore irrelevant services
127 if (service.getHost().getModel() != this) return;
128 // notify all listeners
129 Iterator mmlIter = mmListeners.iterator();
130 while (mmlIter.hasNext()) {
131 ((MarsModelListener)mmlIter.next()).serviceChanged(service);
132 }
133 }
134
135 public void hostListChanged() {
136 // notify all listeners
137 Iterator mmlIter = mmListeners.iterator();
138 while (mmlIter.hasNext()) {
139 ((MarsModelListener)mmlIter.next()).hostListChanged();
140 }
141 }
142
143 public void serviceListChanged(Host host) {
144 // ignore irrelevant hosts
145 if (host.getModel() != this) return;
146 // notify all listeners
147 Iterator mmlIter = mmListeners.iterator();
148 while (mmlIter.hasNext()) {
149 ((MarsModelListener)mmlIter.next()).serviceListChanged(host);
150 }
151 }
152
153 /*------------------------------------------------------------
154 TreeNode implementation
155 ------------------------------------------------------------*/
156
157 public boolean getAllowsChildren() {
158 return true;
159 }
160
161 public int getChildCount() {
162 return hostListCache.size();
163 }
164
165 public boolean isLeaf() {
166 return (getChildCount() == 0);
167 }
168
169 public TreeNode getParent() {
170 return null;
171 }
172
173 public Enumeration children() {
174 return Collections.enumeration(hostListCache);
175 }
176
177 public TreeNode getChildAt(int childIndex) {
178 return (TreeNode)hostListCache.get(childIndex);
179 }
180
181 public int getIndex(TreeNode node) {
182 return hostListCache.indexOf(node);
183 }
184
185 /*------------------------------------------------------------
186 XML parsing/generation methods
187 ------------------------------------------------------------*/
188
189 public Element toJDOMElem(boolean includeStatus) {
190 // create the root element
191 Element root = new Element("model", NAMESPACE);
192 // create the host-list child element
193 Element hostlist = new Element("hostlist", NAMESPACE);
194 root.addContent(hostlist);
195
196 // now run through the object tree depth first
197 Iterator hostNames = getHostNames();
198 while (hostNames.hasNext()) {
199 Host host = getHost(((String)hostNames.next()));
200 // create an element for this host and add it to the list
201 Element hostelem = host.toJDOMElem(includeStatus);
202 hostlist.addContent(hostelem);
203 }
204
205 // now put the root element into a document and return it
206 return root;
207 }
208
209 public static MarsModel fromJDOMElem(Element root)
210 throws InvalidDocumentException, UnknownHostException {
211 // First, create a new Model to attach everything to.
212 MarsModel out = new MarsModel();
213
214 // Verify the root element
215 if ((!root.getNamespace().equals(NAMESPACE)) ||
216 (!root.getName().equals("model")))
217 throw new InvalidDocumentException("Invalid model element");
218
219 // Get the first hostlist element within the root
220 Element hostlist = root.getChild("hostlist",NAMESPACE);
221 if (hostlist == null)
222 throw new InvalidDocumentException("Missing hostlist element");
223
224 // Now extract all the host elements from the hostlist
225 Iterator hostElems = hostlist.getChildren("host",NAMESPACE).iterator();
226 while (hostElems.hasNext()) {
227 Element thisHostElem = (Element)hostElems.next();
228 Host.fromJDOMElem(out, thisHostElem);
229 }
230 // the internal data structure is now built. enjoy!
231 return out;
232 }
233 }