summaryrefslogtreecommitdiff
path: root/java/broker-core/src/main/java/org/apache/qpid/server/store/DurableConfigurationStoreHelper.java
blob: fc5806ccce2bbbe1a5e10c27fa771bea3fea7f24 (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
/*
 *
 * 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.store;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;

import java.util.Set;

import org.apache.qpid.server.model.Binding;
import org.apache.qpid.server.model.Exchange;
import org.apache.qpid.server.model.LifetimePolicy;
import org.apache.qpid.server.model.Queue;
import org.apache.qpid.server.queue.AMQQueue;
import org.apache.qpid.server.util.ServerScopedRuntimeException;

public class DurableConfigurationStoreHelper
{

    private static final String BINDING = Binding.class.getSimpleName();
    private static final String EXCHANGE = Exchange.class.getSimpleName();
    private static final String QUEUE = Queue.class.getSimpleName();
    private static final Set<String> QUEUE_ARGUMENTS_EXCLUDES = new HashSet<String>(Arrays.asList(Queue.NAME,
                                                                                                  Queue.OWNER,
                                                                                                  Queue.EXCLUSIVE,
                                                                                                  Queue.ALTERNATE_EXCHANGE));

    public static void updateQueue(DurableConfigurationStore store, AMQQueue<?,?,?> queue)
    {
        Map<String, Object> attributesMap = new LinkedHashMap<String, Object>();
        attributesMap.put(Queue.NAME, queue.getName());
        attributesMap.put(Queue.OWNER, queue.getOwner());
        attributesMap.put(Queue.EXCLUSIVE, queue.isExclusive());

        if (queue.getAlternateExchange() != null)
        {
            attributesMap.put(Queue.ALTERNATE_EXCHANGE, queue.getAlternateExchange().getId());
        }

        Collection<String> availableAttrs = queue.getAvailableAttributes();

        for(String attrName : availableAttrs)
        {
            if(!QUEUE_ARGUMENTS_EXCLUDES.contains(attrName))
            {
                attributesMap.put(attrName, queue.getAttribute(attrName));
            }
        }

        store.update(queue.getId(), QUEUE, attributesMap);
    }

    public static void createQueue(DurableConfigurationStore store, AMQQueue<?,?,?> queue)
    {
        Map<String, Object> attributesMap = new HashMap<String, Object>();
        attributesMap.put(Queue.NAME, queue.getName());
        attributesMap.put(Queue.OWNER, queue.getOwner());
        attributesMap.put(Queue.EXCLUSIVE, queue.isExclusive());
        if (queue.getAlternateExchange() != null)
        {
            attributesMap.put(Queue.ALTERNATE_EXCHANGE, queue.getAlternateExchange().getId());
        }

        for(String attrName : queue.getAvailableAttributes())
        {
            if(!QUEUE_ARGUMENTS_EXCLUDES.contains(attrName))
            {
                attributesMap.put(attrName, queue.getAttribute(attrName));
            }
        }
        store.create(queue.getId(), QUEUE, attributesMap);
    }

    public static void removeQueue(DurableConfigurationStore store, AMQQueue queue)
    {
        store.remove(queue.getId(), QUEUE);
    }

    public static void createExchange(DurableConfigurationStore store, org.apache.qpid.server.exchange.Exchange exchange)
    {
        Map<String, Object> attributesMap = new HashMap<String, Object>();
        attributesMap.put(Exchange.NAME, exchange.getName());
        attributesMap.put(Exchange.TYPE, exchange.getTypeName());
        attributesMap.put(Exchange.LIFETIME_POLICY, exchange.isAutoDelete() ? LifetimePolicy.AUTO_DELETE.name()
                : LifetimePolicy.PERMANENT.name());

        store.create(exchange.getId(), EXCHANGE, attributesMap);

    }


    public static void removeExchange(DurableConfigurationStore store, org.apache.qpid.server.exchange.Exchange exchange)
    {
        store.remove(exchange.getId(), EXCHANGE);
    }

    public static void createBinding(DurableConfigurationStore store, org.apache.qpid.server.binding.Binding binding)
    {
        Map<String, Object> attributesMap = new HashMap<String, Object>();
        attributesMap.put(Binding.NAME, binding.getBindingKey());
        attributesMap.put(Binding.EXCHANGE, binding.getExchange().getId());
        attributesMap.put(Binding.QUEUE, binding.getQueue().getId());
        Map<String, Object> arguments = binding.getArguments();
        if (arguments != null)
        {
            attributesMap.put(Binding.ARGUMENTS, arguments);
        }

        store.create(binding.getId(), BINDING, attributesMap);
    }


    public static void removeBinding(DurableConfigurationStore store, org.apache.qpid.server.binding.Binding binding)
    {
        store.remove(binding.getId(), BINDING);
    }

}