/** Test that the <code>Message.getPropertyNames()</code> methods. */
 @Test
 public void testPropertyIteration() {
   try {
     Message message = senderSession.createMessage();
     Enumeration enumeration = message.getPropertyNames();
     // there can be some properties already defined (e.g. JMSXDeliveryCount)
     int originalCount = 0;
     while (enumeration.hasMoreElements()) {
       enumeration.nextElement();
       originalCount++;
     }
     message.setDoubleProperty("pi", 3.14159);
     enumeration = message.getPropertyNames();
     boolean foundPiProperty = false;
     int newCount = 0;
     while (enumeration.hasMoreElements()) {
       String propName = (String) enumeration.nextElement();
       newCount++;
       if ("pi".equals(propName)) {
         foundPiProperty = true;
       }
     }
     Assert.assertEquals(originalCount + 1, newCount);
     Assert.assertTrue(foundPiProperty);
   } catch (JMSException e) {
     fail(e);
   }
 }
  @Override
  public MessageDetails createMessageDetailsFor(Destination incomingDestination, Message message) {
    try {
      Destination replyTo = message.getJMSReplyTo();
      String correlationId = message.getJMSCorrelationID();
      Map<String, String> customHeaders = null;

      if (correlationId != null && replyTo != null) {
        customHeaders = new HashMap<String, String>(3, 1.0f);
        Enumeration<String> enumeration = message.getPropertyNames();
        while (enumeration.hasMoreElements()) {
          String key = enumeration.nextElement();
          customHeaders.put(key, String.valueOf(message.getObjectProperty(key)));
        }
      }

      return new MessageDetails.Builder<Destination>()
          .withDestination(incomingDestination)
          .withCorrelationId(correlationId)
          .withReplyTo(replyTo)
          .withCustomHeaders(customHeaders)
          .build();
    } catch (JMSException e) {
      throw new RuntimeException("Unable to parse incoming message", e);
    }
  }
Exemplo n.º 3
0
  public static final void printMessageHeaders(Message pMessge, Object pHandler) {
    StringWriter stringWriter = new StringWriter();

    PrintWriter writer = new PrintWriter(stringWriter);

    try {
      writer.println("===================================================");
      writer.println("MDB Event Handler class=[" + pHandler.getClass().getName() + "]");
      writer.println("");
      writer.println("JMS Message Id: " + pMessge.getJMSMessageID());

      for (Enumeration em = pMessge.getPropertyNames(); em.hasMoreElements(); ) {
        String property = (String) em.nextElement();
        writer.println(
            "JMS Prop=[" + property + "] value=[" + pMessge.getStringProperty(property) + "]");
      }

      writer.println("===================================================");
    } catch (JMSException e) {
      e.printStackTrace();
    }

    writer.flush();
    System.out.println(stringWriter.toString());
  }
  protected JBossMessage(final Message foreign, final byte type, final ClientSession session)
      throws JMSException {
    this(type, session);

    setJMSTimestamp(foreign.getJMSTimestamp());

    try {
      byte[] corrIDBytes = foreign.getJMSCorrelationIDAsBytes();
      setJMSCorrelationIDAsBytes(corrIDBytes);
    } catch (JMSException e) {
      // specified as String
      String corrIDString = foreign.getJMSCorrelationID();
      if (corrIDString != null) {
        setJMSCorrelationID(corrIDString);
      }
    }

    setJMSReplyTo(foreign.getJMSReplyTo());
    setJMSDestination(foreign.getJMSDestination());
    setJMSDeliveryMode(foreign.getJMSDeliveryMode());
    setJMSExpiration(foreign.getJMSExpiration());
    setJMSPriority(foreign.getJMSPriority());
    setJMSType(foreign.getJMSType());

    // We can't avoid a cast warning here since getPropertyNames() is on the JMS API
    for (Enumeration<String> props = foreign.getPropertyNames(); props.hasMoreElements(); ) {
      String name = (String) props.nextElement();

      Object prop = foreign.getObjectProperty(name);

      this.setObjectProperty(name, prop);
    }
  }
  /**
   * Get property names
   *
   * @return The values
   * @throws JMSException Thrown if an error occurs
   */
  @Override
  @SuppressWarnings("rawtypes")
  public Enumeration getPropertyNames() throws JMSException {
    if (ActiveMQRAMessage.trace) {
      ActiveMQRALogger.LOGGER.trace("getPropertyNames()");
    }

    return message.getPropertyNames();
  }
