Repository: qpid-jms Updated Branches: refs/heads/master 6a221b168 -> 3fc2d8b1a
Add the ability for adding in a custom intercepter to manage conversion of foreign destination types that we don't know how to convert ourselves. Default implementation will look for isTopic or isQueue method in the class before giving up. Add some tests around this. Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/3fc2d8b1 Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/3fc2d8b1 Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/3fc2d8b1 Branch: refs/heads/master Commit: 3fc2d8b1a1e879a092a881f083cd33b256dff7ac Parents: 6a221b1 Author: Timothy Bish <tabish...@gmail.com> Authored: Wed Oct 15 18:28:07 2014 -0400 Committer: Timothy Bish <tabish...@gmail.com> Committed: Wed Oct 15 18:28:07 2014 -0400 ---------------------------------------------------------------------- ...DefaultUnresolvedDestinationTransformer.java | 77 ++++++++++++++++++++ .../jms/message/JmsMessageTransformation.java | 25 +++++-- .../JmsUnresolvedDestinationTransformer.java | 56 ++++++++++++++ .../message/JmsMessageTransformationTest.java | 41 +++++++++++ 4 files changed, 194 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/3fc2d8b1/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsDefaultUnresolvedDestinationTransformer.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsDefaultUnresolvedDestinationTransformer.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsDefaultUnresolvedDestinationTransformer.java new file mode 100644 index 0000000..1d90cd0 --- /dev/null +++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsDefaultUnresolvedDestinationTransformer.java @@ -0,0 +1,77 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.qpid.jms.message; + +import java.lang.reflect.Method; + +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Queue; +import javax.jms.Topic; + +import org.apache.qpid.jms.JmsDestination; +import org.apache.qpid.jms.JmsQueue; +import org.apache.qpid.jms.JmsTopic; + +/** + * Default Destination resolver that will try and find a way to convert an unknown foreign + * JMS Destination object by looking for method in the object to identify the true type. + * + * For a String destination this class will always return a Queue. + */ +public class JmsDefaultUnresolvedDestinationTransformer implements JmsUnresolvedDestinationTransformer { + + @Override + public JmsDestination transform(Destination destination) throws JMSException { + + String queueName = null; + String topicName = null; + + if (destination instanceof Queue) { + queueName = ((Queue) destination).getQueueName(); + } + + if (destination instanceof Topic) { + topicName = ((Topic) destination).getTopicName(); + } + + if (queueName == null && topicName == null) { + throw new JMSException("Unresolvable destination: Both queue and topic names are null: " + destination); + } + + try { + Method isQueueMethod = destination.getClass().getMethod("isQueue"); + Method isTopicMethod = destination.getClass().getMethod("isTopic"); + Boolean isQueue = (Boolean) isQueueMethod.invoke(destination); + Boolean isTopic = (Boolean) isTopicMethod.invoke(destination); + if (isQueue) { + return new JmsQueue(queueName); + } else if (isTopic) { + return new JmsTopic(topicName); + } else { + throw new JMSException("Unresolvable destination: Neither Queue nor Topic: " + destination); + } + } catch (Exception e) { + throw new JMSException("Unresolvable destination: " + e.getMessage() + ": " + destination); + } + } + + @Override + public JmsDestination transform(String destination) throws JMSException { + return new JmsQueue(destination); + } +} http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/3fc2d8b1/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsMessageTransformation.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsMessageTransformation.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsMessageTransformation.java index 896bc6a..f087fc8 100644 --- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsMessageTransformation.java +++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsMessageTransformation.java @@ -45,6 +45,9 @@ import org.apache.qpid.jms.JmsTopic; */ public final class JmsMessageTransformation { + private static JmsUnresolvedDestinationTransformer unresolvedDestinationHandler = + new JmsDefaultUnresolvedDestinationTransformer(); + /** * Creates a an available JMS message from another provider. * @@ -62,16 +65,16 @@ public final class JmsMessageTransformation { if (destination != null) { if (destination instanceof JmsDestination) { - return (JmsDestination) destination; + result = (JmsDestination) destination; } else if (destination instanceof Queue && destination instanceof Topic) { String queueName = ((Queue) destination).getQueueName(); String topicName = ((Topic) destination).getTopicName(); if (queueName != null && topicName == null) { - return new JmsQueue(queueName); + result = new JmsQueue(queueName); } else if (queueName == null && topicName != null) { - return new JmsTopic(topicName); + result = new JmsTopic(topicName); } else { - throw new JMSException("Could not transform destination: " + destination); + result = unresolvedDestinationHandler.transform(destination); } } else { if (destination instanceof TemporaryQueue) { @@ -83,7 +86,7 @@ public final class JmsMessageTransformation { } else if (destination instanceof Topic) { result = new JmsTopic(((Topic) destination).getTopicName()); } else { - throw new JMSException("Could not transform destination: " + destination); + result = unresolvedDestinationHandler.transform(destination); } } } @@ -207,4 +210,16 @@ public final class JmsMessageTransformation { target.setObjectProperty(name, obj); } } + + public static void setUnresolvedDestinationHandler(JmsUnresolvedDestinationTransformer handler) { + unresolvedDestinationHandler = handler; + } + + public static JmsUnresolvedDestinationTransformer getUnresolvedDestinationTransformer() { + if (unresolvedDestinationHandler == null) { + unresolvedDestinationHandler = new JmsDefaultUnresolvedDestinationTransformer(); + } + + return unresolvedDestinationHandler; + } } http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/3fc2d8b1/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsUnresolvedDestinationTransformer.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsUnresolvedDestinationTransformer.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsUnresolvedDestinationTransformer.java new file mode 100644 index 0000000..693d1f8 --- /dev/null +++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsUnresolvedDestinationTransformer.java @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.qpid.jms.message; + +import javax.jms.Destination; +import javax.jms.JMSException; + +import org.apache.qpid.jms.JmsDestination; + +/** + * Defines an interface for a handler object that will be called when the + * transformation of a JMS Destination object fails to determine the proper + * destination type to create. + */ +public interface JmsUnresolvedDestinationTransformer { + + /** + * Given a JMS Destination attempt to determine the type of JmsDestination to + * create. + * + * @param destination + * the JMS destination that requires conversion to a JmsDestination type. + * + * @return a new JmsDestination instance to match the foreign destination. + * + * @throws JMSException if an error occurs during the transformation. + */ + public JmsDestination transform(Destination destination) throws JMSException; + + /** + * Given a destination name return a matching JmsDestination object. + * + * @param destination + * the name of the destination to create a JmsDestination type for. + * + * @return a new JmsDestination object that matches the given name. + * + * @throws JMSException if an error occurs while transforming the name. + */ + public JmsDestination transform(String destination) throws JMSException; + +} http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/3fc2d8b1/qpid-jms-client/src/test/java/org/apache/qpid/jms/message/JmsMessageTransformationTest.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/message/JmsMessageTransformationTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/message/JmsMessageTransformationTest.java index f974e03..7fe8a80 100644 --- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/message/JmsMessageTransformationTest.java +++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/message/JmsMessageTransformationTest.java @@ -35,6 +35,7 @@ import javax.jms.Topic; import org.apache.qpid.jms.JmsConnection; import org.apache.qpid.jms.JmsDestination; +import org.apache.qpid.jms.JmsQueue; import org.apache.qpid.jms.JmsTopic; import org.apache.qpid.jms.message.facade.defaults.JmsDefaultMessageFacade; import org.apache.qpid.jms.message.facade.defaults.JmsDefaultMessageFactory; @@ -301,6 +302,13 @@ public class JmsMessageTransformationTest { } @Test + public void testUnresolvedDestinationTransformerGetReturnsNonNull() throws JMSException { + assertNotNull(JmsMessageTransformation.getUnresolvedDestinationTransformer()); + JmsMessageTransformation.setUnresolvedDestinationHandler(null); + assertNotNull(JmsMessageTransformation.getUnresolvedDestinationTransformer()); + } + + @Test public void testPlainDestinationThrowsJMSEx() throws JMSException { ForeignDestination destination = new ForeignDestination(DESTINATION_NAME); try { @@ -311,6 +319,16 @@ public class JmsMessageTransformationTest { } @Test + public void testPlainDestinationWithCustomInterceper() throws JMSException { + ForeignDestination destination = new ForeignDestination(DESTINATION_NAME); + JmsMessageTransformation.setUnresolvedDestinationHandler(new AlwaysQueueUnresolvedDestinationHandler()); + JmsDestination result = JmsMessageTransformation.transformDestination(createMockJmsConnection(), destination); + + assertNotNull(result); + assertTrue(result instanceof JmsQueue); + } + + @Test public void testCompositeTopicAndQueueDestinationThrowsJMSEx() throws JMSException { ForeignDestination destination = new ForeignTopicAndQueue(DESTINATION_NAME); try { @@ -321,6 +339,16 @@ public class JmsMessageTransformationTest { } @Test + public void testCompositeTopicAndQueueDestinationCanBeInterceper() throws JMSException { + ForeignDestination destination = new ForeignTopicAndQueue(DESTINATION_NAME); + JmsMessageTransformation.setUnresolvedDestinationHandler(new AlwaysQueueUnresolvedDestinationHandler()); + JmsDestination result = JmsMessageTransformation.transformDestination(createMockJmsConnection(), destination); + + assertNotNull(result); + assertTrue(result instanceof JmsQueue); + } + + @Test public void testCompositeTopicAndQueueDestinationNoNameThrowsJMSEx() throws JMSException { ForeignTopicAndQueue destination = new ForeignTopicAndQueue(DESTINATION_NAME); destination.setReturnQueueName(false); @@ -521,4 +549,17 @@ public class JmsMessageTransformationTest { this.returnQueueName = returnQueueName; } } + + private class AlwaysQueueUnresolvedDestinationHandler implements JmsUnresolvedDestinationTransformer { + + @Override + public JmsDestination transform(Destination destination) throws JMSException { + return new JmsQueue(destination.toString()); + } + + @Override + public JmsDestination transform(String destination) throws JMSException { + return new JmsQueue(destination); + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org