View Javadoc
1   package org.apache.commons.jcs3.utils.discovery;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.Objects;
25  
26  /**
27   * This contains info about a discovered service. These objects are stored in a set in the
28   * UDPDiscoveryService.
29   */
30  public class DiscoveredService
31      implements Serializable
32  {
33      /** For serialization. Don't change. */
34      private static final long serialVersionUID = -7810164772089509751L;
35  
36      /** region names */
37      private ArrayList<String> cacheNames;
38  
39      /** service address */
40      private String serviceAddress;
41  
42      /** service port */
43      private int servicePort;
44  
45      /** last time we heard from this service? */
46      private long lastHearFromTime;
47  
48      /**
49       * Default constructor
50       */
51      public DiscoveredService()
52      {
53          // empty
54      }
55  
56      /**
57       * Constructor
58       *
59       * @param message incoming message
60       * @since 3.1
61       */
62      public DiscoveredService(UDPDiscoveryMessage message)
63      {
64          setServiceAddress( message.getHost() );
65          setCacheNames( message.getCacheNames() );
66          setServicePort( message.getPort() );
67          setLastHearFromTime( System.currentTimeMillis() );
68      }
69  
70      /**
71       * @param cacheNames the cacheNames to set
72       */
73      public void setCacheNames( final ArrayList<String> cacheNames )
74      {
75          this.cacheNames = cacheNames;
76      }
77  
78      /**
79       * @return the cacheNames
80       */
81      public ArrayList<String> getCacheNames()
82      {
83          return cacheNames;
84      }
85  
86      /**
87       * @param serviceAddress The serviceAddress to set.
88       */
89      public void setServiceAddress( final String serviceAddress )
90      {
91          this.serviceAddress = serviceAddress;
92      }
93  
94      /**
95       * @return Returns the serviceAddress.
96       */
97      public String getServiceAddress()
98      {
99          return serviceAddress;
100     }
101 
102     /**
103      * @param servicePort The servicePort to set.
104      */
105     public void setServicePort( final int servicePort )
106     {
107         this.servicePort = servicePort;
108     }
109 
110     /**
111      * @return Returns the servicePort.
112      */
113     public int getServicePort()
114     {
115         return servicePort;
116     }
117 
118     /**
119      * @param lastHearFromTime The lastHearFromTime to set.
120      */
121     public void setLastHearFromTime( final long lastHearFromTime )
122     {
123         this.lastHearFromTime = lastHearFromTime;
124     }
125 
126     /**
127      * @return Returns the lastHearFromTime.
128      */
129     public long getLastHearFromTime()
130     {
131         return lastHearFromTime;
132     }
133 
134     /** @return hash code based on address/port */
135 	@Override
136 	public int hashCode()
137 	{
138 		return Objects.hash(serviceAddress, servicePort);
139 	}
140 
141 	/**
142      * NOTE - this object is often put into sets, so equals needs to be overridden.
143      * <p>
144      * We can't use cache names as part of the equals unless we manually only use the address and
145      * port in a contains check. So that we can use normal set functionality, I've kept the cache
146      * names out.
147      * <p>
148      * @param otherArg other
149      * @return equality based on the address/port
150      */
151 	@Override
152 	public boolean equals(final Object otherArg)
153 	{
154 		if (this == otherArg)
155 		{
156 			return true;
157 		}
158 		if (otherArg == null)
159 		{
160 			return false;
161 		}
162 		if (!(otherArg instanceof DiscoveredService))
163 		{
164 			return false;
165 		}
166 		final DiscoveredService other = (DiscoveredService) otherArg;
167 		if (!Objects.equals(serviceAddress, other.serviceAddress))
168 		{
169 			return false;
170 		}
171         return servicePort == other.servicePort;
172     }
173 
174     /**
175      * @return string for debugging purposes.
176      */
177     @Override
178     public String toString()
179     {
180         final StringBuilder buf = new StringBuilder();
181         buf.append( "\n DiscoveredService" );
182         buf.append( "\n CacheNames = [" + getCacheNames() + "]" );
183         buf.append( "\n ServiceAddress = [" + getServiceAddress() + "]" );
184         buf.append( "\n ServicePort = [" + getServicePort() + "]" );
185         buf.append( "\n LastHearFromTime = [" + getLastHearFromTime() + "]" );
186         return buf.toString();
187     }
188 }