OW2 Consortium joram

Rev

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