View Javadoc
1   package org.apache.commons.jcs.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  
25  /**
26   * This contains info about a discovered service. These objects are stored in a set in the
27   * UDPDiscoveryService.
28   * <p>
29   * @author Aaron Smuts
30   */
31  public class DiscoveredService
32      implements Serializable
33  {
34      /** For serialization. Don't change. */
35      private static final long serialVersionUID = -7810164772089509751L;
36  
37      /** region names */
38      private ArrayList<String> cacheNames;
39  
40      /** service address */
41      private String serviceAddress;
42  
43      /** service port */
44      private int servicePort;
45  
46      /** last time we heard from this service? */
47      private long lastHearFromTime = 0;
48  
49      /**
50       * @param cacheNames the cacheNames to set
51       */
52      public void setCacheNames( ArrayList<String> cacheNames )
53      {
54          this.cacheNames = cacheNames;
55      }
56  
57      /**
58       * @return the cacheNames
59       */
60      public ArrayList<String> getCacheNames()
61      {
62          return cacheNames;
63      }
64  
65      /**
66       * @param serviceAddress The serviceAddress to set.
67       */
68      public void setServiceAddress( String serviceAddress )
69      {
70          this.serviceAddress = serviceAddress;
71      }
72  
73      /**
74       * @return Returns the serviceAddress.
75       */
76      public String getServiceAddress()
77      {
78          return serviceAddress;
79      }
80  
81      /**
82       * @param servicePort The servicePort to set.
83       */
84      public void setServicePort( int servicePort )
85      {
86          this.servicePort = servicePort;
87      }
88  
89      /**
90       * @return Returns the servicePort.
91       */
92      public int getServicePort()
93      {
94          return servicePort;
95      }
96  
97      /**
98       * @param lastHearFromTime The lastHearFromTime to set.
99       */
100     public void setLastHearFromTime( long lastHearFromTime )
101     {
102         this.lastHearFromTime = lastHearFromTime;
103     }
104 
105     /**
106      * @return Returns the lastHearFromTime.
107      */
108     public long getLastHearFromTime()
109     {
110         return lastHearFromTime;
111     }
112 
113     /** @return hashcode based on address/port */
114 	@Override
115 	public int hashCode()
116 	{
117 		final int prime = 31;
118 		int result = 1;
119 		result = prime * result
120 				+ ((serviceAddress == null) ? 0 : serviceAddress.hashCode());
121 		result = prime * result + servicePort;
122 		return result;
123 	}
124 
125 	/**
126      * NOTE - this object is often put into sets, so equals needs to be overridden.
127      * <p>
128      * We can't use cache names as part of the equals unless we manually only use the address and
129      * port in a contains check. So that we can use normal set functionality, I've kept the cache
130      * names out.
131      * <p>
132      * @param otherArg other
133      * @return equality based on the address/port
134      */
135 	@Override
136 	public boolean equals(Object otherArg)
137 	{
138 		if (this == otherArg)
139 		{
140 			return true;
141 		}
142 		if (otherArg == null)
143 		{
144 			return false;
145 		}
146 		if (!(otherArg instanceof DiscoveredService))
147 		{
148 			return false;
149 		}
150 		DiscoveredService other = (DiscoveredService) otherArg;
151 		if (serviceAddress == null)
152 		{
153 			if (other.serviceAddress != null)
154 			{
155 				return false;
156 			}
157 		} else if (!serviceAddress.equals(other.serviceAddress))
158 		{
159 			return false;
160 		}
161 		if (servicePort != other.servicePort)
162 		{
163 			return false;
164 		}
165 
166 		return true;
167 	}
168 
169     /**
170      * @return string for debugging purposes.
171      */
172     @Override
173     public String toString()
174     {
175         StringBuilder buf = new StringBuilder();
176         buf.append( "\n DiscoveredService" );
177         buf.append( "\n CacheNames = [" + getCacheNames() + "]" );
178         buf.append( "\n ServiceAddress = [" + getServiceAddress() + "]" );
179         buf.append( "\n ServicePort = [" + getServicePort() + "]" );
180         buf.append( "\n LastHearFromTime = [" + getLastHearFromTime() + "]" );
181         return buf.toString();
182     }
183 }