Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAMES-1908 add support for multiple cassandra node ip in configuration
- Loading branch information
Showing
12 changed files
with
474 additions
and
38 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
...sandra/src/main/java/org/apache/james/backends/cassandra/init/CassandraNodeIpAndPort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/**************************************************************** | ||
* Licensed to the Apache Software Foundation (ASF) under one * | ||
* or more contributor license agreements. See the NOTICE file * | ||
* distributed with this work for additional information * | ||
* regarding copyright ownership. The ASF licenses this file * | ||
* to you under the Apache License, Version 2.0 (the * | ||
* "License"); you may not use this file except in compliance * | ||
* with the License. You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * | ||
* KIND, either express or implied. See the License for the * | ||
* specific language governing permissions and limitations * | ||
* under the License. * | ||
****************************************************************/ | ||
|
||
package org.apache.james.backends.cassandra.init; | ||
|
||
import java.util.List; | ||
|
||
import com.google.common.base.Objects; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.base.Splitter; | ||
|
||
public class CassandraNodeIpAndPort { | ||
public static final int DEFAULT_CASSANDRA_PORT = 9042; | ||
|
||
public static CassandraNodeIpAndPort parseConfString(String ipAndPort) { | ||
Preconditions.checkNotNull(ipAndPort); | ||
Preconditions.checkArgument(!ipAndPort.isEmpty()); | ||
|
||
List<String> parts = Splitter.on(':') | ||
.trimResults() | ||
.splitToList(ipAndPort); | ||
|
||
if (parts.size() < 1 || parts.size() > 2) { | ||
throw new IllegalArgumentException(ipAndPort + " is not a valid cassandra node"); | ||
} | ||
|
||
String ip = parts.get(0); | ||
int port = getPortFromConfPart(parts); | ||
|
||
return new CassandraNodeIpAndPort(ip, port); | ||
} | ||
|
||
private static int getPortFromConfPart(List<String> parts) { | ||
if (parts.size() == 2) { | ||
return Integer.valueOf(parts.get(1)); | ||
} else { | ||
return DEFAULT_CASSANDRA_PORT; | ||
} | ||
} | ||
|
||
private final String ip; | ||
private final int port; | ||
|
||
public CassandraNodeIpAndPort(String ip, int port) { | ||
this.ip = ip; | ||
this.port = port; | ||
} | ||
|
||
public CassandraNodeIpAndPort(String ip) { | ||
this(ip, DEFAULT_CASSANDRA_PORT); | ||
} | ||
|
||
public String getIp() { | ||
return ip; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(ip, port); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (object instanceof CassandraNodeIpAndPort) { | ||
CassandraNodeIpAndPort that = (CassandraNodeIpAndPort) object; | ||
return Objects.equal(this.ip, that.ip) && Objects.equal(this.port, that.port); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.ip + ":" + this.port; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...ra/src/test/java/org/apache/james/backends/cassandra/init/CassandraNodeIpAndPortTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/**************************************************************** | ||
* Licensed to the Apache Software Foundation (ASF) under one * | ||
* or more contributor license agreements. See the NOTICE file * | ||
* distributed with this work for additional information * | ||
* regarding copyright ownership. The ASF licenses this file * | ||
* to you under the Apache License, Version 2.0 (the * | ||
* "License"); you may not use this file except in compliance * | ||
* with the License. You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * | ||
* KIND, either express or implied. See the License for the * | ||
* specific language governing permissions and limitations * | ||
* under the License. * | ||
****************************************************************/ | ||
|
||
package org.apache.james.backends.cassandra.init; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
public class CassandraNodeIpAndPortTest { | ||
|
||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
@Test | ||
public void parseConfStringShouldParseConfWithIpAndPort() { | ||
//Given | ||
String ipAndPort = "142.145.254.111:44"; | ||
int expectedPort = 44; | ||
String expectedIp = "142.145.254.111"; | ||
|
||
//When | ||
CassandraNodeIpAndPort actual = CassandraNodeIpAndPort.parseConfString(ipAndPort); | ||
|
||
//Then | ||
assertThat(actual).isEqualTo(new CassandraNodeIpAndPort(expectedIp, expectedPort)); | ||
} | ||
|
||
@Test | ||
public void parseConfStringShouldParseConfWithIpOnly() { | ||
//Given | ||
String ipAndPort = "142.145.254.111"; | ||
int expectedPort = CassandraNodeIpAndPort.DEFAULT_CASSANDRA_PORT; | ||
String expectedIp = "142.145.254.111"; | ||
|
||
//When | ||
CassandraNodeIpAndPort actual = CassandraNodeIpAndPort.parseConfString(ipAndPort); | ||
|
||
//Then | ||
assertThat(actual).isEqualTo(new CassandraNodeIpAndPort(expectedIp, expectedPort)); | ||
} | ||
|
||
@Test | ||
public void parseConfStringShouldFailWhenConfigIsAnEmptyString() { | ||
expectedException.expect(IllegalArgumentException.class); | ||
|
||
//Given | ||
String ipAndPort = ""; | ||
|
||
//When | ||
CassandraNodeIpAndPort.parseConfString(ipAndPort); | ||
} | ||
|
||
@Test | ||
public void parseConfStringShouldFailWhenConfigIsANullString() { | ||
expectedException.expect(NullPointerException.class); | ||
|
||
//Given | ||
String ipAndPort = null; | ||
|
||
//When | ||
CassandraNodeIpAndPort.parseConfString(ipAndPort); | ||
} | ||
|
||
|
||
@Test | ||
public void parseConfStringShouldFailWhenConfigIsInvalid() { | ||
expectedException.expect(IllegalArgumentException.class); | ||
|
||
//Given | ||
String ipAndPort = "10.10.10.10:42:43"; | ||
|
||
//When | ||
CassandraNodeIpAndPort.parseConfString(ipAndPort); | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
dockerfiles/run/guice/cassandra/destination/conf/cassandra.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
server/container/guice/cassandra-guice/sample-configuration/cassandra.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# Configuration file for cassandra mailbox | ||
|
||
cassandra.ip=172.17.0.2 | ||
cassandra.port=9042 | ||
cassandra.nodes=172.17.0.2:9042 | ||
cassandra.keyspace=apache_james | ||
cassandra.replication.factor=1 |
31 changes: 31 additions & 0 deletions
31
...a-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/**************************************************************** | ||
* Licensed to the Apache Software Foundation (ASF) under one * | ||
* or more contributor license agreements. See the NOTICE file * | ||
* distributed with this work for additional information * | ||
* regarding copyright ownership. The ASF licenses this file * | ||
* to you under the Apache License, Version 2.0 (the * | ||
* "License"); you may not use this file except in compliance * | ||
* with the License. You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * | ||
* KIND, either express or implied. See the License for the * | ||
* specific language governing permissions and limitations * | ||
* under the License. * | ||
****************************************************************/ | ||
|
||
package org.apache.james.modules.mailbox; | ||
|
||
import java.io.FileNotFoundException; | ||
|
||
import org.apache.commons.configuration.ConfigurationException; | ||
import org.apache.commons.configuration.PropertiesConfiguration; | ||
|
||
public interface CassandraSessionConfiguration { | ||
|
||
PropertiesConfiguration getConfiguration() throws FileNotFoundException, ConfigurationException; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.