summaryrefslogtreecommitdiff
path: root/client/src/org/apache/qpid/client/AMQDestination.java
blob: 6401e3b23f1d2462c9b7b239125ecaddd8ba3b77 (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
/*
 *
 * 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;

import org.apache.qpid.url.BindingURL;
import org.apache.qpid.url.AMQBindingURL;
import org.apache.qpid.url.URLSyntaxException;
import org.apache.qpid.url.URLHelper;
import org.apache.qpid.exchange.ExchangeDefaults;

import javax.naming.Reference;
import javax.naming.NamingException;
import javax.naming.StringRefAddr;
import javax.naming.Referenceable;
import javax.jms.Destination;


public abstract class AMQDestination implements Destination, Referenceable
{
    protected final String _exchangeName;

    protected final String _exchangeClass;

    protected final String _destinationName;

    protected boolean _isDurable;

    protected final boolean _isExclusive;

    protected final boolean _isAutoDelete;

    protected String _queueName;

    protected AMQDestination(String url) throws URLSyntaxException
    {
        this(new AMQBindingURL(url));
    }

    protected AMQDestination(BindingURL binding)
    {
        _exchangeName = binding.getExchangeName();
        _exchangeClass = binding.getExchangeClass();
        _destinationName = binding.getDestinationName();

        _isExclusive = Boolean.parseBoolean(binding.getOption(BindingURL.OPTION_EXCLUSIVE));
        _isAutoDelete = Boolean.parseBoolean(binding.getOption(BindingURL.OPTION_AUTODELETE));
        _isDurable = Boolean.parseBoolean(binding.getOption(BindingURL.OPTION_DURABLE));
        _queueName = binding.getQueueName();
    }

    protected AMQDestination(String exchangeName, String exchangeClass, String destinationName, String queueName)
    {
        this(exchangeName, exchangeClass, destinationName, false, false, queueName);
    }

    protected AMQDestination(String exchangeName, String exchangeClass, String destinationName)
    {
        this(exchangeName, exchangeClass, destinationName, false, false, null);
    }

    protected AMQDestination(String exchangeName, String exchangeClass, String destinationName, boolean isExclusive,
                             boolean isAutoDelete, String queueName)
    {
        if (destinationName == null)
        {
            throw new IllegalArgumentException("Destination name must not be null");
        }
        if (exchangeName == null)
        {
            throw new IllegalArgumentException("Exchange name must not be null");
        }
        if (exchangeClass == null)
        {
            throw new IllegalArgumentException("Exchange class must not be null");
        }
        _exchangeName = exchangeName;
        _exchangeClass = exchangeClass;
        _destinationName = destinationName;
        _isExclusive = isExclusive;
        _isAutoDelete = isAutoDelete;
        _queueName = queueName;
    }

    public abstract String getEncodedName();

    public boolean isDurable()
    {
        return _isDurable;
    }

    public String getExchangeName()
    {
        return _exchangeName;
    }

    public String getExchangeClass()
    {
        return _exchangeClass;
    }

    public boolean isTopic()
    {
        return ExchangeDefaults.TOPIC_EXCHANGE_NAME.equals(_exchangeName);
    }

    public boolean isQueue()
    {
        return ExchangeDefaults.DIRECT_EXCHANGE_NAME.equals(_exchangeName);
    }

    public String getDestinationName()
    {
        return _destinationName;
    }

    public String getQueueName()
    {
        return _queueName;
    }

    public void setQueueName(String queueName)
    {
        _queueName = queueName;
    }

    public abstract String getRoutingKey();

    public boolean isExclusive()
    {
        return _isExclusive;
    }

    public boolean isAutoDelete()
    {
        return _isAutoDelete;
    }

    public abstract boolean isNameRequired();

    public String toString()
    {
        return toURL();

        /*
        return "Destination: " + _destinationName + ", " +
               "Queue Name: " + _queueName + ", Exchange: " + _exchangeName +
               ", Exchange class: " + _exchangeClass + ", Exclusive: " + _isExclusive +
               ", AutoDelete: " + _isAutoDelete + ", Routing  Key: " + getRoutingKey();
         */
    }

    public String toURL()
    {
        StringBuffer sb = new StringBuffer();

        sb.append(_exchangeClass);
        sb.append("://");
        sb.append(_exchangeName);

        sb.append("/");

        if (_destinationName != null)
        {
            sb.append(_destinationName);
        }

        sb.append("/");

        if (_queueName != null)
        {
            sb.append(_queueName);
        }

        sb.append("?");

        if (_isDurable)
        {
            sb.append(BindingURL.OPTION_DURABLE);
            sb.append("='true'");
            sb.append(URLHelper.DEFAULT_OPTION_SEPERATOR);
        }

        if (_isExclusive)
        {
            sb.append(BindingURL.OPTION_EXCLUSIVE);
            sb.append("='true'");
            sb.append(URLHelper.DEFAULT_OPTION_SEPERATOR);
        }

        if (_isAutoDelete)
        {
            sb.append(BindingURL.OPTION_AUTODELETE);
            sb.append("='true'");
            sb.append(URLHelper.DEFAULT_OPTION_SEPERATOR);
        }

        //remove the last char '?' if there is no options , ',' if there are.
        sb.deleteCharAt(sb.length() - 1);

        return sb.toString();
    }

    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (o == null || getClass() != o.getClass())
        {
            return false;
        }

        final AMQDestination that = (AMQDestination) o;

        if (!_destinationName.equals(that._destinationName))
        {
            return false;
        }
        if (!_exchangeClass.equals(that._exchangeClass))
        {
            return false;
        }
        if (!_exchangeName.equals(that._exchangeName))
        {
            return false;
        }
        if ((_queueName == null && that._queueName != null) ||
                (_queueName != null && !_queueName.equals(that._queueName)))
        {
            return false;
        }
        if (_isExclusive != that._isExclusive)
        {
            return false;
        }
        if (_isAutoDelete != that._isAutoDelete)
        {
            return false;
        }
        return true;
    }

    public int hashCode()
    {
        int result;
        result = _exchangeName.hashCode();
        result = 29 * result + _exchangeClass.hashCode();
        result = 29 * result + _destinationName.hashCode();
        if (_queueName != null)
        {
            result = 29 * result + _queueName.hashCode();
        }
        result = result * (_isExclusive ? 13 : 7);
        result = result * (_isAutoDelete ? 13 : 7);
        return result;
    }

    public Reference getReference() throws NamingException
    {
        return new Reference(
                this.getClass().getName(),
                new StringRefAddr(this.getClass().getName(), toURL()),
                AMQConnectionFactory.class.getName(),
                null);          // factory location
    }
}