public StompSession getTransactedSession(StompConnection connection, String txID)
     throws Exception {
   StompSession stompSession = transactedSessions.get(txID);
   if (stompSession == null) {
     stompSession =
         new StompSession(connection, this, server.getStorageManager().newContext(executor));
     String name = UUIDGenerator.getInstance().generateStringUUID();
     ServerSession session =
         server.createSession(
             name,
             connection.getLogin(),
             connection.getPasscode(),
             ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE,
             connection,
             false,
             false,
             false,
             false,
             null,
             stompSession,
             null,
             true);
     stompSession.setServerSession(session);
     transactedSessions.put(txID, stompSession);
   }
   server.getStorageManager().setContext(stompSession.getContext());
   return stompSession;
 }
  public void handleBuffer(final RemotingConnection connection, final ActiveMQBuffer buffer) {
    StompConnection conn = (StompConnection) connection;

    conn.setDataReceived();

    do {
      StompFrame request;
      try {
        request = conn.decode(buffer);
      } catch (Exception e) {
        ActiveMQServerLogger.LOGGER.errorDecodingPacket(e);
        return;
      }

      if (request == null) {
        break;
      }

      try {
        invokeInterceptors(this.incomingInterceptors, request, conn);
        conn.handleFrame(request);
      } finally {
        server.getStorageManager().clearContext();
      }
    } while (conn.hasBytes());
  }
  public boolean send(final StompConnection connection, final StompFrame frame) {
    if (ActiveMQServerLogger.LOGGER.isTraceEnabled()) {
      ActiveMQServerLogger.LOGGER.trace("sent " + frame);
    }

    invokeInterceptors(this.outgoingInterceptors, frame, connection);

    synchronized (connection) {
      if (connection.isDestroyed()) {
        ActiveMQStompProtocolLogger.LOGGER.connectionClosed(connection);
        return false;
      }

      try {
        connection.physicalSend(frame);
      } catch (Exception e) {
        ActiveMQStompProtocolLogger.LOGGER.errorSendingFrame(e, frame);
        return false;
      }
      return true;
    }
  }
 public void createSubscription(
     StompConnection connection,
     String subscriptionID,
     String durableSubscriptionName,
     String destination,
     String selector,
     String ack,
     boolean noLocal)
     throws Exception {
   StompSession stompSession = getSession(connection);
   stompSession.setNoLocal(noLocal);
   if (stompSession.containsSubscription(subscriptionID)) {
     throw new ActiveMQStompException(
         connection,
         "There already is a subscription for: "
             + subscriptionID
             + ". Either use unique subscription IDs or do not create multiple subscriptions for the same destination");
   }
   long consumerID = server.getStorageManager().generateID();
   String clientID = (connection.getClientID() != null) ? connection.getClientID() : null;
   stompSession.addSubscription(
       consumerID, subscriptionID, clientID, durableSubscriptionName, destination, selector, ack);
 }
  public void cleanup(final StompConnection connection) {
    connection.setValid(false);

    // Close the session outside of the lock on the StompConnection, otherwise it could dead lock
    this.executor.execute(
        new Runnable() {
          public void run() {
            StompSession session = sessions.remove(connection.getID());
            if (session != null) {
              try {
                session.getSession().stop();
                session.getSession().rollback(true);
                session.getSession().close(false);
              } catch (Exception e) {
                ActiveMQServerLogger.LOGGER.errorCleaningStompConn(e);
              }
            }

            // removed the transacted session belonging to the connection
            Iterator<Entry<String, StompSession>> iterator =
                transactedSessions.entrySet().iterator();
            while (iterator.hasNext()) {
              Map.Entry<String, StompSession> entry = iterator.next();
              if (entry.getValue().getConnection() == connection) {
                ServerSession serverSession = entry.getValue().getSession();
                try {
                  serverSession.rollback(true);
                  serverSession.close(false);
                } catch (Exception e) {
                  ActiveMQServerLogger.LOGGER.errorCleaningStompConn(e);
                }
                iterator.remove();
              }
            }
          }
        });
  }