OW2 Consortium joram

Rev

Rev 2634 | Rev 2746 | 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
2634 afreyssin 3
 * Copyright (C) 2004 - 2008 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;
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;
1669 afreyssin 37
import org.objectweb.joram.shared.JoramTracing;
2663 tachker 38
import org.objectweb.joram.shared.admin.AdminReply;
39
import org.objectweb.joram.shared.admin.ClearSubscription;
40
import org.objectweb.joram.shared.admin.CreateUserReply;
41
import org.objectweb.joram.shared.admin.CreateUserRequest;
42
import org.objectweb.joram.shared.admin.DeleteSubscriptionMessage;
43
import org.objectweb.joram.shared.admin.DeleteUser;
44
import org.objectweb.joram.shared.admin.GetSubscription;
45
import org.objectweb.joram.shared.admin.GetSubscriptionMessage;
46
import org.objectweb.joram.shared.admin.GetSubscriptionMessageIds;
47
import org.objectweb.joram.shared.admin.GetSubscriptionMessageIdsRep;
48
import org.objectweb.joram.shared.admin.GetSubscriptionMessageRep;
49
import org.objectweb.joram.shared.admin.GetSubscriptionRep;
50
import org.objectweb.joram.shared.admin.GetSubscriptions;
51
import org.objectweb.joram.shared.admin.GetSubscriptionsRep;
52
import org.objectweb.joram.shared.admin.Monitor_GetDMQSettings;
53
import org.objectweb.joram.shared.admin.Monitor_GetDMQSettingsRep;
54
import org.objectweb.joram.shared.admin.Monitor_GetNbMaxMsg;
55
import org.objectweb.joram.shared.admin.Monitor_GetNbMaxMsgRep;
56
import org.objectweb.joram.shared.admin.SetNbMaxMsg;
57
import org.objectweb.joram.shared.admin.SetUserDMQ;
58
import org.objectweb.joram.shared.admin.SetUserThreshold;
59
import org.objectweb.joram.shared.admin.UpdateUser;
60
import org.objectweb.joram.shared.security.Identity;
1331 tachker 61
import org.objectweb.util.monolog.api.BasicLevel;
62
 
2663 tachker 63
import fr.dyade.aaa.util.management.MXWrapper;
64
 
861 fmaistre 65
/**
66
 * The <code>User</code> class is a utility class needed for administering
67
 * JORAM users.
68
 */
