Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/altara/mars/Host.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 org.jdom.*;
27  import javax.swing.tree.*;
28  
29  /** Represents a single host monitored by MARS. Each Host contains a
30    set of Services, which contain service specific information.
31  */
32  
33  public class Host implements java.io.Serializable, TreeNode {
34  
35    private MarsModel model;
36    private String name;
37    private InetAddress address;
38    private SortedMap services;
39    private LinkedList serviceListCache;
40  
41    public Host(MarsModel model, String nickname, InetAddress address) {
42      this.model = model;
43      this.name = nickname;
44      this.address = address;
45      this.services = new TreeMap();
46      updateServiceListCache();
47      model.addHost(this);
48    }
49    
50    public Host(MarsModel model, String nickname, String hostname)
51        throws UnknownHostException {
52      this(model,nickname,InetAddress.getByName(hostname));
53    }
54  
55    public Host(MarsModel model, String hostname)
56        throws UnknownHostException {
57      this(model,hostname,hostname);
58    }
59  
60    public void setName(String name) {
61      this.name = name;
62    }
63  
64    public void setAddress(InetAddress address) {
65      this.address = address;
66    }
67  
68    public void addService(Service service) {
69      services.put(service.getName(),service);
70      updateServiceListCache();
71      model.serviceListChanged(this);
72    }
73  
74    public void fireHostChanged() {
75      model.hostChanged(this);
76    }
77  
78    public MarsModel getModel() {
79      return model;
80    }
81  
82    public String getName() {
83      return name;
84    }
85  
86    public InetAddress getAddress() {
87      return address;
88    }
89  
90    public Service getService(String name) {
91      return (Service)services.get(name);
92    }
93  
94    public Iterator getServiceNames() {
95      return services.keySet().iterator();
96    }
97  
98    public Iterator getServices() {
99      return services.values().iterator();
100   }
101 
102   public void removeService(String name) {
103     services.remove(name);
104     updateServiceListCache();
105     model.serviceListChanged(this);
106   }
107 
108   public void removeService(Service service) {
109     removeService(service.getName());
110   }
111 
112   private void updateServiceListCache() {
113     serviceListCache = new LinkedList();
114     serviceListCache.addAll(services.values());
115   }
116 
117     public boolean isOK() {
118         Iterator svcIter = getServices();
119         while (svcIter.hasNext()) {
120             if (((Service)svcIter.next()).getStatus().isFault()) {
121                 return false;
122             }
123         }
124         return true;
125     }
126 
127   public String toString() {
128     return name+" at "+address.getHostName();
129   }
130 
131   /*------------------------------------------------------------
132     TreeNode implementation
133   ------------------------------------------------------------*/
134 
135   public boolean getAllowsChildren() {
136     return true;
137   }
138 
139   public int getChildCount() {
140     return serviceListCache.size();
141   }
142 
143   public boolean isLeaf() {
144     return (getChildCount() == 0);
145   }
146 
147   public TreeNode getParent() {
148     return model;
149   }
150 
151   public Enumeration children() {
152     return Collections.enumeration(serviceListCache);
153   }
154 
155   public TreeNode getChildAt(int childIndex) {
156     return (TreeNode)serviceListCache.get(childIndex);
157   }
158 
159   public int getIndex(TreeNode node) {
160     return serviceListCache.indexOf(node);
161   }
162 
163   /*------------------------------------------------------------
164     Cloning
165   ------------------------------------------------------------*/
166 
167   public Host duplicate() {
168     Host newHost = new Host(model, name+" [duplicate]", address);
169     Iterator services = getServices();
170     while (services.hasNext()) {
171       ((Service)services.next()).duplicate(newHost);
172     }
173     return newHost;
174   }
175 
176   /*------------------------------------------------------------
177     XML parsing/generation methods
178   ------------------------------------------------------------*/
179 
180   public Element toJDOMElem(boolean includeStatus) {
181     Element hostelem = new Element("host", MarsModel.NAMESPACE);
182     hostelem.setAttribute("name",getName());
183     hostelem.setAttribute("address",getAddress().getHostName());
184     // get this host's services
185     Iterator serviceNames = getServiceNames();
186     while (serviceNames.hasNext()) {
187       Service service = getService(((String)serviceNames.next()));
188       // create an element for this service and add it to the host
189       Element servelem = service.toJDOMElem(includeStatus);
190       hostelem.addContent(servelem);
191     }
192     return hostelem;
193   }
194 
195   public static Host fromJDOMElem(MarsModel model, Element in)
196       throws InvalidDocumentException, UnknownHostException {
197     // get host name and address from attributes
198     String hostName = in.getAttributeValue("name");
199     if (hostName == null)
200       throw new InvalidDocumentException("Missing host name");
201     String addressStr = in.getAttributeValue("address");
202     InetAddress address = InetAddress.getByName(addressStr);
203     // create the Host
204     Host out = new Host(model,hostName,address);
205     // find all this host's services and create them
206     Iterator svcElems =
207       in.getChildren("service",MarsModel.NAMESPACE).iterator();
208     while (svcElems.hasNext()) {
209       Element thisSvcElem = (Element)svcElems.next();
210       Service.fromJDOMElem(out,thisSvcElem);
211     }
212     // done, return the new host
213     return out;
214   }
215 }