public void removeMessage(ConnectionContext context, final MessageAck ack) throws IOException {
    final boolean debug = LOG.isDebugEnabled();
    JournalQueueAck remove = new JournalQueueAck();
    remove.setDestination(destination);
    remove.setMessageAck(ack);

    final RecordLocation location =
        peristenceAdapter.writeCommand(remove, ack.isResponseRequired());
    if (!context.isInTransaction()) {
      if (debug) {
        LOG.debug("Journalled message remove for: " + ack.getLastMessageId() + ", at: " + location);
      }
      removeMessage(ack, location);
    } else {
      if (debug) {
        LOG.debug(
            "Journalled transacted message remove for: "
                + ack.getLastMessageId()
                + ", at: "
                + location);
      }
      synchronized (this) {
        inFlightTxLocations.add(location);
      }
      transactionStore.removeMessage(this, ack, location);
      context
          .getTransaction()
          .addSynchronization(
              new Synchronization() {
                public void afterCommit() throws Exception {
                  if (debug) {
                    LOG.debug(
                        "Transacted message remove commit for: "
                            + ack.getLastMessageId()
                            + ", at: "
                            + location);
                  }
                  synchronized (JournalMessageStore.this) {
                    inFlightTxLocations.remove(location);
                    removeMessage(ack, location);
                  }
                }

                public void afterRollback() throws Exception {
                  if (debug) {
                    LOG.debug(
                        "Transacted message remove rollback for: "
                            + ack.getLastMessageId()
                            + ", at: "
                            + location);
                  }
                  synchronized (JournalMessageStore.this) {
                    inFlightTxLocations.remove(location);
                  }
                }
              });
    }
  }
 private void discardExpiredMessage(MessageReference reference) {
   LOG.debug("Discarding expired message {}", reference);
   if (broker.isExpired(reference)) {
     ConnectionContext context = new ConnectionContext(new NonCachedMessageEvaluationContext());
     context.setBroker(broker);
     ((Destination) reference.getRegionDestination())
         .messageExpired(context, null, new IndirectMessageReference(reference.getMessage()));
   }
 }
  /**
   * Not synchronized since the Journal has better throughput if you increase the number of
   * concurrent writes that it is doing.
   */
  public void addMessage(final ConnectionContext context, final Message message)
      throws IOException {

    final MessageId id = message.getMessageId();

    final boolean debug = LOG.isDebugEnabled();
    message.incrementReferenceCount();

    final RecordLocation location =
        peristenceAdapter.writeCommand(message, message.isResponseRequired());
    if (!context.isInTransaction()) {
      if (debug) {
        LOG.debug("Journalled message add for: " + id + ", at: " + location);
      }
      addMessage(context, message, location);
    } else {
      if (debug) {
        LOG.debug("Journalled transacted message add for: " + id + ", at: " + location);
      }
      synchronized (this) {
        inFlightTxLocations.add(location);
      }
      transactionStore.addMessage(this, message, location);
      context
          .getTransaction()
          .addSynchronization(
              new Synchronization() {
                public void afterCommit() throws Exception {
                  if (debug) {
                    LOG.debug("Transacted message add commit for: " + id + ", at: " + location);
                  }
                  synchronized (JournalMessageStore.this) {
                    inFlightTxLocations.remove(location);
                    addMessage(context, message, location);
                  }
                }

                public void afterRollback() throws Exception {
                  if (debug) {
                    LOG.debug("Transacted message add rollback for: " + id + ", at: " + location);
                  }
                  synchronized (JournalMessageStore.this) {
                    inFlightTxLocations.remove(location);
                  }
                  message.decrementReferenceCount();
                }
              });
    }
  }
  public void testRemoveConnection() throws Exception {
    connectionContext.setSecurityContext(new StubSecurityContext());

    authBroker.removeConnection(connectionContext, connectionInfo, new Throwable());

    assertEquals(
        "removeConnection should clear ConnectionContext.",
        null,
        connectionContext.getSecurityContext());

    assertEquals(
        "Incorrect number of calls to underlying broker were made.",
        1,
        receiveBroker.removeConnectionData.size());
  }
  @Override
  protected void setUp() throws Exception {
    System.setProperty("brokername", "testbroker");
    brokerService = createBroker();
    broker = brokerService.getBroker();

    // started automatically
    // brokerService.start();

    context = new ConnectionContext();
    context.setBroker(broker);
    info = new ConnectionInfo();
    info.setClientId("James");
    info.setUserName("James");
    info.setConnectionId(new ConnectionId("1234"));

    try {
      broker.addConnection(context, info);
    } catch (Throwable e) {
      e.printStackTrace();
      fail(e.getMessage());
    }

    assertNotNull("No broker created!");
  }
 @Override
 public Response messagePull(ConnectionContext context, MessagePull pull) throws Exception {
   if (isLogAll() || isLogConsumerEvents()) {
     LOG.info(
         "Message Pull from: {} on {}",
         context.getClientId(),
         pull.getDestination().getPhysicalName());
   }
   return super.messagePull(context, pull);
 }
  public void testAddConnectionSuccess() {
    String dnUserName = "******";

    HashSet<String> userNames = new HashSet<String>();
    userNames.add(dnUserName);

    HashSet<String> groupNames = new HashSet<String>();
    groupNames.add("testGroup1");
    groupNames.add("testGroup2");
    groupNames.add("tesetGroup3");

    setConfiguration(userNames, groupNames, true);

    try {
      authBroker.addConnection(connectionContext, connectionInfo);
    } catch (Exception e) {
      fail("Call to addConnection failed: " + e.getMessage());
    }

    assertEquals(
        "Number of addConnection calls to underlying Broker must match number of calls made to "
            + "AuthenticationBroker.",
        1,
        receiveBroker.addConnectionData.size());

    ConnectionContext receivedContext =
        receiveBroker.addConnectionData.getFirst().connectionContext;

    assertEquals(
        "The SecurityContext's userName must be set to that of the UserPrincipal.",
        dnUserName,
        receivedContext.getSecurityContext().getUserName());

    Set<Principal> receivedPrincipals = receivedContext.getSecurityContext().getPrincipals();

    for (Iterator<Principal> iter = receivedPrincipals.iterator(); iter.hasNext(); ) {
      Principal currentPrincipal = iter.next();

      if (currentPrincipal instanceof UserPrincipal) {
        if (userNames.remove(currentPrincipal.getName())) {
          // Nothing, we did good.
        } else {
          // Found an unknown userName.
          fail("Unknown UserPrincipal found");
        }
      } else if (currentPrincipal instanceof GroupPrincipal) {
        if (groupNames.remove(currentPrincipal.getName())) {
          // Nothing, we did good.
        } else {
          fail("Unknown GroupPrincipal found.");
        }
      } else {
        fail("Unexpected Principal subclass found.");
      }
    }

    if (!userNames.isEmpty()) {
      fail("Some usernames were not added as UserPrincipals");
    }

    if (!groupNames.isEmpty()) {
      fail("Some group names were not added as GroupPrincipals");
    }
  }
 public ConnectionContext copy() {
   ConnectionContext rc = new ConnectionContext(this.messageEvaluationContext);
   rc.connection = this.connection;
   rc.connector = this.connector;
   rc.broker = this.broker;
   rc.inRecoveryMode = this.inRecoveryMode;
   rc.transaction = this.transaction;
   rc.transactions = this.transactions;
   rc.securityContext = this.securityContext;
   rc.connectionId = this.connectionId;
   rc.clientId = this.clientId;
   rc.userName = this.userName;
   rc.reconnect = this.reconnect;
   rc.wireFormatInfo = this.wireFormatInfo;
   rc.longTermStoreContext = this.longTermStoreContext;
   rc.producerFlowControl = this.producerFlowControl;
   rc.messageAuthorizationPolicy = this.messageAuthorizationPolicy;
   rc.networkConnection = this.networkConnection;
   rc.faultTolerant = this.faultTolerant;
   rc.stopping.set(this.stopping.get());
   rc.dontSendReponse = this.dontSendReponse;
   rc.clientMaster = this.clientMaster;
   return rc;
 }