summaryrefslogtreecommitdiff
path: root/java/client/src/main/java/org/apache/qpid/client/messaging/address/AddressHelper.java
blob: 318fe32d36e051f3078991737e8ee7afc2d8797d (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
/*
 *
 * 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.client.messaging.address;

import org.apache.qpid.client.AMQDestination;
import org.apache.qpid.client.AMQDestination.Binding;
import org.apache.qpid.client.messaging.address.Link.Reliability;
import org.apache.qpid.client.messaging.address.Link.Subscription;
import org.apache.qpid.client.messaging.address.Node.ExchangeNode;
import org.apache.qpid.client.messaging.address.Node.QueueNode;
import org.apache.qpid.configuration.Accessor;
import org.apache.qpid.configuration.Accessor.MapAccessor;
import org.apache.qpid.messaging.Address;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Utility class for extracting information from the address class
 */
public class AddressHelper
{
    public static final String NODE = "node";
    public static final String LINK = "link";
    public static final String X_DECLARE = "x-declare";
    public static final String X_BINDINGS = "x-bindings";
    public static final String X_SUBSCRIBE = "x-subscribes";
    public static final String CREATE = "create";
    public static final String ASSERT = "assert";
    public static final String DELETE = "delete";
    public static final String FILTER = "filter";
    public static final String NO_LOCAL = "no-local";
    public static final String DURABLE = "durable";
    public static final String EXCLUSIVE = "exclusive";
    public static final String AUTO_DELETE = "auto-delete";
    public static final String TYPE = "type";
    public static final String ALT_EXCHANGE = "alternate-exchange";
    public static final String BINDINGS = "bindings";
    public static final String BROWSE = "browse";
    public static final String MODE = "mode";
    public static final String CAPACITY = "capacity";
    public static final String CAPACITY_SOURCE = "source";
    public static final String CAPACITY_TARGET = "target";
    public static final String NAME = "name";
    public static final String EXCHANGE = "exchange";
    public static final String QUEUE = "queue";
    public static final String KEY = "key";
    public static final String ARGUMENTS = "arguments";
    public static final String RELIABILITY = "reliability";

    private Address address;
    private Accessor addressProps;
    private Accessor nodeProps;
    private Accessor linkProps;

    public AddressHelper(Address address)
    {
        this.address = address;
        addressProps = new MapAccessor(address.getOptions());
        Map node_props = address.getOptions() == null
                || address.getOptions().get(NODE) == null ? null
                : (Map) address.getOptions().get(NODE);

        if (node_props != null)
        {
            nodeProps = new MapAccessor(node_props);
        }

        Map link_props = address.getOptions() == null
                || address.getOptions().get(LINK) == null ? null
                : (Map) address.getOptions().get(LINK);

        if (link_props != null)
        {
            linkProps = new MapAccessor(link_props);
        }
    }

    public String getCreate()
    {
        return addressProps.getString(CREATE);
    }

    public String getAssert()
    {
        return addressProps.getString(ASSERT);
    }

    public String getDelete()
    {
        return addressProps.getString(DELETE);
    }

    public boolean isNoLocal()
    {
        Boolean b = nodeProps.getBoolean(NO_LOCAL);
        return b == null ? false : b;
    }

    public boolean isBrowseOnly()
    {
        String mode = addressProps.getString(MODE);
        return mode != null && mode.equals(BROWSE) ? true : false;
    }

    @SuppressWarnings("unchecked")
    public List<Binding> getBindings(Map props)
    {
        List<Binding> bindings = new ArrayList<Binding>();
        List<Map> bindingList = (List<Map>) props.get(X_BINDINGS);
        if (bindingList != null)
        {
            for (Map bindingMap : bindingList)
            {
                Binding binding = new Binding(
                        (String) bindingMap.get(EXCHANGE),
                        (String) bindingMap.get(QUEUE),
                        (String) bindingMap.get(KEY),
                        bindingMap.get(ARGUMENTS) == null ? Collections.EMPTY_MAP
                                : (Map<String, Object>) bindingMap
                                        .get(ARGUMENTS));
                bindings.add(binding);
            }
        }
        return bindings;
    }

    public Map getDeclareArgs(Map props)
    {
        if (props != null && props.get(X_DECLARE) != null)
        {
            return (Map) props.get(X_DECLARE);
            
        } else
        {
            return Collections.EMPTY_MAP;
        }
    }

    public int getTargetNodeType() throws Exception
    {
        if (nodeProps == null || nodeProps.getString(TYPE) == null)
        {
            // need to query and figure out
            return AMQDestination.UNKNOWN_TYPE;
        } else if (nodeProps.getString(TYPE).equals("queue"))
        {
            return AMQDestination.QUEUE_TYPE;
        } else if (nodeProps.getString(TYPE).equals("topic"))
        {
            return AMQDestination.TOPIC_TYPE;
        } else
        {
            throw new Exception("unkown exchange type");
        }
    }

    public Node getTargetNode(int addressType)
    {
        // target node here is the default exchange
        if (nodeProps == null || addressType == AMQDestination.QUEUE_TYPE)
        {
            return new ExchangeNode();
        } else if (addressType == AMQDestination.TOPIC_TYPE)
        {
            Map node = (Map) address.getOptions().get(NODE);
            return createExchangeNode(node);
        } else
        {
            // don't know yet
            return null;
        }
    }

    private Node createExchangeNode(Map parent)
    {
        Map declareArgs = getDeclareArgs(parent);
        MapAccessor argsMap = new MapAccessor(declareArgs);
        ExchangeNode node = new ExchangeNode();
        node.setExchangeType(argsMap.getString(TYPE) == null ? null : argsMap
                .getString(TYPE));
        fillInCommonNodeArgs(node, parent, argsMap);
        return node;
    }

    private Node createQueueNode(Map parent)
    {
        Map declareArgs = getDeclareArgs(parent);
        MapAccessor argsMap = new MapAccessor(declareArgs);
        QueueNode node = new QueueNode();
        node.setAlternateExchange(argsMap.getString(ALT_EXCHANGE));
        node.setExclusive(argsMap.getBoolean(EXCLUSIVE) == null ? false
                : argsMap.getBoolean(EXCLUSIVE));
        fillInCommonNodeArgs(node, parent, argsMap);

        return node;
    }

    private void fillInCommonNodeArgs(Node node, Map parent, MapAccessor argsMap)
    {
        node.setDurable(getDurability(parent));
        node.setAutoDelete(argsMap.getBoolean(AUTO_DELETE) == null ? false
                : argsMap.getBoolean(AUTO_DELETE));
        node.setAlternateExchange(argsMap.getString(ALT_EXCHANGE));
        node.setBindings(getBindings(parent));
        if (getDeclareArgs(parent).containsKey(ARGUMENTS))
        {
            node.setDeclareArgs((Map<String,Object>)getDeclareArgs(parent).get(ARGUMENTS));
        }
    }
    
    private boolean getDurability(Map map)
    {
        Accessor access = new MapAccessor(map);
        Boolean result = access.getBoolean(DURABLE);
        return (result == null) ? false : result.booleanValue();
    }

    /**
     * if the type == queue x-declare args from the node props is used. if the
     * type == exchange x-declare args from the link props is used else just
     * create a default temp queue.
     */
    public Node getSourceNode(int addressType)
    {
        if (addressType == AMQDestination.QUEUE_TYPE && nodeProps != null)
        {
            return createQueueNode((Map) address.getOptions().get(NODE));
        }
        if (addressType == AMQDestination.TOPIC_TYPE && linkProps != null)
        {
            return createQueueNode((Map) address.getOptions().get(LINK));
        } else
        {
            // need to query the info
            return new QueueNode();
        }
    }

    public Link getLink() throws Exception
    {
        Link link = new Link();
        link.setSubscription(new Subscription());
        if (linkProps != null)
        {
            link.setDurable(linkProps.getBoolean(DURABLE) == null ? false
                    : linkProps.getBoolean(DURABLE));
            link.setName(linkProps.getString(NAME));

            String reliability = linkProps.getString(RELIABILITY);
            if ( reliability != null)
            {
                if (reliability.equalsIgnoreCase("unreliable"))
                {
                    link.setReliability(Reliability.UNRELIABLE);
                }
                else if (reliability.equalsIgnoreCase("at-least-once"))
                {
                    link.setReliability(Reliability.AT_LEAST_ONCE);
                }
                else
                {
                    throw new Exception("The reliability mode '" + 
                            reliability + "' is not yet supported");
                }
                
            }
            
            if (((Map) address.getOptions().get(LINK)).get(CAPACITY) instanceof Map)
            {
                MapAccessor capacityProps = new MapAccessor(
                        (Map) ((Map) address.getOptions().get(LINK))
                                .get(CAPACITY));
                link
                        .setConsumerCapacity(capacityProps
                                .getInt(CAPACITY_SOURCE) == null ? 0
                                : capacityProps.getInt(CAPACITY_SOURCE));
                link
                        .setProducerCapacity(capacityProps
                                .getInt(CAPACITY_TARGET) == null ? 0
                                : capacityProps.getInt(CAPACITY_TARGET));
            } 
            else
            {
                int cap = linkProps.getInt(CAPACITY) == null ? 0 : linkProps
                        .getInt(CAPACITY);
                link.setConsumerCapacity(cap);
                link.setProducerCapacity(cap);
            }
            link.setFilter(linkProps.getString(FILTER));
            // so far filter type not used
            
            if (((Map) address.getOptions().get(LINK)).containsKey(X_SUBSCRIBE))
            {   
                Map x_subscribe = (Map)((Map) address.getOptions().get(LINK)).get(X_SUBSCRIBE);
                
                if (x_subscribe.containsKey(ARGUMENTS))
                {
                    link.getSubscription().setArgs((Map<String,Object>)x_subscribe.get(ARGUMENTS));
                }
                
                boolean exclusive = x_subscribe.containsKey(EXCLUSIVE) ?
                                    Boolean.parseBoolean((String)x_subscribe.get(EXCLUSIVE)): false;
                
                link.getSubscription().setExclusive(exclusive);
            }
        }

        return link;
    }
}