OW2 Consortium joram

Rev

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