Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #153 from camielza/master
Introduce hazelcast StateRepository
- Loading branch information
Showing
5 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
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,41 @@ | ||
<?xml version="1.0"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.togglz</groupId> | ||
<artifactId>togglz-project</artifactId> | ||
<version>2.3.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>togglz-hazelcast</artifactId> | ||
<name>Togglz - Hazelcast integration</name> | ||
<description>Togglz - Hazelcast integration</description> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.togglz</groupId> | ||
<artifactId>togglz-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.hazelcast</groupId> | ||
<artifactId>hazelcast</artifactId> | ||
<version>3.5.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>3.4</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
161 changes: 161 additions & 0 deletions
161
hazelcast/src/main/java/org/togglz/hazelcast/HazelcastStateRepository.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,161 @@ | ||
package org.togglz.hazelcast; | ||
|
||
import org.togglz.core.Feature; | ||
import org.togglz.core.repository.FeatureState; | ||
import org.togglz.core.repository.StateRepository; | ||
|
||
import com.hazelcast.config.Config; | ||
import com.hazelcast.core.Hazelcast; | ||
import com.hazelcast.core.HazelcastInstance; | ||
import com.hazelcast.core.IMap; | ||
|
||
/** | ||
* <p> | ||
* A state repository which stores the feature state in a Hazelcast distributed map. | ||
* </p> | ||
* | ||
* <p> | ||
* The class provides a builder which can be used to configure the Hazelcast instance and map to be used: | ||
* </p> | ||
* | ||
* <pre> | ||
* StateRepository repository = HazelcastStateRepository.newBuilder().mapName("my_map") | ||
* .config(hazelcastConfig).build(); | ||
* </pre> | ||
* | ||
* @author Camiel de Vleeschauwer | ||
*/ | ||
public class HazelcastStateRepository implements StateRepository { | ||
|
||
protected final HazelcastInstance hazelcastInstance; | ||
protected final Config hazelcastConfig; | ||
protected final String mapName; | ||
|
||
public HazelcastStateRepository(Config hazelcastConfig, String mapName) | ||
{ | ||
this.mapName = mapName; | ||
this.hazelcastConfig = hazelcastConfig; | ||
hazelcastInstance=createHazelcastInstance(); | ||
} | ||
|
||
private HazelcastStateRepository(Builder builder) { | ||
|
||
mapName = builder.mapName; | ||
hazelcastConfig = builder.hazelcastConfig; | ||
hazelcastInstance=createHazelcastInstance(); | ||
} | ||
|
||
private HazelcastInstance createHazelcastInstance() { | ||
if (hazelcastConfig != null) { | ||
return Hazelcast.newHazelcastInstance(hazelcastConfig); | ||
|
||
} else { | ||
return Hazelcast.newHazelcastInstance(); | ||
} | ||
} | ||
|
||
@Override | ||
public FeatureState getFeatureState(Feature feature) { | ||
|
||
IMap<Feature, FeatureState> map = hazelcastInstance.getMap(mapName); | ||
|
||
FeatureState featureState = map.get(feature); | ||
|
||
return featureState; | ||
|
||
} | ||
|
||
@Override | ||
public void setFeatureState(FeatureState featureState) { | ||
|
||
IMap<Feature, FeatureState> map = hazelcastInstance.getMap(mapName); | ||
|
||
map.set(featureState.getFeature(), featureState); | ||
} | ||
|
||
/** | ||
* Creates a new builder for creating a {@link HazelcastStateRepository}. | ||
* | ||
*/ | ||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
/** | ||
* Creates a new builder for creating a {@link HazelcastStateRepository}. | ||
* | ||
* @param mapName | ||
* the Hazelcast map name | ||
*/ | ||
public static Builder newBuilder(String mapName) { | ||
return new Builder(mapName); | ||
} | ||
|
||
/** | ||
* Builder for a {@link HazelcastStateRepository}. | ||
*/ | ||
public static class Builder { | ||
|
||
private String mapName = "togglz"; | ||
private Config hazelcastConfig = null; | ||
|
||
/** | ||
* Creates a new builder for a {@link HazelcastStateRepository}. | ||
* | ||
*/ | ||
public Builder() { | ||
} | ||
|
||
/** | ||
* Creates a new builder for a {@link HazelcastStateRepository}. | ||
* | ||
* @param mapName | ||
* the Hazelcast map name to use for feature state store | ||
*/ | ||
public Builder(String mapName) { | ||
this.mapName = mapName; | ||
} | ||
|
||
/** | ||
* Creates a new builder for a {@link HazelcastStateRepository}. | ||
* | ||
* @param hazelcastConfig | ||
* the Hazelcast configuration {@link Config} | ||
*/ | ||
public Builder(Config hazelcastConfig) { | ||
this.hazelcastConfig = hazelcastConfig; | ||
} | ||
|
||
/** | ||
* Sets the Hazelcast map name to use. | ||
* | ||
* @param mapName | ||
* the Hazelcast map name to use for feature state store | ||
*/ | ||
public Builder mapName(String mapName) { | ||
this.mapName = mapName; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the Hazelcast configuration. | ||
* | ||
* @param hazelcastConfig | ||
* the Hazelcast configuration {@link Config} | ||
*/ | ||
public Builder config(Config hazelcastConfig) { | ||
this.hazelcastConfig = hazelcastConfig; | ||
return this; | ||
} | ||
|
||
/** | ||
* Creates a new {@link HazelcastStateRepository} using the current | ||
* settings. | ||
*/ | ||
public HazelcastStateRepository build() { | ||
return new HazelcastStateRepository(this); | ||
} | ||
|
||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
hazelcast/src/test/java/org/togglz/hazelcast/HazelcastStateRepositoryTest.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,47 @@ | ||
package org.togglz.hazelcast; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.apache.commons.lang3.builder.EqualsBuilder; | ||
import org.junit.Test; | ||
import org.togglz.core.Feature; | ||
import org.togglz.core.repository.FeatureState; | ||
import org.togglz.core.repository.StateRepository; | ||
import org.togglz.core.util.NamedFeature; | ||
|
||
public class HazelcastStateRepositoryTest { | ||
|
||
private StateRepository stateRepository = HazelcastStateRepository.newBuilder().mapName("togglzMap").build(); | ||
|
||
@Test | ||
public void testSetFeatureStateNotExisitingInMap() { | ||
Feature feature = new NamedFeature("SAMPLE_FEATURE"); | ||
FeatureState featureState = new FeatureState(feature, true); | ||
stateRepository.setFeatureState(featureState); | ||
|
||
FeatureState storedFeatureState = stateRepository.getFeatureState(feature); | ||
|
||
assertTrue(EqualsBuilder.reflectionEquals(featureState, storedFeatureState, true)); | ||
|
||
} | ||
|
||
@Test | ||
public void testSetFeatureStateExistingInMap() { | ||
Feature feature = new NamedFeature("SAMPLE_FEATURE"); | ||
FeatureState featureState = new FeatureState(feature, true); | ||
stateRepository.setFeatureState(featureState); | ||
|
||
FeatureState storedFeatureState = stateRepository.getFeatureState(feature); | ||
assertTrue(storedFeatureState.isEnabled()); | ||
assertTrue(EqualsBuilder.reflectionEquals(featureState, storedFeatureState, true)); | ||
|
||
featureState.setEnabled(false); | ||
stateRepository.setFeatureState(featureState); | ||
storedFeatureState = stateRepository.getFeatureState(feature); | ||
assertFalse(storedFeatureState.isEnabled()); | ||
|
||
assertTrue(EqualsBuilder.reflectionEquals(featureState, storedFeatureState, true)); | ||
|
||
} | ||
} |
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,11 @@ | ||
Manifest-Version: 1.0 | ||
Bundle-ManifestVersion: 2 | ||
Bundle-SymbolicName: org.togglz.hazelcast | ||
Bundle-Vendor: http://www.togglz.org/ | ||
Bundle-Version: ${osgi.bundles.version} | ||
Bundle-Name: ${project.name} | ||
Version-Patterns: | ||
default;pattern="[=.=.=, =.+1.0)" | ||
Import-Template: | ||
org.togglz.core.*;version="${osgi.bundles.version:default}", | ||
com.hazelcast.*;version="[3.5.2, 4.0)" |
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