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 createTableIfMissing(final boolean createTableIfMissing) {
074                
075                this.attributes.attribute(SQLStoreConfiguration.CREATE_TABLE_IF_MISSING).set(createTableIfMissing);
076                return this;
077        }
078        
079        
080        @Override
081        public SQLStoreConfigurationBuilder connectionPool(final String cacheName) {
082                
083                this.attributes.attribute(SQLStoreConfiguration.CONNECTION_POOL).set(cacheName);
084                return this;
085        }
086        
087        
088        @Override
089        public SQLStoreConfigurationBuilder withProperties(final Properties properties) {
090                
091                return properties(properties);
092        }
093        
094        
095        @Override
096        public void validate() {
097                
098                super.validate();
099                
100                if (this.attributes.attribute(SQLStoreConfiguration.RECORD_TRANSFORMER).get() == null) {
101                        throw new CacheConfigurationException("An SQL store record transformer class must be specified");
102                }
103                
104                if (this.attributes.attribute(SQLStoreConfiguration.SQL_DIALECT).get() == null) {
105                        throw new CacheConfigurationException("An SQL store dialect must be specified");
106                }
107                
108                Properties props = this.attributes.attribute(AbstractStoreConfiguration.PROPERTIES).get();
109                
110                if (this.attributes.attribute(SQLStoreConfiguration.CONNECTION_POOL).get() == null && (props == null || props.isEmpty())) {
111                        throw new CacheConfigurationException("Missing SQL store properties, such as jdbcUrl, check the service documentation");
112                }
113        }
114
115
116        @Override
117        public SQLStoreConfigurationBuilder self() {
118                return this;
119        }
120}