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

be.bagofwords.application.ApplicationContext Maven / Gradle / Ivy

Go to download

Utility classes that are used in the count-db project and other bow-* projects

The newest version!
package be.bagofwords.application;

import be.bagofwords.ui.UI;
import be.bagofwords.util.SafeThread;
import be.bagofwords.util.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ApplicationContext {

    private List beans;
    private Map config;
    private boolean applicationWasTerminated = false;
    private String applicationName;

    public ApplicationContext(Map config) {
        this.applicationName = config.getOrDefault("application_name", "some_application");
        this.config = config;
        this.beans = new ArrayList<>();
    }

    public String getApplicationName() {
        return applicationName;
    }

    public String getConfig(String name, String defaultValue) {
        String value = config.getOrDefault(name, null);
        if (value == null) {
            if (defaultValue == null) {
                throw new RuntimeException("The configuration option " + name + " was not found");
            } else {
                UI.writeWarning("No configuration found for " + name + ", using default value " + defaultValue);
                value = defaultValue;
            }
        }
        return value;
    }

    public String getConfig(String name) {
        return getConfig(name, null);
    }

    public synchronized  void registerBean(T bean) {
        beans.add(bean);
    }

    public  List getBeans(Class interfaceClass) {
        List result = new ArrayList<>();
        for (Object bean : beans) {
            if (interfaceClass.isAssignableFrom(bean.getClass())) {
                result.add(interfaceClass.cast(bean));
            }
        }
        return result;
    }

    public  T getBeanIfPresent(Class interfaceClass) {
        List beans = getBeans(interfaceClass);
        if (beans.size() == 1) {
            return beans.get(0);
        } else if (beans.size() == 0) {
            return null;
        } else {
            throw new RuntimeException("Found " + beans.size() + " beans of type " + interfaceClass);
        }
    }

    public  T getBean(Class interfaceClass) {
        List beans = getBeans(interfaceClass);
        if (beans.size() == 1) {
            return beans.get(0);
        } else {
            throw new RuntimeException("Found " + beans.size() + " beans of type " + interfaceClass);
        }
    }

    public synchronized void terminateApplication() {
        if (!applicationWasTerminated) {
            List terminatableBeans = getBeans(CloseableComponent.class);
            for (CloseableComponent object : terminatableBeans) {
                if (!(object instanceof LateCloseableComponent)) {
                    object.terminate();
                }
            }
            for (CloseableComponent object : terminatableBeans) {
                if (!(object instanceof LateCloseableComponent) && object instanceof SafeThread) {
                    ((SafeThread) object).waitForFinish();
                }
            }
            for (CloseableComponent object : terminatableBeans) {
                if (object instanceof LateCloseableComponent) {
                    object.terminate();
                }
            }
            for (CloseableComponent object : terminatableBeans) {
                if (object instanceof LateCloseableComponent && object instanceof SafeThread) {
                    ((SafeThread) object).waitForFinish();
                }
            }
            applicationWasTerminated = true;
            UI.write("Application has terminated. Goodbye!");
        }
    }

    public void waitUntilTerminated() {
        while (!applicationWasTerminated) {
            Utils.threadSleep(500);
        }
    }

    public boolean applicationWasTerminated() {
        return applicationWasTerminated;
    }
}