Skip to content

Commit

Permalink
Merge pull request #153 from camielza/master
Browse files Browse the repository at this point in the history
Introduce hazelcast StateRepository
  • Loading branch information
chkal committed Feb 12, 2016
2 parents 79bf213 + 9e9e2e8 commit 2f89edc
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 0 deletions.
41 changes: 41 additions & 0 deletions hazelcast/pom.xml
@@ -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>
@@ -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(&quot;my_map&quot;)
* .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);
}

}

}
@@ -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));

}
}
11 changes: 11 additions & 0 deletions hazelcast/template.mf
@@ -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)"
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -186,6 +186,7 @@
<module>junit</module>
<module>distribution</module>
<module>cassandra</module>
<module>hazelcast</module>
</modules>

<developers>
Expand Down

0 comments on commit 2f89edc

Please sign in to comment.