Exemplo n.º 6
0
  protected HornetQMessage(final Message foreign, final byte type, final ClientSession session)
      throws JMSException {
    this(type, session);

    setJMSTimestamp(foreign.getJMSTimestamp());

    String value =
        System.getProperty(
            HornetQJMSConstants.JMS_HORNETQ_ENABLE_BYTE_ARRAY_JMS_CORRELATION_ID_PROPERTY_NAME);

    boolean supportBytesId = !"false".equals(value);

    if (supportBytesId) {
      try {
        byte[] corrIDBytes = foreign.getJMSCorrelationIDAsBytes();
        setJMSCorrelationIDAsBytes(corrIDBytes);
      } catch (JMSException e) {
        // specified as String
        String corrIDString = foreign.getJMSCorrelationID();
        if (corrIDString != null) {
          setJMSCorrelationID(corrIDString);
        }
      }
    } else {
      // Some providers, like WSMQ do automatic conversions between native byte[] correlation id
      // and String correlation id. This makes it impossible for HQ to guarantee to return the
      // correct
      // type as set by the user
      // So we allow the behaviour to be overridden by a system property
      // https://jira.jboss.org/jira/browse/HORNETQ-356
      // https://jira.jboss.org/jira/browse/HORNETQ-332
      String corrIDString = foreign.getJMSCorrelationID();
      if (corrIDString != null) {
        setJMSCorrelationID(corrIDString);
      }
    }

    setJMSReplyTo(foreign.getJMSReplyTo());
    setJMSDestination(foreign.getJMSDestination());
    setJMSDeliveryMode(foreign.getJMSDeliveryMode());
    setJMSExpiration(foreign.getJMSExpiration());
    setJMSPriority(foreign.getJMSPriority());
    setJMSType(foreign.getJMSType());

    // We can't avoid a cast warning here since getPropertyNames() is on the JMS API
    for (Enumeration<String> props = foreign.getPropertyNames(); props.hasMoreElements(); ) {
      String name = props.nextElement();

      Object prop = foreign.getObjectProperty(name);

      setObjectProperty(name, prop);
    }
  }
Exemplo n.º 7
0
 @SuppressWarnings("unchecked")
 private static Map<String, Object> properties(final javax.jms.Message message)
     throws JMSException {
   final HashMap<String, Object> result = new HashMap<>();
   final Enumeration<String> propertyNames = message.getPropertyNames();
   while (propertyNames.hasMoreElements()) {
     final String propertyName = propertyNames.nextElement();
     result.put(propertyName, message.getObjectProperty(propertyName));
   }
   result.put(JMS_MESSAGE_TYPE, getJmsMessageType(message));
   return result;
 }
