OW2 Consortium joram

Rev

Rev 904 | Rev 1331 | 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
3
 * Copyright (C) 2004 - Bull SA
4
 * Copyright (C) 2004 - ScalAgent Distributed Technologies
5
 * Copyright (C) 1996 - Dyade
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
20
 * USA.
21
 *
22
 * Initial developer(s): Frederic Maistre (INRIA)
23
 * Contributor(s): Nicolas Tachker (ScalAgent DT)
24
 */
25
package org.objectweb.joram.client.jms.admin;
26
 
27
import java.util.Vector;
28
import java.util.Hashtable;
29
import java.net.ConnectException;
30
 
31
import javax.naming.*;
1254 afreyssin 32
import javax.jms.JMSException;
861 fmaistre 33
 
1254 afreyssin 34
import org.objectweb.joram.client.jms.Message;
861 fmaistre 35
import org.objectweb.joram.shared.admin.*;
36
 
37
/**
38
 * The <code>User</code> class is a utility class needed for administering
39
 * JORAM users.
40
 */
41
public class User extends AdministeredObject
42
{
43
  /** The name of the user. */
904 fmaistre 44
  String name;
861 fmaistre 45
  /** Identifier of the user's proxy agent. */
904 fmaistre 46
  String proxyId;
861 fmaistre 47
 
1254 afreyssin 48
  // Used by jndi2 SoapObjectHelper
49
  public User() {}
861 fmaistre 50
 
51
  /**
52
   * Constructs an <code>User</code> instance.
53
   *
54
   * @param name  The name of the user.
55
   * @param proxyId  Identifier of the user's proxy agent.
56
   */
1254 afreyssin 57
  public User(String name, String proxyId) {
861 fmaistre 58
    this.name = name;
59
    this.proxyId = proxyId;
60
  }
61
 
62
 
63
  /** Returns a string view of this <code>User</code> instance. */
64
  public String toString()
65
  {
66
    return "User[" + name + "]:" + proxyId;
67
  }
68
 
883 fmaistre 69
 
70
  /** Returns the user name. */
71
  public String getName()
72
  {
73
    return name;
74
  }
75
 
76
  /** Provides a reliable way to compare <code>User</code> instances. */
77
  public boolean equals(Object o)
78
  {
79
    if (! (o instanceof User))
80
      return false;
81
 
82
    User other = (User) o;
83
 
84
    return other.proxyId ==proxyId;
85
  }
861 fmaistre 86
 
87
  /**
88
   * Admin method creating a user for a given server and instanciating the
89
   * corresponding <code>User</code> object.
90
   * <p>
91
   * If the user has already been set on this server, the method simply
92
   * returns the corresponding <code>User</code> object. Its fails if the
93
   * target server does not belong to the platform, or if a proxy could not
94
   * be deployed server side for a new user.
95
   *
96
   * @param name  Name of the user.
97
   * @param password  Password of the user.
98
   * @param serverId  The identifier of the user's server.
99
   *
100
   * @exception ConnectException  If the connection fails.
101
   * @exception AdminException  If the request fails.
102
   */
103
  public static User create(String name, String password, int serverId)
104
         throws ConnectException, AdminException
105
  {
106
    AdminReply reply = AdminModule.doRequest(
107
      new CreateUserRequest(name, password, serverId));
108
    return new User(name, ((CreateUserReply) reply).getProxId());
109
  }
110
 
111
  /**
112
   * Admin method creating a user on the local server and instanciating the
113
   * corresponding <code>User</code> object.
114
   * <p>
115
   * If the user has already been set on this server, the method simply
116
   * returns the corresponding <code>User</code> object. It fails if a
117
   * proxy could not be deployed server side for a new user.
118
   *
119
   * @param name  Name of the user.
120
   * @param password  Password of the user.
121
   *
122
   * @exception ConnectException  If the connection fails.
123
   * @exception AdminException  If the request fails.
124
   */
125
  public static User create(String name, String password)
126
         throws ConnectException, AdminException
127
  {
1254 afreyssin 128
    return create(name, password, AdminModule.getLocalServerId());
861 fmaistre 129
  }
130
 
131
  /**
132
   * Admin method updating this user identification.
133
   * <p>
134
   * The request fails if the user does not exist server side, or if the new
135
   * identification is already taken by a user on the same server.
136
   *
137
   * @param newName  The new name of the user.
138
   * @param newPassword  The new password of the user.
139
   *
140
   * @exception ConnectException  If the connection fails.
141
   * @exception AdminException  If the request fails.
142
   */
143
  public void update(String newName, String newPassword)
144
         throws ConnectException, AdminException
145
  {
146
    AdminModule.doRequest(new UpdateUser(name, proxyId, newName, newPassword));
147
    name = newName;
148
  }
149
 
150
  /**
151
   * Removes this user.
152
   *
153
   * @exception ConnectException  If the connection fails.
154
   * @exception AdminException  Never thrown.
155
   */
156
  public void delete() throws ConnectException, AdminException
157
  {
158
    AdminModule.doRequest(new DeleteUser(name, proxyId));
159
  }
160
 
161
  /**
162
   * Admin method setting a given dead message queue for this user.
163
   * <p>
164
   * The request fails if the user is deleted server side.
165
   *
166
   * @param dmq  The dead message queue to be set.
167
   *
168
   * @exception ConnectException  If the connection fails.
169
   * @exception AdminException  If the request fails.
170
   */
171
  public void setDMQ(DeadMQueue dmq) throws ConnectException, AdminException
172
  {
173
    AdminModule.doRequest(new SetUserDMQ(proxyId, dmq.getName()));
174
  }
175
 
176
  /**
177
   * Admin method setting a given value as the threshold for this user.
178
   * <p>
179
   * The request fails if the user is deleted server side.
180
   *
181
   * @param threshold  The threshold value to be set.
182
   *
183
   * @exception ConnectException  If the connection fails.
184
   * @exception AdminException  If the request fails.
185
   */
186
  public void setThreshold(int thresh) throws ConnectException, AdminException
187
  {
188
    AdminModule.doRequest(new SetUserThreshold(proxyId, thresh));
189
  }
190
 
191
  /**
192
   * Returns the dead message queue for this user, null if not set.
193
   * <p>
194
   * The request fails if the user is deleted server side.
195
   *
196
   * @exception ConnectException  If the connection fails.
197
   * @exception AdminException  If the request fails.
198
   */
199
  public DeadMQueue getDMQ() throws ConnectException, AdminException
200
  {
201
    Monitor_GetDMQSettings request;
202
    request = new Monitor_GetDMQSettings(proxyId);
203
    Monitor_GetDMQSettingsRep reply;
204
    reply = (Monitor_GetDMQSettingsRep) AdminModule.doRequest(request);
205
 
206
    if (reply.getDMQName() == null)
207
      return null;
208
    else
209
      return new DeadMQueue(reply.getDMQName());
210
  }
211
 
212
  /**
213
   * Returns the threshold for this user, -1 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 int getThreshold() 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.getThreshold() == null)
228
      return -1;
229
    else
230
      return reply.getThreshold().intValue();
231
  }
232
 
1254 afreyssin 233
  /**
234
   * Returns the subscriptions owned by a user.
235
   *
236
   * @param serverId the identifier of the server where the user has been
237
   *        created.
238
   *
239
   * @param userName name of the user.
240
   *
241
   * @exception AdminException If an error is raised by the
242
   *                           administration operation.
243
   *
244
   * @exception ConnectException  If the admin connection is not established.
245
   */
246
  public Subscription[] getSubscriptions()
247
    throws AdminException, ConnectException {
248
    GetSubscriptionsRep reply =
249
      (GetSubscriptionsRep)AdminModule.doRequest(
250
        new GetSubscriptions(proxyId));
251
    String[] subNames = reply.getSubNames();
252
    String[] topicIds = reply.getTopicIds();
253
    int[] messageCounts = reply.getMessageCounts();
254
    boolean[] durable = reply.getDurable();
255
    Subscription[] res = new Subscription[subNames.length];
256
    for (int i = 0; i < res.length; i++) {
257
      res[i] = new Subscription(subNames[i],
258
                                topicIds[i],
259
                                messageCounts[i],
260
                                durable[i]);
261
    }
262
    return res;
263
  }
264
 
265
  /**
266
   * Returns a subscription.
267
   *
268
   * @param serverId the identifier of the server where the user
269
   * owner of the subscription has been created.
270
   *
271
   * @param userName name of the user that owns the subscription.
272
   *
273
   * @param subName the name of the subscription.
274
   *
275
   * @exception AdminException If an error is raised by the
276
   *                           administration operation.
277
   *
278
   * @exception ConnectException  If the admin connection is not established.
279
   */
280
  public Subscription getSubscription(String subName)
281
    throws AdminException, ConnectException {
282
    GetSubscriptionRep reply =
283
      (GetSubscriptionRep)AdminModule.doRequest(
284
        new GetSubscription(proxyId, subName));
285
    return new Subscription(
286
      subName,
287
      reply.getTopicId(),
288
      reply.getMessageCount(),
289
      reply.getDurable());
290
  }
291
 
292
  public String[] getMessageIds(String subName)
293
    throws AdminException, ConnectException {
294
    GetSubscriptionMessageIdsRep reply =
295
      (GetSubscriptionMessageIdsRep)AdminModule.doRequest(
296
        new GetSubscriptionMessageIds(proxyId, subName));
297
    return reply.getMessageIds();
298
  }
299
 
300
  public Message readMessage(
301
    String subName,
302
    String msgId) throws AdminException, ConnectException, JMSException {
303
    GetSubscriptionMessageRep reply =
304
      (GetSubscriptionMessageRep)AdminModule.doRequest(
305
        new GetSubscriptionMessage(proxyId,
306
                                   subName,
307
                                   msgId));
308
    return Message.wrapMomMessage(null, reply.getMessage());
309
  }
310
 
311
  public void deleteMessage(
312
    String subName,
313
    String msgId)
314
    throws AdminException, ConnectException {
315
    AdminModule.doRequest(
316
      new DeleteSubscriptionMessage(proxyId,
317
                                    subName,
318
                                    msgId));
319
  }
320
 
321
  public void clearSubscription(String subName)
322
    throws AdminException, ConnectException {
323
    AdminModule.doRequest(
324
      new ClearSubscription(proxyId,
325
                            subName));
326
  }
327
 
861 fmaistre 328
 
329
  /** Returns the identifier of the user's proxy. */
330
  public String getProxyId()
331
  {
332
    return proxyId;
333
  }
334
 
335
  /** Sets the naming reference of this user. */
336
  public Reference getReference() throws NamingException
337
  {
338
    Reference ref = super.getReference();
339
    ref.add(new StringRefAddr("user.name", name));
340
    ref.add(new StringRefAddr("user.id", proxyId));
341
    return ref;
342
  }
343
 
344
  /**
345
   * Codes an <code>User</code> instance as a Hashtable for travelling
346
   * through the SOAP protocol.
347
   */
348
  public Hashtable code() {
1254 afreyssin 349
    Hashtable h = new Hashtable();
861 fmaistre 350
    h.put("name",name);
351
    h.put("proxyId",proxyId);
352
    return h;
353
  }
354
 
355
  /**
356
   * Decodes an <code>User</code> which travelled through the SOAP protocol.
357
   */
1254 afreyssin 358
  public void decode(Hashtable h) {
359
    name = (String) h.get("name");
360
    proxyId = (String) h.get("proxyId");
861 fmaistre 361
  }
362
}