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

org.browsermob.proxy.ProxyManager Maven / Gradle / Ivy

The newest version!
package org.browsermob.proxy;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.Provider;

import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Singleton
public class ProxyManager {
    private AtomicInteger portCounter = new AtomicInteger(9090);
    private Provider proxyServerProvider;
    private Map proxies = new ConcurrentHashMap();

    @Inject
    public ProxyManager(Provider proxyServerProvider) {
        this.proxyServerProvider = proxyServerProvider;
    }

    public ProxyServer create(Map options, int port) throws Exception {
        ProxyServer proxy = proxyServerProvider.get();
        proxy.setPort(port);
        proxy.start();
        proxy.setOptions(options);
        proxies.put(port, proxy);
        return proxy;
    }

    public ProxyServer create(Map options) throws Exception {
        int port = portCounter.incrementAndGet();
        ProxyServer proxy = proxyServerProvider.get();

        proxy.setPort(port);
        proxy.start();
        proxy.setOptions(options);

        proxies.put(port, proxy);

        return proxy;
    }

    public ProxyServer get(int port) {
        return proxies.get(port);
    }

    public void delete(int port) throws Exception {
        ProxyServer proxy = proxies.remove(port);
        proxy.stop();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy