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.camel.component.broker;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.activemq.broker.view.MessageBrokerView;
025import org.apache.activemq.broker.view.MessageBrokerViewRegistry;
026import org.apache.activemq.command.ActiveMQDestination;
027import org.apache.camel.ComponentConfiguration;
028import org.apache.camel.Endpoint;
029import org.apache.camel.component.jms.JmsConfiguration;
030import org.apache.camel.impl.UriEndpointComponent;
031import org.apache.camel.spi.EndpointCompleter;
032
033import static org.apache.camel.util.ObjectHelper.removeStartingCharacters;
034
035/**
036 * The <a href="http://activemq.apache.org/broker-camel-component.html">Broker Camel component</a> allows to use Camel
037 * routing to move messages through the broker.
038 */
039public class BrokerComponent extends UriEndpointComponent implements EndpointCompleter {
040
041    public BrokerComponent() {
042        super(BrokerEndpoint.class);
043    }
044
045    @Override
046    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
047        BrokerConfiguration brokerConfiguration = new BrokerConfiguration();
048        setProperties(brokerConfiguration, parameters);
049
050        byte destinationType = ActiveMQDestination.QUEUE_TYPE;
051
052        if (remaining.startsWith(JmsConfiguration.QUEUE_PREFIX)) {
053            remaining = removeStartingCharacters(remaining.substring(JmsConfiguration.QUEUE_PREFIX.length()), '/');
054        } else if (remaining.startsWith(JmsConfiguration.TOPIC_PREFIX)) {
055            destinationType = ActiveMQDestination.TOPIC_TYPE;
056            remaining = removeStartingCharacters(remaining.substring(JmsConfiguration.TOPIC_PREFIX.length()), '/');
057        } else if (remaining.startsWith(JmsConfiguration.TEMP_QUEUE_PREFIX)) {
058            destinationType = ActiveMQDestination.TEMP_QUEUE_TYPE;
059            remaining = removeStartingCharacters(remaining.substring(JmsConfiguration.TEMP_QUEUE_PREFIX.length()), '/');
060        } else if (remaining.startsWith(JmsConfiguration.TEMP_TOPIC_PREFIX)) {
061            destinationType = ActiveMQDestination.TEMP_TOPIC_TYPE;
062            remaining = removeStartingCharacters(remaining.substring(JmsConfiguration.TEMP_TOPIC_PREFIX.length()), '/');
063        }
064
065        ActiveMQDestination destination = ActiveMQDestination.createDestination(remaining, destinationType);
066        BrokerEndpoint brokerEndpoint = new BrokerEndpoint(uri, this, remaining, destination, brokerConfiguration);
067        setProperties(brokerEndpoint, parameters);
068        return brokerEndpoint;
069    }
070
071    @Override
072    public List<String> completeEndpointPath(ComponentConfiguration componentConfiguration, String completionText) {
073        String brokerName = String.valueOf(componentConfiguration.getParameter("brokerName"));
074        MessageBrokerView messageBrokerView = MessageBrokerViewRegistry.getInstance().lookup(brokerName);
075        if (messageBrokerView != null) {
076            String destinationName = completionText;
077            Set<? extends ActiveMQDestination> set = messageBrokerView.getQueues();
078            if (completionText.startsWith("topic:")) {
079                set = messageBrokerView.getTopics();
080                destinationName = completionText.substring(6);
081            } else if (completionText.startsWith("queue:")) {
082                destinationName = completionText.substring(6);
083            }
084            ArrayList<String> answer = new ArrayList<String>();
085            for (ActiveMQDestination destination : set) {
086                if (destination.getPhysicalName().startsWith(destinationName)) {
087                    answer.add(destination.getPhysicalName());
088                }
089            }
090            return answer;
091
092        }
093        return null;
094    }
095}