Exemplo n.º 8
0
  public Map<String, Object> extractHeadersFromJms(Message jmsMessage, Exchange exchange) {
    Map<String, Object> map = new HashMap<String, Object>();
    if (jmsMessage != null) {
      // lets populate the standard JMS message headers
      try {
        map.put("JMSCorrelationID", jmsMessage.getJMSCorrelationID());
        map.put("JMSCorrelationIDAsBytes", JmsMessageHelper.getJMSCorrelationIDAsBytes(jmsMessage));
        map.put("JMSDeliveryMode", jmsMessage.getJMSDeliveryMode());
        map.put("JMSDestination", jmsMessage.getJMSDestination());
        map.put("JMSExpiration", jmsMessage.getJMSExpiration());
        map.put("JMSMessageID", jmsMessage.getJMSMessageID());
        map.put("JMSPriority", jmsMessage.getJMSPriority());
        map.put("JMSRedelivered", jmsMessage.getJMSRedelivered());
        map.put("JMSTimestamp", jmsMessage.getJMSTimestamp());

        map.put("JMSReplyTo", JmsMessageHelper.getJMSReplyTo(jmsMessage));
        map.put("JMSType", JmsMessageHelper.getJMSType(jmsMessage));

        // this works around a bug in the ActiveMQ property handling
        map.put(JMS_X_GROUP_ID, JmsMessageHelper.getStringProperty(jmsMessage, JMS_X_GROUP_ID));
        map.put("JMSXUserID", JmsMessageHelper.getStringProperty(jmsMessage, "JMSXUserID"));
      } catch (JMSException e) {
        throw new RuntimeCamelException(e);
      }

      Enumeration<?> names;
      try {
        names = jmsMessage.getPropertyNames();
      } catch (JMSException e) {
        throw new RuntimeCamelException(e);
      }
      while (names.hasMoreElements()) {
        String name = names.nextElement().toString();
        try {
          Object value = JmsMessageHelper.getProperty(jmsMessage, name);
          if (headerFilterStrategy != null
              && headerFilterStrategy.applyFilterToExternalHeaders(name, value, exchange)) {
            continue;
          }

          // must decode back from safe JMS header name to original header name
          // when storing on this Camel JmsMessage object.
          String key = jmsKeyFormatStrategy.decodeKey(name);
          map.put(key, value);
        } catch (JMSException e) {
          throw new RuntimeCamelException(name, e);
        }
      }
    }

    return map;
  }
Exemplo n.º 9
0
 private void addData(Message message, String text) throws JMSException {
   MessageData data = new MessageData();
   data.setPayload(text);
   Map<String, String> headers = new HashMap<String, String>();
   Enumeration<String> names = message.getPropertyNames();
   while (names.hasMoreElements()) {
     String name = names.nextElement();
     String value = message.getStringProperty(name);
     headers.put(name, value);
   }
   data.setHeaders(headers);
   messages.add(data);
 }
Exemplo n.º 10
0
 public static void constructConnectorMessageCustomProperties(
     Message jmsMsg, CustomMessagePropertyList customMessagePropertyList) throws JMSException {
   List<CustomMessageProperty> customMessageProperties =
       customMessagePropertyList.getCustomMessageProperty();
   Enumeration enumProps = jmsMsg.getPropertyNames();
   while (enumProps.hasMoreElements()) {
     String key = (String) enumProps.nextElement();
     CustomMessageProperty customMessageProperty = new CustomMessageProperty();
     customMessageProperty.setName(key);
     customMessageProperty.setValue(jmsMsg.getStringProperty(key));
     customMessageProperties.add(customMessageProperty);
   }
 }
Exemplo n.º 11
0
 public Map<String, Object> getPropertiesMap() throws JMSException {
   Map<String, Object> answer = new HashMap<String, Object>();
   Message aMessage = getMessage();
   Enumeration iter = aMessage.getPropertyNames();
   while (iter.hasMoreElements()) {
     String name = (String) iter.nextElement();
     Object value = aMessage.getObjectProperty(name);
     if (value != null) {
       answer.put(name, value);
     }
   }
   return answer;
 }
