@Override
  public void stop() throws IOException {
    stopDelayedExecutor();
    final NIOTransport localTransport = transport;
    transport = null;
    if (localTransport != null) {
      localTransport.shutdownNow();
    }

    if (workerExecutorService != null) {
      final ExecutorService localExecutorService = workerExecutorService;
      workerExecutorService = null;
      localExecutorService.shutdownNow();
    }
    rootFilterChain = null;
  }
  protected void configureThreadPool(
      final ServiceLocator habitat,
      final NetworkListener networkListener,
      final ThreadPool threadPool) {

    final String classname = threadPool.getClassname();
    if (classname != null && !ThreadPool.DEFAULT_THREAD_POOL_CLASS_NAME.equals(classname)) {

      // Use custom thread pool
      try {
        final ExecutorService customThreadPool =
            Utils.newInstance(habitat, ExecutorService.class, classname, classname);

        if (customThreadPool != null) {
          if (!configureElement(habitat, networkListener, threadPool, customThreadPool)) {
            LOGGER.log(
                Level.INFO,
                "The ThreadPool configuration bean can not be "
                    + "passed to the custom thread-pool: {0}"
                    + " instance, because it's not {1}.",
                new Object[] {classname, ConfigAwareElement.class.getName()});
          }

          workerExecutorService = customThreadPool;
          transport.setWorkerThreadPool(customThreadPool);
          return;
        }

        LOGGER.log(Level.WARNING, "Can not initalize custom thread pool: {0}", classname);

      } catch (Throwable t) {
        LOGGER.log(Level.WARNING, "Can not initalize custom thread pool: " + classname, t);
      }
    }

    try {
      // Use standard Grizzly thread pool
      workerExecutorService =
          GrizzlyExecutorService.createInstance(
              configureThreadPoolConfig(networkListener, threadPool));
      transport.setWorkerThreadPool(workerExecutorService);
    } catch (NumberFormatException ex) {
      LOGGER.log(Level.WARNING, "Invalid thread-pool attribute", ex);
    }
  }
  protected void configureTransport(
      final NetworkListener networkListener,
      final Transport transportConfig,
      final FilterChainBuilder filterChainBuilder) {

    final String transportClassName = transportConfig.getClassname();
    if (TCPNIOTransport.class.getName().equals(transportClassName)) {
      transport = configureTCPTransport(transportConfig);
    } else if (UDPNIOTransport.class.getName().equals(transportClassName)) {
      transport = configureUDPTransport();
    } else {
      throw new GrizzlyConfigException("Unsupported transport type " + transportConfig.getName());
    }

    String selectorName = transportConfig.getSelectionKeyHandler();
    if (selectorName != null) {
      if (getSelectionKeyHandlerByName(selectorName, transportConfig) != null) {
        if (LOGGER.isLoggable(Level.INFO)) {
          LOGGER.warning(
              "Element, selection-key-handler, has been deprecated and is effectively ignored by the runtime.");
        }
      }
    }

    if (!Transport.BYTE_BUFFER_TYPE.equalsIgnoreCase(transportConfig.getByteBufferType())) {
      transport.setMemoryManager(
          new ByteBufferManager(
              true,
              AbstractMemoryManager.DEFAULT_MAX_BUFFER_SIZE,
              ByteBufferManager.DEFAULT_SMALL_BUFFER_SIZE));
    }

    final int acceptorThreads = Integer.parseInt(transportConfig.getAcceptorThreads());
    transport.setSelectorRunnersCount(acceptorThreads);

    final int readSize = Integer.parseInt(transportConfig.getSocketReadBufferSize());
    if (readSize > 0) {
      transport.setReadBufferSize(readSize);
    }

    final int writeSize = Integer.parseInt(transportConfig.getSocketWriteBufferSize());
    if (writeSize > 0) {
      transport.setWriteBufferSize(writeSize);
    }

    final ThreadPoolConfig kernelThreadPoolConfig = transport.getKernelThreadPoolConfig();

    kernelThreadPoolConfig.setPoolName(networkListener.getName() + "-kernel");
    if (acceptorThreads > 0) {
      kernelThreadPoolConfig.setCorePoolSize(acceptorThreads).setMaxPoolSize(acceptorThreads);
    }

    transport.setIOStrategy(loadIOStrategy(transportConfig.getIoStrategy()));
    transport.setNIOChannelDistributor(
        new RoundRobinConnectionDistributor(
            transport, Boolean.parseBoolean(transportConfig.getDedicatedAcceptorEnabled())));

    filterChainBuilder.add(new TransportFilter());
  }
  // TODO: Must get the information from domain.xml Config objects.
  // TODO: Pending Grizzly issue 54
  @Override
  public void configure(final ServiceLocator habitat, final NetworkListener networkListener)
      throws IOException {
    setName(networkListener.getName());
    setAddress(InetAddress.getByName(networkListener.getAddress()));
    setPort(Integer.parseInt(networkListener.getPort()));

    final FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless();

    configureTransport(networkListener, networkListener.findTransport(), filterChainBuilder);

    configureProtocol(habitat, networkListener, networkListener.findProtocol(), filterChainBuilder);

    configureThreadPool(habitat, networkListener, networkListener.findThreadPool());

    rootFilterChain = filterChainBuilder.build();
    transport.setProcessor(rootFilterChain);
  }
 @Override
 public void start() throws IOException {
   startDelayedExecutor();
   ((SocketBinder) transport).bind(new InetSocketAddress(address, port));
   transport.start();
 }