package io.netty.channel;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

/* 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);

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public final void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {
        initChannel(channelHandlerContext.channel());
        channelHandlerContext.pipeline().remove(this);
        channelHandlerContext.pipeline().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) throws Exception {
        logger.warn("Failed to initialize a channel. Closing: " + channelHandlerContext.channel(), th);
        try {
            ChannelPipeline channelPipelinePipeline = channelHandlerContext.pipeline();
            if (channelPipelinePipeline.context(this) != null) {
                channelPipelinePipeline.remove(this);
            }
        } finally {
            channelHandlerContext.close();
        }
    }

    protected abstract void initChannel(C c2) throws Exception;
}
