com.folion.config.PersistenceConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for com.folion.config.PersistenceConfiguration.java

Source

/*
 * Copyright 2016 Folion Media, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.folion.config;

import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.folion.persistence.ProxyBucket;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableAsync
@ComponentScan(basePackages = { "com.folion.persistence" })
@PropertySource(value = "classpath:persistence-${spring.profiles.active}.properties")
public class PersistenceConfiguration {

    @Bean(name = "users")
    public ProxyBucket usersBucket(Environment environment) {

        return getProxyBucket(environment, "user");
    }

    @Bean(name = "features")
    public ProxyBucket featuresBucket(Environment environment) {

        return getProxyBucket(environment, "feature");
    }

    @Bean(name = "competitions")
    public ProxyBucket competitionsBucket(Environment environment) {

        return getProxyBucket(environment, "competition");
    }

    private ProxyBucket getProxyBucket(Environment environment, String bucketName) {
        String couchbaseServersFromConfig = environment.getProperty("srv.couchbase.servers");

        List<String> servers = new ArrayList<>(Arrays.asList(couchbaseServersFromConfig.split(",")));
        Cluster cluster = CouchbaseCluster.create(servers);
        Bucket bucket = cluster.openBucket(environment.getProperty("srv.couchbase." + bucketName), 1,
                TimeUnit.DAYS);

        return new ProxyBucket(cluster, bucket);
    }
}