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

org.kiwiproject.dropwizard.util.bundle.DynamicPortsBundle Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
package org.kiwiproject.dropwizard.util.bundle;

import static org.kiwiproject.dropwizard.util.bundle.PortAssigners.allowablePortRangeFrom;
import static org.kiwiproject.dropwizard.util.bundle.PortAssigners.portAssignmentFrom;
import static org.kiwiproject.dropwizard.util.bundle.PortAssigners.portSecurityFrom;
import static org.kiwiproject.dropwizard.util.server.DropwizardConnectors.getAdminPorts;
import static org.kiwiproject.dropwizard.util.server.DropwizardConnectors.getApplicationPorts;

import com.google.common.annotations.VisibleForTesting;
import io.dropwizard.core.Configuration;
import io.dropwizard.core.ConfiguredBundle;
import io.dropwizard.core.setup.Environment;
import lombok.extern.slf4j.Slf4j;
import org.kiwiproject.dropwizard.util.startup.PortAssigner;
import org.kiwiproject.net.LocalPortChecker;

/**
 * Dropwizard bundle that assigns random ports dynamically during application initialization.
 * 

* Port assignment occurs before the Dropwizard application starts the Jetty server. *

* This bundle should be registered before any other bundles that use ports, for example, the * {@code ConsulBundle} in dropwizard-consul. * This ensures downstream bundles obtain the correct ports to use when registering with * a service registry, such as Consul. */ @Slf4j public abstract class DynamicPortsBundle implements ConfiguredBundle, DynamicPortsConfigured { @Override public void run(C configuration, Environment environment) { LOG.trace("Running DynamicPortsBundle"); var dynamicPortsConfig = getDynamicPortsConfiguration(configuration); var portAssignment = portAssignmentFrom(dynamicPortsConfig); var portRange = allowablePortRangeFrom(dynamicPortsConfig); var portSecurity = portSecurityFrom(dynamicPortsConfig); var portAssigner = PortAssigner.builder() .portAssignment(portAssignment) .allowablePortRange(portRange) .localPortChecker(getLocalPortChecker()) .portSecurity(portSecurity) .serverFactory(configuration.getServerFactory()) .tlsConfiguration(dynamicPortsConfig.getTlsContextConfiguration()) .build(); portAssigner.assignDynamicPorts(); LOG.info("Dynamic ports? {} ; application port(s): {} / admin port(s): {}", dynamicPortsConfig.isUseDynamicPorts(), getApplicationPorts(configuration), getAdminPorts(configuration)); } @VisibleForTesting LocalPortChecker getLocalPortChecker() { return new LocalPortChecker(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy