导航菜单

页面标题

页面副标题

bbinstant v6.24.0 - SocketReceiver.java 源代码

正在查看: bbinstant v6.24.0 应用的 SocketReceiver.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package ch.qos.logback.classic.net;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.b;
import ch.qos.logback.classic.net.server.a;
import ch.qos.logback.classic.spi.c;
import ch.qos.logback.core.net.d;
import ch.qos.logback.core.net.h;
import ch.qos.logback.core.util.CloseUtil;
import java.io.Closeable;
import java.io.EOFException;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import javax.net.SocketFactory;

public class SocketReceiver extends ReceiverBase implements Runnable, h.a {
    private static final int DEFAULT_ACCEPT_CONNECTION_DELAY = 5000;
    private int acceptConnectionTimeout = 5000;
    private InetAddress address;
    private Future<Socket> connectorTask;
    private int port;
    private String receiverId;
    private int reconnectionDelay;
    private String remoteHost;
    private volatile Socket socket;

    private Future<Socket> activateConnector(h hVar) {
        try {
            return getContext().getScheduledExecutorService().submit(hVar);
        } catch (RejectedExecutionException unused) {
            return null;
        }
    }

    private h createConnector(InetAddress inetAddress, int i, int i2, int i3) {
        h newConnector = newConnector(inetAddress, i, i2, i3);
        newConnector.setExceptionHandler(this);
        newConnector.setSocketFactory(getSocketFactory());
        return newConnector;
    }

    private void dispatchEvents(LoggerContext loggerContext) {
        a aVar;
        StringBuilder sb;
        try {
            try {
                this.socket.setSoTimeout(this.acceptConnectionTimeout);
                aVar = new a(this.socket.getInputStream());
                try {
                    this.socket.setSoTimeout(0);
                    addInfo(this.receiverId + "connection established");
                    while (true) {
                        c cVar = (c) aVar.readObject();
                        b m81getLogger = loggerContext.m81getLogger(cVar.getLoggerName());
                        if (m81getLogger.isEnabledFor(cVar.getLevel())) {
                            m81getLogger.callAppenders(cVar);
                        }
                    }
                } catch (EOFException unused) {
                    addInfo(this.receiverId + "end-of-stream detected");
                    CloseUtil.closeQuietly(aVar);
                    CloseUtil.closeQuietly(this.socket);
                    this.socket = null;
                    sb = new StringBuilder();
                    sb.append(this.receiverId);
                    sb.append("connection closed");
                    addInfo(sb.toString());
                } catch (IOException e) {
                    e = e;
                    addInfo(this.receiverId + "connection failed: " + e);
                    CloseUtil.closeQuietly(aVar);
                    CloseUtil.closeQuietly(this.socket);
                    this.socket = null;
                    sb = new StringBuilder();
                    sb.append(this.receiverId);
                    sb.append("connection closed");
                    addInfo(sb.toString());
                } catch (ClassNotFoundException e2) {
                    e = e2;
                    addInfo(this.receiverId + "unknown event class: " + e);
                    CloseUtil.closeQuietly(aVar);
                    CloseUtil.closeQuietly(this.socket);
                    this.socket = null;
                    sb = new StringBuilder();
                    sb.append(this.receiverId);
                    sb.append("connection closed");
                    addInfo(sb.toString());
                }
            } catch (Throwable th) {
                th = th;
                CloseUtil.closeQuietly((Closeable) null);
                CloseUtil.closeQuietly(this.socket);
                this.socket = null;
                addInfo(this.receiverId + "connection closed");
                throw th;
            }
        } catch (EOFException unused2) {
            aVar = null;
        } catch (IOException e3) {
            e = e3;
            aVar = null;
        } catch (ClassNotFoundException e4) {
            e = e4;
            aVar = null;
        } catch (Throwable th2) {
            th = th2;
            CloseUtil.closeQuietly((Closeable) null);
            CloseUtil.closeQuietly(this.socket);
            this.socket = null;
            addInfo(this.receiverId + "connection closed");
            throw th;
        }
    }

    private Socket waitForConnectorToReturnASocket() throws InterruptedException {
        try {
            Socket socket = this.connectorTask.get();
            this.connectorTask = null;
            return socket;
        } catch (ExecutionException unused) {
            return null;
        }
    }

    @Override
    public void connectionFailed(h hVar, Exception exc) {
        if (exc instanceof InterruptedException) {
            addInfo("connector interrupted");
            return;
        }
        if (exc instanceof ConnectException) {
            addInfo(this.receiverId + "connection refused");
            return;
        }
        addInfo(this.receiverId + exc);
    }

    @Override
    protected Runnable getRunnableTask() {
        return this;
    }

    protected SocketFactory getSocketFactory() {
        return SocketFactory.getDefault();
    }

    protected h newConnector(InetAddress inetAddress, int i, int i2, int i3) {
        return new d(inetAddress, i, i2, i3);
    }

    @Override
    protected void onStop() {
        if (this.socket != null) {
            CloseUtil.closeQuietly(this.socket);
        }
    }

    @Override
    public void run() {
        try {
            LoggerContext loggerContext = (LoggerContext) getContext();
            while (!Thread.currentThread().isInterrupted()) {
                Future<Socket> activateConnector = activateConnector(createConnector(this.address, this.port, 0, this.reconnectionDelay));
                this.connectorTask = activateConnector;
                if (activateConnector == null) {
                    break;
                }
                this.socket = waitForConnectorToReturnASocket();
                if (this.socket == null) {
                    break;
                } else {
                    dispatchEvents(loggerContext);
                }
            }
        } catch (InterruptedException unused) {
        }
        addInfo("shutting down");
    }

    public void setAcceptConnectionTimeout(int i) {
        this.acceptConnectionTimeout = i;
    }

    public void setPort(int i) {
        this.port = i;
    }

    public void setReconnectionDelay(int i) {
        this.reconnectionDelay = i;
    }

    public void setRemoteHost(String str) {
        this.remoteHost = str;
    }

    @Override
    protected boolean shouldStart() {
        int i;
        if (this.port == 0) {
            addError("No port was configured for receiver. For more information, please visit http://logback.qos.ch/codes.html#receiver_no_port");
            i = 1;
        } else {
            i = 0;
        }
        if (this.remoteHost == null) {
            i++;
            addError("No host name or address was configured for receiver. For more information, please visit http://logback.qos.ch/codes.html#receiver_no_host");
        }
        if (this.reconnectionDelay == 0) {
            this.reconnectionDelay = 30000;
        }
        if (i == 0) {
            try {
                this.address = InetAddress.getByName(this.remoteHost);
            } catch (UnknownHostException unused) {
                addError("unknown host: " + this.remoteHost);
                i++;
            }
        }
        if (i == 0) {
            this.receiverId = "receiver " + this.remoteHost + ":" + this.port + ": ";
        }
        return i == 0;
    }
}