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

net.oneandone.stool.util.Ports Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/*
 * Copyright 1&1 Internet AG, https://github.com/1and1/
 *
 * 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 net.oneandone.stool.util;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/** Manage ports used for one stage. Immutable. Do not create directly, use Pool class instead. */
public class Ports {
    public static final String STOP_WRAPPER = "+stop+wrapper";
    public static final String JMX_DEBUG = "+jmx+debug";

    private final List vhosts;
    private final int stopWrapper;
    private final int jmxDebug;

    public Ports(List vhosts) {
        String id;

        if (vhosts.isEmpty()) {
            throw new IllegalStateException();
        }
        id = null;
        for (Vhost v : vhosts) {
            if (id == null) {
                id = v.id;
            } else {
                if (!id.equals(v.id)) {
                    throw new IllegalArgumentException(id + " vs " + v.id);
                }
            }
        }
        this.vhosts = vhosts;
        this.stopWrapper = indexOf(STOP_WRAPPER);
        this.jmxDebug = indexOf(JMX_DEBUG);
        if (stopWrapper == -1) {
            throw new IllegalArgumentException(vhosts.toString());
        }
        if (jmxDebug == -1) {
            throw new IllegalArgumentException(vhosts.toString());
        }
    }

    public int stop() {
        return vhosts.get(stopWrapper).even;
    }

    public int wrapper() {
        return vhosts.get(stopWrapper).even + 1;
    }

    public int jmx() {
        return vhosts.get(jmxDebug).even;
    }

    public int debug() {
        return vhosts.get(jmxDebug).even + 1;
    }

    public List vhosts() {
        return vhosts;
    }

    public Vhost firstWebapp() {
        for (Vhost vhost : vhosts()) {
            if (vhost.isWebapp()) {
                return vhost;
            }
        }
        throw new IllegalStateException();
    }

    public Vhost lookup(String name) {
        int idx;

        idx = indexOf(name);
        return idx == -1 ? null : vhosts.get(idx);
    }

    private int indexOf(String name) {
        Vhost vhost;

        for (int i = 0; i < vhosts.size(); i++) {
            vhost = vhosts.get(i);
            if (name.equals(vhost.name)) {
                return i;
            }
        }
        return -1;
    }

    public Map urlMap(String hostname, String url) {
        Map result;

        result = new LinkedHashMap<>();
        for (Vhost vhost : vhosts()) {
            if (vhost.isWebapp()) {
                result.putAll(vhost.urlMap(hostname, url));
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy