001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.command;
018
019import javax.jms.Destination;
020import javax.jms.JMSException;
021import javax.jms.Queue;
022import javax.jms.Topic;
023import java.lang.reflect.Method;
024
025public class DefaultUnresolvedDestinationTransformer implements UnresolvedDestinationTransformer {
026
027    @Override
028    public ActiveMQDestination transform(Destination dest) throws JMSException {
029        String queueName = ((Queue) dest).getQueueName();
030        String topicName = ((Topic) dest).getTopicName();
031
032        if (queueName == null && topicName == null) {
033            throw new JMSException("Unresolvable destination: Both queue and topic names are null: " + dest);
034        }
035        try {
036            Method isQueueMethod = dest.getClass().getMethod("isQueue");
037            Method isTopicMethod = dest.getClass().getMethod("isTopic");
038            Boolean isQueue = (Boolean) isQueueMethod.invoke(dest);
039            Boolean isTopic = (Boolean) isTopicMethod.invoke(dest);
040            if (isQueue) {
041                return new ActiveMQQueue(queueName);
042            } else if (isTopic) {
043                return new ActiveMQTopic(topicName);
044            } else {
045                throw new JMSException("Unresolvable destination: Neither Queue nor Topic: " + dest);
046            }
047        } catch (Exception e)  {
048            throw new JMSException("Unresolvable destination: "  + e.getMessage() + ": " + dest);
049        }
050    }
051
052    @Override
053    public ActiveMQDestination transform(String dest) throws JMSException {
054        return new ActiveMQQueue(dest);
055    }
056}