001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.activemq.camel.component;
019    
020    import java.util.Set;
021    
022    import javax.annotation.PostConstruct;
023    import javax.annotation.PreDestroy;
024    import javax.jms.Connection;
025    import javax.jms.ConnectionFactory;
026    
027    import org.apache.activemq.EnhancedConnection;
028    import org.apache.activemq.advisory.DestinationEvent;
029    import org.apache.activemq.advisory.DestinationListener;
030    import org.apache.activemq.advisory.DestinationSource;
031    import org.apache.activemq.command.ActiveMQDestination;
032    import org.apache.activemq.command.ActiveMQQueue;
033    import org.apache.activemq.command.ActiveMQTopic;
034    import org.apache.camel.CamelContext;
035    import org.apache.camel.CamelContextAware;
036    import org.apache.camel.Endpoint;
037    import org.apache.camel.component.jms.JmsEndpoint;
038    import org.apache.camel.component.jms.JmsQueueEndpoint;
039    import org.apache.camel.util.ObjectHelper;
040    import org.slf4j.Logger;
041    import org.slf4j.LoggerFactory;
042    
043    /**
044     * A helper bean which populates a {@link CamelContext} with ActiveMQ Queue endpoints
045     *
046     *
047     * @org.apache.xbean.XBean
048     */
049    public class CamelEndpointLoader implements CamelContextAware {
050        private static final transient Logger LOG = LoggerFactory.getLogger(CamelEndpointLoader.class);
051        private CamelContext camelContext;
052        private EnhancedConnection connection;
053        private ConnectionFactory connectionFactory;
054        private ActiveMQComponent component;
055    
056        public CamelEndpointLoader() {
057        }
058    
059        public CamelEndpointLoader(CamelContext camelContext) {
060            this.camelContext = camelContext;
061        }
062    
063        /**
064         *
065         * @throws Exception
066         * @org.apache.xbean.InitMethod
067         */
068        @PostConstruct
069        public void afterPropertiesSet() throws Exception {
070            ObjectHelper.notNull(camelContext, "camelContext");
071            if (connection == null) {
072                Connection value = getConnectionFactory().createConnection();
073                if (value instanceof EnhancedConnection) {
074                    connection = (EnhancedConnection) value;
075                }
076                else {
077                    throw new IllegalArgumentException("Created JMS Connection is not an EnhancedConnection: " + value);
078                }
079            }
080            connection.start();
081            DestinationSource source = connection.getDestinationSource();
082            source.setDestinationListener(new DestinationListener() {
083                public void onDestinationEvent(DestinationEvent event) {
084                    try {
085                        ActiveMQDestination destination = event.getDestination();
086                        if (destination instanceof ActiveMQQueue) {
087                            ActiveMQQueue queue = (ActiveMQQueue) destination;
088                            if (event.isAddOperation()) {
089                                addQueue(queue);
090                            }
091                            else {
092                                removeQueue(queue);
093                            }
094                        }
095                        else if (destination instanceof ActiveMQTopic) {
096                          ActiveMQTopic topic = (ActiveMQTopic) destination;
097                          if (event.isAddOperation()) {
098                              addTopic(topic);
099                          }
100                          else {
101                              removeTopic(topic);
102                          }
103                        }
104                    }
105                    catch (Exception e) {
106                        LOG.warn("Caught: " + e, e);
107                    }
108                }
109            });
110    
111            Set<ActiveMQQueue> queues = source.getQueues();
112            for (ActiveMQQueue queue : queues) {
113                addQueue(queue);
114            }
115    
116            Set<ActiveMQTopic> topics = source.getTopics();
117            for (ActiveMQTopic topic : topics) {
118                addTopic(topic);
119            }
120        }
121    
122    
123        /**
124         *
125         * @throws Exception
126         * @org.apache.xbean.DestroyMethod
127         */
128        @PreDestroy
129        public void destroy() throws Exception {
130            if (connection != null) {
131                connection.close();
132                connection = null;
133            }
134        }
135    
136        // Properties
137        //-------------------------------------------------------------------------
138        public CamelContext getCamelContext() {
139            return camelContext;
140        }
141    
142        public void setCamelContext(CamelContext camelContext) {
143            this.camelContext = camelContext;
144        }
145    
146        public EnhancedConnection getConnection() {
147            return connection;
148        }
149    
150        public ConnectionFactory getConnectionFactory() {
151            if (connectionFactory == null
152                    && getComponent().getConfiguration() instanceof ActiveMQConfiguration) {
153                connectionFactory = ((ActiveMQConfiguration) getComponent()
154                        .getConfiguration()).createConnectionFactory();
155            }
156            return connectionFactory;
157        }
158    
159        public void setConnectionFactory(ConnectionFactory connectionFactory) {
160            this.connectionFactory = connectionFactory;
161        }
162    
163        public ActiveMQComponent getComponent() {
164            if (component == null) {
165                component = camelContext.getComponent("activemq", ActiveMQComponent.class);
166            }
167            return component;
168        }
169    
170        public void setComponent(ActiveMQComponent component) {
171            this.component = component;
172        }
173    
174        // Implementation methods
175        //-------------------------------------------------------------------------
176    
177        protected void addQueue(ActiveMQQueue queue) throws Exception {
178            String queueUri = getQueueUri(queue);
179            ActiveMQComponent jmsComponent = getComponent();
180            Endpoint endpoint = new JmsQueueEndpoint(queueUri, jmsComponent, queue.getPhysicalName(), jmsComponent.getConfiguration());
181            camelContext.addEndpoint(queueUri, endpoint);
182        }
183    
184        protected String getQueueUri(ActiveMQQueue queue) {
185            return "activemq:" + queue.getPhysicalName();
186        }
187    
188        protected void removeQueue(ActiveMQQueue queue) throws Exception {
189            String queueUri = getQueueUri(queue);
190            // lur cache of endpoints so they will disappear in time
191            // this feature needs a new component api - list available endpoints
192            camelContext.removeEndpoints(queueUri);
193        }
194    
195        protected void addTopic(ActiveMQTopic topic) throws Exception {
196            String topicUri = getTopicUri(topic);
197            ActiveMQComponent jmsComponent = getComponent();
198            Endpoint endpoint = new JmsEndpoint(topicUri, jmsComponent, topic.getPhysicalName(), true, jmsComponent.getConfiguration());
199            camelContext.addEndpoint(topicUri, endpoint);
200        }
201    
202        protected String getTopicUri(ActiveMQTopic topic) {
203            return "activemq:topic:" + topic.getPhysicalName();
204        }
205    
206        protected void removeTopic(ActiveMQTopic topic) throws Exception {
207            String topicUri = getTopicUri(topic);
208            // lur cache of endpoints so they will disappear in time
209            // this feature needs a new component api - list available endpoints
210            camelContext.removeEndpoints(topicUri);
211        }
212    }