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.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  /***
23   * @author jmccrindle
24   */
25  public class DefaultRemoteHost implements RemoteHost {
26  
27      private String name = null;
28      private Map agentsMap = new HashMap();
29      
30      public DefaultRemoteHost() {
31          
32      }
33  
34      /***
35       * @param name
36       * @param agents
37       */
38      public DefaultRemoteHost(String name) {
39          this.name = name;
40      }
41  
42      /***
43       * @param name The name to set.
44       */
45      public void setName(String name) {
46          this.name = name;
47      }
48      
49      /* (non-Javadoc)
50       * @see org.jdiagnose.remote.RemoteHost#getName()
51       */
52      public String getName() {
53          return name;
54      }
55  
56      /* (non-Javadoc)
57       * @see org.jdiagnose.remote.RemoteHost#getAgents()
58       */
59      public Iterator getAgents() {
60          return agentsMap.values().iterator();
61      }
62  
63      /***
64       * @return Returns the agentsMap.
65       */
66      public Map getAgentsMap() {
67          return agentsMap;
68      }
69      
70      public RemoteAgentAtHost getAgentAtHost(String name) {
71          return (RemoteAgentAtHost) agentsMap.get(name);
72      }
73  
74      /***
75       * @param agentsMap The agentsMap to set.
76       */
77      public void setAgentsMap(Map agentsMap) {
78          this.agentsMap = agentsMap;
79      }
80  
81      /* (non-Javadoc)
82       * @see org.jdiagnose.remote.Monitorable#isUp()
83       */
84      public boolean isUp() {
85          for (Iterator agentIterator = agentsMap.values().iterator(); agentIterator.hasNext();) {
86              RemoteAgentAtHost remoteAgent = (RemoteAgentAtHost) agentIterator.next();
87              if(!remoteAgent.isUp()) return false;
88          }
89          return true;
90      }
91      
92      /* (non-Javadoc)
93       * @see java.lang.Object#toString()
94       */
95      public String toString() {
96          return "{" + name + ", " + agentsMap.toString() + "}";
97      }
98  
99      /* (non-Javadoc)
100      * @see org.jdiagnose.remote.RemoteHost#getAgent(java.lang.String)
101      */
102     public RemoteAgentAtHost getAgent(String agent) {
103         return (RemoteAgentAtHost) agentsMap.get(agent);
104     }
105     
106     public CompositeStatistics getCompositeStatistics() {
107         int total, up, down;
108         total = up = down = 0;
109         for (Iterator agentIterator = this.getAgents(); agentIterator.hasNext();) {
110             RemoteAgentAtHost agentAtHost = (RemoteAgentAtHost) agentIterator.next();
111             total++;
112             if(agentAtHost.isUp()) up++; else down++;
113         }
114         return new DefaultCompositeStatistics(total, up, down);
115     }
116 
117 }