public void finish() {
   synchronized (monitorAgent) {
     monitorAgent.finish();
   }
   senderReceiver.sendAsync(
       new Message(Type.DEREG, location, PhysicalLocation.coordinatorLocation(), null));
   senderReceiver.shutdown();
 }
  public void testMessagingClientTopic() throws Exception {

    String clientId = "0";
    MessagingClient messagingClient = new JmsMessagingClient(clientId, this, properties, false);

    messagingClient.start();
    sendMessage(TOPIC_NAME);
    checkMessage(clientId, TOPIC);
    messagingClient.stop(true);
  }
  public void testMessagingClientQueue() throws Exception {

    String clientId = "3";
    MessagingClient messagingClient = new JmsMessagingClient(clientId, this, properties, false);

    messagingClient.start();
    sendMessage(QUEUE_NAME);
    checkMessage(clientId, QUEUE);
    messagingClient.stop(true);
  }
  public void testMessagingClientMultipleTopics() throws Exception {

    String clientId = "2";
    String topicName = "additionalTopic";
    String topic = "fedora.test.additional";
    properties.setProperty("topic." + topicName, topic);
    MessagingClient messagingClient = new JmsMessagingClient(clientId, this, properties, true);

    messagingClient.start();
    sendMessage(TOPIC_NAME);
    checkMessage(clientId, TOPIC);
    sendMessage(topicName);
    checkMessage(clientId, topic);
    messagingClient.stop(true);
  }
  public void testMessageSelectors() throws Exception {

    String clientId = "13";
    // Selector to include test message
    String messageSelector = propertyName + " LIKE 'test%'";
    MessagingClient messagingClient =
        new JmsMessagingClient(clientId, this, properties, messageSelector, false);
    messagingClient.start();
    sendMessage(TOPIC_NAME);
    checkMessage(clientId, TOPIC);
    messagingClient.stop(true);

    clientId = "14";
    // Selector to omit test message
    messageSelector = propertyName + " LIKE 'testing%'";
    messagingClient = new JmsMessagingClient(clientId, this, properties, messageSelector, false);
    messagingClient.start();
    sendMessage(TOPIC_NAME);
    checkNoMessages();
    messagingClient.stop(true);
  }
 private void setCrossTaskDownstreamTaint(Tuple keys, Set<String> tags) {
   for (String neighbor : crossTaskDownstreamNeighbors) {
     Tuple body = new DefaultTuple();
     body.append("cross");
     body.append(keys);
     for (String tag : tags) {
       body.append(tag);
     }
     senderReceiver.sendAsync(
         new Message(Message.Type.TAINT, location, new LogicalLocation(neighbor), body));
   }
 }
  public void testMessagingClientDurableTopic() throws Exception {

    String clientId = "1";
    MessagingClient messagingClient = new JmsMessagingClient(clientId, this, properties, true);

    // Establish that the client can start and receive messages
    messagingClient.start();
    sendMessage(TOPIC_NAME);
    checkMessage(clientId, TOPIC);
    messagingClient.stop(false);

    // Check to see if messages are received in a durable fashion
    sendMessage(TOPIC_NAME);
    messagingClient.start();
    checkMessage(clientId, TOPIC);
    messagingClient.stop(true);

    // Make sure durable subscriptions were closed
    sendMessage(TOPIC_NAME);
    messagingClient.start();
    checkNoMessages();
    messagingClient.stop(true);
  }
  public void testInvalidProperties() throws Exception {
    // Null properties
    try {
      new JmsMessagingClient("4", this, null, false);
      fail("Creating a Messagingient with null properties " + "should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Connection properties may not be null"));
    }

    // Missing all properties
    properties = new Properties();

    try {
      new JmsMessagingClient("5", this, properties, false);
      fail("Creating a Messaging Client with no properties " + "should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Propery values"));
      assertTrue(me.getMessage().contains("must be provided"));
    }

    // Missing connection factory property
    properties = new Properties();
    properties.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    properties.setProperty(
        Context.PROVIDER_URL,
        "vm://localhost?broker.useShutdownHook=false&broker.persistent=false");

    try {
      new JmsMessagingClient("6", this, properties, false);
      fail(
          "Creating a Messaging Client with no connection factory "
              + "property should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Propery values"));
      assertTrue(me.getMessage().contains("must be provided"));
    }

    // Missing provider url property
    properties = new Properties();
    properties.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "ConnectionFactory");

    try {
      new JmsMessagingClient("7", this, properties, false);
      fail(
          "Creating a Messaging Client with no provider url "
              + "property should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Propery values"));
      assertTrue(me.getMessage().contains("must be provided"));
    }

    // Missing initial context factory property
    properties = new Properties();
    properties.setProperty(
        Context.PROVIDER_URL,
        "vm://localhost?broker.useShutdownHook=false&broker.persistent=false");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "ConnectionFactory");

    try {
      new JmsMessagingClient("8", this, properties, false);
      fail(
          "Creating a Messaging Client with no initial context factory "
              + "property should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Propery values"));
      assertTrue(me.getMessage().contains("must be provided"));
    }

    // Missing destination properties
    properties = new Properties();
    properties.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    properties.setProperty(
        Context.PROVIDER_URL,
        "vm://localhost?broker.useShutdownHook=false&broker.persistent=false");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "ConnectionFactory");
    try {
      MessagingClient messagingClient = new JmsMessagingClient("9", this, properties, false);
      messagingClient.start();
      fail(
          "Starting a Messaging Client with no destination "
              + "properties should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("No destinations available"));
    }

    // Invalid initial context factory
    properties = new Properties();
    properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "test.InvalidInitialContextFactory");
    properties.setProperty(
        Context.PROVIDER_URL,
        "vm://localhost?broker.useShutdownHook=false&broker.persistent=false");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "ConnectionFactory");
    try {
      MessagingClient messagingClient = new JmsMessagingClient("10", this, properties, false);
      messagingClient.start();
      fail(
          "Starting a Messaging Client with an invalid initial "
              + "context factory should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Cannot instantiate"));
    }

    // Invalid provider url
    properties = new Properties();
    properties.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    properties.setProperty(Context.PROVIDER_URL, "tcp://localhost:00000");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "ConnectionFactory");
    try {
      MessagingClient messagingClient = new JmsMessagingClient("11", this, properties, false);
      messagingClient.start();
      fail(
          "Starting a Messaging Client with an invalid "
              + "provider url should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Could not connect to broker URL"));
    }

    // Invalid connection factory
    properties = new Properties();
    properties.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    properties.setProperty(
        Context.PROVIDER_URL,
        "vm://localhost?broker.useShutdownHook=false&broker.persistent=false");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "InvalidValue");
    try {
      MessagingClient messagingClient = new JmsMessagingClient("12", this, properties, false);
      messagingClient.start();
      fail(
          "Starting a Messaging Client with an invalid connection "
              + "factory name should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("jndiLookup(InvalidValue) failed"));
    }
  }
  public void initialize() throws Exception {
    this.communicator =
        new Communicator() {

          @Override
          public Location myLocation() {
            return location;
          }

          @Override
          public void sendToCoordinator(Tuple message) {
            senderReceiver.sendAsync(
                new Message(location, PhysicalLocation.coordinatorLocation(), message));
          }

          @Override
          public void sendDownstream(Tuple message) throws NoSuchLocationException {
            if (withinTaskDownstreamNeighbors.isEmpty() && crossTaskDownstreamNeighbors.isEmpty())
              throw new NoSuchLocationException(
                  "Line " + logicalLocation.logId() + " has no downstream neighbor.");
            for (String neighbor : withinTaskDownstreamNeighbors) {
              sendWithinTaskMessage(
                  neighbor, new Message(location, new LogicalLocation(neighbor), message));
            }
            for (String neighbor : crossTaskDownstreamNeighbors) {
              senderReceiver.sendAsync(
                  new Message(location, new LogicalLocation(neighbor), message));
            }
          }

          @Override
          public void sendUpstream(Tuple message) throws NoSuchLocationException {
            if (withinTaskUpstreamNeighbors.isEmpty())
              throw new NoSuchLocationException(
                  "Line " + logicalLocation.logId() + " has no within-task upstream neighbor.");
            for (String neighbor : withinTaskUpstreamNeighbors) {
              sendWithinTaskMessage(
                  neighbor, new Message(location, new LogicalLocation(neighbor), message));
            }
          }

          @Override
          public void sendToAgents(LogicalLocation destination, Tuple tuple)
              throws NoSuchLocationException {
            if (!logicalIds.contains(destination.logId()))
              throw new NoSuchLocationException(
                  "Logical location " + destination + " does not exist.");
            senderReceiver.sendAsync(new Message(location, destination, tuple));
          }
        };

    this.messageReceiptCallback =
        new MessageReceiptCallback() {
          public Tuple receive(Message m) {
            if (m.type() == Message.Type.TAINT) {
              try {
                handleTaintMessage(m);
              } catch (ExecException e) {
                throw new RuntimeException("Error in taint logic.", e);
              }
            } else {
              synchronized (monitorAgent) { // avoid concurrent calls to
                // monitorAgent, and also
                // wait for initialization
                // to finish
                monitorAgent.receiveMessage(m.source(), m.body());
              }
            }
            return null; // default ack
          }

          @Override
          public void ioError(PhysicalLocation addr, Exception e) {}
        };

    synchronized (monitorAgent) { // block tuple-processing and message
      // receipt activity until initialization
      // finishes
      this.senderReceiver = new MessagingClient(masterAddr, messageReceiptCallback);
      this.senderReceiver.connect();

      monitorAgent.initialize(communicator);

      // send registration message to master
      Tuple regResponse = senderReceiver.sendSync(createRegistrationMessage());

      // first part of registration response contains assigned physical
      // location id
      this.location = new PhysicalLocation(logicalLocation, (Integer) regResponse.get(0));

      // next part of registration response contains pending incoming
      // async messages
      for (Iterator<Tuple> it = ((DataBag) regResponse.get(1)).iterator(); it.hasNext(); ) {
        messageReceiptCallback.receive(Message.fromTuple(it.next()));
      }
    }
  }