001package com.nimbusds.infinispan.persistence.sql.config;
002
003
004import java.util.Properties;
005
006import org.infinispan.commons.CacheConfigurationException;
007import org.infinispan.configuration.cache.AbstractStoreConfiguration;
008import org.infinispan.configuration.cache.AbstractStoreConfigurationBuilder;
009import org.infinispan.configuration.cache.PersistenceConfigurationBuilder;
010import org.jooq.SQLDialect;
011
012
013/**
014 * SQL store configuration builder.
015 *
016 * <p>Used by the Infinispan ConfigurationBuilder to implement fluent
017 * configuration for the SQL CacheLoader / CacheWriter. Methods should use
018 * the fluent style, rather than the setter/getter style and should return an
019 * instance of this object.
020 */
021public class SQLStoreConfigurationBuilder
022        extends AbstractStoreConfigurationBuilder<SQLStoreConfiguration,SQLStoreConfigurationBuilder>
023        implements SQLStoreConfigurationChildBuilder<SQLStoreConfigurationBuilder> {
024        
025        
026        /**
027         * Creates a new SQL store configuration builder.
028         *
029         * @param builder The general persistence configuration builder.
030         */
031        public SQLStoreConfigurationBuilder(final PersistenceConfigurationBuilder builder) {
032                super(builder, SQLStoreConfiguration.attributeDefinitionSet());
033        }
034
035
036        @Override
037        public SQLStoreConfiguration create() {
038                // This method should construct a new instance of a
039                // SQLStoreConfiguration object. There will be one instance
040                // per cache.
041                return new SQLStoreConfiguration(
042                        this.attributes.protect(),
043                        this.async.create(),
044                        this.singleton().create());
045        }
046        
047        
048        @Override
049        public SQLStoreConfigurationBuilder recordTransformerClass(final Class recordTransformerClass) {
050                
051                this.attributes.attribute(SQLStoreConfiguration.RECORD_TRANSFORMER).set(recordTransformerClass);
052                return this;
053        }
054        
055        
056        @Override
057        public SQLStoreConfigurationBuilder queryExecutorClass(final Class queryExecutorClass) {
058                
059                this.attributes.attribute(SQLStoreConfiguration.QUERY_EXECUTOR).set(queryExecutorClass);
060                return this;
061        }
062        
063        
064        @Override
065        public SQLStoreConfigurationBuilder sqlDialect(final SQLDialect sqlDialect) {
066                
067                this.attributes.attribute(SQLStoreConfiguration.SQL_DIALECT).set(sqlDialect);
068                return this;
069        }
070        
071        
072        @Override
073        public SQLStoreConfigurationBuilder withProperties(final Properties properties) {
074                
075                return properties(properties);
076        }
077        
078        
079        @Override
080        public void validate() {
081                
082                super.validate();
083                
084                if (this.attributes.attribute(SQLStoreConfiguration.RECORD_TRANSFORMER).get() == null) {
085                        throw new CacheConfigurationException("SQL store record transformer class must be defined");
086                }
087                
088                if (this.attributes.attribute(SQLStoreConfiguration.SQL_DIALECT).get() == null) {
089                        throw new CacheConfigurationException("SQL store dialect must be defined");
090                }
091                
092                Properties props = this.attributes.attribute(AbstractStoreConfiguration.PROPERTIES).get();
093                
094                if (props == null || props.isEmpty()) {
095                        throw new CacheConfigurationException("Missing SQL store properties, such as jdbcUrl, check the service documentation");
096                }
097        }
098
099
100        @Override
101        public SQLStoreConfigurationBuilder self() {
102                return this;
103        }
104}