View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.remote.system;
17  
18  import java.util.Iterator;
19  import java.util.Map;
20  
21  import org.jdiagnose.concurrent.ConcurrentHashMap;
22  
23  /***
24   * @author jmccrindle
25   */
26  public class DefaultRemoteAgentAtHost implements RemoteAgentAtHost {
27  
28      private String agent;
29      private String host;
30      private Map diagnosticsMap = new ConcurrentHashMap();
31  
32      /***
33       * @param host
34       * @param agent
35       */
36      public DefaultRemoteAgentAtHost(String host, String agent) {
37          this.host = host;
38          this.agent = agent;
39      }
40  
41      /* (non-Javadoc)
42       * @see org.jdiagnose.remote.RemoteAgentAtHost#getAgent()
43       */
44      public String getAgent() {
45          return agent;
46      }
47  
48      /* (non-Javadoc)
49       * @see org.jdiagnose.remote.RemoteAgentAtHost#getHost()
50       */
51      public String getHost() {
52          // TODO Auto-generated method stub
53          return host;
54      }
55  
56      /* (non-Javadoc)
57       * @see org.jdiagnose.remote.RemoteAgentAtHost#getDiagnostics()
58       */
59      public Iterator getDiagnostics() {
60          // TODO Auto-generated method stub
61          return diagnosticsMap.values().iterator();
62      }
63  
64      /***
65       * @param name
66       * @return
67       */
68      public RemoteDiagnostic getDiagnostic(String name) {
69          return (RemoteDiagnostic) diagnosticsMap.get(name);
70      }
71  
72      /***
73       * @return
74       */
75      public Map getDiagnosticMap() {
76          return diagnosticsMap;
77      }
78  
79      /* (non-Javadoc)
80       * @see org.jdiagnose.remote.Monitorable#isUp()
81       */
82      public boolean isUp() {
83          for (Iterator diagnosticIterator = diagnosticsMap.values().iterator(); diagnosticIterator.hasNext();) {
84              RemoteDiagnostic remoteDiagnostic = (RemoteDiagnostic) diagnosticIterator.next();
85              if(!remoteDiagnostic.isUp()) return false;
86          }
87          return true;
88      }
89      
90      public CompositeStatistics getCompositeStatistics() {
91          int total, up, down;
92          total = up = down = 0;
93          for (Iterator diagnosticIterator = this.getDiagnostics(); diagnosticIterator.hasNext();) {
94              Monitorable monitorable = (Monitorable) diagnosticIterator.next();
95              total++;
96              if(monitorable.isUp()) up++; else down++;
97          }
98          return new DefaultCompositeStatistics(total, up, down);
99      }
100 
101     /* (non-Javadoc)
102      * @see java.lang.Object#toString()
103      */
104     public String toString() {
105         return "{" + host + ", " + agent + ", " + diagnosticsMap.toString() + "}";
106     }
107 
108 }