All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jolira.st4j.impl.ClientFactory Maven / Gradle / Ivy

Go to download

A client library for the Server Tracker. (http://github.com/jolira/server-tracker)

There is a newer version: 1.1.1
Show newest version
/**
 * Copyright (c) 2011 jolira. All rights reserved. This program and the accompanying materials are made available under
 * the terms of the GNU Public License 2.0 which is available at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */

package com.jolira.st4j.impl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;

/**
 * A simple abstraction for creating network clients. The clients implement a very simple fail-over mechanism.
 * 
 * @author jfk
 * @date Sep 17, 2011 8:41:28 AM
 * @since 1.0
 * 
 */
public class ClientFactory {
    private static URL makeURL(final String server) throws IllegalArgumentException {
        try {
            return new URL("http://" + server + "/submit/events");
        } catch (final MalformedURLException e) {
            throw new IllegalArgumentException("invalid server " + server, e);
        }
    }

    private final int timeout;

    private final Collection urls;

    ClientFactory(final String servers, final int timeout) throws IllegalArgumentException {
        final Collection urls_ = new ArrayList();
        final StringTokenizer izer = new StringTokenizer(servers, ",");

        while (izer.hasMoreTokens()) {
            final String token = izer.nextToken();
            final String server = token.trim();
            final URL url = makeURL(server);

            urls_.add(url);
        }

        urls = urls_;
        this.timeout = timeout;
    }

    Client makeClient() {
        return new Client(urls, timeout);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy