summaryrefslogtreecommitdiff
path: root/qpid/java/broker-core/src/main/java/org/apache/qpid/server/exchange/DefaultExchange.java
blob: 33c5218b4cd85b3f4c0054612ef19d3c2b350eec (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * 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 java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.log4j.Logger;
import org.apache.qpid.AMQException;
import org.apache.qpid.AMQInternalException;
import org.apache.qpid.AMQSecurityException;
import org.apache.qpid.exchange.ExchangeDefaults;
import org.apache.qpid.server.binding.Binding;
import org.apache.qpid.server.consumer.Consumer;
import org.apache.qpid.server.logging.LogSubject;
import org.apache.qpid.server.logging.actors.CurrentActor;
import org.apache.qpid.server.logging.messages.ExchangeMessages;
import org.apache.qpid.server.message.InstanceProperties;
import org.apache.qpid.server.message.MessageInstance;
import org.apache.qpid.server.message.MessageReference;
import org.apache.qpid.server.message.ServerMessage;
import org.apache.qpid.server.model.UUIDGenerator;
import org.apache.qpid.server.plugin.ExchangeType;
import org.apache.qpid.server.queue.AMQQueue;
import org.apache.qpid.server.queue.BaseQueue;
import org.apache.qpid.server.queue.QueueEntry;
import org.apache.qpid.server.queue.QueueRegistry;
import org.apache.qpid.server.txn.ServerTransaction;
import org.apache.qpid.server.util.Action;
import org.apache.qpid.server.virtualhost.VirtualHost;

public class DefaultExchange implements Exchange
{

    private final QueueRegistry _queueRegistry;
    private UUID _id;
    private VirtualHost _virtualHost;
    private static final Logger _logger = Logger.getLogger(DefaultExchange.class);
    private final AtomicBoolean _closed = new AtomicBoolean();

    private LogSubject _logSubject;
    private Map<ExchangeReferrer,Object> _referrers = new ConcurrentHashMap<ExchangeReferrer,Object>();

    public DefaultExchange(QueueRegistry queueRegistry)
    {
        _queueRegistry = queueRegistry;
    }


    @Override
    public void initialise(UUID id,
                           VirtualHost host,
                           String name,
                           boolean durable,
                           boolean autoDelete) throws AMQException
    {
        _id = id;
        _virtualHost = host;
    }

    @Override
    public String getName()
    {
        return ExchangeDefaults.DEFAULT_EXCHANGE_NAME;
    }

    @Override
    public ExchangeType getType()
    {
        return DirectExchange.TYPE;
    }

    @Override
    public long getBindingCount()
    {
        return _virtualHost.getQueues().size();
    }

    @Override
    public long getByteDrops()
    {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public long getByteReceives()
    {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public long getMsgDrops()
    {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public long getMsgReceives()
    {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean addBinding(String bindingKey, AMQQueue queue, Map<String, Object> arguments)
            throws AMQSecurityException, AMQInternalException
    {
        throw new AMQSecurityException("Cannot add bindings to the default exchange");
    }

    @Override
    public boolean replaceBinding(UUID id, String bindingKey, AMQQueue queue, Map<String, Object> arguments)
            throws AMQSecurityException, AMQInternalException
    {
        throw new AMQSecurityException("Cannot replace bindings on the default exchange");
    }

    @Override
    public void restoreBinding(UUID id, String bindingKey, AMQQueue queue, Map<String, Object> argumentMap)
            throws AMQSecurityException, AMQInternalException
    {
        _logger.warn("Bindings to the default exchange should not be stored in the configuration store");
    }

    @Override
    public void removeBinding(Binding b) throws AMQSecurityException, AMQInternalException
    {
        throw new AMQSecurityException("Cannot remove bindings to the default exchange");
    }

    @Override
    public Binding removeBinding(String bindingKey, AMQQueue queue, Map<String, Object> arguments)
            throws AMQSecurityException, AMQInternalException
    {
        throw new AMQSecurityException("Cannot remove bindings to the default exchange");
    }

    @Override
    public Binding getBinding(String bindingKey, AMQQueue queue, Map<String, Object> arguments)
    {
        if(_virtualHost.getQueue(bindingKey) == queue && (arguments == null || arguments.isEmpty()))
        {
            return convertToBinding(queue);
        }
        else
        {
            return null;
        }

    }

    private Binding convertToBinding(AMQQueue queue)
    {
        String queueName = queue.getName();

        UUID exchangeId = UUIDGenerator.generateBindingUUID(ExchangeDefaults.DEFAULT_EXCHANGE_NAME,
                                                            queueName,
                                                            queueName,
                                                            _virtualHost.getName());

        return new Binding(exchangeId, queueName, queue, this, Collections.EMPTY_MAP);
    }

    @Override
    public String getTypeName()
    {
        return getType().getType();
    }

    @Override
    public boolean isDurable()
    {
        return false;
    }

    @Override
    public boolean isAutoDelete()
    {
        return false;
    }

    @Override
    public void close() throws AMQException
    {
        if(_closed.compareAndSet(false,true))
        {

            CurrentActor.get().message(_logSubject, ExchangeMessages.DELETED());

        }
    }

    @Override
    public boolean isBound(AMQQueue queue)
    {
        return _virtualHost.getQueue(queue.getName()) == queue;
    }

    @Override
    public boolean hasBindings()
    {
        return getBindingCount() != 0;
    }

    @Override
    public boolean isBound(String bindingKey, AMQQueue queue)
    {
        return isBound(queue) && queue.getName().equals(bindingKey);
    }

    @Override
    public boolean isBound(String bindingKey, Map<String, Object> arguments, AMQQueue queue)
    {
        return isBound(bindingKey, queue) && (arguments == null || arguments.isEmpty());
    }

    @Override
    public boolean isBound(Map<String, Object> arguments, AMQQueue queue)
    {
        return (arguments == null || arguments.isEmpty()) && isBound(queue);
    }

    @Override
    public boolean isBound(String bindingKey, Map<String, Object> arguments)
    {
        return (arguments == null || arguments.isEmpty()) && isBound(bindingKey);
    }

    @Override
    public boolean isBound(Map<String, Object> arguments)
    {
        return (arguments == null || arguments.isEmpty()) && hasBindings();
    }

    @Override
    public boolean isBound(String bindingKey)
    {
        return _virtualHost.getQueue(bindingKey) != null;
    }

    @Override
    public Exchange getAlternateExchange()
    {
        return null;
    }

    @Override
    public void setAlternateExchange(Exchange exchange)
    {
        _logger.warn("Cannot set the alternate exchange for the default exchange");
    }

    @Override
    public void removeReference(ExchangeReferrer exchange)
    {
        _referrers.remove(exchange);
    }

    @Override
    public void addReference(ExchangeReferrer exchange)
    {
        _referrers.put(exchange, Boolean.TRUE);
    }

    @Override
    public boolean hasReferrers()
    {
        return !_referrers.isEmpty();
    }

    @Override
    public Collection<Binding> getBindings()
    {
        List<Binding> bindings = new ArrayList<Binding>();
        for(AMQQueue q : _virtualHost.getQueues())
        {
            bindings.add(convertToBinding(q));
        }
        return bindings;
    }

    @Override
    public void addBindingListener(BindingListener listener)
    {
        _queueRegistry.addRegistryChangeListener(convertListener(listener));
    }

    private QueueRegistry.RegistryChangeListener convertListener(final BindingListener listener)
    {
        return new QueueRegistry.RegistryChangeListener()
        {
            @Override
            public void queueRegistered(AMQQueue queue)
            {
                listener.bindingAdded(DefaultExchange.this, convertToBinding(queue));
            }

            @Override
            public void queueUnregistered(AMQQueue queue)
            {
                listener.bindingRemoved(DefaultExchange.this, convertToBinding(queue));
            }
        };
    }

    @Override
    public void removeBindingListener(BindingListener listener)
    {
        // TODO
    }

    @Override
    public UUID getId()
    {
        return _id;
    }

    public final int send(final ServerMessage message,
                          final InstanceProperties instanceProperties,
                          final ServerTransaction txn,
                          final Action<MessageInstance<? extends Consumer>> postEnqueueAction)
    {
        final AMQQueue q = _virtualHost.getQueue(message.getRoutingKey());
        if(q == null)
        {
            return 0;
        }
        else
        {
            txn.enqueue(q,message, new ServerTransaction.Action()
            {
                MessageReference _reference = message.newReference();

                public void postCommit()
                {
                    try
                    {
                        q.enqueue(message, postEnqueueAction);
                    }
                    catch (AMQException e)
                    {
                        // TODO
                        throw new RuntimeException(e);
                    }
                    finally
                    {
                        _reference.release();
                    }
                }

                public void onRollback()
                {
                    _reference.release();
                }
            });
            return 1;
        }
    }

}