com.scoopit.weedfs.client.WeedFSClientBuilder.java Source code

Java tutorial

Introduction

Here is the source code for com.scoopit.weedfs.client.WeedFSClientBuilder.java

Source

/*
 * (C) Copyright 2013 Scoop IT SAS (http://scoop.it/) and others.
 *
 * 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.
 * 
 * Contributors:
 *     Philippe GASSMANN
 *     Jean-Baptiste BELLET
 */
package com.scoopit.weedfs.client;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;

import com.scoopit.weedfs.client.caching.LookupCache;

public class WeedFSClientBuilder {

    HttpClient httpClient;

    URL masterUrl;

    LookupCache lookupCache;

    public WeedFSClientBuilder() {

    }

    public static WeedFSClientBuilder createBuilder() {
        return new WeedFSClientBuilder();
    }

    public WeedFSClientBuilder setHttpClient(HttpClient httpClient) {
        this.httpClient = httpClient;
        return this;
    }

    public WeedFSClientBuilder setMasterUrl(URL masterUrl) {
        this.masterUrl = masterUrl;
        return this;
    }

    public WeedFSClientBuilder setLookupCache(LookupCache lookupCache) {
        this.lookupCache = lookupCache;
        return this;
    }

    public WeedFSClient build() {
        if (masterUrl == null) {
            try {
                // default url for testing purpose
                masterUrl = new URL("http://localhost:9333");
            } catch (MalformedURLException e) {
                // This cannot happen by construction
                throw new Error(e);
            }
        }

        if (httpClient == null) {
            // minimal http client
            RequestConfig config = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();

            httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
        }

        return new WeedFSClientImpl(masterUrl, httpClient, lookupCache);
    }

}