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.activemq.broker.region.virtual;
018    
019    import org.apache.activemq.broker.Broker;
020    import org.apache.activemq.broker.ConnectionContext;
021    import org.apache.activemq.broker.region.Destination;
022    import org.apache.activemq.command.ActiveMQDestination;
023    import org.apache.activemq.command.ActiveMQQueue;
024    import org.apache.activemq.command.ActiveMQTopic;
025    import org.apache.activemq.filter.DestinationFilter;
026    
027    /**
028     * Creates <a href="http://activemq.org/site/virtual-destinations.html">Virtual
029     * Topics</a> using a prefix and postfix. The virtual destination creates a
030     * wildcard that is then used to look up all active queue subscriptions which
031     * match.
032     * 
033     * @org.apache.xbean.XBean
034     * 
035     * 
036     */
037    public class VirtualTopic implements VirtualDestination {
038    
039        private String prefix = "Consumer.*.";
040        private String postfix = "";
041        private String name = ">";
042        private boolean selectorAware = false;
043        private boolean local = false;
044    
045    
046        public ActiveMQDestination getVirtualDestination() {
047            return new ActiveMQTopic(getName());
048        }
049    
050        public Destination intercept(Destination destination) {
051            return selectorAware ? new SelectorAwareVirtualTopicInterceptor(destination, getPrefix(), getPostfix(), isLocal()) :
052                new VirtualTopicInterceptor(destination, getPrefix(), getPostfix(), isLocal());
053        }
054        
055    
056        public void create(Broker broker, ConnectionContext context, ActiveMQDestination destination) throws Exception {
057            if (destination.isQueue() && destination.isPattern() && broker.getDestinations(destination).isEmpty()) {
058                DestinationFilter filter = DestinationFilter.parseFilter(new ActiveMQQueue(prefix + DestinationFilter.ANY_DESCENDENT));
059                if (filter.matches(destination)) {
060                    broker.addDestination(context, destination, false);
061                }
062            }
063        }
064    
065        public void remove(Destination destination) {        
066        }
067        
068        // Properties
069        // -------------------------------------------------------------------------
070    
071        public String getPostfix() {
072            return postfix;
073        }
074    
075        /**
076         * Sets any postix used to identify the queue consumers
077         */
078        public void setPostfix(String postfix) {
079            this.postfix = postfix;
080        }
081    
082        public String getPrefix() {
083            return prefix;
084        }
085    
086        /**
087         * Sets the prefix wildcard used to identify the queue consumers for a given
088         * topic
089         */
090        public void setPrefix(String prefix) {
091            this.prefix = prefix;
092        }
093    
094        public String getName() {
095            return name;
096        }
097    
098        public void setName(String name) {
099            this.name = name;
100        }
101        
102        /**
103         * Indicates whether the selectors of consumers are used to determine dispatch
104         * to a virtual destination, when true only messages matching an existing 
105         * consumer will be dispatched.
106         * @param selectorAware when true take consumer selectors into consideration
107         */
108        public void setSelectorAware(boolean selectorAware) {
109            this.selectorAware = selectorAware;
110        }
111        
112        public boolean isSelectorAware() {
113            return selectorAware;
114        }
115    
116        public boolean isLocal() {
117            return local;
118        }
119    
120        public void setLocal(boolean local) {
121            this.local = local;
122        }
123    
124        @Override
125        public String toString() {
126            return new StringBuilder("VirtualTopic:").append(prefix).append(',').append(name).append(',').append(postfix).append(',').append(selectorAware).append(',').append(local).toString();
127        }
128    }