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     */
017    package org.apache.camel.component.jms;
018    
019    import javax.jms.JMSException;
020    import javax.jms.Session;
021    import javax.jms.TemporaryQueue;
022    import javax.jms.TemporaryTopic;
023    
024    import org.springframework.jms.core.JmsOperations;
025    import org.springframework.jms.core.SessionCallback;
026    
027    /**
028     * A class which represents some metadata about the underlying JMS provider
029     * so that we can properly bridge JMS providers such as for dealing with temporary destinations.
030     *
031     * @version $Revision: 469 $
032     */
033    public class JmsProviderMetadata {
034        private Class<? extends TemporaryQueue> temporaryQueueType;
035        private Class<? extends TemporaryTopic> temporaryTopicType;
036    
037        /**
038         * Lazily loads the temporary queue type if one has not been explicitly configured
039         * via calling the {@link #setTemporaryQueueType(Class)}
040         */
041        public Class<? extends TemporaryQueue> getTemporaryQueueType(JmsOperations template) {
042            Class<? extends TemporaryQueue> answer = getTemporaryQueueType();
043            if (answer == null) {
044                loadTemporaryDestinationTypes(template);
045                answer = getTemporaryQueueType();
046            }
047            return answer;
048        }
049    
050        /**
051         * Lazily loads the temporary topic type if one has not been explicitly configured
052         * via calling the {@link #setTemporaryTopicType(Class)}
053         */
054        public Class<? extends TemporaryTopic> getTemporaryTopicType(JmsOperations template) {
055            Class<? extends TemporaryTopic> answer = getTemporaryTopicType();
056            if (answer == null) {
057                loadTemporaryDestinationTypes(template);
058                answer = getTemporaryTopicType();
059            }
060            return answer;
061        }
062    
063        // Properties
064        //-------------------------------------------------------------------------
065    
066        public Class<? extends TemporaryQueue> getTemporaryQueueType() {
067            return temporaryQueueType;
068        }
069    
070        public void setTemporaryQueueType(Class<? extends TemporaryQueue> temporaryQueueType) {
071            this.temporaryQueueType = temporaryQueueType;
072        }
073    
074        public Class<? extends TemporaryTopic> getTemporaryTopicType() {
075            return temporaryTopicType;
076        }
077    
078        public void setTemporaryTopicType(Class<? extends TemporaryTopic> temporaryTopicType) {
079            this.temporaryTopicType = temporaryTopicType;
080        }
081    
082        // Implementation methods
083        //-------------------------------------------------------------------------
084        protected void loadTemporaryDestinationTypes(JmsOperations template) {
085            if (template == null) {
086                throw new IllegalArgumentException("No JmsTemplate supplied!");
087            }
088            template.execute(new SessionCallback() {
089                public Object doInJms(Session session) throws JMSException {
090                    TemporaryQueue queue = session.createTemporaryQueue();
091                    setTemporaryQueueType(queue.getClass());
092    
093                    TemporaryTopic topic = session.createTemporaryTopic();
094                    setTemporaryTopicType(topic.getClass());
095    
096                    queue.delete();
097                    topic.delete();
098                    return null;
099                }
100            });
101        }
102    }