OW2 Consortium joram

Rev

Rev 6406 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
861 fmaistre 1
/*
2
 * JORAM: Java(TM) Open Reliable Asynchronous Messaging
6262 afreyssin 3
 * Copyright (C) 2004 - 2015 ScalAgent Distributed Technologies
1669 afreyssin 4
 * Copyright (C) 2004 Bull SA
861 fmaistre 5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19
 * USA.
20
 *
1669 afreyssin 21
 * Initial developer(s): Frederic Maistre (Bull SA)
22
 * Contributor(s): ScalAgent Distributed Technologies
861 fmaistre 23
 */
24
package org.objectweb.joram.client.jms.admin;
25
 
2663 tachker 26
import java.net.ConnectException;
1331 tachker 27
import java.util.List;
2751 afreyssin 28
import java.util.Properties;
2663 tachker 29
import java.util.Vector;
861 fmaistre 30
 
1254 afreyssin 31
import javax.jms.JMSException;
2663 tachker 32
import javax.naming.NamingException;
33
import javax.naming.Reference;
34
import javax.naming.StringRefAddr;
861 fmaistre 35
 
1254 afreyssin 36
import org.objectweb.joram.client.jms.Message;
3155 surrel 37
import org.objectweb.joram.client.jms.Queue;
4364 tachker 38
import org.objectweb.joram.shared.admin.AdminCommandConstant;
39
import org.objectweb.joram.shared.admin.AdminCommandReply;
2663 tachker 40
import org.objectweb.joram.shared.admin.AdminReply;
2889 afreyssin 41
import org.objectweb.joram.shared.admin.AdminRequest;
2663 tachker 42
import org.objectweb.joram.shared.admin.ClearSubscription;
43
import org.objectweb.joram.shared.admin.CreateUserReply;
44
import org.objectweb.joram.shared.admin.CreateUserRequest;
45
import org.objectweb.joram.shared.admin.DeleteSubscriptionMessage;
46
import org.objectweb.joram.shared.admin.DeleteUser;
4546 surrel 47
import org.objectweb.joram.shared.admin.GetDMQSettingsReply;
48
import org.objectweb.joram.shared.admin.GetDMQSettingsRequest;
49
import org.objectweb.joram.shared.admin.GetNbMaxMsgRequest;
4025 afreyssin 50
import org.objectweb.joram.shared.admin.GetNumberReply;
2663 tachker 51
import org.objectweb.joram.shared.admin.GetSubscription;
52
import org.objectweb.joram.shared.admin.GetSubscriptionMessage;
53
import org.objectweb.joram.shared.admin.GetSubscriptionMessageIds;
54
import org.objectweb.joram.shared.admin.GetSubscriptionMessageIdsRep;
55
import org.objectweb.joram.shared.admin.GetSubscriptionMessageRep;
56
import org.objectweb.joram.shared.admin.GetSubscriptionRep;
57
import org.objectweb.joram.shared.admin.GetSubscriptions;
58
import org.objectweb.joram.shared.admin.GetSubscriptionsRep;
4025 afreyssin 59
import org.objectweb.joram.shared.admin.SetDMQRequest;
60
import org.objectweb.joram.shared.admin.SetNbMaxMsgRequest;
61
import org.objectweb.joram.shared.admin.SetThresholdRequest;
2663 tachker 62
import org.objectweb.joram.shared.admin.UpdateUser;
63
import org.objectweb.joram.shared.security.Identity;
2813 afreyssin 64
import org.objectweb.joram.shared.security.SimpleIdentity;
1331 tachker 65
import org.objectweb.util.monolog.api.BasicLevel;
2889 afreyssin 66
import org.objectweb.util.monolog.api.Logger;
1331 tachker 67
 
3161 surrel 68
import fr.dyade.aaa.common.Debug;
2663 tachker 69
import fr.dyade.aaa.util.management.MXWrapper;
70
 
861 fmaistre 71
/**
5241 afreyssin 72
 * The <code>User</code> class is a utility class needed for administering Joram users.
73
 * <p>
74
 * The User class is a factory for Joram's users through the create static methods, the
75
 * User object provides Joram specific administration and monitoring methods.
76
 * <p>
77
 * The User object provides methods to add and remove Interceptors, such an interceptor
78
 * can handle each incoming and outgoing message. Interceptors can read and also modify
79
 * the messages. This enables filtering, transformation or content enrichment, for example
80
 * adding a property into the message. Also Interceptors can stop the Interceptor chain by
81
 * simply returning false to their intercept method invocation, in this case the transmission
82
 * of the message is stopped.
83
 * <p>
84
 * There is two distinct chains of interceptors:<ul>
85
 * <li>The first one “interceptors_in” handles each message that’s entering the server (result
86
 * of a send method on a connection from the selected user).</li>
87
 * <li>The second one “interceptors_out” handles each message that’s exiting the server (result
88
 * of a receive method on a connection from the selected user).</li>
5363 afreyssin 89
 * </ul>
5241 afreyssin 90
 * These two interceptor chains are configurable for each user.
861 fmaistre 91
 */