Exemplo n.º 12
0
 public String send(
     final T message,
     final Message correlatedMessage,
     final String correlationId,
     final URI dvbEndpointSelector,
     final String jmsGroupId,
     final Destination replyQueue) {
   try {
     final M jmsMessage = this.messageHandler.createEmptyMessage(this.sessionDelegate);
     jmsMessage.setJMSCorrelationID(determineCorrelationId(correlationId, correlatedMessage));
     Destination replyTo =
         replyQueue == null ? this.defaultReplyDestinationDelegate.delegate() : replyQueue;
     if (replyTo != null) {
       jmsMessage.setJMSReplyTo(replyTo);
     }
     final URI temp = DataUtilities.coalesce(dvbEndpointSelector, this.defaultEndpoint);
     if (temp != null) {
       jmsMessage.setStringProperty(
           JMSMessageSender.ENDPOINT_NAMESPACE, dvbEndpointSelector.toASCIIString());
     }
     if (correlatedMessage != null && this.copyPropertyPattern != null) {
       //noinspection unchecked
       for (final String name :
           Collections.list((Enumeration<String>) correlatedMessage.getPropertyNames())) {
         if (this.copyPropertyPattern.matcher(name).matches()) {
           if (correlatedMessage.getStringProperty(name) != null) {
             jmsMessage.setStringProperty(name, correlatedMessage.getStringProperty(name));
           } else {
             jmsMessage.setObjectProperty(name, correlatedMessage.getObjectProperty(name));
           }
         }
       }
     }
     this.messageHandler.fillMessage(message, jmsMessage);
     if (jmsGroupId != null) {
       jmsMessage.setStringProperty(JMSMessageSender.JMSX_GROUP_ID, jmsGroupId);
     }
     Delegate<MessageProducer> producer = this.producerDelegate;
     if (correlatedMessage != null && correlatedMessage.getJMSReplyTo() != null) {
       producer =
           new JMSMessageProducerDelegate(
               sessionDelegate, new UnmodifiableDelegate<>(correlatedMessage.getJMSReplyTo()));
     }
     producer.delegate().send(jmsMessage);
     return jmsMessage.getJMSMessageID();
   } catch (final JMSException e) {
     throw DelegatedException.delegate(e);
   }
 }
 static Map<String, Set<String>> get(final Message msg, final String prefix) throws JMSException {
   String regexSafeDelimiter = Pattern.quote(DELIMITER);
   final Map<String, Set<String>> map = new HashMap<String, Set<String>>();
   final Enumeration names = msg.getPropertyNames();
   while (names.hasMoreElements()) {
     final String name = names.nextElement().toString();
     final String value = msg.getStringProperty(name);
     if (name.startsWith(prefix) && name.endsWith(COPY) == false) {
       final String property = msg.getStringProperty(name + COPY);
       if (property != null) {
         map.put(value, new HashSet<String>(Arrays.asList(property.split(regexSafeDelimiter))));
       }
     }
   }
   return map;
 }
  static Map<String, Object> mockAttributes(Message message) throws JMSException {
    final Map<String, Object> mockMap = new HashMap<String, Object>();
    mockMap.put("test-string", "test-value");
    mockMap.put("test-int", Integer.valueOf(1));

    when(message.getPropertyNames())
        .thenReturn(
            new Enumeration<String>() {
              Iterator<String> iter = mockMap.keySet().iterator();

              public boolean hasMoreElements() {
                return iter.hasNext();
              }

              public String nextElement() {
                return iter.next();
              }
            });

    when(message.getObjectProperty(
            argThat(
                new BaseMatcher<String>() {
                  public boolean matches(Object val) {
                    return "test-string".equals(val);
                  }

                  public void describeTo(Description desc) {
                    // do nothing
                  }
                })))
        .thenReturn(mockMap.get("test-string"));

    when(message.getObjectProperty(
            argThat(
                new BaseMatcher<String>() {
                  public boolean matches(Object val) {
                    return "test-int".equals(val);
                  }

                  public void describeTo(Description desc) {
                    // do nothing
                  }
                })))
        .thenReturn(mockMap.get("test-int"));
    return mockMap;
  }
  public static void copyProperties(Message inputMessage, Message outputMessage, List excludeProps)
      throws JMSException {

    if (inputMessage == null || outputMessage == null) {
      return;
    }

    Enumeration propNames = inputMessage.getPropertyNames();

    while (propNames.hasMoreElements()) {
      String propName = (String) propNames.nextElement();

      if (excludeProps == null || !excludeProps.contains(propName)) {
        Object value = getPropertyValue(propName, inputMessage);
        setProperty(propName, value, outputMessage);
      }
    }
  }
  public static HashMap getAllProperties(Message jmsMessage) throws JMSException {
    HashMap props = new HashMap();

    if (jmsMessage == null) {
      return props;
    }

    Enumeration propNames = jmsMessage.getPropertyNames();

    while (propNames.hasMoreElements()) {
      String propName = (String) propNames.nextElement();
      Object value = getPropertyValue(propName, jmsMessage);

      props.put(propName, value);
    }

    return props;
  }
 /**
  * Test that the <code>Message.getPropertyNames()</code> method does not return the name of the
  * JMS standard header fields (e.g. <code>JMSCorrelationID</code>).
  */
 @Test
 public void testGetPropertyNames() {
   try {
     Message message = senderSession.createMessage();
     message.setJMSCorrelationID("foo");
     Enumeration enumeration = message.getPropertyNames();
     while (enumeration.hasMoreElements()) {
       String propName = (String) enumeration.nextElement();
       boolean valid = !propName.startsWith("JMS") || propName.startsWith("JMSX");
       Assert.assertTrue(
           "sec. 3.5.6 The getPropertyNames method does not return the names of "
               + "the JMS standard header field [e.g. JMSCorrelationID]: "
               + propName,
           valid);
     }
   } catch (JMSException e) {
     fail(e);
   }
 }
  public void onMessage(Message message) {
    if (DEBUG) {
      StringWriter stringWriter = new StringWriter();
      PrintWriter writer = new PrintWriter(stringWriter);

      try {
        writer.println("===================================================");
        writer.println("MDB class=[" + this.getClass().getName() + "]");
        writer.println("");
        writer.println("JMS Message Id: " + message.getJMSMessageID());

        for (Enumeration em = message.getPropertyNames(); em.hasMoreElements(); ) {
          String property = (String) em.nextElement();
          writer.println(
              "JMS Prop=[" + property + "] value=[" + message.getStringProperty(property) + "]");
        }

        writer.println("===================================================");
      } catch (JMSException e) {
        e.printStackTrace(writer);
      }

      writer.flush();
      System.out.println(stringWriter.toString());
    }

    if (message instanceof TextMessage) {
      TextMessage msg = (TextMessage) message;

      try {
        System.out.println("B -- RDTListenerBeanB Message:" + msg.getText());
      } catch (JMSException e) {
        e.printStackTrace();
      }
    } else {
      // DEMO is expecting text messages
    }

    try {
      Thread.sleep(1500);
    } catch (Exception ignore) {
    }
  }
