/*
 * Decompiled with CFR 0.152.
 */
package org.apache.logging.log4j.core.appender.db;

import java.util.ArrayList;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractManager;
import org.apache.logging.log4j.core.appender.ManagerFactory;

public abstract class AbstractDatabaseManager
extends AbstractManager {
    private final ArrayList<LogEvent> buffer;
    private final int bufferSize;
    private boolean running = false;

    protected AbstractDatabaseManager(String name, int bufferSize) {
        super(name);
        this.bufferSize = bufferSize;
        this.buffer = new ArrayList(bufferSize + 1);
    }

    protected abstract void startupInternal() throws Exception;

    public final synchronized void startup() {
        if (!this.isRunning()) {
            try {
                this.startupInternal();
                this.running = true;
            }
            catch (Exception e) {
                LOGGER.error("Could not perform database startup operations using logging manager [{}].", this.getName(), e);
            }
        }
    }

    protected abstract void shutdownInternal() throws Exception;

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public final synchronized void shutdown() {
        this.flush();
        if (this.isRunning()) {
            try {
                this.shutdownInternal();
            }
            catch (Exception e) {
                LOGGER.warn("Error while performing database shutdown operations using logging manager [{}].", this.getName(), e);
            }
            finally {
                this.running = false;
            }
        }
    }

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

    protected abstract void connectAndStart();

    protected abstract void writeInternal(LogEvent var1);

    protected abstract void commitAndClose();

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public final synchronized void flush() {
        if (this.isRunning() && this.buffer.size() > 0) {
            this.connectAndStart();
            try {
                for (LogEvent event : this.buffer) {
                    this.writeInternal(event);
                }
            }
            finally {
                this.commitAndClose();
                this.buffer.clear();
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public final synchronized void write(LogEvent event) {
        if (this.bufferSize > 0) {
            this.buffer.add(event);
            if (this.buffer.size() >= this.bufferSize || event.isEndOfBatch()) {
                this.flush();
            }
        } else {
            this.connectAndStart();
            try {
                this.writeInternal(event);
            }
            finally {
                this.commitAndClose();
            }
        }
    }

    @Override
    public final void releaseSub() {
        this.shutdown();
    }

    public final String toString() {
        return this.getName();
    }

    protected static <M extends AbstractDatabaseManager, T extends AbstractFactoryData> M getManager(String name, T data, ManagerFactory<M, T> factory) {
        return (M)((AbstractDatabaseManager)AbstractManager.getManager(name, factory, data));
    }

    protected static abstract class AbstractFactoryData {
        private final int bufferSize;

        protected AbstractFactoryData(int bufferSize) {
            this.bufferSize = bufferSize;
        }

        public int getBufferSize() {
            return this.bufferSize;
        }
    }
}