1669 afreyssin 92
public class User extends AdministeredObject implements UserMBean {
2889 afreyssin 93
  /** define serialVersionUID for interoperability */
1977 tachker 94
  private static final long serialVersionUID = 1L;
2889 afreyssin 95
 
6406 afreyssin 96
  public static final String REDELIVERY_DELAY = "redeliveryDelay";
97
 
2889 afreyssin 98
  public static Logger logger = Debug.getLogger(User.class.getName());
99
 
861 fmaistre 100
  /** The name of the user. */
904 fmaistre 101
  String name;
861 fmaistre 102
  /** Identifier of the user's proxy agent. */
904 fmaistre 103
  String proxyId;
861 fmaistre 104
 
1254 afreyssin 105
  // Used by jndi2 SoapObjectHelper
106
  public User() {}
861 fmaistre 107
 
108
  /**
109
   * Constructs an <code>User</code> instance.
110
   *
111
   * @param name  The name of the user.
4203 afreyssin 112
   */
113
  public User(String name) {
114
    this.name = name;
115
  }
116
 
117
  /**
118
   * Constructs an <code>User</code> instance.
119
   *
120
   * @param name  The name of the user.
861 fmaistre 121
   * @param proxyId  Identifier of the user's proxy agent.
122
   */
1254 afreyssin 123
  public User(String name, String proxyId) {
861 fmaistre 124
    this.name = name;
125
    this.proxyId = proxyId;
126
  }
127
 
2889 afreyssin 128
 
861 fmaistre 129
  /** Returns a string view of this <code>User</code> instance. */
1788 afreyssin 130
  public String toString() {
861 fmaistre 131
    return "User[" + name + "]:" + proxyId;
132
  }
133
 
883 fmaistre 134
 
135
  /** Returns the user name. */
1788 afreyssin 136
  public String getName() {
883 fmaistre 137
    return name;
138
  }
139
 
5241 afreyssin 140
  /**
141
   * Returns <code>true</code> if the parameter object is a Joram user wrapping
142
   * the same Joram's User.
143
   */
1788 afreyssin 144
  public boolean equals(Object o) {
883 fmaistre 145
    if (! (o instanceof User))
146
      return false;
147
 
148
    User other = (User) o;
149
 
4025 afreyssin 150
    return other.proxyId.equals(proxyId);
883 fmaistre 151
  }
2889 afreyssin 152
 
4025 afreyssin 153
  public int hashCode() {
154
    return getName().hashCode();
155
  }
156
 
861 fmaistre 157
  /**
2889 afreyssin 158
   * Administration wrapper used to perform administration stuff.
159
   * <p>
160
   * It is defined through AdminModule element, it is closed at the end of
161
   * the script. if it is not defined the wrapper set at creation is used, if
162
   * none the static AdminModule connection is used.
163
   */
164
  AdminWrapper wrapper = null;
165
 
166
  /**
167
   * Returns the administration wrapper to use.
168
   *
169
   * @return The wrapper to use.
170
   * @throws ConnectException if no wrapper is defined.
171
   */
172
  protected final AdminWrapper getWrapper() throws ConnectException {
2988 afreyssin 173
    if ((wrapper != null) && (! wrapper.isClosed()))
174
      return wrapper;
2889 afreyssin 175
    return AdminModule.getWrapper();
176
  }
177
 
178
  /**
179
   * Sets the administration wrapper to use.
180
   * If not set the AdminModule static connection is used by default.
181
   *
182
   * @param wrapper The wrapper to use or null to unset.
183
   */
184
  public void setWrapper(AdminWrapper wrapper) {
185
    this.wrapper = wrapper;
186
  }
187
 
188
  /**
189
   * Method actually sending an <code>AdminRequest</code> instance to
190
   * the platform and getting an <code>AdminReply</code> instance.
191
   *
192
   * @param request the administration request to send
193
   * @return  the reply message
194
   *
195
   * @exception ConnectException  If the connection to the platform fails.
196
   * @exception AdminException  If the platform's reply is invalid, or if
197
   *              the request failed.
198
   */
5212 afreyssin 199
  private final AdminReply doRequest(AdminRequest request) throws AdminException, ConnectException {
2889 afreyssin 200
    return getWrapper().doRequest(request);
201
  }
202
 
203
  /**
2795 surrel 204
   * Admin method creating a user for a given server and instantiating the
861 fmaistre 205
   * corresponding <code>User</code> object.
206
   * <p>
207
   * If the user has already been set on this server, the method simply
208
   * returns the corresponding <code>User</code> object. Its fails if the
209
   * target server does not belong to the platform, or if a proxy could not
210
   * be deployed server side for a new user.
2889 afreyssin 211
   * <p>
212
   * Be careful this method use the static AdminModule connection.
861 fmaistre 213
   *
214
   * @param name  Name of the user.
215
   * @param password  Password of the user.
216
   * @param serverId  The identifier of the user's server.
217
   *
218
   * @exception ConnectException  If the connection fails.
219
   * @exception AdminException  If the request fails.
220
   */
2889 afreyssin 221
  public static User create(String name, String password,
222
                            int serverId) throws ConnectException, AdminException {
2813 afreyssin 223
    return create(name, password, serverId, SimpleIdentity.class.getName());
861 fmaistre 224
  }
2889 afreyssin 225
 
861 fmaistre 226
  /**
2795 surrel 227
   * Admin method creating a user on the local server and instantiating the
861 fmaistre 228
   * corresponding <code>User</code> object.
229
   * <p>
230
   * If the user has already been set on this server, the method simply
231
   * returns the corresponding <code>User</code> object. It fails if a
232
   * proxy could not be deployed server side for a new user.
2889 afreyssin 233
   * <p>
234
   * Be careful this method use the static AdminModule connection.
861 fmaistre 235
   *
236
   * @param name  Name of the user.
237
   * @param password  Password of the user.
238
   *
239
   * @exception ConnectException  If the connection fails.
240
   * @exception AdminException  If the request fails.
241
   */
2889 afreyssin 242
  public static User create(String name, String password) throws ConnectException, AdminException {
2813 afreyssin 243
    return create(name, password, AdminModule.getLocalServerId(), SimpleIdentity.class.getName());
861 fmaistre 244
  }
2889 afreyssin 245
 
861 fmaistre 246
  /**
2795 surrel 247
   * Admin method creating a user for a given server and instantiating the
2663 tachker 248
   * corresponding <code>User</code> object.
249
   * <p>
250
   * If the user has already been set on this server, the method simply
251
   * returns the corresponding <code>User</code> object. Its fails if the
252
   * target server does not belong to the platform, or if a proxy could not
253
   * be deployed server side for a new user.
2889 afreyssin 254
   * <p>
255
   * Be careful this method use the static AdminModule connection.
2663 tachker 256
   *
257
   * @param name  Name of the user.
258
   * @param password  Password of the user.
259
   * @param serverId  The identifier of the user's server.
2795 surrel 260
   * @param identityClassName user/password or JAAS... (default SimpleIdentity).
2663 tachker 261
   *
262
   * @exception ConnectException  If the connection fails.
263
   * @exception AdminException  If the request fails.
264
   */
2889 afreyssin 265
  public static User create(String name, String password,
266
                            int serverId,
267
                            String identityClassName) throws ConnectException, AdminException {
4393 tachker 268
  	return create(name, password, serverId, identityClassName, null);
269
  }
270
 
271
  /**
272
   * Admin method creating a user for a given server and instantiating the
273
   * corresponding <code>User</code> object.
274
   * <p>
275
   * If the user has already been set on this server, the method simply
276
   * returns the corresponding <code>User</code> object. Its fails if the
277
   * target server does not belong to the platform, or if a proxy could not
278
   * be deployed server side for a new user.
279
   * <p>
280
   * Be careful this method use the static AdminModule connection.
281
   *
282
   * @param name  Name of the user.
283
   * @param password  Password of the user.
284
   * @param serverId  The identifier of the user's server.
285
   * @param identityClassName user/password or JAAS... (default SimpleIdentity).
286
   * @param prop properties
287
   *
288
   * @exception ConnectException  If the connection fails.
289
   * @exception AdminException  If the request fails.
290
   */
291
  public static User create(String name, String password,
292
                            int serverId,
293
                            String identityClassName,
294
                            Properties prop) throws ConnectException, AdminException {
6262 afreyssin 295
    if ((name == null) || name.equals(""))
296
      throw new AdminException("User name can not be null or empty");
297
 
2663 tachker 298
    Identity identity = createIdentity(name, password, identityClassName);
2889 afreyssin 299
 
4203 afreyssin 300
    User user = new User(name);
4393 tachker 301
    AdminReply reply = user.getWrapper().doRequest(new CreateUserRequest(identity, serverId, prop));
4203 afreyssin 302
    user.proxyId = ((CreateUserReply) reply).getProxId();
303
 
2931 afreyssin 304
    // Be careful, MBean registration is now done explicitly
2889 afreyssin 305
 
306
    return user;
307
  }
308
 
2931 afreyssin 309
  // Object name of the MBean if it is registered.
2889 afreyssin 310
  transient protected String JMXBeanName = null;
5044 tachker 311
 
312
  public static String getJMXBeanName(String base, User user) {
313
    int sid = Integer.parseInt(user.proxyId.substring(user.proxyId.indexOf('.') +1, user.proxyId.lastIndexOf('.')));
314
    StringBuffer buf = new StringBuffer();
315
    buf.append(base);
316
    buf.append(":type=User,location=server#").append(sid).append(",name=").append(user.getName()).append('[').append(user.getProxyId()).append(']');
317
    return buf.toString();
318
  }
2889 afreyssin 319
 
2931 afreyssin 320
  public String registerMBean(String base) {
5044 tachker 321
    JMXBeanName = getJMXBeanName(base, this);
2931 afreyssin 322
 
2663 tachker 323
    try {
2931 afreyssin 324
      MXWrapper.registerMBean(this, JMXBeanName);
2663 tachker 325
    } catch (Exception e) {
2971 afreyssin 326
      if (logger.isLoggable(BasicLevel.DEBUG))
327
        logger.log(BasicLevel.DEBUG, "User.registerMBean: " + JMXBeanName, e);
2663 tachker 328
    }
2931 afreyssin 329
 
330
    return JMXBeanName;
2663 tachker 331
  }
2889 afreyssin 332
 
333
  public void unregisterMBean() {
4557 surrel 334
    if (JMXBeanName == null)
335
      return;
2889 afreyssin 336
 
337
    try {
2931 afreyssin 338
      MXWrapper.unregisterMBean(JMXBeanName);
2889 afreyssin 339
    } catch (Exception e) {
2971 afreyssin 340
      if (logger.isLoggable(BasicLevel.DEBUG))
341
        logger.log(BasicLevel.DEBUG, "User.unregisterMBean: " + JMXBeanName, e);
2889 afreyssin 342
    }
343
  }
344
 
2663 tachker 345
  /**
346
   * Create a user Identity.
347
   *
348
   * @param user              Name of the user.
349
   * @param passwd            Password of the user.
350
   * @param identityClassName identity class name (simple, jaas).
351
   * @return identity user Identity.
352
   * @throws AdminException
353
   */
2889 afreyssin 354
  private static Identity createIdentity(String user, String passwd,
355
                                         String identityClassName) throws AdminException {
2663 tachker 356
    Identity identity = null;
357
    try {
358
      Class clazz = Class.forName(identityClassName);
359
      identity = (Identity) clazz.newInstance();
360
      if (passwd != null)
361
        identity.setIdentity(user, passwd);
362
      else
363
        identity.setUserName(user);
364
    } catch (Exception e) {
365
      throw new AdminException(e.getMessage());
366
    }
367
    return identity;
368
  }
2889 afreyssin 369
 
2663 tachker 370
  /**
861 fmaistre 371
   * Admin method updating this user identification.
372
   * <p>
373
   * The request fails if the user does not exist server side, or if the new
374
   * identification is already taken by a user on the same server.
375
   *
376
   * @param newName  The new name of the user.
377
   * @param newPassword  The new password of the user.
378
   *
379
   * @exception ConnectException  If the connection fails.
380
   * @exception AdminException  If the request fails.
381
   */
2889 afreyssin 382
  public void update(String newName, String newPassword) throws ConnectException, AdminException {
2813 afreyssin 383
    update(newName, newPassword, SimpleIdentity.class.getName());
2663 tachker 384
  }
2889 afreyssin 385
 
2663 tachker 386
  /**
387
   * Admin method updating this user identification.
388
   * <p>
389
   * The request fails if the user does not exist server side, or if the new
390
   * identification is already taken by a user on the same server.
391
   *
392
   * @param newName  The new name of the user.
393
   * @param newPassword  The new password of the user.
2795 surrel 394
   * @param identityClassName user/password or JAAS... (default SimpleIdentity).
2663 tachker 395
   *
396
   * @exception ConnectException  If the connection fails.
397
   * @exception AdminException  If the request fails.
398
   */
2889 afreyssin 399
  public void update(String newName, String newPassword,
400
                     String identityClassName) throws ConnectException, AdminException {
2663 tachker 401
    Identity newIdentity = createIdentity(newName, newPassword, identityClassName);
2889 afreyssin 402
    doRequest(new UpdateUser(name, proxyId, newIdentity));
861 fmaistre 403
    name = newName;
404
  }
405
 
406
  /**
407
   * Removes this user.
408
   *
409
   * @exception ConnectException  If the connection fails.
410
   * @exception AdminException  Never thrown.
411
   */
1331 tachker 412
  public void delete() throws ConnectException, AdminException {
2889 afreyssin 413
    doRequest(new DeleteUser(name, proxyId));
2931 afreyssin 414
    unregisterMBean();
861 fmaistre 415
  }
416
 
417
  /**
418
   * Admin method setting a given dead message queue for this user.
419
   * <p>
420
   * The request fails if the user is deleted server side.
421
   *
422
   * @param dmq  The dead message queue to be set.
423
   *
424
   * @exception ConnectException  If the connection fails.
425
   * @exception AdminException  If the request fails.
426
   */
3155 surrel 427
  public void setDMQ(Queue dmq) throws ConnectException, AdminException {
1873 tachker 428
    setDMQId(dmq.getName());
861 fmaistre 429
  }
430
 
431
  /**
1873 tachker 432
   * Admin method setting a given dead message queue for this user.
433
   * <p>
434
   * The request fails if the user is deleted server side.
435
   *
436
   * @param dmqId  The dead message queue Id to be set.
437
   *
438
   * @exception ConnectException  If the connection fails.
439
   * @exception AdminException  If the request fails.
440
   */
441
  public void setDMQId(String dmqId) throws ConnectException, AdminException {
4025 afreyssin 442
    doRequest(new SetDMQRequest(proxyId, dmqId));
1873 tachker 443
  }
4025 afreyssin 444
 
445
  /**
446
   * Monitoring method returning the dead message queue of this user,
447
   * null if not set.
861 fmaistre 448
   * <p>
449
   * The request fails if the user is deleted server side.
450
   *
4025 afreyssin 451
   * @exception ConnectException  If the administration connection is closed or broken.
861 fmaistre 452
   * @exception AdminException  If the request fails.
453
   */
4025 afreyssin 454
  public Queue getDMQ() throws ConnectException, AdminException {
455
    String dmqId = getDMQId();
456
    Queue dmq = null;
457
    if (dmqId != null) {
458
      dmq = new Queue(dmqId);
459
      if (wrapper != null)
460
        dmq.setWrapper(wrapper);
461
    }
2889 afreyssin 462
    return dmq;
861 fmaistre 463
  }
464
 
4025 afreyssin 465
  /**
466
   * Monitoring method returning the dead message queue id of this user,
467
   * null if not set.
1873 tachker 468
   * <p>
4025 afreyssin 469
   * The request fails if the destination is deleted server side.
1873 tachker 470
   *
4025 afreyssin 471
   * @exception ConnectException  If the administration connection is closed or broken.
1873 tachker 472
   * @exception AdminException  If the request fails.
473
   */
474
  public String getDMQId() throws ConnectException, AdminException {
4025 afreyssin 475
    GetDMQSettingsRequest request = new GetDMQSettingsRequest(proxyId);
476
    GetDMQSettingsReply reply = (GetDMQSettingsReply) doRequest(request);
477
 
478
    return reply.getDMQName();
1873 tachker 479
  }
2889 afreyssin 480
 
4753 afreyssin 481
  /**
482
   * Admin method setting a given value as the threshold for this user.
483
   * <p>
484
   * The request fails if the user is deleted server side.
485
   *
486
   * @param threshold  The threshold value to be set (-1 for unsetting
487
   *                   previous value).
488
   *
489
   * @exception ConnectException  If the connection fails.
490
   * @exception AdminException  If the request fails.
491
   */
492
  public void setThreshold(int threshold) throws ConnectException, AdminException {
493
    doRequest(new SetThresholdRequest(proxyId, threshold));
494
  }
495
 
496
  /**
497
   * Admin method setting a given value as the threshold for a particular
498
   * subscription of this user.
499
   * <p>
500
   * The request fails if the user is deleted server side.
501
   *
502
   * @param subname    The subscription name.
503
   * @param threshold  The threshold value to be set (-1 for unsetting
504
   *                   previous value).
505
   *
506
   * @exception ConnectException  If the connection fails.
507
   * @exception AdminException  If the request fails.
508
   */
509
  public void setThreshold(String subname, int threshold) throws ConnectException, AdminException {
510
    doRequest(new SetThresholdRequest(proxyId, subname, threshold));
511
  }
512
 
1873 tachker 513
  /**
861 fmaistre 514
   * Returns the threshold for this user, -1 if not set.
515
   * <p>
516
   * The request fails if the user is deleted server side.
517
   *
518
   * @exception ConnectException  If the connection fails.
519
   * @exception AdminException  If the request fails.
520
   */
1788 afreyssin 521
  public int getThreshold() throws ConnectException, AdminException {
4025 afreyssin 522
    GetDMQSettingsRequest request = new GetDMQSettingsRequest(proxyId);
523
    GetDMQSettingsReply reply = (GetDMQSettingsReply) doRequest(request);
2889 afreyssin 524
 
4753 afreyssin 525
    return reply.getThreshold();
861 fmaistre 526
  }
527
 
4753 afreyssin 528
  /**
529
   * Returns the threshold for a particular subscription of this user,
530
   * -1 if not set.
531
   * <p>
532
   * The request fails if the user is deleted server side.
533
   *
534
   * @param subname The subscription name.
535
   *
536
   * @exception ConnectException  If the connection fails.
537
   * @exception AdminException  If the request fails.
538
   */
539
  public int getThreshold(String subname) throws ConnectException, AdminException {
540
    GetDMQSettingsRequest request = new GetDMQSettingsRequest(proxyId, subname);
541
    GetDMQSettingsReply reply = (GetDMQSettingsReply) doRequest(request);
542
 
543
    return reply.getThreshold();
544
  }
545
 
1254 afreyssin 546
  /**
1331 tachker 547
   * Admin method setting nbMaxMsg for this subscription.
548
   * <p>
549
   * The request fails if the sub is deleted server side.
550
   *
551
   * @param subName the name of the subscription.
552
   * @param nbMaxMsg  nb Max of Message (-1 no limit).
553
   *
554
   * @exception ConnectException  If the admin connection is closed or broken.
555
   * @exception AdminException  If the request fails.
556
   */
2889 afreyssin 557
  public void setNbMaxMsg(String subName, int nbMaxMsg) throws ConnectException, AdminException {
558
    //  TODO: Subscription sub = getSubscription(subName);
4025 afreyssin 559
    doRequest(new SetNbMaxMsgRequest(proxyId, nbMaxMsg, subName));
1331 tachker 560
  }
561
 
562
  /**
563
   * Monitoring method returning the nbMaxMsg of this subscription, -1 if no limit.
564
   * <p>
565
   * The request fails if the sub is deleted server side.
566
   *
567
   * @param subName the name of the subscription.
568
   *
569
   * @exception ConnectException  If the admin connection is closed or broken.
570
   * @exception AdminException  If the request fails.
571
   */
2889 afreyssin 572
  public int getNbMaxMsg(String subName) throws ConnectException, AdminException {
573
    //  TODO: Subscription sub = getSubscription(subName);
4025 afreyssin 574
    GetNbMaxMsgRequest request = new GetNbMaxMsgRequest(proxyId, subName);
575
    GetNumberReply reply = (GetNumberReply) doRequest(request);
576
    return reply.getNumber();
1331 tachker 577
  }
578
 
579
  /**
1254 afreyssin 580
   * Returns the subscriptions owned by a user.
581
   *
3080 afreyssin 582
   * @return The subscriptions owned by the user.
1254 afreyssin 583
   *
3080 afreyssin 584
   * @exception AdminException If an error is raised by the administration operation.
1254 afreyssin 585
   * @exception ConnectException  If the admin connection is not established.
586
   */
2889 afreyssin 587
  public Subscription[] getSubscriptions() throws AdminException, ConnectException {
588
    GetSubscriptionsRep reply = (GetSubscriptionsRep) doRequest(new GetSubscriptions(proxyId));
589
 
1254 afreyssin 590
    String[] subNames = reply.getSubNames();
591
    String[] topicIds = reply.getTopicIds();
592
    int[] messageCounts = reply.getMessageCounts();
5097 tachker 593
    int[] ackCounts = reply.getDeliveredMessageCount();
1254 afreyssin 594
    boolean[] durable = reply.getDurable();
2889 afreyssin 595
 
1254 afreyssin 596
    Subscription[] res = new Subscription[subNames.length];
597
    for (int i = 0; i < res.length; i++) {
5097 tachker 598
      res[i] = new Subscription(subNames[i], topicIds[i], messageCounts[i], ackCounts[i], durable[i]);
1254 afreyssin 599
    }
600
    return res;
601
  }
602
 
1331 tachker 603
  /** used by MBean jmx */
2889 afreyssin 604
  public List getSubscriptionList() throws AdminException, ConnectException {
1331 tachker 605
    Vector list = new Vector();
606
    Subscription[] sub = getSubscriptions();
607
    for (int i = 0; i < sub.length; i++) {
608
      list.add(sub[i].toString());
609
    }
610
    return list;
611
  }
612
 
1254 afreyssin 613
  /**
614
   * Returns a subscription.
615
   *
616
   * @param subName the name of the subscription.
3080 afreyssin 617
   * @return The subscription.
1254 afreyssin 618
   *
3080 afreyssin 619
   * @exception AdminException If an error is raised by the administration operation.
1254 afreyssin 620
   * @exception ConnectException  If the admin connection is not established.
621
   */
2587 afreyssin 622
  public Subscription getSubscription(String subName) throws AdminException, ConnectException {
2889 afreyssin 623
    GetSubscriptionRep reply = (GetSubscriptionRep) doRequest(new GetSubscription(proxyId, subName));
5097 tachker 624
    Subscription sub = new Subscription(subName, reply.getTopicId(), reply.getMessageCount(), reply.getDeliveredMessageCount(), reply.getDurable());
2889 afreyssin 625
 
626
    return sub;
1254 afreyssin 627
  }
628
 
5212 afreyssin 629
  /** used by MBean jmx */
2587 afreyssin 630
  public String getSubscriptionString(String subName) throws AdminException, ConnectException {
631
    return getSubscription(subName).toString();
1331 tachker 632
  }
633
 
2587 afreyssin 634
  public String[] getMessageIds(String subName) throws AdminException, ConnectException {
2751 afreyssin 635
    GetSubscriptionMessageIdsRep reply =
2889 afreyssin 636
      (GetSubscriptionMessageIdsRep) doRequest(new GetSubscriptionMessageIds(proxyId, subName));
1254 afreyssin 637
    return reply.getMessageIds();
638
  }
639
 
5212 afreyssin 640
  /**
641
   * Returns a copy of a message of the subscription.
642
   *
643
   * @param subName       The name of the related subscription.
644
   * @param msgId         The identifier of the message.
645
   * @return The message
646
   *
647
   * @throws AdminException
648
   * @throws ConnectException
649
   * @throws JMSException
650
   */
2751 afreyssin 651
  public Message getMessage(String subName,
652
                            String msgId) throws AdminException, ConnectException, JMSException {
653
    GetSubscriptionMessageRep reply =
2889 afreyssin 654
      (GetSubscriptionMessageRep) doRequest(new GetSubscriptionMessage(proxyId, subName, msgId, true));
2751 afreyssin 655
    return Message.wrapMomMessage(null, reply.getMessage());
656
  }
2889 afreyssin 657
 
2751 afreyssin 658
  public String getMessageDigest(String subName,
659
                                 String msgId) throws AdminException, ConnectException, JMSException {
660
    GetSubscriptionMessageRep reply =
2889 afreyssin 661
      (GetSubscriptionMessageRep) doRequest(new GetSubscriptionMessage(proxyId, subName, msgId, true));
2751 afreyssin 662
    Message msg =  Message.wrapMomMessage(null, reply.getMessage());
2889 afreyssin 663
 
2751 afreyssin 664
    StringBuffer strbuf = new StringBuffer();
665
    strbuf.append("Message: ").append(msg.getJMSMessageID());
666
    strbuf.append("\n\tTo: ").append(msg.getJMSDestination());
667
    strbuf.append("\n\tCorrelationId: ").append(msg.getJMSCorrelationID());
668
    strbuf.append("\n\tDeliveryMode: ").append(msg.getJMSDeliveryMode());
669
    strbuf.append("\n\tExpiration: ").append(msg.getJMSExpiration());
670
    strbuf.append("\n\tPriority: ").append(msg.getJMSPriority());
671
    strbuf.append("\n\tRedelivered: ").append(msg.getJMSRedelivered());
672
    strbuf.append("\n\tReplyTo: ").append(msg.getJMSReplyTo());
673
    strbuf.append("\n\tTimestamp: ").append(msg.getJMSTimestamp());
674
    strbuf.append("\n\tType: ").append(msg.getJMSType());
675
    return strbuf.toString();
2889 afreyssin 676
  }
2751 afreyssin 677
 
678
  public Properties getMessageHeader(String subName,
679
                                     String msgId) throws AdminException, ConnectException, JMSException {
680
    GetSubscriptionMessageRep reply =
2889 afreyssin 681
      (GetSubscriptionMessageRep) doRequest(new GetSubscriptionMessage(proxyId, subName, msgId, false));
2751 afreyssin 682
    Message msg =  Message.wrapMomMessage(null, reply.getMessage());
683
 
684
    Properties prop = new Properties();
685
    prop.setProperty("JMSMessageID", msg.getJMSMessageID());
686
    prop.setProperty("JMSDestination", msg.getJMSDestination().toString());
687
    if (msg.getJMSCorrelationID() != null)
688
      prop.setProperty("JMSCorrelationID", msg.getJMSCorrelationID());
689
    prop.setProperty("JMSDeliveryMode",
690
                     new Integer(msg.getJMSDeliveryMode()).toString());
691
    prop.setProperty("JMSExpiration",
692
                     new Long(msg.getJMSExpiration()).toString());
693
    prop.setProperty("JMSPriority",
694
                     new Integer(msg.getJMSPriority()).toString());
695
    prop.setProperty("JMSRedelivered",
696
                     new Boolean(msg.getJMSRedelivered()).toString());
697
    if (msg.getJMSReplyTo() != null)
698
      prop.setProperty("JMSReplyTo", msg.getJMSReplyTo().toString());
699
    prop.setProperty("JMSTimestamp",
700
                     new Long(msg.getJMSTimestamp()).toString());
701
    if (msg.getJMSType() != null)
702
      prop.setProperty("JMSType", msg.getJMSType());
703
 
704
    return prop;
2889 afreyssin 705
  }
2751 afreyssin 706
 
2889 afreyssin 707
  public Properties getMessageProperties(String subName,
708
                                         String msgId) throws AdminException, ConnectException, JMSException {
709
    GetSubscriptionMessageRep reply =
710
      (GetSubscriptionMessageRep) doRequest(new GetSubscriptionMessage(proxyId, subName, msgId, false));
711
    Message msg =  Message.wrapMomMessage(null, reply.getMessage());
2751 afreyssin 712
 
2889 afreyssin 713
    Properties prop = new Properties();
714
    msg.getProperties(prop);
2751 afreyssin 715
 
2889 afreyssin 716
    return prop;
717
  }
2751 afreyssin 718
 
719
  /**
4557 surrel 720
   * Add interceptors
4364 tachker 721
   *
722
   * @param interceptors list of string className interceptor (separate with ",")
723
   * @throws ConnectException
724
   * @throws AdminException
725
   */
726
  public void addInterceptorsIN(String interceptors) throws ConnectException, AdminException {
727
  	Properties prop = new Properties();
4393 tachker 728
    prop.put(AdminCommandConstant.INTERCEPTORS_IN, interceptors);
4364 tachker 729
    getWrapper().processAdmin(getProxyId(), AdminCommandConstant.CMD_ADD_INTERCEPTORS, prop);
730
  }
731
 
732
  /**
5681 tachker 733
   * Add interceptor
734
   *
735
   * @param interceptor the className interceptor
736
   * @param properties the interceptor properties
737
   * @throws ConnectException
738
   * @throws AdminException
739
   */
740
  public void addInterceptorIN(String interceptor, Properties properties) throws ConnectException, AdminException {
741
    if (properties == null)
742
      properties = new Properties();
743
    properties.put(AdminCommandConstant.INTERCEPTORS_IN, interceptor);
744
    getWrapper().processAdmin(getProxyId(), AdminCommandConstant.CMD_ADD_INTERCEPTORS, properties);
745
  }
746
 
747
  /**
4557 surrel 748
   * Get interceptors.
4364 tachker 749
   *
750
   * @return list of string className interceptor (separate with ",")
751
   * @throws ConnectException
752
   * @throws AdminException
753
   */
754
  public String getInterceptorsIN() throws ConnectException, AdminException {
4557 surrel 755
    AdminCommandReply reply = (AdminCommandReply) getWrapper().processAdmin(getProxyId(),
756
        AdminCommandConstant.CMD_GET_INTERCEPTORS, null);
4393 tachker 757
    return (String) reply.getProp().get(AdminCommandConstant.INTERCEPTORS_IN);
4364 tachker 758
  }
759
 
760
  /**
4557 surrel 761
   * Remove interceptors
4364 tachker 762
   *
763
   * @param interceptors list of string className interceptor (separate with ",")
764
   * @throws ConnectException
765
   * @throws AdminException
766
   */
767
  public void removeInterceptorsIN(String interceptors) throws ConnectException, AdminException {
768
  	Properties prop = new Properties();
4393 tachker 769
  	prop.put(AdminCommandConstant.INTERCEPTORS_IN, interceptors);
4557 surrel 770
    getWrapper().processAdmin(getProxyId(), AdminCommandConstant.CMD_REMOVE_INTERCEPTORS, prop);
4364 tachker 771
  }
772
 
773
  /**
4557 surrel 774
   * Add interceptors
4364 tachker 775
   *
776
   * @param interceptors list of string className interceptor (separate with ",")
777
   * @throws ConnectException
778
   * @throws AdminException
779
   */
780
  public void addInterceptorsOUT(String interceptors) throws ConnectException, AdminException {
781
  	Properties prop = new Properties();
4393 tachker 782
    prop.put(AdminCommandConstant.INTERCEPTORS_OUT, interceptors);
4364 tachker 783
    getWrapper().processAdmin(getProxyId(), AdminCommandConstant.CMD_ADD_INTERCEPTORS, prop);
784
  }
785
 
786
  /**
5681 tachker 787
   * Add interceptor
788
   *
789
   * @param interceptor the className interceptor
790
   * @param properties the interceptor properties
791
   * @throws ConnectException
792
   * @throws AdminException
793
   */
794
  public void addInterceptorOUT(String interceptor, Properties properties) throws ConnectException, AdminException {
795
    if (properties == null)
796
      properties = new Properties();
797
    properties.put(AdminCommandConstant.INTERCEPTORS_OUT, interceptor);
798
    getWrapper().processAdmin(getProxyId(), AdminCommandConstant.CMD_ADD_INTERCEPTORS, properties);
799
  }
800
 
801
  /**
4557 surrel 802
   * Get interceptors.
4364 tachker 803
   *
804
   * @return list of string className interceptor (separate with ",")
805
   * @throws ConnectException
806
   * @throws AdminException
807
   */
808
  public String getInterceptorsOUT() throws ConnectException, AdminException {
4557 surrel 809
    AdminCommandReply reply = (AdminCommandReply) getWrapper().processAdmin(getProxyId(),
810
        AdminCommandConstant.CMD_GET_INTERCEPTORS, null);
4393 tachker 811
    return (String) reply.getProp().get(AdminCommandConstant.INTERCEPTORS_OUT);
4364 tachker 812
  }
813
 
814
  /**
4557 surrel 815
   * Remove interceptors
4364 tachker 816
   *
817
   * @param interceptors list of string className interceptor (separate with ",")
818
   * @throws ConnectException
819
   * @throws AdminException
820
   */
821
  public void removeInterceptorsOUT(String interceptors) throws ConnectException, AdminException {
822
  	Properties prop = new Properties();
4393 tachker 823
  	prop.put(AdminCommandConstant.INTERCEPTORS_OUT, interceptors);
4557 surrel 824
    getWrapper().processAdmin(getProxyId(), AdminCommandConstant.CMD_REMOVE_INTERCEPTORS, prop);
4364 tachker 825
  }
826
 
827
  /**
4557 surrel 828
   * Replace interceptor IN
4393 tachker 829
   *
830
   * @param newInterceptor the new className interceptor.
831
   * @param oldInterceptor the old className interceptor.
832
   * @throws ConnectException
833
   * @throws AdminException
834
   */
835
  public void replaceInterceptorIN(String newInterceptor, String oldInterceptor) throws ConnectException, AdminException {
836
  	Properties prop = new Properties();
837
    prop.put(AdminCommandConstant.INTERCEPTORS_IN_NEW, newInterceptor);
838
    prop.put(AdminCommandConstant.INTERCEPTORS_IN_OLD, oldInterceptor);
839
    getWrapper().processAdmin(getProxyId(), AdminCommandConstant.CMD_REPLACE_INTERCEPTORS, prop);
840
  }
841
 
842
  /**
5681 tachker 843
   * Replace interceptor IN
844
   *
845
   * @param newInterceptor the new className interceptor.
846
   * @param oldInterceptor the old className interceptor.
847
   * @param properties the new interceptor properties
848
   * @throws ConnectException
849
   * @throws AdminException
850
   */
851
  public void replaceInterceptorIN(String newInterceptor, String oldInterceptor, Properties properties) throws ConnectException, AdminException {
852
    if (properties == null)
853
      properties = new Properties();
854
    properties.put(AdminCommandConstant.INTERCEPTORS_IN_NEW, newInterceptor);
855
    properties.put(AdminCommandConstant.INTERCEPTORS_IN_OLD, oldInterceptor);
856
    getWrapper().processAdmin(getProxyId(), AdminCommandConstant.CMD_REPLACE_INTERCEPTORS, properties);
857
  }
858
 
859
  /**
4557 surrel 860
   * Replace interceptor OUT
4393 tachker 861
   *
862
   * @param newInterceptor the new className interceptor.
863
   * @param oldInterceptor the old className interceptor.
864
   * @throws ConnectException
865
   * @throws AdminException
866
   */
867
  public void replaceInterceptorOUT(String newInterceptor, String oldInterceptor) throws ConnectException, AdminException {
868
  	Properties prop = new Properties();
869
    prop.put(AdminCommandConstant.INTERCEPTORS_OUT_NEW, newInterceptor);
870
    prop.put(AdminCommandConstant.INTERCEPTORS_OUT_OLD, oldInterceptor);
871
    getWrapper().processAdmin(getProxyId(), AdminCommandConstant.CMD_REPLACE_INTERCEPTORS, prop);
872
  }
873
 
874
  /**
5681 tachker 875
   * Replace interceptor OUT
876
   *
877
   * @param newInterceptor the new className interceptor.
878
   * @param oldInterceptor the old className interceptor.
879
   * @param properties the new interceptor properties
880
   * @throws ConnectException
881
   * @throws AdminException
882
   */
883
  public void replaceInterceptorOUT(String newInterceptor, String oldInterceptor, Properties properties) throws ConnectException, AdminException {
884
    if (properties == null)
885
      properties = new Properties();
886
    properties.put(AdminCommandConstant.INTERCEPTORS_OUT_NEW, newInterceptor);
887
    properties.put(AdminCommandConstant.INTERCEPTORS_OUT_OLD, oldInterceptor);
888
    getWrapper().processAdmin(getProxyId(), AdminCommandConstant.CMD_REPLACE_INTERCEPTORS, properties);
889
  }
890
 
891
  /**
6426 afreyssin 892
   * Set redeliveryDelay attribute.
893
   *
894
   * @param redeliveryDelay The delay use to wait before re-delivering messages after a deny.
895
   * @throws ConnectException
896
   * @throws AdminException
897
   */
898
  public void setRedeliveryDelay(int redeliveryDelay) throws ConnectException, AdminException {
899
    Properties properties = new Properties();
900
    properties.put(AdminCommandConstant.REDELIVERY_DELAY, "" + redeliveryDelay);
901
    getWrapper().processAdmin(getProxyId(), AdminCommandConstant.CMD_SET_REDELIVERY_DELAY, properties);
902
  }
903
 
904
  /**
2751 afreyssin 905
   * @deprecated
906
   */
1669 afreyssin 907
  public Message readMessage(String subName,
908
                             String msgId) throws AdminException, ConnectException, JMSException {
2751 afreyssin 909
    return getMessage(subName, msgId);
1254 afreyssin 910
  }
911
 
2889 afreyssin 912
  public void deleteMessage(String subName,
913
                            String msgId)  throws AdminException, ConnectException {
914
    doRequest(new DeleteSubscriptionMessage(proxyId, subName, msgId));
1254 afreyssin 915
  }
916
 
2889 afreyssin 917
  public void clearSubscription(String subName) throws AdminException, ConnectException {
918
    doRequest(new ClearSubscription(proxyId, subName));
1254 afreyssin 919
  }
920
 
2889 afreyssin 921
 
861 fmaistre 922
  /** Returns the identifier of the user's proxy. */
1788 afreyssin 923
  public String getProxyId() {
861 fmaistre 924
    return proxyId;
925
  }
926
 
1788 afreyssin 927
  /** Sets the naming reference of a connection factory. */
928
  public void toReference(Reference ref) throws NamingException {
861 fmaistre 929
    ref.add(new StringRefAddr("user.name", name));
930
    ref.add(new StringRefAddr("user.id", proxyId));
931
  }
932
 
1788 afreyssin 933
  /** Restores the administered object from a naming reference. */
934
  public void fromReference(Reference ref) throws NamingException {
935
    name = (String) ref.get("user.name").getContent();
936
    proxyId = (String) ref.get("user.id").getContent();
937
  }
861 fmaistre 938
}