Exemplo n.º 19
0
 @Test
 public void testIterator() throws JMSException {
   Message msg = createSession().createMessage();
   Properties properties = new Properties();
   for (int i = 0; i < 100; ++i) {
     String key = "a" + i;
     String value = RandomData.readString();
     properties.setProperty(key, value);
     msg.setStringProperty(key, value);
   }
   Message msgOut = sendAndReceive(msg);
   Enumeration e = msg.getPropertyNames();
   int count = 0;
   while (e.hasMoreElements()) {
     ++count;
     String key = (String) e.nextElement();
     String value = properties.getProperty(key);
     Assert.assertEquals(value, msg.getStringProperty(key));
   }
   Assert.assertEquals(
       "Message did not return the expected # of properties", properties.size(), count);
 }
Exemplo n.º 20
0
  /*
   * JMS does not let you add a property on received message without first
   * calling clearProperties, so we need to save and re-add all the old properties so we
   * don't lose them!!
   */
  private static void copyProperties(final Message msg) throws JMSException {
    @SuppressWarnings("unchecked")
    Enumeration<String> en = msg.getPropertyNames();

    Map<String, Object> oldProps = null;

    while (en.hasMoreElements()) {
      String propName = en.nextElement();

      if (oldProps == null) {
        oldProps = new HashMap<String, Object>();
      }

      oldProps.put(propName, msg.getObjectProperty(propName));
    }

    msg.clearProperties();

    if (oldProps != null) {
      Iterator<Entry<String, Object>> oldPropsIter = oldProps.entrySet().iterator();

      while (oldPropsIter.hasNext()) {
        Entry<String, Object> entry = oldPropsIter.next();

        String propName = entry.getKey();

        Object val = entry.getValue();

        if (val instanceof byte[] == false) {
          // Can't set byte[] array props through the JMS API - if we're bridging a HornetQ message
          // it might have such props
          msg.setObjectProperty(propName, entry.getValue());
        } else if (msg instanceof HornetQMessage) {
          ((HornetQMessage) msg).getCoreMessage().putBytesProperty(propName, (byte[]) val);
        }
      }
    }
  }
