Package org.togglz.core.repository.jdbc
Class JDBCStateRepository
- java.lang.Object
-
- org.togglz.core.repository.jdbc.JDBCStateRepository
-
- All Implemented Interfaces:
StateRepository
public class JDBCStateRepository extends Object implements StateRepository
This repository implementation can be used to store the feature state in SQL database using the standard JDBC API.
JDBCStateRepositorystores the feature state in a single database table. You can choose the name of this table using a constructor argument. If the repository doesn't find the required table in the database, it will automatically create it.The database table has the following format:
CREATE TABLE <table> ( FEATURE_NAME VARCHAR(100) PRIMARY KEY, FEATURE_ENABLED INTEGER, STRATEGY_ID VARCHAR(200), STRATEGY_PARAMS VARCHAR(2000) )
The class provides a builder which can be used to configure the repository:
StateRepository repository = JDBCStateRepository.newBuilder(dataSource) .tableName("features") .createTable(false) .serializer(DefaultMapSerializer.singleline()) .noCommit(true) .build();Please note that the structure of the database table changed with version 2.0.0 because of the new extensible activation strategy mechanism. The table structure will be automatically migrated to the new format.
- Author:
- Christian Kaltepoth
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJDBCStateRepository.BuilderBuilder for aJDBCStateRepository.
-
Field Summary
Fields Modifier and Type Field Description protected DataSourcedataSourceprotected booleannoCommitprotected MapSerializerserializerprotected StringtableName
-
Constructor Summary
Constructors Constructor Description JDBCStateRepository(DataSource dataSource)Constructor ofJDBCStateRepository.JDBCStateRepository(DataSource dataSource, String tableName)Constructor ofJDBCStateRepository.JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable)Deprecated.usenewBuilder(DataSource)to create a builder that can be used to configure all aspects of the repository in a fluent wayJDBCStateRepository(DataSource dataSource, String tableName, boolean createTable, MapSerializer serializer)Deprecated.usenewBuilder(DataSource)to create a builder that can be used to configure all aspects of the repository in a fluent wayJDBCStateRepository(DataSource dataSource, String tableName, boolean createTable, MapSerializer serializer, boolean noCommit)Deprecated.usenewBuilder(DataSource)to create a builder that can be used to configure all aspects of the repository in a fluent way
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafterSchemaMigration(Connection connection)Method called after the database schema migration has been performed.protected voidbeforeSchemaMigration(Connection connection)Method called before the database schema migration is performed.FeatureStategetFeatureState(Feature feature)Get the persisted state of a feature from the repository.protected voidmigrateSchema()Method for creating/migrating the database schemastatic JDBCStateRepository.BuildernewBuilder(DataSource dataSource)Creates a new builder for creating aJDBCStateRepository.voidsetFeatureState(FeatureState featureState)Persist the supplied feature state.
-
-
-
Field Detail
-
dataSource
protected final DataSource dataSource
-
tableName
protected final String tableName
-
serializer
protected final MapSerializer serializer
-
noCommit
protected final boolean noCommit
-
-
Constructor Detail
-
JDBCStateRepository
public JDBCStateRepository(DataSource dataSource)
Constructor ofJDBCStateRepository. A database table calledTOGGLZwill be created automatically for you.- Parameters:
dataSource- The JDBCDataSourceto obtain connections from- See Also:
JDBCStateRepository(DataSource, String)
-
JDBCStateRepository
public JDBCStateRepository(DataSource dataSource, String tableName)
Constructor ofJDBCStateRepository. The database table will be created automatically for you.- Parameters:
dataSource- The JDBCDataSourceto obtain connections fromtableName- The name of the database table to use
-
JDBCStateRepository
@Deprecated public JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable)
Deprecated.usenewBuilder(DataSource)to create a builder that can be used to configure all aspects of the repository in a fluent wayConstructor ofJDBCStateRepository.- Parameters:
dataSource- The JDBCDataSourceto obtain connections fromtableName- The name of the database table to usecreateTable- If set totrue, the table will be automatically created if it is missing
-
JDBCStateRepository
@Deprecated public JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable, MapSerializer serializer)
Deprecated.usenewBuilder(DataSource)to create a builder that can be used to configure all aspects of the repository in a fluent wayConstructor ofJDBCStateRepository.- Parameters:
dataSource- The JDBCDataSourceto obtain connections fromtableName- The name of the database table to usecreateTable- If set totrue, the table will be automatically created if it is missingserializer- TheMapSerializerfor storing parameters
-
JDBCStateRepository
public JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable, MapSerializer serializer, boolean noCommit)
Deprecated.usenewBuilder(DataSource)to create a builder that can be used to configure all aspects of the repository in a fluent wayConstructor ofJDBCStateRepository.- Parameters:
dataSource- The JDBCDataSourceto obtain connections fromtableName- The name of the database table to usecreateTable- If set totrue, the table will be automatically created if it is missingserializer- TheMapSerializerfor storing parameters
-
-
Method Detail
-
migrateSchema
protected void migrateSchema()
Method for creating/migrating the database schema
-
beforeSchemaMigration
protected void beforeSchemaMigration(Connection connection)
Method called before the database schema migration is performed.
-
afterSchemaMigration
protected void afterSchemaMigration(Connection connection)
Method called after the database schema migration has been performed.
-
getFeatureState
public FeatureState getFeatureState(Feature feature)
Description copied from interface:StateRepositoryGet the persisted state of a feature from the repository. If the repository doesn't contain any information regarding this feature it must returnnull.- Specified by:
getFeatureStatein interfaceStateRepository- Parameters:
feature- The feature to read the state for- Returns:
- The persisted feature state or
null
-
setFeatureState
public void setFeatureState(FeatureState featureState)
Description copied from interface:StateRepositoryPersist the supplied feature state. The repository implementation must ensure that subsequent calls toStateRepository.getFeatureState(Feature)return the same state as persisted using this method.- Specified by:
setFeatureStatein interfaceStateRepository- Parameters:
featureState- The feature state to persist
-
newBuilder
public static JDBCStateRepository.Builder newBuilder(DataSource dataSource)
Creates a new builder for creating aJDBCStateRepository.- Parameters:
dataSource- theDataSourceTogglz should use to obtain JDBC connections
-
-