summaryrefslogtreecommitdiff
path: root/java/common/src/main/java/org/apache/qpid/BrokerDetailsImpl.java
blob: 201d43e21fad6a9d5e2f5062957423526b4fd711 (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
/* 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;

import java.util.HashMap;
import java.util.Map;

/**
 * Implements the interface BrokerDetails
 */
public class BrokerDetailsImpl implements BrokerDetails
{
    //--- Those are the default values
    private final String DEFAULT_USERNAME = "guest";
    private final String DEFAULT_PASSWORD = "guest";
    private final String DEFAULT_VIRTUALHOST = "";

    //---- The brker details
    private String _host;
    private int _port = 0;
    private String _virtualHost;
    private String _userName = DEFAULT_USERNAME;
    private String _password = DEFAULT_PASSWORD;
    private String _protocol;
    private Map<String, String> _props = new HashMap<String, String>();
    ;

    //--- Constructors

    public BrokerDetailsImpl()
    {
    }

    /**
     * Create a new broker details given all the reuqired information
     *
     * @param protocol    The protocol used for this broker connection
     * @param host        The host name.
     * @param port        The port number.
     * @param virtualHost The virtual host.
     * @param userName    The user name.
     * @param password    The user password.
     */
    public BrokerDetailsImpl(String protocol, String host, int port, String virtualHost, String userName,
                             String password, Map<String, String> props)
    {
        _protocol = protocol;
        _host = host;
        _port = port;
        _virtualHost = virtualHost;
        _userName = userName;
        _password = password;
        _props = props;
    }

    /**
     * Create a new broker details given the host name and the procol type,
     * default values are used for the other details.
     *
     * @param protocol The protocol used for this broker connection
     * @param host     The host name.
     */
    public BrokerDetailsImpl(String protocol, String host)
    {
        _protocol = protocol;
        _host = host;
        _virtualHost = DEFAULT_VIRTUALHOST;
        _userName = DEFAULT_USERNAME;
        _password = DEFAULT_PASSWORD;
    }

    //--- API BrokerDetails
    /**
     * Get the user password
     *
     * @return The user password
     */
    public String getPassword()
    {
        return _password;
    }

    /**
     * Get the broker host name.
     *
     * @return The broker host name.
     */
    public String getHost()
    {
        return _host;
    }

    /**
     * Get the broker port number.
     *
     * @return The broker port number.
     */
    public int getPort()
    {
        if (_port == 0)
        {
            if (getProtocol().equals(BrokerDetails.PROTOCOL_TCP))
            {
                _port = 5672;
            }
            else if (getProtocol().equals(BrokerDetails.PROTOCOL_TLS))
            {
                _port = 5555;
            }
        }
        return _port;
    }

    /**
     * Get the virtual host to connect to.
     *
     * @return The virtual host of this broker.
     */
    public String getVirtualHost()
    {
        return _virtualHost;
    }

    /**
     * Get the user name.
     *
     * @return The user name
     */
    public String getUserName()
    {
        return _userName;
    }

    /**
     * Get the protocol used to connect to hise broker.
     *
     * @return the protocol used to connect to the broker.
     */
    public String getProtocol()
    {
        return _protocol;
    }

    /**
     * Set the broker host name.
     *
     * @param host The broker host name.
     */
    public void setHost(String host)
    {
        _host = host;
    }

    /**
     * Set the broker port number.
     *
     * @param port The broker port number.
     */
    public void setPort(int port)
    {
        _port = port;
    }

    /**
     * Set the virtual host to connect to.
     *
     * @param virtualHost The virtual host of this broker.
     */
    public void setVirtualHost(String virtualHost)
    {
        _virtualHost = virtualHost;
    }

    /**
     * Set the user name.
     *
     * @param userName The user name
     */
    public void setUserName(String userName)
    {
        _userName = userName;
    }

    /**
     * Set the user password
     *
     * @param password The user password
     */
    public void setPassword(String password)
    {
        _password = password;
    }

    /**
     * Set the protocol used to connect to hise broker.
     *
     * @param protocol the protocol used to connect to the broker.
     */
    public void setProtocol(String protocol)
    {
        _protocol = protocol;
    }

    /**
     * Ex: keystore path
     *
     * @return the Properties associated with this connection.
     */
    public Map<String, String> getProperties()
    {
        return _props;
    }

    /**
     * Sets the properties associated with this connection
     *
     * @param props
     */
    public void setProperties(Map<String, String> props)
    {
        _props = props;
    }

    public void setProperty(String key, String value)
    {
        _props.put(key, value);
    }

    public String toString()
    {
        StringBuilder b = new StringBuilder();
        b.append("[username=" + _userName);
        b.append(",password=" + _password);
        b.append(",transport=" + _protocol);
        b.append(",host=" + _host);
        b.append(",port=" + getPort() + "]");
        b.append(" - Properties[");
        if (_props != null)
        {
            for (String k : _props.keySet())
            {
                b.append(k + "=" + _props.get(k) + ",");
            }
        }
        b.append("]");

        return b.toString();
    }
}