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

org.zodiac.autoconfigure.bootstrap.registry.AbstractAutoAppRegistration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.bootstrap.registry;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.zodiac.commons.constants.Constants;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.core.bootstrap.registry.AppRegistration;
import org.zodiac.core.bootstrap.registry.AppRegistry;
import org.zodiac.core.bootstrap.registry.AutoAppRegistration;
import org.zodiac.core.event.discovery.AppInstancePreRegisteredEvent;
import org.zodiac.core.event.discovery.AppInstanceRegisteredEvent;
import org.zodiac.core.util.AppInstanceUtil;

public abstract class AbstractAutoAppRegistration
    implements AutoAppRegistration, ApplicationContextAware, ApplicationListener {

    private static final Logger logger = LoggerFactory.getLogger(AbstractAutoAppRegistration.class);

    private final AppRegistry serviceRegistry;

    private boolean autoStartup = true;

    private AtomicBoolean running = new AtomicBoolean(false);

    private int order = 0;

    private ApplicationContext context;

    private AtomicInteger port = new AtomicInteger(0);

    private AutoAppRegistrationProperties properties;

    @Deprecated
    protected AbstractAutoAppRegistration(AppRegistry serviceRegistry) {
        this.serviceRegistry = serviceRegistry;
    }

    protected AbstractAutoAppRegistration(AppRegistry serviceRegistry, AutoAppRegistrationProperties properties) {
        this.serviceRegistry = serviceRegistry;
        this.properties = properties;
    }

    protected ApplicationContext getContext() {
        return this.context;
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        bind(event);
    }

    @Deprecated
    public void bind(WebServerInitializedEvent event) {
        ApplicationContext context = event.getApplicationContext();
        if (context instanceof ConfigurableWebServerApplicationContext) {
            if ("management".equals(((ConfigurableWebServerApplicationContext)context).getServerNamespace())) {
                return;
            }
        }
        this.port.compareAndSet(0, event.getWebServer().getPort());
        this.start();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    protected Environment getEnvironment() {
        return this.context.getEnvironment();
    }

    @Deprecated
    protected AtomicInteger getPort() {
        return this.port;
    }

    public boolean isAutoStartup() {
        return this.autoStartup;
    }

    public void start() {
        if (!isEnabled()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Discovery Lifecycle disabled. Not starting");
            }
            return;
        }

        // only initialize if nonSecurePort is greater than 0 and it isn't already running
        // because of containerPortInitializer below
        if (!this.running.get()) {
            this.context.publishEvent(new AppInstancePreRegisteredEvent(this, getRegistration()));
            register();
            if (shouldRegisterManagement()) {
                registerManagement();
            }
            this.context.publishEvent(new AppInstanceRegisteredEvent<>(this, getConfiguration()));
            this.running.compareAndSet(false, true);
        }

    }

    /**
     * @return Whether the management service should be registered with the {@link AppRegistry}.
     */
    protected boolean shouldRegisterManagement() {
        if (this.properties == null || this.properties.isRegisterManagement()) {
            return getManagementPort() != null && AppInstanceUtil.isDifferent(this.context);
        }
        return false;
    }

    /**
     * @return The object used to configure the registration.
     */
    @Deprecated
    protected abstract Object getConfiguration();

    /**
     * @return True, if this is enabled.
     */
    protected abstract boolean isEnabled();

    /**
     * @return The serviceId of the Management Service.
     */
    @Deprecated
    protected String getManagementServiceId() {
        // TODO: configurable management suffix
        return this.context.getId() + ":management";
    }

    /**
     * @return The service name of the Management Service.
     */
    @Deprecated
    protected String getManagementServiceName() {
        // TODO: configurable management suffix
        return getAppName() + ":management";
    }

    /**
     * @return The management server port.
     */
    @Deprecated
    protected Integer getManagementPort() {
        return AppInstanceUtil.getPort(this.context);
    }

    /**
     * @return The app name (currently the spring.application.name property).
     */
    @Deprecated
    protected String getAppName() {
        return getEnvironment().getProperty(SystemPropertiesConstants.Spring.SPRING_APP_NAME,
            Constants.Spring.DEFAULT_APP_NAME);
    }

    @PreDestroy
    public void destroy() {
        stop();
    }

    public boolean isRunning() {
        return this.running.get();
    }

    protected AtomicBoolean getRunning() {
        return this.running;
    }

    public int getOrder() {
        return this.order;
    }

    public int getPhase() {
        return 0;
    }

    protected AppRegistry getServiceRegistry() {
        return this.serviceRegistry;
    }

    protected abstract R getRegistration();

    protected abstract R getManagementRegistration();

    /**
     * Register the local service with the {@link AppRegistry}.
     */
    protected void register() {
        this.serviceRegistry.register(getRegistration());
    }

    /**
     * Register the local management service with the {@link AppRegistry}.
     */
    protected void registerManagement() {
        R registration = getManagementRegistration();
        if (registration != null) {
            this.serviceRegistry.register(registration);
        }
    }

    /**
     * De-register the local service with the {@link AppRegistry}.
     */
    protected void deregister() {
        this.serviceRegistry.deregister(getRegistration());
    }

    /**
     * De-register the local management service with the {@link AppRegistry}.
     */
    protected void deregisterManagement() {
        R registration = getManagementRegistration();
        if (registration != null) {
            this.serviceRegistry.deregister(registration);
        }
    }

    public void stop() {
        if (this.getRunning().compareAndSet(true, false) && isEnabled()) {
            deregister();
            if (shouldRegisterManagement()) {
                deregisterManagement();
            }
            this.serviceRegistry.close();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy