summaryrefslogtreecommitdiff
path: root/broker/src/org/apache/qpid/server/exchange/DestWildExchange.java
blob: f6abd530767db7da5f9985acc6680a0165336bc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.server.exchange;

import org.apache.log4j.Logger;
import org.apache.qpid.AMQException;
import org.apache.qpid.framing.FieldTable;
import org.apache.qpid.framing.BasicPublishBody;
import org.apache.qpid.server.queue.AMQQueue;
import org.apache.qpid.server.queue.AMQMessage;
import org.apache.qpid.server.registry.ApplicationRegistry;
import org.apache.qpid.server.management.MBeanDescription;
import org.apache.qpid.server.management.MBeanConstructor;

import javax.management.openmbean.*;
import javax.management.JMException;
import javax.management.MBeanException;
import javax.management.NotCompliantMBeanException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class DestWildExchange extends AbstractExchange
{
    private static final Logger _logger = Logger.getLogger(DestWildExchange.class);

    private ConcurrentHashMap<String, List<AMQQueue>> _routingKey2queues = new ConcurrentHashMap<String, List<AMQQueue>>();

    /**
     *  DestWildExchangeMBean class implements the management interface for the
     *  Topic exchanges.
     */
    @MBeanDescription("Management Bean for Topic Exchange")
    private final class DestWildExchangeMBean extends ExchangeMBean
    {
        private String[]   _bindingItemNames = {"BindingKey", "QueueNames"};
        private String[]   _bindingItemDescriptions = {"Binding key", "Queue Names"};
        private String[]   _bindingItemIndexNames = {"BindingKey"};
        private OpenType[] _bindingItemTypes = new OpenType[2];

        private CompositeType      _bindingDataType = null;
        private TabularType        _bindinglistDataType = null;
        private TabularDataSupport _bindingList = null;

        @MBeanConstructor("Creates an MBean for AMQ topic exchange")
        public DestWildExchangeMBean()  throws NotCompliantMBeanException
        {
            super();
            init();
        }

        /**
         * initialises the OpenType objects.
         */
        private void init()
        {
            try
            {
                _bindingItemTypes[0] = SimpleType.STRING;
                _bindingItemTypes[1] = new ArrayType(1, SimpleType.STRING);

                _bindingDataType = new CompositeType("QueueBinding",
                                             "Binding key and bound Queue names",
                                             _bindingItemNames,
                                             _bindingItemDescriptions,
                                             _bindingItemTypes);
                _bindinglistDataType = new TabularType("Bindings",
                                             "List of queue bindings for " + getName(),
                                             _bindingDataType,
                                             _bindingItemIndexNames);
            }
            catch(OpenDataException ex)
            {
                //It should never occur.
                _logger.error("OpenDataTypes could not be created.", ex);
                throw new RuntimeException(ex);
            }
        }

        public TabularData viewBindings()
            throws OpenDataException
        {
            _bindingList = new TabularDataSupport(_bindinglistDataType);

            for (Map.Entry<String, List<AMQQueue>> entry : _routingKey2queues.entrySet())
            {
                String key = entry.getKey();
                List<String> queueList = new ArrayList<String>();

                List<AMQQueue> queues = entry.getValue();
                for (AMQQueue q : queues)
                {
                    queueList.add(q.getName());
                }

                Object[] bindingItemValues = {key, queueList.toArray(new String[0])};
                CompositeData bindingData = new CompositeDataSupport(_bindingDataType,
                                                                     _bindingItemNames,
                                                                     bindingItemValues);
                _bindingList.put(bindingData);
            }

            return _bindingList;
        }

        public void createBinding(String queueName, String binding)
            throws JMException
        {
            AMQQueue queue = ApplicationRegistry.getInstance().getQueueRegistry().getQueue(queueName);

            if (queue == null)
                throw new JMException("Queue \"" + queueName + "\" is not registered with the exchange.");

            try
            {
                registerQueue(binding, queue, null);
                queue.bind(binding, DestWildExchange.this);
            }
            catch (AMQException ex)
            {
                throw new MBeanException(ex);
            }
        }

    } // End of MBean class


    public void registerQueue(String routingKey, AMQQueue queue, FieldTable args) throws AMQException
    {
        assert queue != null;
        assert routingKey != null;
        // we need to use putIfAbsent, which is an atomic operation, to avoid a race condition
        List<AMQQueue> queueList = _routingKey2queues.putIfAbsent(routingKey, new CopyOnWriteArrayList<AMQQueue>());
        // if we got null back, no previous value was associated with the specified routing key hence
        // we need to read back the new value just put into the map
        if (queueList == null)
        {
            queueList = _routingKey2queues.get(routingKey);
        }
        if (!queueList.contains(queue))
        {
            queueList.add(queue);
        }
        else if(_logger.isDebugEnabled())
        {
            _logger.debug("Queue " + queue + " is already registered with routing key " + routingKey);
        }

    }

    public void route(AMQMessage payload) throws AMQException
    {
        BasicPublishBody publishBody = payload.getPublishBody();

        final String routingKey = publishBody.routingKey;
        List<AMQQueue> queues = _routingKey2queues.get(routingKey);
        // if we have no registered queues we have nothing to do
        // TODO: add support for the immediate flag
        if (queues == null)
        {
            //todo Check for valid topic - mritchie
            return;
        }

        for (AMQQueue q : queues)
        {
            // TODO: modify code generator to add clone() method then clone the deliver body
            // without this addition we have a race condition - we will be modifying the body
            // before the encoder has encoded the body for delivery
            q.deliver(payload);
        }
    }

    public void deregisterQueue(String routingKey, AMQQueue queue) throws AMQException
    {
        assert queue != null;
        assert routingKey != null;

        List<AMQQueue> queues = _routingKey2queues.get(routingKey);
        if (queues == null)
        {
            throw new AMQException("Queue " + queue + " was not registered with exchange " + this.getName() +
                                   " with routing key " + routingKey + ". No queue was registered with that routing key");

        }
        boolean removedQ = queues.remove(queue);
        if (!removedQ)
        {
            throw new AMQException("Queue " + queue + " was not registered with exchange " + this.getName() +
                                   " with routing key " + routingKey);
        }
    }

    protected ExchangeMBean createMBean()  throws AMQException
    {
        try
        {
            return new DestWildExchangeMBean();
        }
        catch (NotCompliantMBeanException ex)
        {
            _logger.error("Exception occured in creating the DestWildExchenge", ex);
            throw new AMQException("Exception occured in creating the DestWildExchenge", ex);
        }
    }
}