OW2 Consortium joram

Rev

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