001package org.apache.commons.jcs.utils.discovery;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.Serializable;
023import java.util.ArrayList;
024
025/**
026 * This contains info about a discovered service. These objects are stored in a set in the
027 * UDPDiscoveryService.
028 * <p>
029 * @author Aaron Smuts
030 */
031public class DiscoveredService
032    implements Serializable
033{
034    /** For serialization. Don't change. */
035    private static final long serialVersionUID = -7810164772089509751L;
036
037    /** region names */
038    private ArrayList<String> cacheNames;
039
040    /** service address */
041    private String serviceAddress;
042
043    /** service port */
044    private int servicePort;
045
046    /** last time we heard from this service? */
047    private long lastHearFromTime = 0;
048
049    /**
050     * @param cacheNames the cacheNames to set
051     */
052    public void setCacheNames( ArrayList<String> cacheNames )
053    {
054        this.cacheNames = cacheNames;
055    }
056
057    /**
058     * @return the cacheNames
059     */
060    public ArrayList<String> getCacheNames()
061    {
062        return cacheNames;
063    }
064
065    /**
066     * @param serviceAddress The serviceAddress to set.
067     */
068    public void setServiceAddress( String serviceAddress )
069    {
070        this.serviceAddress = serviceAddress;
071    }
072
073    /**
074     * @return Returns the serviceAddress.
075     */
076    public String getServiceAddress()
077    {
078        return serviceAddress;
079    }
080
081    /**
082     * @param servicePort The servicePort to set.
083     */
084    public void setServicePort( int servicePort )
085    {
086        this.servicePort = servicePort;
087    }
088
089    /**
090     * @return Returns the servicePort.
091     */
092    public int getServicePort()
093    {
094        return servicePort;
095    }
096
097    /**
098     * @param lastHearFromTime The lastHearFromTime to set.
099     */
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}