-
Notifications
You must be signed in to change notification settings - Fork 33
/
BrokerListProperties.java
406 lines (335 loc) · 12.9 KB
/
BrokerListProperties.java
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.admin.apps.console;
import java.util.Properties;
import java.util.Enumeration;
import com.sun.messaging.jmq.admin.util.Globals;
import com.sun.messaging.jmq.admin.util.UserProperties;
import com.sun.messaging.jmq.admin.util.UserPropertiesException;
import com.sun.messaging.jmq.admin.bkrutil.BrokerAdmin;
import com.sun.messaging.jmq.admin.bkrutil.BrokerAdminException;
import com.sun.messaging.jmq.admin.resources.AdminConsoleResources;
/**
* This class encapsulates the information needed to save and restore the list of <EM>favourite</EM> object stores in
* the JMQ Administration Console.
*
* <P>
* It represents the property object containing the list of brokers.
*
* The format of the property file is:
*
* <PRE>
* version=2.0
* broker.count=5
* broker0.key=<STRONG>value</STRONG>
* broker0.userName=<STRONG>value</STRONG>
* broker0.password=<STRONG>value</STRONG>
* broker0.attrs.<STRONG>JMQBrokerHostName=someHost</STRONG>
* broker0.attrs.<STRONG>JMQBrokerHostPort=somePort</STRONG>
* broker0.attrs.<STRONG>attrName=value</STRONG>
* ...
* </PRE>
*/
public class BrokerListProperties extends UserProperties {
private static final long serialVersionUID = 7356903960060999063L;
public final static String FIRST_VERSION = "2.0";
public final static String VERSION = "2.0";
private final static String PROP_NAME_VERSION = "version";
private final static String PROP_NAME_BROKER_BASENAME = "broker";
private final static String PROP_NAME_BROKER_COUNT = PROP_NAME_BROKER_BASENAME + ".count";
private final static String PROP_NAME_BROKER_KEY_PREFIX = "key";
private final static String PROP_NAME_BROKER_USERNAME_PREFIX = "userName";
private final static String PROP_NAME_BROKER_PASSWD_PREFIX = "password";
private final static String PROP_NAME_BROKER_ATTR_PREFIX = "attrs";
private boolean DEBUG = false;
private static AdminConsoleResources acr = Globals.getAdminConsoleResources();
/**
* Instantiate a BrokerListProperties object.
*/
public BrokerListProperties() {
setProperty(PROP_NAME_VERSION, VERSION);
}
public String getVersion() {
String val = getProperty(PROP_NAME_VERSION);
return (val);
}
/**
* Returns the number of brokers
*
* @return The number of brokers
*/
public int getBrokerCount() {
String val = getProperty(PROP_NAME_BROKER_COUNT);
int ret;
if ((val == null) || val.equals("")) {
return (0);
}
try {
ret = Integer.parseInt(val);
} catch (Exception e) {
ret = 0;
}
return (ret);
}
/**
* Returns a BrokerAdmin array containing the BrokerAdmin objects representing brokers the admin console can connect to.
*
* @return Returns a BrokerAdmin array containing the BrokerAdmin objects.
*/
public BrokerAdmin[] getBrokerAdmin() throws BrokerAdminException {
int count = getBrokerCount();
if (count <= 0) {
return (null);
}
BrokerAdmin[] baArray = new BrokerAdmin[count];
for (int i = 0; i < count; ++i) {
baArray[i] = getBrokerAdmin(i);
}
return (baArray);
}
/**
* Returns a BrokerAdmin object for the given index.
*
* @param index Index for specifying the broker in question.
*
* @return The BrokerAdmin object for the given index.
*/
public BrokerAdmin getBrokerAdmin(int index) throws BrokerAdminException {
BrokerAdmin ba;
Properties brokerAttrs = new Properties();
String basePropName, brokerAttrsStr, keyPropName, usernamePropName, passwdPropName;
// String timeoutPropName;
String keyValue, usernameValue, passwdValue;
int brokerAttrsLen;
long timeout;
/*
* Construct base property name string: broker0.
*/
basePropName = PROP_NAME_BROKER_BASENAME + Integer.toString(index) + ".";
/*
* Construct property name string: broker0.key
*/
keyPropName = basePropName + PROP_NAME_BROKER_KEY_PREFIX;
/*
* Get key value, if any The value of this property will be used later.
*/
keyValue = getProperty(keyPropName, null);
/*
* Construct property name string: broker0.receiveTimeout timeoutPropName = basePropName +
* PROP_NAME_BROKER_RECV_TIMEOUT_PREFIX;
*/
/*
* For 2.0 FCS, we won't write out or read the timeout value. Defaulting to -1 here. timeoutValue =
* getProperty(timeoutPropName, ""); try { timeout = Long.parseLong(timeoutValue); } catch (Exception e) { timeout = -1;
* }
*/
timeout = -1;
/*
* Construct property name string: broker0.userName
*/
usernamePropName = basePropName + PROP_NAME_BROKER_USERNAME_PREFIX;
/*
* Get username value
*/
usernameValue = getProperty(usernamePropName, "");
/*
* Construct property name string: broker0.password
*/
passwdPropName = basePropName + PROP_NAME_BROKER_PASSWD_PREFIX;
/*
* Get passwd value
*/
passwdValue = getProperty(passwdPropName, "");
/*
* Construct base property name string: broker0.attrs.
*/
brokerAttrsStr = basePropName + PROP_NAME_BROKER_ATTR_PREFIX + ".";
brokerAttrsLen = brokerAttrsStr.length();
for (Enumeration e = propertyNames(); e.hasMoreElements();) {
String propName = (String) e.nextElement();
if (propName.startsWith(brokerAttrsStr)) {
String newPropName, value;
newPropName = propName.substring(brokerAttrsLen);
value = getProperty(propName);
brokerAttrs.setProperty(newPropName, value);
}
}
ba = new BrokerAdmin(brokerAttrs, usernameValue, passwdValue, timeout);
/*
* Set key string if present.
*/
if (keyValue != null) {
ba.setKey(keyValue);
}
return (ba);
}
public void addBrokerAdmin(BrokerAdmin ba) {
Properties brokerAttrs;
String basePropName, brokerAttrsStr, keyPropName, usernamePropName, passwdPropName;
String keyValue, usernameValue, passwdValue;
int index = getBrokerCount();
if (DEBUG) {
System.err.println("Setting properties:");
}
/*
* Construct base property name string: broker0.
*/
basePropName = PROP_NAME_BROKER_BASENAME + Integer.toString(index) + ".";
/*
* Construct property name string: broker0.key
*/
keyPropName = basePropName + PROP_NAME_BROKER_KEY_PREFIX;
/*
* Get key value from BrokerAdmin and set it in properties.
*/
keyValue = ba.getKey();
setProperty(keyPropName, keyValue);
/*
* For 2.0 FCS, we won't write out or read the timeout value. Construct property name string: broker0.receiveTimeout
* timeoutPropName = basePropName + PROP_NAME_BROKER_RECV_TIMEOUT_PREFIX;
*/
/*
* For 2.0 FCS, we won't write out or read the timeout value. Get timeout value from BrokerAdmin and set it in
* properties. timeout = ba.getTimeout(); setProperty(timeoutPropName, new Long(timeout).toString());
*/
/*
* Construct property name string: broker0.userName
*/
usernamePropName = basePropName + PROP_NAME_BROKER_USERNAME_PREFIX;
/*
* Get username value from BrokerAdmin and set it in properties.
*/
usernameValue = ba.getUserName();
setProperty(usernamePropName, usernameValue);
/*
* Construct property name string: broker0.password
*/
passwdPropName = basePropName + PROP_NAME_BROKER_PASSWD_PREFIX;
/*
* Get passwd value from BrokerAdmin and set it in properties.
*/
passwdValue = ba.getPassword();
setProperty(passwdPropName, passwdValue);
/*
* Construct base property name string: broker0.attrs.
*/
brokerAttrsStr = basePropName + PROP_NAME_BROKER_ATTR_PREFIX + ".";
brokerAttrs = ba.getBrokerAttrs();
for (Enumeration e = brokerAttrs.propertyNames(); e.hasMoreElements();) {
String propName = (String) e.nextElement(), newPropName, newValue;
newValue = brokerAttrs.getProperty(propName);
newPropName = brokerAttrsStr + propName;
setProperty(newPropName, newValue);
}
index++;
setProperty(PROP_NAME_BROKER_COUNT, Integer.toString(index));
}
@Override
public Object setProperty(String key, String value) {
if (DEBUG) {
System.err.println("\tSetting property: " + key + "=" + value);
}
return (super.setProperty(key, value));
}
/** @throws SecurityException */
@Override
public void load() throws UserPropertiesException {
super.load();
String v = getVersion();
/*
* If no version property found, assume the file is the current version (i.e. OK)
*/
if (v == null) {
return;
}
checkVersion(v, VERSION, FIRST_VERSION);
}
private void checkVersion(String fileVersionStr, String expectedVersionStr, String firstVersionStr) throws UserPropertiesException {
double expectedVersion, fileVersion, firstVersion;
/*
* Convert current version string to double.
*/
try {
expectedVersion = Double.parseDouble(expectedVersionStr);
} catch (NumberFormatException nfe) {
UserPropertiesException upe;
expectedVersion = 0;
upe = new UserPropertiesException(acr.getString(acr.E_BAD_INT_BKR_LIST_VER, expectedVersionStr));
throw (upe);
}
/*
* Convert first version string to double.
*/
try {
firstVersion = Double.parseDouble(firstVersionStr);
} catch (NumberFormatException nfe) {
UserPropertiesException upe;
firstVersion = 0;
upe = new UserPropertiesException(acr.getString(acr.E_BAD_INT_BKR_LIST_VER, firstVersionStr));
throw (upe);
}
/*
* Convert file version string to double.
*/
try {
fileVersion = Double.parseDouble(fileVersionStr);
} catch (NumberFormatException nfe) {
UserPropertiesException upe;
Object args[] = new Object[4];
fileVersion = 0;
args[0] = getAbsoluteFileName();
args[1] = PROP_NAME_VERSION;
args[2] = fileVersionStr;
args[3] = expectedVersionStr;
String s = acr.getString(acr.E_BAD_FILE_BKR_LIST_VER, args);
upe = new UserPropertiesException(s);
throw (upe);
}
/*
* File version is less than our first version - error !
*/
if (fileVersion < firstVersion) {
UserPropertiesException upe;
Object args[] = new Object[4];
args[0] = getAbsoluteFileName();
args[1] = PROP_NAME_VERSION;
args[2] = fileVersionStr;
args[3] = expectedVersionStr;
String s = acr.getString(acr.E_BAD_FILE_BKR_LIST_VER, args);
upe = new UserPropertiesException(s);
throw (upe);
}
/*
* File version is greater than our current version - error !
*/
if (fileVersion > expectedVersion) {
UserPropertiesException upe;
Object args[] = new Object[4];
args[0] = getAbsoluteFileName();
args[1] = PROP_NAME_VERSION;
args[2] = fileVersionStr;
args[3] = expectedVersionStr;
String s = acr.getString(acr.E_BAD_FILE_BKR_LIST_VER, args);
upe = new UserPropertiesException(s);
throw (upe);
}
/*
* Add checks for firstVersion < fileVersion < expectedVersion here. Currently we don't have any - since this is our
* first official version.
*/
}
}