Exemplo n.º 21
0
  private void convertMessageProperties(
      final javax.jms.Message message, final MessageImpl replacementMessage) throws JMSException {
    Enumeration propertyNames = message.getPropertyNames();
    while (propertyNames.hasMoreElements()) {
      String propertyName = String.valueOf(propertyNames.nextElement());
      // TODO: Shouldn't need to check for JMS properties here as don't think getPropertyNames()
      // should return them
      if (!propertyName.startsWith("JMSX_")) {
        Object value = message.getObjectProperty(propertyName);
        replacementMessage.setObjectProperty(propertyName, value);
      }
    }

    replacementMessage.setJMSDeliveryMode(message.getJMSDeliveryMode());

    if (message.getJMSReplyTo() != null) {
      replacementMessage.setJMSReplyTo(message.getJMSReplyTo());
    }

    replacementMessage.setJMSType(message.getJMSType());

    replacementMessage.setJMSCorrelationID(message.getJMSCorrelationID());
  }
Exemplo n.º 22
0
 /** {@inheritDoc} */
 @Override
 public void mapFrom(JMSBindingData source, Context context) throws Exception {
   Message message = source.getMessage();
   Enumeration<?> e = message.getPropertyNames();
   while (e.hasMoreElements()) {
     String key = e.nextElement().toString();
     if (matches(key)) {
       Object value = null;
       try {
         value = message.getObjectProperty(key);
       } catch (JMSException pce) {
         // ignore and keep going (here just to keep checkstyle happy)
         pce.getMessage();
       }
       if (value != null) {
         // JMS Message properties -> Context EXCHANGE properties
         context
             .setProperty(key, value, Scope.EXCHANGE)
             .addLabels(JCAComposition.JCA_MESSAGE_PROPERTY);
       }
     }
   }
 }
Exemplo n.º 23
0
  /** {@inheritDoc} */
  @Override
  public void mapFrom(JMSBindingData source, Context context) throws Exception {
    super.mapFrom(source, context);

    Message message = source.getMessage();

    // process JMS headers
    if (matches(HEADER_JMS_DESTINATION)) {
      context
          .setProperty(HEADER_JMS_DESTINATION, message.getJMSDestination())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_DELIVERY_MODE)) {
      context
          .setProperty(HEADER_JMS_DELIVERY_MODE, message.getJMSDeliveryMode())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_EXPIRATION)) {
      context
          .setProperty(HEADER_JMS_EXPIRATION, message.getJMSExpiration())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_PRIORITY)) {
      context
          .setProperty(HEADER_JMS_PRIORITY, message.getJMSPriority())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_MESSAGE_ID)) {
      context
          .setProperty(HEADER_JMS_MESSAGE_ID, message.getJMSMessageID())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_TIMESTAMP)) {
      context
          .setProperty(HEADER_JMS_TIMESTAMP, message.getJMSTimestamp())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_CORRELATION_ID)) {
      context
          .setProperty(HEADER_JMS_CORRELATION_ID, message.getJMSCorrelationID())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_REPLY_TO)) {
      context
          .setProperty(HEADER_JMS_REPLY_TO, message.getJMSReplyTo())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_TYPE)) {
      context.setProperty(HEADER_JMS_TYPE, message.getJMSType()).addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_REDELIVERED)) {
      context
          .setProperty(HEADER_JMS_REDELIVERED, message.getJMSRedelivered())
          .addLabels(JMS_HEADER_LABELS);
    }

    // process JMS properties
    Enumeration<?> e = message.getPropertyNames();
    while (e.hasMoreElements()) {
      String key = e.nextElement().toString();
      if (matches(key)) {
        Object value = null;
        try {
          value = message.getObjectProperty(key);
        } catch (JMSException pce) {
          // ignore and keep going (here just to keep checkstyle happy)
          pce.getMessage();
        }
        if (value != null) {
          context.setProperty(key, value).addLabels(JMS_PROPERTY_LABELS);
        }
      } else if (matches(key, getIncludeRegexes(), new ArrayList<Pattern>())) {
        Object value = null;
        try {
          value = message.getObjectProperty(key);
        } catch (JMSException pce) {
          // ignore and keep going (here just to keep checkstyle happy)
          pce.getMessage();
        }
        if (value != null) {
          context.setProperty(key, value).addLabels(JMS_PROPERTY_LABELS);
        }
      }
    }
  }