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

org.graylog2.shared.inputs.InputRegistry Maven / Gradle / Ivy

There is a newer version: 6.0.6
Show newest version
/*
 * Copyright (C) 2020 Graylog, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Server Side Public License, version 1,
 * as published by MongoDB, Inc.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Server Side Public License for more details.
 *
 * You should have received a copy of the Server Side Public License
 * along with this program. If not, see
 * .
 */
package org.graylog2.shared.inputs;


import com.google.common.collect.ImmutableSet;
import org.graylog2.plugin.IOState;
import org.graylog2.plugin.inputs.MessageInput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

public class InputRegistry extends HashSet> {
    private static final Logger LOG = LoggerFactory.getLogger(InputRegistry.class);

    public InputRegistry() {
        super();
    }


    public Set> getInputStates() {
        return ImmutableSet.copyOf(this);
    }

    public IOState getInputState(String inputId) {
        for (IOState inputState : this) {
            if (inputState.getStoppable().getPersistId().equals(inputId)) {
                return inputState;
            }
        }

        return null;
    }

    public Set> getRunningInputs() {
        ImmutableSet.Builder> runningInputs = ImmutableSet.builder();
        for (IOState inputState : this) {
            if (inputState.getState() == IOState.Type.RUNNING) {
                runningInputs.add(inputState);
            }
        }
        return runningInputs.build();
    }

    public boolean hasTypeRunning(Class klazz) {
        for (IOState inputState : this) {
            if (inputState.getStoppable().getClass().equals(klazz)) {
                return true;
            }
        }

        return false;
    }

    public int runningCount() {
        return getRunningInputs().size();
    }

    public MessageInput getRunningInput(String inputId) {
        for (IOState inputState : this) {
            if (inputState.getStoppable().getId().equals(inputId)) {
                return inputState.getStoppable();
            }
        }

        return null;
    }

    public IOState getRunningInputState(String inputStateId) {
        for (IOState inputState : this) {
            if (inputState.getStoppable().getId().equals(inputStateId)) {
                return inputState;
            }
        }

        return null;
    }

    public boolean remove(MessageInput input) {
        final IOState inputState = this.stop(input);
        input.terminate();
        if (inputState != null) {
            inputState.setState(IOState.Type.TERMINATED);
        }

        return super.remove(inputState);
    }

    public boolean remove(IOState inputState) {
        final MessageInput messageInput = inputState.getStoppable();
        return remove(messageInput);
    }

    public IOState stop(MessageInput input) {
        IOState inputState = getRunningInputState(input.getId());

        if (inputState != null) {
            inputState.setState(IOState.Type.STOPPING);
            try {
                input.stop();
            } catch (Exception e) {
                LOG.warn("Stopping input <{}> failed, removing anyway: {}", input.getId(), e);
            }
            inputState.setState(IOState.Type.STOPPED);
        }

        return inputState;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy