Package me.jtalk.networking

Source Code of me.jtalk.networking.NettyChildHandler$Initializer$ClientHandler

/*
* Copyright (C) 2014 Roman Nazarenko <me@jtalk.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package me.jtalk.networking;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;

import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import me.jtalk.jnetwork.netty.NettyClient;
import me.jtalk.jnetwork.netty.NettyClientsManager;
import me.jtalk.jnetwork.netty.NettyPacketHandler;
import me.jtalk.jnetwork.netty.NettyPacketParser;
import me.jtalk.jnetwork.netty.NettyPacketWriter;

@Singleton(name = NettyController.CHILD_HANDLER_NAME)
@LocalBean
public class NettyChildHandler implements NettyChannelInitializerFabric {

  @Override
  public ChannelInitializer<SocketChannel> get() throws NamingException {
    return new Initializer();
  }

  private static class Initializer extends ChannelInitializer<SocketChannel> {
    private static final String JNDI_CLIENT_PATH = "java:module/" + NettyEEClient.JNDI_NAME;

    private final NettyPacketWriter writer = new NettyPacketWriter();

    private final ClientHandler clientsManager = this.new ClientHandler();

    private final InitialContext context;

    public Initializer() throws NamingException {
      this.context = new InitialContext();
    }

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
      NettyPacketHandler handler = new NettyPacketHandler(this.clientsManager);
      channel.pipeline().addLast(new NettyPacketParser(), handler, this.writer);
    }

    private NettyClient getClient() {
      try {
        return (NettyClient)this.context.lookup(JNDI_CLIENT_PATH);
      } catch (NamingException e) {
        throw new RuntimeException(e);
      }
    }

    private class ClientHandler implements NettyClientsManager {

      @Override
      public NettyClient get() {
        return Initializer.this.getClient();
      }

      @Override
      public void free(NettyClient client) {
        // Does nothing as client is a stateful bean and thus
        // will be handled by application server itself.
      }

    }
  }
}
TOP

Related Classes of me.jtalk.networking.NettyChildHandler$Initializer$ClientHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.