1669 afreyssin 69
public class User extends AdministeredObject implements UserMBean {
1977 tachker 70
  /**
71
   *
72
   */
73
  private static final long serialVersionUID = 1L;
861 fmaistre 74
  /** The name of the user. */
904 fmaistre 75
  String name;
861 fmaistre 76
  /** Identifier of the user's proxy agent. */
904 fmaistre 77
  String proxyId;
861 fmaistre 78
 
1254 afreyssin 79
  // Used by jndi2 SoapObjectHelper
80
  public User() {}
861 fmaistre 81
 
82
  /**
83
   * Constructs an <code>User</code> instance.
84
   *
85
   * @param name  The name of the user.
86
   * @param proxyId  Identifier of the user's proxy agent.
87
   */
1254 afreyssin 88
  public User(String name, String proxyId) {
861 fmaistre 89
    this.name = name;
90
    this.proxyId = proxyId;
91
  }
92
 
93
 
94
  /** Returns a string view of this <code>User</code> instance. */
1788 afreyssin 95
  public String toString() {
861 fmaistre 96
    return "User[" + name + "]:" + proxyId;
97
  }
98
 
883 fmaistre 99
 
100
  /** Returns the user name. */
1788 afreyssin 101
  public String getName() {
883 fmaistre 102
    return name;
103
  }
104
 
105
  /** Provides a reliable way to compare <code>User</code> instances. */
1788 afreyssin 106
  public boolean equals(Object o) {
883 fmaistre 107
    if (! (o instanceof User))
108
      return false;
109
 
110
    User other = (User) o;
111
 
112
    return other.proxyId ==proxyId;
113
  }
861 fmaistre 114
 
115
  /**
116
   * Admin method creating a user for a given server and instanciating the
117
   * corresponding <code>User</code> object.
118
   * <p>
119
   * If the user has already been set on this server, the method simply
120
   * returns the corresponding <code>User</code> object. Its fails if the
121
   * target server does not belong to the platform, or if a proxy could not
122
   * be deployed server side for a new user.
123
   *
124
   * @param name  Name of the user.
125
   * @param password  Password of the user.
126
   * @param serverId  The identifier of the user's server.
127
   *
128
   * @exception ConnectException  If the connection fails.
129
   * @exception AdminException  If the request fails.
130
   */
131
  public static User create(String name, String password, int serverId)
1331 tachker 132
    throws ConnectException, AdminException {
2663 tachker 133
    return create(name, password, serverId, Identity.SIMPLE_IDENTITY_CLASS);
861 fmaistre 134
  }
135
 
136
  /**
137
   * Admin method creating a user on the local server and instanciating the
138
   * corresponding <code>User</code> object.
139
   * <p>
140
   * If the user has already been set on this server, the method simply
141
   * returns the corresponding <code>User</code> object. It fails if a
142
   * proxy could not be deployed server side for a new user.
143
   *
144
   * @param name  Name of the user.
145
   * @param password  Password of the user.
146
   *
147
   * @exception ConnectException  If the connection fails.
148
   * @exception AdminException  If the request fails.
149
   */
150
  public static User create(String name, String password)
1788 afreyssin 151
         throws ConnectException, AdminException {
2663 tachker 152
    return create(name, password, AdminModule.getLocalServerId(), Identity.SIMPLE_IDENTITY_CLASS);
861 fmaistre 153
  }
154
 
155
  /**
2663 tachker 156
   * Admin method creating a user for a given server and instanciating the
157
   * corresponding <code>User</code> object.
158
   * <p>
159
   * If the user has already been set on this server, the method simply
160
   * returns the corresponding <code>User</code> object. Its fails if the
161
   * target server does not belong to the platform, or if a proxy could not
162
   * be deployed server side for a new user.
163
   *
164
   * @param name  Name of the user.
165
   * @param password  Password of the user.
166
   * @param serverId  The identifier of the user's server.
167
   * @param identityClassName user/password or JAAS... (delault SimpleIdentity).
168
   *
169
   * @exception ConnectException  If the connection fails.
170
   * @exception AdminException  If the request fails.
171
   */
172
  public static User create(String name, String password, int serverId, String identityClassName)
173
    throws ConnectException, AdminException {
174
    Identity identity = createIdentity(name, password, identityClassName);
175
    AdminReply reply = AdminModule.doRequest(
176
      new CreateUserRequest(identity, serverId));
177
    User user = new User(name, ((CreateUserReply) reply).getProxId());
178
    try {
179
      MXWrapper.registerMBean(user,
180
                              "joramClient",
181
                              "type=User,name="+ name + "[" + user.getProxyId() + "]");
182
    } catch (Exception e) {
183
      JoramTracing.dbgClient.log(BasicLevel.WARN, "registerMBean",e);
184
    }
185
    return user;
186
  }
187
 
188
  /**
189
   * Create a user Identity.
190
   *
191
   * @param user              Name of the user.
192
   * @param passwd            Password of the user.
193
   * @param identityClassName identity class name (simple, jaas).
194
   * @return identity user Identity.
195
   * @throws AdminException
196
   */
197
  private static Identity createIdentity(String user, String passwd, String identityClassName) throws AdminException {
198
    Identity identity = null;
199
    try {
200
      Class clazz = Class.forName(identityClassName);
201
      identity = (Identity) clazz.newInstance();
202
      if (passwd != null)
203
        identity.setIdentity(user, passwd);
204
      else
205
        identity.setUserName(user);
206
    } catch (Exception e) {
207
      throw new AdminException(e.getMessage());
208
    }
209
    return identity;
210
  }
211
 
212
  /**
861 fmaistre 213
   * Admin method updating this user identification.
214
   * <p>
215
   * The request fails if the user does not exist server side, or if the new
216
   * identification is already taken by a user on the same server.
217
   *
218
   * @param newName  The new name of the user.
219
   * @param newPassword  The new password of the user.
220
   *
221
   * @exception ConnectException  If the connection fails.
222
   * @exception AdminException  If the request fails.
223
   */
224
  public void update(String newName, String newPassword)
1331 tachker 225
    throws ConnectException, AdminException {
2663 tachker 226
    update(newName, newPassword, Identity.SIMPLE_IDENTITY_CLASS);
227
  }
228
 
229
  /**
230
   * Admin method updating this user identification.
231
   * <p>
232
   * The request fails if the user does not exist server side, or if the new
233
   * identification is already taken by a user on the same server.
234
   *
235
   * @param newName  The new name of the user.
236
   * @param newPassword  The new password of the user.
237
   * @param identityClassName user/password or JAAS... (delault SimpleIdentity).
238
   *
239
   * @exception ConnectException  If the connection fails.
240
   * @exception AdminException  If the request fails.
241
   */
242
  public void update(String newName, String newPassword, String identityClassName)
243
    throws ConnectException, AdminException {
244
    Identity newIdentity = createIdentity(newName, newPassword, identityClassName);
245
    AdminModule.doRequest(new UpdateUser(name, proxyId, newIdentity));
861 fmaistre 246
    name = newName;
247
  }
248
 
249
  /**
250
   * Removes this user.
251
   *
252
   * @exception ConnectException  If the connection fails.
253
   * @exception AdminException  Never thrown.
254
   */
1331 tachker 255
  public void delete() throws ConnectException, AdminException {
861 fmaistre 256
    AdminModule.doRequest(new DeleteUser(name, proxyId));
1331 tachker 257
    try {
258
      MXWrapper.unregisterMBean("joramClient",
259
                                "type=User,name="+name+
260
                                "["+proxyId+"]");
261
    } catch (Exception e) {
262
      if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
263
        JoramTracing.dbgClient.log(BasicLevel.DEBUG,
264
                                   "unregisterMBean",e);
265
    }
861 fmaistre 266
  }
267
 
268
  /**
269
   * Admin method setting a given dead message queue for this user.
270
   * <p>
271
   * The request fails if the user is deleted server side.
272
   *
273
   * @param dmq  The dead message queue to be set.
274
   *
275
   * @exception ConnectException  If the connection fails.
276
   * @exception AdminException  If the request fails.
277
   */
1788 afreyssin 278
  public void setDMQ(DeadMQueue dmq) throws ConnectException, AdminException {
1873 tachker 279
    setDMQId(dmq.getName());
861 fmaistre 280
  }
281
 
282
  /**
1873 tachker 283
   * Admin method setting a given dead message queue for this user.
284
   * <p>
285
   * The request fails if the user is deleted server side.
286
   *
287
   * @param dmqId  The dead message queue Id to be set.
288
   *
289
   * @exception ConnectException  If the connection fails.
290
   * @exception AdminException  If the request fails.
291
   */
292
  public void setDMQId(String dmqId) throws ConnectException, AdminException {
293
    AdminModule.doRequest(new SetUserDMQ(proxyId, dmqId));
294
  }
295
 
296
  /**
861 fmaistre 297
   * Admin method setting a given value as the threshold for this user.
298
   * <p>
299
   * The request fails if the user is deleted server side.
300
   *
301
   * @param threshold  The threshold value to be set.
302
   *
303
   * @exception ConnectException  If the connection fails.
304
   * @exception AdminException  If the request fails.
305
   */
1788 afreyssin 306
  public void setThreshold(int thresh) throws ConnectException, AdminException {
861 fmaistre 307
    AdminModule.doRequest(new SetUserThreshold(proxyId, thresh));
308
  }
309
 
310
  /**
311
   * Returns the dead message queue for this user, null if not set.
312
   * <p>
313
   * The request fails if the user is deleted server side.
314
   *
315
   * @exception ConnectException  If the connection fails.
316
   * @exception AdminException  If the request fails.
317
   */
1788 afreyssin 318
  public DeadMQueue getDMQ() throws ConnectException, AdminException {
861 fmaistre 319
    Monitor_GetDMQSettings request;
320
    request = new Monitor_GetDMQSettings(proxyId);
321
    Monitor_GetDMQSettingsRep reply;
322
    reply = (Monitor_GetDMQSettingsRep) AdminModule.doRequest(request);
323
 
324
    if (reply.getDMQName() == null)
325
      return null;
326
    else
327
      return new DeadMQueue(reply.getDMQName());
328
  }
329
 
330
  /**
1873 tachker 331
   * Returns the dead message queue Id for this user, null if not set.
332
   * <p>
333
   * The request fails if the user is deleted server side.
334
   *
335
   * @exception ConnectException  If the connection fails.
336
   * @exception AdminException  If the request fails.
337
   */
338
  public String getDMQId() throws ConnectException, AdminException {
339
    DeadMQueue dmq = getDMQ();
340
    if (dmq == null)
341
      return null;
342
    else
343
      return dmq.getName();
344
  }
345
 
346
  /**
861 fmaistre 347
   * Returns the threshold for this user, -1 if not set.
348
   * <p>
349
   * The request fails if the user is deleted server side.
350
   *
351
   * @exception ConnectException  If the connection fails.
352
   * @exception AdminException  If the request fails.
353
   */
1788 afreyssin 354
  public int getThreshold() throws ConnectException, AdminException {
861 fmaistre 355
    Monitor_GetDMQSettings request;
356
    request = new Monitor_GetDMQSettings(proxyId);
357
    Monitor_GetDMQSettingsRep reply;
358
    reply = (Monitor_GetDMQSettingsRep) AdminModule.doRequest(request);
359
 
360
    if (reply.getThreshold() == null)
361
      return -1;
362
    else
363
      return reply.getThreshold().intValue();
364
  }
365
 
1254 afreyssin 366
  /**
1331 tachker 367
   * Admin method setting nbMaxMsg for this subscription.
368
   * <p>
369
   * The request fails if the sub is deleted server side.
370
   *
371
   * @param subName the name of the subscription.
372
   * @param nbMaxMsg  nb Max of Message (-1 no limit).
373
   *
374
   * @exception ConnectException  If the admin connection is closed or broken.
375
   * @exception AdminException  If the request fails.
376
   */
377
  public void setNbMaxMsg(String subName, int nbMaxMsg)
378
    throws ConnectException, AdminException {
2587 afreyssin 379
//  TODO: Subscription sub = getSubscription(subName);
1331 tachker 380
    AdminModule.doRequest(new SetNbMaxMsg(proxyId, nbMaxMsg, subName));
381
  }
382
 
383
  /**
384
   * Monitoring method returning the nbMaxMsg of this subscription, -1 if no limit.
385
   * <p>
386
   * The request fails if the sub is deleted server side.
387
   *
388
   * @param subName the name of the subscription.
389
   *
390
   * @exception ConnectException  If the admin connection is closed or broken.
391
   * @exception AdminException  If the request fails.
392
   */
393
  public int getNbMaxMsg(String subName)
394
    throws ConnectException, AdminException {
2587 afreyssin 395
//  TODO: Subscription sub = getSubscription(subName);
1331 tachker 396
    Monitor_GetNbMaxMsg request = new Monitor_GetNbMaxMsg(proxyId, subName);
397
    Monitor_GetNbMaxMsgRep reply;
398
    reply = (Monitor_GetNbMaxMsgRep) AdminModule.doRequest(request);
399
    return reply.getNbMaxMsg();
400
  }
401
 
402
  /**
1254 afreyssin 403
   * Returns the subscriptions owned by a user.
404
   *
405
   * @param serverId the identifier of the server where the user has been
406
   *        created.
407
   *
408
   * @param userName name of the user.
409
   *
410
   * @exception AdminException If an error is raised by the
411
   *                           administration operation.
412
   *
413
   * @exception ConnectException  If the admin connection is not established.
414
   */
415
  public Subscription[] getSubscriptions()
416
    throws AdminException, ConnectException {
417
    GetSubscriptionsRep reply =
418
      (GetSubscriptionsRep)AdminModule.doRequest(
419
        new GetSubscriptions(proxyId));
420
    String[] subNames = reply.getSubNames();
421
    String[] topicIds = reply.getTopicIds();
422
    int[] messageCounts = reply.getMessageCounts();
423
    boolean[] durable = reply.getDurable();
424
    Subscription[] res = new Subscription[subNames.length];
425
    for (int i = 0; i < res.length; i++) {
426
      res[i] = new Subscription(subNames[i],
427
                                topicIds[i],
428
                                messageCounts[i],
429
                                durable[i]);
430
    }
431
    return res;
432
  }
433
 
1331 tachker 434
  /** used by MBean jmx */
435
  public List getSubscriptionList()
436
    throws AdminException, ConnectException {
437
    Vector list = new Vector();
438
    Subscription[] sub = getSubscriptions();
439
    for (int i = 0; i < sub.length; i++) {
440
      list.add(sub[i].toString());
441
    }
442
    return list;
443
  }
444
 
1254 afreyssin 445
  /**
446
   * Returns a subscription.
447
   *
448
   * @param serverId the identifier of the server where the user
449
   * owner of the subscription has been created.
450
   *
451
   * @param userName name of the user that owns the subscription.
452
   *
453
   * @param subName the name of the subscription.
454
   *
455
   * @exception AdminException If an error is raised by the
456
   *                           administration operation.
457
   *
458
   * @exception ConnectException  If the admin connection is not established.
459
   */
2587 afreyssin 460
  public Subscription getSubscription(String subName) throws AdminException, ConnectException {
461
    GetSubscriptionRep reply = (GetSubscriptionRep) AdminModule.doRequest(new GetSubscription(proxyId, subName));
462
    return new Subscription(subName,
463
                            reply.getTopicId(),
464
                            reply.getMessageCount(),
465
                            reply.getDurable());
1254 afreyssin 466
  }
467
 
2587 afreyssin 468
  public String getSubscriptionString(String subName) throws AdminException, ConnectException {
469
    return getSubscription(subName).toString();
1331 tachker 470
  }
471
 
2587 afreyssin 472
  public String[] getMessageIds(String subName) throws AdminException, ConnectException {
473
    GetSubscriptionMessageIdsRep reply = (GetSubscriptionMessageIdsRep) AdminModule.doRequest(new GetSubscriptionMessageIds(proxyId, subName));
1254 afreyssin 474
    return reply.getMessageIds();
475
  }
476
 
1669 afreyssin 477
  public Message readMessage(String subName,
478
                             String msgId) throws AdminException, ConnectException, JMSException {
2587 afreyssin 479
    GetSubscriptionMessageRep reply = (GetSubscriptionMessageRep) AdminModule.doRequest(new GetSubscriptionMessage(proxyId,
480
                                                                                                                   subName,
481
                                                                                                                   msgId));
1254 afreyssin 482
    return Message.wrapMomMessage(null, reply.getMessage());
483
  }
484
 
485
  public void deleteMessage(
486
    String subName,
487
    String msgId)
488
    throws AdminException, ConnectException {
489
    AdminModule.doRequest(
490
      new DeleteSubscriptionMessage(proxyId,
491
                                    subName,
492
                                    msgId));
493
  }
494
 
495
  public void clearSubscription(String subName)
496
    throws AdminException, ConnectException {
497
    AdminModule.doRequest(
498
      new ClearSubscription(proxyId,
499
                            subName));
500
  }
501
 
861 fmaistre 502
 
503
  /** Returns the identifier of the user's proxy. */
1788 afreyssin 504
  public String getProxyId() {
861 fmaistre 505
    return proxyId;
506
  }
507
 
1788 afreyssin 508
  /** Sets the naming reference of a connection factory. */
509
  public void toReference(Reference ref) throws NamingException {
861 fmaistre 510
    ref.add(new StringRefAddr("user.name", name));
511
    ref.add(new StringRefAddr("user.id", proxyId));
512
  }
513
 
1788 afreyssin 514
  /** Restores the administered object from a naming reference. */
515
  public void fromReference(Reference ref) throws NamingException {
516
    name = (String) ref.get("user.name").getContent();
517
    proxyId = (String) ref.get("user.id").getContent();
518
  }
519
 
861 fmaistre 520
  /**
521
   * Codes an <code>User</code> instance as a Hashtable for travelling
522
   * through the SOAP protocol.
523
   */
524
  public Hashtable code() {
1254 afreyssin 525
    Hashtable h = new Hashtable();
861 fmaistre 526
    h.put("name",name);
527
    h.put("proxyId",proxyId);
528
    return h;
529
  }
530
 
531
  /**
532
   * Decodes an <code>User</code> which travelled through the SOAP protocol.
533
   */
1254 afreyssin 534
  public void decode(Hashtable h) {
535
    name = (String) h.get("name");
536
    proxyId = (String) h.get("proxyId");
861 fmaistre 537
  }
538
}