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.
JDBCStateRepository
stores 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 class
JDBCStateRepository.Builder
Builder for aJDBCStateRepository
.
-
Field Summary
Fields Modifier and Type Field Description protected DataSource
dataSource
protected boolean
noCommit
protected MapSerializer
serializer
protected String
tableName
protected boolean
usePostgresTextColumns
-
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 void
afterSchemaMigration(Connection connection)
Method called after the database schema migration has been performed.protected void
beforeSchemaMigration(Connection connection)
Method called before the database schema migration is performed.FeatureState
getFeatureState(Feature feature)
Get the persisted state of a feature from the repository.protected void
migrateSchema()
Method for creating/migrating the database schemastatic JDBCStateRepository.Builder
newBuilder(DataSource dataSource)
Creates a new builder for creating aJDBCStateRepository
.void
setFeatureState(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
-
usePostgresTextColumns
protected final boolean usePostgresTextColumns
-
-
Constructor Detail
-
JDBCStateRepository
public JDBCStateRepository(DataSource dataSource)
Constructor ofJDBCStateRepository
. A database table calledTOGGLZ
will be created automatically for you.- Parameters:
dataSource
- The JDBCDataSource
to 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 JDBCDataSource
to 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 JDBCDataSource
to 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 JDBCDataSource
to obtain connections fromtableName
- The name of the database table to usecreateTable
- If set totrue
, the table will be automatically created if it is missingserializer
- TheMapSerializer
for 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 JDBCDataSource
to obtain connections fromtableName
- The name of the database table to usecreateTable
- If set totrue
, the table will be automatically created if it is missingserializer
- TheMapSerializer
for 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:StateRepository
Get 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:
getFeatureState
in 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:StateRepository
Persist 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:
setFeatureState
in 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
- theDataSource
Togglz should use to obtain JDBC connections
-
-