001package com.nimbusds.infinispan.persistence.sql.config;
002
003
004import org.infinispan.commons.CacheConfigurationException;
005import org.infinispan.configuration.cache.AbstractStoreConfiguration;
006import org.infinispan.configuration.cache.AbstractStoreConfigurationBuilder;
007import org.infinispan.configuration.cache.PersistenceConfigurationBuilder;
008import org.jooq.SQLDialect;
009
010import java.util.Properties;
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        }
045        
046        
047        @Override
048        public SQLStoreConfigurationBuilder recordTransformerClass(final Class recordTransformerClass) {
049                
050                this.attributes.attribute(SQLStoreConfiguration.RECORD_TRANSFORMER).set(recordTransformerClass);
051                return this;
052        }
053        
054        
055        @Override
056        public SQLStoreConfigurationBuilder queryExecutorClass(final Class queryExecutorClass) {
057                
058                this.attributes.attribute(SQLStoreConfiguration.QUERY_EXECUTOR).set(queryExecutorClass);
059                return this;
060        }
061        
062        
063        @Override
064        public SQLStoreConfigurationBuilder sqlDialect(final SQLDialect sqlDialect) {
065                
066                this.attributes.attribute(SQLStoreConfiguration.SQL_DIALECT).set(sqlDialect);
067                return this;
068        }
069        
070        
071        @Override
072        public SQLStoreConfigurationBuilder createTableIfMissing(final boolean createTableIfMissing) {
073                
074                this.attributes.attribute(SQLStoreConfiguration.CREATE_TABLE_IF_MISSING).set(createTableIfMissing);
075                return this;
076        }
077        
078        
079        @Override
080        public SQLStoreConfigurationBuilder createTableIgnoreErrors(final boolean createTableIgnoreErrors) {
081                
082                this.attributes.attribute(SQLStoreConfiguration.CREATE_TABLE_IGNORE_ERRORS).set(createTableIgnoreErrors);
083                return this;
084        }
085        
086        
087        @Override
088        public SQLStoreConfigurationBuilder connectionPool(final String cacheName) {
089                
090                this.attributes.attribute(SQLStoreConfiguration.CONNECTION_POOL).set(cacheName);
091                return this;
092        }
093
094
095        @Override
096        public SQLStoreConfigurationBuilder expiredQueryPageLimit(final int pageLimit) {
097
098                if (pageLimit < 1) {
099                        throw new IllegalArgumentException("The expired query page limit must a positive integer");
100                }
101
102                this.attributes.attribute(SQLStoreConfiguration.EXPIRED_QUERY_PAGE_LIMIT).set(pageLimit);
103                return this;
104        }
105
106
107        @Override
108        public SQLStoreConfigurationBuilder withProperties(final Properties properties) {
109                
110                return properties(properties);
111        }
112        
113        
114        @Override
115        public void validate() {
116                
117                super.validate();
118                
119                if (this.attributes.attribute(SQLStoreConfiguration.RECORD_TRANSFORMER).get() == null) {
120                        throw new CacheConfigurationException("An SQL store record transformer class must be specified");
121                }
122                
123                if (this.attributes.attribute(SQLStoreConfiguration.SQL_DIALECT).get() == null) {
124                        throw new CacheConfigurationException("An SQL store dialect must be specified");
125                }
126                
127                Properties props = this.attributes.attribute(AbstractStoreConfiguration.PROPERTIES).get();
128                
129                if (this.attributes.attribute(SQLStoreConfiguration.CONNECTION_POOL).get() == null && (props == null || props.isEmpty())) {
130                        throw new CacheConfigurationException("Missing SQL store properties, such as jdbcUrl, check the service documentation");
131                }
132
133                if (this.attributes.attribute(SQLStoreConfiguration.EXPIRED_QUERY_PAGE_LIMIT).get() < 1) {
134                        throw new CacheConfigurationException("The expired select query page size must be greater than zero");
135                }
136        }
137
138
139        @Override
140        public SQLStoreConfigurationBuilder self() {
141                return this;
142        }
143}