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 DefaultRemoteAgent implements RemoteAgent {
27  
28      private String name = null;
29      private Map hostsMap = new ConcurrentHashMap();
30      
31      /***
32       * @param agent
33       */
34      public DefaultRemoteAgent(String agent) {
35          this.name = agent;
36      }
37  
38      /* (non-Javadoc)
39       * @see org.jdiagnose.remote.RemoteAgent#getName()
40       */
41      public String getName() {
42          return name;
43      }
44  
45      /* (non-Javadoc)
46       * @see org.jdiagnose.remote.RemoteAgent#getHosts()
47       */
48      public Iterator getHosts() {
49          return hostsMap.values().iterator();
50      }
51      
52      public RemoteAgentAtHost getHost(String host) {
53          return (RemoteAgentAtHost) hostsMap.get(host);
54      }
55  
56      /***
57       * @return
58       */
59      public Map getHostsMap() {
60          return hostsMap;
61      }
62  
63      public boolean isUp() {
64          for (Iterator agentIterator = hostsMap.values().iterator(); agentIterator.hasNext();) {
65              RemoteAgentAtHost remoteAgent = (RemoteAgentAtHost) agentIterator.next();
66              if(!remoteAgent.isUp()) return false;
67          }
68          return true;
69      }
70      
71      public CompositeStatistics getCompositeStatistics() {
72          int total, up, down;
73          total = up = down = 0;
74          for (Iterator hostIterator = this.getHosts(); hostIterator.hasNext();) {
75              RemoteAgentAtHost agentAtHost = (RemoteAgentAtHost) hostIterator.next();
76              total++;
77              if(agentAtHost.isUp()) up++; else down++;
78          }
79          return new DefaultCompositeStatistics(total, up, down);
80      }
81      
82      /* (non-Javadoc)
83       * @see java.lang.Object#toString()
84       */
85      public String toString() {
86          return "{" + name + ", " + hostsMap.toString() + "}";
87      }
88  
89  }