View Javadoc
1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo.cassandra;
22  
23  import com.datastax.driver.core.*;
24  import com.datastax.driver.core.policies.LoadBalancingPolicy;
25  import com.datastax.driver.core.policies.ReconnectionPolicy;
26  import com.datastax.driver.core.policies.RetryPolicy;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.synchronoss.cpo.AbstractDataSourceInfo;
30  import org.synchronoss.cpo.CpoException;
31  
32  import java.util.Collection;
33  
34  /**
35   * Created with IntelliJ IDEA.
36   * User: dberry
37   * Date: 9/10/13
38   * Time: 12:44 PM
39   * To change this template use File | Settings | File Templates.
40   */
41  public class ClusterDataSourceInfo extends AbstractDataSourceInfo<ClusterDataSource>{
42    private static final Logger logger = LoggerFactory.getLogger(ClusterDataSourceInfo.class);
43    private String[] contactPoints;
44    private String keySpace;
45    private String clusterName;
46    private Integer port;
47    private LoadBalancingPolicy loadBalancingPolicy;
48    private ReconnectionPolicy reconnectionPolicy;
49    private RetryPolicy retryPolicy;
50    private boolean credentials;
51    private String userName;
52    private String password;
53  //  private AuthProvider authProvider;
54    private ProtocolOptions.Compression compressionType;
55    private Boolean useMetrics;
56    private SSLOptions sslOptions;
57    private Collection<Host.StateListener> listeners;
58    private Boolean useJmxReporting;
59    private PoolingOptions poolingOptions;
60    private SocketOptions socketOptions;
61  //  private QueryOptions queryOptions;
62  
63    public ClusterDataSourceInfo(String clusterName, String keySpace, String[] contactPoints) {
64      super(buildDataSourceName(clusterName, keySpace, contactPoints));
65      this.keySpace=keySpace;
66      this.clusterName=clusterName;
67      this.contactPoints=contactPoints;
68    }
69  
70    public String getClusterName() {
71      return clusterName;
72    }
73  
74    public void setClusterName(String clusterName) {
75      this.clusterName = clusterName;
76    }
77  
78    public String getKeySpace() {
79      return keySpace;
80    }
81  
82    public int getPort() {
83      return port;
84    }
85  
86    public void setPort(int port) {
87      this.port = port;
88    }
89  
90    public LoadBalancingPolicy getLoadBalancingPolicy() {
91      return loadBalancingPolicy;
92    }
93  
94    public void setLoadBalancingPolicy(LoadBalancingPolicy loadBalancingPolicy) {
95      this.loadBalancingPolicy = loadBalancingPolicy;
96    }
97  
98    public ReconnectionPolicy getReconnectionPolicy() {
99      return reconnectionPolicy;
100   }
101 
102   public void setReconnectionPolicy(ReconnectionPolicy reconnectionPolicy) {
103     this.reconnectionPolicy = reconnectionPolicy;
104   }
105 
106   public RetryPolicy getRetryPolicy() {
107     return retryPolicy;
108   }
109 
110   public void setRetryPolicy(RetryPolicy retryPolicy) {
111     this.retryPolicy = retryPolicy;
112   }
113 
114   public boolean hasCredentials() {
115     return credentials;
116   }
117 
118   public void setHasCredentials(boolean credentials) {
119     this.credentials = credentials;
120   }
121 
122   public String getUserName() {
123     return userName;
124   }
125 
126   public void setUserName(String userName) {
127     this.userName = userName;
128   }
129 
130   public String getPassword() {
131     return password;
132   }
133 
134   public void setPassword(String password) {
135     this.password = password;
136   }
137 
138 //  public AuthProvider getAuthProvider() {
139 //    return authProvider;
140 //  }
141 //
142 //  public void setAuthProvider(AuthProvider authProvider) {
143 //    this.authProvider = authProvider;
144 //  }
145 
146   public ProtocolOptions.Compression getCompressionType() {
147     return compressionType;
148   }
149 
150   public void setCompressionType(ProtocolOptions.Compression compressionType) {
151     this.compressionType = compressionType;
152   }
153 
154   public Boolean getUseMetrics() {
155     return useMetrics;
156   }
157 
158   public void setUseMetrics(Boolean useMetrics) {
159     this.useMetrics = useMetrics;
160   }
161 
162   public SSLOptions getSslOptions() {
163     return sslOptions;
164   }
165 
166   public void setSslOptions(SSLOptions sslOptions) {
167     this.sslOptions = sslOptions;
168   }
169 
170   public Collection<Host.StateListener> getListeners() {
171     return listeners;
172   }
173 
174   public void setListeners(Collection<Host.StateListener> listeners) {
175     this.listeners = listeners;
176   }
177 
178   public Boolean getUseJmxReporting() {
179     return useJmxReporting;
180   }
181 
182   public void setUseJmxReporting(Boolean useJmxReporting) {
183     this.useJmxReporting = useJmxReporting;
184   }
185 
186   public PoolingOptions getPoolingOptions() {
187     return poolingOptions;
188   }
189 
190   public void setPoolingOptions(PoolingOptions poolingOptions) {
191     this.poolingOptions = poolingOptions;
192   }
193 
194   public SocketOptions getSocketOptions() {
195     return socketOptions;
196   }
197 
198   public void setSocketOptions(SocketOptions socketOptions) {
199     this.socketOptions = socketOptions;
200   }
201 
202 //  public QueryOptions getQueryOptions() {
203 //    return queryOptions;
204 //  }
205 //
206 //  public void setQueryOptions(QueryOptions queryOptions) {
207 //    this.queryOptions = queryOptions;
208 //  }
209 
210   @Override
211   protected ClusterDataSource createDataSource() throws CpoException {
212     Cluster.Builder clusterBuilder = Cluster.builder();
213 
214     // add the contact points
215     for(String s : contactPoints)
216       clusterBuilder.addContactPoint(s);
217 
218     // add clusterName
219 //      clusterBuilder.withClusterName(clusterName);
220 
221     // add port
222     if (port != null)
223       clusterBuilder.withPort(port);
224 
225     // add loadBalancing
226     if (loadBalancingPolicy != null)
227       clusterBuilder.withLoadBalancingPolicy(loadBalancingPolicy);
228 
229     // add reconnectionPolicy
230     if (reconnectionPolicy != null)
231       clusterBuilder.withReconnectionPolicy(reconnectionPolicy);
232 
233     // add retryPolicy
234     if (retryPolicy != null)
235       clusterBuilder.withRetryPolicy(retryPolicy);
236 
237     // add credentials
238     if (hasCredentials())
239       clusterBuilder.withCredentials(userName, password);
240 
241     // add AuthProvider
242 //    if (authProvider != null)
243 //      clusterBuilder.withAuthProvider(authProvider);
244 
245     // add Compression
246     if (compressionType != null)
247       clusterBuilder.withCompression(compressionType);
248 
249     // add Metrics
250     if (useMetrics != null && !useMetrics)
251       clusterBuilder.withoutMetrics();
252 
253     // add SSL
254     if (sslOptions != null)
255       clusterBuilder.withSSL(sslOptions);
256 
257     // add Listeners
258 //    if (listeners!=null && !listeners.isEmpty() && listeners.size()>0)
259 //      clusterBuilder.withInitialListeners(listeners);
260 
261     // add JMX Reporting
262     if (useJmxReporting != null && !useJmxReporting)
263       clusterBuilder.withoutJMXReporting();
264 
265     // add pooling options
266 //    if (poolingOptions != null)
267 //      clusterBuilder.withPoolingOptions(poolingOptions);
268 
269     // add socket options
270 //    if (socketOptions != null)
271 //      clusterBuilder.withSocketOptions(socketOptions);
272 
273     // add query options
274 //    if (queryOptions != null)
275 //      clusterBuilder.withQueryOptions(queryOptions);
276 
277     return new ClusterDataSource(clusterBuilder.build(), keySpace);
278   }
279 
280   private static String buildDataSourceName(String clusterName, String keySpace, String[] contactPoints) {
281     StringBuilder sb = new StringBuilder();
282     sb.append(clusterName);
283     sb.append(keySpace);
284     for (String s : contactPoints)
285       sb.append(s);
286     logger.debug("DatasourceName="+sb.toString());
287     return sb.toString();
288   }
289 
290 }