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

Quick Search    Search Deep

Source code: org/altara/mars/Status.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.text.*;
25  import java.util.*;
26  import org.jdom.*;
27  
28  /** Represents the status of a given service monitored by MARS.
29    Status is a tuple of a status code and a detail string explaining
30    what return value from the server led the probe to decide on
31    the given status code.
32  */
33  
34  public class Status implements java.io.Serializable {
35  
36    private StatusCode code;
37    private HashMap properties;
38    private long timestamp;
39    private long responseTime;
40  
41    public Status (StatusCode code, long responseTime) {
42      this.code = code;
43      this.responseTime = responseTime;
44      this.timestamp = System.currentTimeMillis();
45      if (code == UNKNOWN) {
46        this.timestamp = -1;  // no check time for unknown status
47      }
48      this.properties = new HashMap();
49    }
50  
51    public Status (StatusCode code) {
52      this(code, -1);
53    }
54  
55    public StatusCode getCode() {
56      return code;
57    }
58  
59    public boolean isFault() {
60      return code.isFault();
61    }
62  
63    public long getTimestamp() {
64      return timestamp;
65    }
66  
67    public long getResponseTime() {
68      return responseTime;
69    }
70  
71    public String getProperty(String name) {
72      return (String)properties.get(name);
73    }
74  
75    public Iterator getPropertyNames() {
76      return properties.keySet().iterator();
77    }
78  
79    public void setProperty(String name, String value) {
80      properties.put(name,value);
81    }
82  
83    void setTimestamp(long timestamp) {
84      this.timestamp = timestamp;
85    }
86  
87    public String toString() {
88      if (getCode() == PROBEFAIL) return "probe error";
89      if (getCode() == DOWN) return "down";
90      if (getCode() == FASTCLOSE) return "closing";
91      if (getCode() == TIMEOUT) return "timed out";
92      if (getCode() == UNEXPECTED) return "bad reply";
93      if (getCode() == UP) return "up";
94      return "unknown";
95    }
96  
97    /*------------------------------------------------------------
98      XML parsing/generation methods
99    ------------------------------------------------------------*/
100 
101   public Element toJDOMElem() {
102     // build the status element
103     Element statelem = new Element("status", MarsModel.NAMESPACE);
104     statelem.setAttribute("code", String.valueOf(getCode().intValue()));
105     statelem.setAttribute("timestamp", String.valueOf(getTimestamp()));
106     statelem.setAttribute("resptime", String.valueOf(getResponseTime()));
107     // now build properties
108     Iterator propNames = getPropertyNames();
109     while (propNames.hasNext()) {
110       String propName = (String)propNames.next();
111       String propValue = getProperty(propName);
112       // create an element for this status property
113       Element propelem =
114         new Element("property", MarsModel.NAMESPACE);
115       propelem.setAttribute("name",propName);
116       propelem.addContent(propValue);
117       statelem.addContent(propelem);
118     }
119     
120     return statelem;
121   }
122 
123   public static Status fromJDOMElem(Element in)
124       throws InvalidDocumentException {
125     // Read status code, timestamp, and response time from element
126     String codeStr = in.getAttributeValue("code");
127     int code = -1;
128     String timestampStr = in.getAttributeValue("timestamp");
129     long timestamp = -1;
130     String resptimeStr = in.getAttributeValue("resptime");
131     long resptime = -1;
132     try {
133       code = Integer.parseInt(codeStr);
134     } catch (Exception ex) {
135       throw new InvalidDocumentException("Bad or missing code");
136     }
137     try {
138       timestamp = Long.parseLong(timestampStr);
139     } catch (Exception ex) {
140       throw new InvalidDocumentException("Bad or missing timestamp");
141     }
142     try {
143       resptime = Long.parseLong(resptimeStr);
144     } catch (Exception ex) {
145       throw new InvalidDocumentException("Bad or missing resptime");
146     }
147     Status out = new Status(STATUSCODES[code], resptime);
148     out.setTimestamp(timestamp);
149     // Now iterate over properties, adding them to the status
150     Iterator propElems =
151       in.getChildren("property",MarsModel.NAMESPACE).iterator();
152     while (propElems.hasNext()) {
153       Element thisPropElem = (Element)propElems.next();
154       String propName = thisPropElem.getAttributeValue("name");
155       if (propName == null)
156         throw new InvalidDocumentException("Missing property name");
157       String propValue = thisPropElem.getText();
158       if (propValue == null)
159         throw new InvalidDocumentException("Missing property value");
160       out.setProperty(propName,propValue);
161     }
162     // all done, return.
163     return out;
164   }
165 
166   /*------------------------------------------------------------
167     StatusCode enumerated type
168   ------------------------------------------------------------*/
169 
170   public static class StatusCode implements java.io.Serializable {
171     
172     private int intcode;
173     private StatusCode(int intcode) {
174       this.intcode = intcode;
175     }
176 
177     public int intValue() {
178       return intcode;
179     }
180 
181     public boolean isFault() {
182       return intcode <= MAX_SOFTFAULTCODE;
183     }
184   }
185 
186   public static int MAX_HARDFAULTCODE = 2;
187   public static int MAX_SOFTFAULTCODE = 4;
188 
189   public static final StatusCode PROBEFAIL = new StatusCode(0);
190   public static final StatusCode DOWN = new StatusCode(1);
191   public static final StatusCode FASTCLOSE = new StatusCode(2);
192   public static final StatusCode TIMEOUT = new StatusCode(3);
193   public static final StatusCode UNEXPECTED = new StatusCode(4);
194   public static final StatusCode UP = new StatusCode(5);
195   public static final StatusCode UNKNOWN = new StatusCode(6);
196   public static final StatusCode[] STATUSCODES =
197     { PROBEFAIL, DOWN, FASTCLOSE, TIMEOUT, UNEXPECTED, UP, UNKNOWN };
198 }