package io.netty.channel;

import g.a.a.a.a;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.concurrent.ConcurrentMap;

/* JADX INFO: loaded from: classes.dex */
@ChannelHandler.Sharable
public abstract class ChannelInitializer<C extends Channel> extends ChannelInboundHandlerAdapter {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) ChannelInitializer.class);
    private final ConcurrentMap<ChannelHandlerContext, Boolean> initMap = PlatformDependent.newConcurrentHashMap();

    private boolean initChannel(ChannelHandlerContext channelHandlerContext) {
        if (this.initMap.putIfAbsent(channelHandlerContext, Boolean.TRUE) != null) {
            return false;
        }
        try {
            initChannel(channelHandlerContext.channel());
        } finally {
            try {
            } finally {
            }
        }
        return true;
    }

    private void remove(ChannelHandlerContext channelHandlerContext) {
        try {
            ChannelPipeline channelPipelinePipeline = channelHandlerContext.pipeline();
            if (channelPipelinePipeline.context(this) != null) {
                channelPipelinePipeline.remove(this);
            }
        } finally {
            this.initMap.remove(channelHandlerContext);
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public final void channelRegistered(ChannelHandlerContext channelHandlerContext) {
        if (initChannel(channelHandlerContext)) {
            channelHandlerContext.pipeline().fireChannelRegistered();
        } else {
            channelHandlerContext.fireChannelRegistered();
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable th) {
        InternalLogger internalLogger = logger;
        StringBuilder sbF = a.F("Failed to initialize a channel. Closing: ");
        sbF.append(channelHandlerContext.channel());
        internalLogger.warn(sbF.toString(), th);
        channelHandlerContext.close();
    }

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public void handlerAdded(ChannelHandlerContext channelHandlerContext) {
        if (channelHandlerContext.channel().isRegistered()) {
            initChannel(channelHandlerContext);
        }
    }

    public abstract void initChannel(C c2);
}
