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

org.mockserver.ui.MockServerEventLogNotifier Maven / Gradle / Ivy

There is a newer version: 5.15.0
Show newest version
package org.mockserver.ui;

import org.mockserver.log.MockServerEventLog;
import org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString;
import org.mockserver.scheduler.Scheduler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author jamesdbloom
 */
public class MockServerEventLogNotifier extends ObjectWithReflectiveEqualsHashCodeToString {

    private boolean listenerAdded = false;
    private final List listeners = Collections.synchronizedList(new ArrayList<>());
    private final Scheduler scheduler;

    public MockServerEventLogNotifier(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    protected void notifyListeners(final MockServerEventLog notifier) {
        if (listenerAdded && !listeners.isEmpty()) {
            scheduler.submit(() -> {
                for (MockServerLogListener listener : listeners.toArray(new MockServerLogListener[0])) {
                    listener.updated(notifier);
                }
            });
        }
    }

    public void registerListener(MockServerLogListener listener) {
        listeners.add(listener);
        listenerAdded = true;
    }

    public void unregisterListener(MockServerLogListener listener) {
        listeners.remove(listener);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy