OW2 Consortium joram

Rev

Rev 883 | Go to most recent revision | Details | 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.*;
32
 
33
import org.objectweb.joram.shared.admin.*;
34
 
35
/**
36
 * The <code>User</code> class is a utility class needed for administering
37
 * JORAM users.
38
 */
39
public class User extends AdministeredObject
40
{
41
  /** The name of the user. */
42
  String name;
43
  /** Identifier of the user's proxy agent. */
44
  String proxyId;
45
 
46
 
47
  /**
48
   * Constructs an <code>User</code> instance.
49
   *
50
   * @param name  The name of the user.
51
   * @param proxyId  Identifier of the user's proxy agent.
52
   */
53
  public User(String name, String proxyId)
54
  {
55
    super(proxyId);
56
    this.name = name;
57
    this.proxyId = proxyId;
58
  }
59
 
60
 
61
  /** Returns a string view of this <code>User</code> instance. */
62
  public String toString()
63
  {
64
    return "User[" + name + "]:" + proxyId;
65
  }
66
 
67
 
68
  /**
69
   * Admin method creating a user for a given server and instanciating the
70
   * corresponding <code>User</code> object.
71
   * <p>
72
   * If the user has already been set on this server, the method simply
73
   * returns the corresponding <code>User</code> object. Its fails if the
74
   * target server does not belong to the platform, or if a proxy could not
75
   * be deployed server side for a new user.
76
   *
77
   * @param name  Name of the user.
78
   * @param password  Password of the user.
79
   * @param serverId  The identifier of the user's server.
80
   *
81
   * @exception ConnectException  If the connection fails.
82
   * @exception AdminException  If the request fails.
83
   */
84
  public static User create(String name, String password, int serverId)
85
         throws ConnectException, AdminException
86
  {
87
    AdminReply reply = AdminModule.doRequest(
88
      new CreateUserRequest(name, password, serverId));
89
    return new User(name, ((CreateUserReply) reply).getProxId());
90
  }
91
 
92
  /**
93
   * Admin method creating a user on the local server and instanciating the
94
   * corresponding <code>User</code> object.
95
   * <p>
96
   * If the user has already been set on this server, the method simply
97
   * returns the corresponding <code>User</code> object. It fails if a
98
   * proxy could not be deployed server side for a new user.
99
   *
100
   * @param name  Name of the user.
101
   * @param password  Password of the user.
102
   *
103
   * @exception ConnectException  If the connection fails.
104
   * @exception AdminException  If the request fails.
105
   */
106
  public static User create(String name, String password)
107
         throws ConnectException, AdminException
108
  {
109
    return create(name, password, AdminModule.getLocalServer());
110
  }
111
 
112
  /**
113
   * Admin method updating this user identification.
114
   * <p>
115
   * The request fails if the user does not exist server side, or if the new
116
   * identification is already taken by a user on the same server.
117
   *
118
   * @param newName  The new name of the user.
119
   * @param newPassword  The new password of the user.
120
   *
121
   * @exception ConnectException  If the connection fails.
122
   * @exception AdminException  If the request fails.
123
   */
124
  public void update(String newName, String newPassword)
125
         throws ConnectException, AdminException
126
  {
127
    AdminModule.doRequest(new UpdateUser(name, proxyId, newName, newPassword));
128
    name = newName;
129
  }
130
 
131
  /**
132
   * Removes this user.
133
   *
134
   * @exception ConnectException  If the connection fails.
135
   * @exception AdminException  Never thrown.
136
   */
137
  public void delete() throws ConnectException, AdminException
138
  {
139
    AdminModule.doRequest(new DeleteUser(name, proxyId));
140
  }
141
 
142
  /**
143
   * Admin method setting a given dead message queue for this user.
144
   * <p>
145
   * The request fails if the user is deleted server side.
146
   *
147
   * @param dmq  The dead message queue to be set.
148
   *
149
   * @exception ConnectException  If the connection fails.
150
   * @exception AdminException  If the request fails.
151
   */
152
  public void setDMQ(DeadMQueue dmq) throws ConnectException, AdminException
153
  {
154
    AdminModule.doRequest(new SetUserDMQ(proxyId, dmq.getName()));
155
  }
156
 
157
  /**
158
   * Admin method setting a given value as the threshold for this user.
159
   * <p>
160
   * The request fails if the user is deleted server side.
161
   *
162
   * @param threshold  The threshold value to be set.
163
   *
164
   * @exception ConnectException  If the connection fails.
165
   * @exception AdminException  If the request fails.
166
   */
167
  public void setThreshold(int thresh) throws ConnectException, AdminException
168
  {
169
    AdminModule.doRequest(new SetUserThreshold(proxyId, thresh));
170
  }
171
 
172
  /**
173
   * Returns the dead message queue for this user, null if not set.
174
   * <p>
175
   * The request fails if the user is deleted server side.
176
   *
177
   * @exception ConnectException  If the connection fails.
178
   * @exception AdminException  If the request fails.
179
   */
180
  public DeadMQueue getDMQ() throws ConnectException, AdminException
181
  {
182
    Monitor_GetDMQSettings request;
183
    request = new Monitor_GetDMQSettings(proxyId);
184
    Monitor_GetDMQSettingsRep reply;
185
    reply = (Monitor_GetDMQSettingsRep) AdminModule.doRequest(request);
186
 
187
    if (reply.getDMQName() == null)
188
      return null;
189
    else
190
      return new DeadMQueue(reply.getDMQName());
191
  }
192
 
193
  /**
194
   * Returns the threshold for this user, -1 if not set.
195
   * <p>
196
   * The request fails if the user is deleted server side.
197
   *
198
   * @exception ConnectException  If the connection fails.
199
   * @exception AdminException  If the request fails.
200
   */
201
  public int getThreshold() throws ConnectException, AdminException
202
  {
203
    Monitor_GetDMQSettings request;
204
    request = new Monitor_GetDMQSettings(proxyId);
205
    Monitor_GetDMQSettingsRep reply;
206
    reply = (Monitor_GetDMQSettingsRep) AdminModule.doRequest(request);
207
 
208
    if (reply.getThreshold() == null)
209
      return -1;
210
    else
211
      return reply.getThreshold().intValue();
212
  }
213
 
214
 
215
  /** Returns the identifier of the user's proxy. */
216
  public String getProxyId()
217
  {
218
    return proxyId;
219
  }
220
 
221
  /** Sets the naming reference of this user. */
222
  public Reference getReference() throws NamingException
223
  {
224
    Reference ref = super.getReference();
225
    ref.add(new StringRefAddr("user.name", name));
226
    ref.add(new StringRefAddr("user.id", proxyId));
227
    return ref;
228
  }
229
 
230
  /**
231
   * Codes an <code>User</code> instance as a Hashtable for travelling
232
   * through the SOAP protocol.
233
   */
234
  public Hashtable code() {
235
    Hashtable h = super.code();
236
    h.put("name",name);
237
    h.put("proxyId",proxyId);
238
    return h;
239
  }
240
 
241
  /**
242
   * Decodes an <code>User</code> which travelled through the SOAP protocol.
243
   */
244
  public Object decode(Hashtable h) {
245
    return new User((String) h.get("name"), (String) h.get("proxyId"));
246
  }
247
}