OW2 Consortium joram

Rev

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