ExchangeBindings.java
01 /*
02  *
03  * Licensed to the Apache Software Foundation (ASF) under one
04  * or more contributor license agreements.  See the NOTICE file
05  * distributed with this work for additional information
06  * regarding copyright ownership.  The ASF licenses this file
07  * to you under the Apache License, Version 2.0 (the
08  * "License"); you may not use this file except in compliance
09  * with the License.  You may obtain a copy of the License at
10  
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  */
21 package org.apache.qpid.server.queue;
22 
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.concurrent.CopyOnWriteArrayList;
26 
27 import org.apache.qpid.AMQException;
28 import org.apache.qpid.framing.AMQShortString;
29 import org.apache.qpid.framing.FieldTable;
30 import org.apache.qpid.server.exchange.Exchange;
31 
32 /**
33  * When a queue is deleted, it should be deregistered from any
34  * exchange it has been bound to. This class assists in this task,
35  * by keeping track of all bindings for a given queue.
36  */
37 class ExchangeBindings
38 {
39     private final List<ExchangeBinding> _bindings = new CopyOnWriteArrayList<ExchangeBinding>();
40     private final AMQQueue _queue;
41 
42     ExchangeBindings(AMQQueue queue)
43     {
44         _queue = queue;
45     }
46 
47     /**
48      * Adds the specified binding to those being tracked.
49      @param routingKey the routing key with which the queue whose bindings
50      * are being tracked by the instance has been bound to the exchange
51      @param exchange the exchange bound to
52      */
53     void addBinding(AMQShortString routingKey, FieldTable arguments, Exchange exchange)
54     {
55         _bindings.add(new ExchangeBinding(routingKey, exchange, arguments));
56     }
57 
58 
59     public boolean remove(AMQShortString routingKey, FieldTable arguments, Exchange exchange)
60     {
61         return _bindings.remove(new ExchangeBinding(routingKey, exchange, arguments));
62     }
63 
64 
65     /**
66      * Deregisters this queue from any exchange it has been bound to
67      */
68     void deregister() throws AMQException
69     {
70         //remove duplicates at this point
71         HashSet<ExchangeBinding> copy = new HashSet<ExchangeBinding>(_bindings);
72         for (ExchangeBinding b : copy)
73         {
74             b.unbind(_queue);
75         }
76     }
77 
78     List<ExchangeBinding> getExchangeBindings()
79     {
80         return _bindings;
81     }
82 }