001 /**
002 * Copyright 2008 Mike Kroutikov.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the Lesser GNU General Public License as
006 * published by the Free Software Foundation; either version 3 of
007 * the License, or (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 * Lesser GNU General Public License for more details.
013 *
014 * You should have received a copy of the Lesser GNU General Public License
015 * along with this program. If not, see <http://www.gnu.org/licenses/>.
016 */
017 package org.otfeed;
018
019 import java.io.Serializable;
020 import java.util.Arrays;
021 import java.util.Set;
022 import java.util.TreeSet;
023
024 import org.otfeed.event.OTHost;
025
026 /**
027 * Connection Specification.
028 * Encapsulates parameters of a connection to the Opentick server.
029 * This includes username/password, connection addresses, et al.
030 *
031 * This object has value semantics, i.e. it compares to true iff
032 * all its properties compare to true.
033 *
034 * Note that it does deep copy of Hosts and re-orders them according to the
035 * natural order. Therefore, order in which host set was populated does not matter
036 * for comparison purposes.
037 */
038 public class OTConnectionSpec implements Serializable {
039 private static final long serialVersionUID = 6180103675354310662L;
040
041 public static final long DEFAULT_HEARTBEAT_INTERVAL = 10000; // 10 seconds
042
043 private String username;
044 private String password;
045 private Set<OTHost> hosts = new TreeSet<OTHost>();
046 private Long heartbeatInterval = new Long(DEFAULT_HEARTBEAT_INTERVAL);
047
048 /**
049 * Creates empty connection specification.
050 */
051 public OTConnectionSpec() {}
052
053 /**
054 * Creates connection specification by cloning the model one.
055 *
056 * @param other model specs.
057 */
058 public OTConnectionSpec(OTConnectionSpec other) {
059 setUsername(other.getUsername());
060 setPassword(other.getPassword());
061 setHosts(other.getHosts()); // remember, this does deep copy!
062 setHeartbeatInterval(other.getHeartbeatInterval());
063 }
064
065 /**
066 * Username for accessing Opentick data.
067 *
068 * @return username.
069 */
070 public String getUsername() {
071 return username;
072 }
073
074 /**
075 * Sets the user name of the Opentick account.
076 *
077 * @param username name
078 */
079 public void setUsername(String username) {
080 this.username = username;
081 }
082
083 /**
084 * Password for accessing Opentick datat.
085 *
086 * @return password
087 */
088 public String getPassword() {
089 return password;
090 }
091
092 /**
093 * Sets the password of the Opentick account.
094 *
095 * @param password
096 */
097 public void setPassword(String password) {
098 this.password = password;
099 }
100
101 /**
102 * Set of hosts to connect.
103 *
104 * @return hosts.
105 */
106 public Set<OTHost> getHosts() {
107 return hosts;
108 }
109
110 /**
111 * Sets hosts to connect. Does deep copy.
112 *
113 * @param val
114 */
115 public void setHosts(Set<OTHost> val) {
116 if(val == null) {
117 this.hosts = null;
118 } else {
119 this.hosts = new TreeSet<OTHost>(val);
120 }
121 }
122
123 /**
124 * Provides convenient access to {@link #getHosts()} property:
125 * allows user to set/get it as a string value
126 * - a comma-separated list of host addresses.
127 * <p>
128 * For example:
129 * <pre>
130 * spec.setHostsString("feed1.opentick.com:10015,feed2.opentick.com:10010");
131 * </pre>
132 *
133 * @return string representation of <code>hosts</code> property.
134 */
135 public String getHostsString() {
136 if(hosts == null || hosts.isEmpty()) return null;
137 StringBuilder out = new StringBuilder();
138 boolean first = true;
139 for(OTHost h : hosts) {
140 if(first) first = false;
141 else out.append(",");
142 out.append(h.toString());
143 }
144 return out.toString();
145 }
146
147 /**
148 * Sets {@link #setHosts} property by parsing comma-separated list of
149 * host addresses.
150 *
151 * @param val value to parse.
152 * @throws IllegalArgumentException if can not parse spring.
153 */
154 public void setHostsString(String val) throws IllegalArgumentException {
155 if(hosts == null) {
156 hosts = new TreeSet<OTHost>();
157 } else {
158 hosts.clear();
159 }
160 if(val == null || val.trim().length() == 0) return;
161 for(String h : val.split(",")) {
162 hosts.add(new OTHost(h.trim()));
163 }
164 }
165
166 /**
167 * Heartbeat interval in millis.
168 *
169 * @return heartbeat interval value.
170 */
171 public Long getHeartbeatInterval() {
172 return heartbeatInterval;
173 }
174
175 /**
176 * Sets the heartbeat interval (in millis).
177 * Default is 10000, which is 10 seconds.
178 *
179 * @param heartbeatInterval new heartbeat interval.
180 */
181 public void setHeartbeatInterval(Long heartbeatInterval) {
182 this.heartbeatInterval = heartbeatInterval;
183 }
184
185 /**
186 * {@inheritDoc}
187 */
188 @Override
189 public int hashCode() {
190 final int prime = 31;
191 int result = 1;
192
193 result = prime * result + getClass().hashCode();
194 result = prime * result
195 + ((hosts == null) ? 0 : Arrays.hashCode(hosts.toArray()));
196 result = prime
197 * result
198 + ((heartbeatInterval == null) ? 0 : heartbeatInterval
199 .hashCode());
200 result = prime * result
201 + ((password == null) ? 0 : password.hashCode());
202 result = prime * result
203 + ((username == null) ? 0 : username.hashCode());
204 return result;
205 }
206
207 /**
208 * {@inheritDoc}
209 */
210 @Override
211 public boolean equals(Object obj) {
212 if (this == obj)
213 return true;
214 if (obj == null)
215 return false;
216 if (getClass() != obj.getClass())
217 return false;
218 final OTConnectionSpec other = (OTConnectionSpec) obj;
219 if (hosts == null) {
220 if (other.hosts != null)
221 return false;
222 } else if (!Arrays.equals(hosts.toArray(), other.hosts.toArray()))
223 return false;
224 if (heartbeatInterval == null) {
225 if (other.heartbeatInterval != null)
226 return false;
227 } else if (!heartbeatInterval.equals(other.heartbeatInterval))
228 return false;
229 if (password == null) {
230 if (other.password != null)
231 return false;
232 } else if (!password.equals(other.password))
233 return false;
234 if (username == null) {
235 if (other.username != null)
236 return false;
237 } else if (!username.equals(other.username))
238 return false;
239 return true;
240 }
241
242 public String toString() {
243 return getClass().getSimpleName() + "{username=" + username
244 + ", password=" + (password == null ? "null" : "*****")
245 + ", hosts=" + (hosts == null ? "null" : Arrays.toString(hosts.toArray()))
246 + ", heartbeatInteval=" + heartbeatInterval
247 + "}";
248 }
249 }