void sendSessionMessages() {

      ClientSessionHandler handler = handlers.get(sessionRefId);
      /*
       * If a local handler exists, forward messages to local
       * handler to send to client session; otherwise log
       * error message.
       */
      if (handler != null && handler.isConnected()) {
        if (sendLoginResult) {
          if (loginSuccess) {
            handler.loginSuccess();
          } else {
            handler.loginFailure(loginException);
            return;
          }
        }
        SessionProtocol protocol = handler.getSessionProtocol();
        if (protocol != null) {
          for (SendEvent sendEvent : messages) {
            try {
              protocol.sessionMessage(ByteBuffer.wrap(sendEvent.message), sendEvent.delivery);
            } catch (Exception e) {
              logger.logThrow(Level.WARNING, e, "sessionMessage throws");
            }
          }
        }
      } else {
        logger.log(Level.FINE, "Discarding messages for disconnected session:{0}", handler);
      }
    }
  /** {@inheritDoc} */
  public void doShutdown() {
    if (protocolAcceptor != null) {
      try {
        protocolAcceptor.close();
      } catch (IOException ignore) {
      }
    }
    for (ClientSessionHandler handler : handlers.values()) {
      handler.shutdown();
    }
    handlers.clear();

    if (exporter != null) {
      try {
        exporter.unexport();
        logger.log(Level.FINEST, "client session server unexported");
      } catch (RuntimeException e) {
        logger.logThrow(Level.FINEST, e, "unexport server throws");
        // swallow exception
      }
    }

    synchronized (flushContextsLock) {
      flushContextsLock.notifyAll();
    }
  }
  /** {@inheritDoc} */
  public SessionProtocol getSessionProtocol(BigInteger sessionRefId) {
    if (sessionRefId == null) {
      throw new NullPointerException("null sessionRefId");
    }
    serviceStats.getSessionProtocolOp.report();
    ClientSessionHandler handler = handlers.get(sessionRefId);

    return handler != null ? handler.getSessionProtocol() : null;
  }
  /** {@inheritDoc} */
  public void doShutdown() {
    final IoFuture<?, ?> future = acceptFuture;
    acceptFuture = null;
    if (future != null) {
      future.cancel(true);
    }

    if (acceptor != null) {
      try {
        acceptor.close();
      } catch (IOException e) {
        logger.logThrow(Level.FINEST, e, "closing acceptor throws");
        // swallow exception
      }
    }

    if (asyncChannelGroup != null) {
      asyncChannelGroup.shutdown();
      boolean groupShutdownCompleted = false;
      try {
        groupShutdownCompleted = asyncChannelGroup.awaitTermination(1, TimeUnit.SECONDS);
      } catch (InterruptedException e) {
        logger.logThrow(Level.FINEST, e, "shutdown acceptor interrupted");
        Thread.currentThread().interrupt();
      }
      if (!groupShutdownCompleted) {
        logger.log(Level.WARNING, "forcing async group shutdown");
        try {
          asyncChannelGroup.shutdownNow();
        } catch (IOException e) {
          logger.logThrow(Level.FINEST, e, "shutdown acceptor throws");
          // swallow exception
        }
      }
    }
    logger.log(Level.FINEST, "acceptor shutdown");

    if (exporter != null) {
      try {
        exporter.unexport();
        logger.log(Level.FINEST, "client session server unexported");
      } catch (RuntimeException e) {
        logger.logThrow(Level.FINEST, e, "unexport server throws");
        // swallow exception
      }
    }

    for (ClientSessionHandler handler : handlers.values()) {
      handler.shutdown();
    }
    handlers.clear();

    flushContextsThread.interrupt();
  }
 void flush() {
   sendMessages();
   if (disconnect) {
     ClientSessionHandler handler = handlers.get(sessionRefId);
     /*
      * If session is local, disconnect session; otherwise, log
      * error message.
      */
     if (handler != null) {
       handler.handleDisconnect(false);
     } else {
       logger.log(
           Level.FINE, "discarding request to disconnect unknown session:{0}", sessionRefId);
     }
   }
 }
 /** {@inheritDoc} */
 public void sendProtocolMessageNonTransactional(
     BigInteger sessionRefId, ByteBuffer message, Delivery delivery) {
   ClientSessionHandler handler = handlers.get(sessionRefId);
   /*
    * If a local handler exists, forward message to local handler
    * to send to client session.
    */
   if (handler != null) {
     byte[] bytes = new byte[message.remaining()];
     message.get(bytes);
     handler.sendProtocolMessage(bytes, delivery);
   } else {
     logger.log(Level.FINE, "Discarding messages for unknown session:{0}", sessionRefId);
     return;
   }
 }
    void sendMessages() {

      ClientSessionHandler handler = handlers.get(sessionRefId);
      /*
       * If a local handler exists, forward messages to local
       * handler to send to client session; otherwise log
       * error message.
       */
      if (handler != null && handler.isConnected()) {
        if (loginAck != null) {
          handler.sendLoginProtocolMessage(loginAck, Delivery.RELIABLE);
        }
        for (byte[] message : messages) {
          handler.sendProtocolMessage(message, Delivery.RELIABLE);
        }
      } else {
        logger.log(Level.FINE, "Discarding messages for disconnected session:{0}", handler);
      }
    }
 /**
  * Validates the {@code identity} of the user logging in and returns {@code true} if the login is
  * allowed to proceed, and {@code false} if the login is denied.
  *
  * <p>A user with the specified {@code identity} is allowed to log in if one of the following
  * conditions holds:
  *
  * <ul>
  *   <li>the {@code identity} is not currently logged in, or
  *   <li>the {@code identity} is logged in, and the {@code
  *       com.sun.sgs.impl.service.session.allow.new.login} property is set to {@code true}.
  * </ul>
  *
  * In the latter case (new login allowed), the existing user session logged in with {@code
  * identity} is forcibly disconnected.
  *
  * <p>If this method returns {@code true}, the {@link #removeUserLogin} method must be invoked
  * when the user with the specified {@code identity} is disconnected.
  *
  * @param identity the user identity
  * @param handler the client session handler
  * @return {@code true} if the user is allowed to log in with the specified {@code identity},
  *     otherwise returns {@code false}
  */
 boolean validateUserLogin(Identity identity, ClientSessionHandler handler) {
   ClientSessionHandler previousHandler = loggedInIdentityMap.putIfAbsent(identity, handler);
   if (previousHandler == null) {
     // No user logged in with the same idenity; allow login.
     return true;
   } else if (!allowNewLogin) {
     // Same user logged in; new login not allowed, so deny login.
     return false;
   } else if (!previousHandler.loginHandled()) {
     // Same user logged in; can't preempt user in the
     // process of logging in; deny login.
     return false;
   } else {
     if (loggedInIdentityMap.replace(identity, previousHandler, handler)) {
       // Disconnect current user; allow new login.
       previousHandler.handleDisconnect(false, true);
       return true;
     } else {
       // Another same user login beat this one; deny login.
       return false;
     }
   }
 }