KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > admin > User


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - 2007 ScalAgent Distributed Technologies
4  * Copyright (C) 2004 Bull SA
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  *
21  * Initial developer(s): Frederic Maistre (Bull SA)
22  * Contributor(s): ScalAgent Distributed Technologies
23  */

24 package org.objectweb.joram.client.jms.admin;
25
26 import java.util.Vector JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.List JavaDoc;
29 import java.net.ConnectException JavaDoc;
30
31 import javax.naming.*;
32
33 import javax.jms.JMSException JavaDoc;
34
35 import fr.dyade.aaa.util.management.MXWrapper;
36 import org.objectweb.joram.client.jms.Message;
37 import org.objectweb.joram.shared.admin.*;
38
39 import org.objectweb.joram.shared.JoramTracing;
40 import org.objectweb.util.monolog.api.BasicLevel;
41
42 /**
43  * The <code>User</code> class is a utility class needed for administering
44  * JORAM users.
45  */

46 public class User extends AdministeredObject implements UserMBean {
47   /** The name of the user. */
48   String JavaDoc name;
49   /** Identifier of the user's proxy agent. */
50   String JavaDoc proxyId;
51
52   // Used by jndi2 SoapObjectHelper
53
public User() {}
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    */

61   public User(String JavaDoc name, String JavaDoc proxyId) {
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 JavaDoc toString() {
69     return "User[" + name + "]:" + proxyId;
70   }
71
72
73   /** Returns the user name. */
74   public String JavaDoc getName() {
75     return name;
76   }
77
78   /** Provides a reliable way to compare <code>User</code> instances. */
79   public boolean equals(Object JavaDoc o) {
80     if (! (o instanceof User))
81       return false;
82
83     User other = (User) o;
84
85     return other.proxyId ==proxyId;
86   }
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 JavaDoc name, String JavaDoc password, int serverId)
105     throws ConnectException JavaDoc, AdminException {
106     AdminReply reply = AdminModule.doRequest(
107       new CreateUserRequest(name, password, serverId));
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 JavaDoc e) {
115       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
116         JoramTracing.dbgClient.log(BasicLevel.DEBUG,
117                                    "registerMBean",e);
118     }
119     return user;
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 JavaDoc name, String JavaDoc password)
137          throws ConnectException JavaDoc, AdminException {
138     return create(name, password, AdminModule.getLocalServerId());
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 JavaDoc newName, String JavaDoc newPassword)
154     throws ConnectException JavaDoc, AdminException {
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    */

165   public void delete() throws ConnectException JavaDoc, AdminException {
166     AdminModule.doRequest(new DeleteUser(name, proxyId));
167     try {
168       MXWrapper.unregisterMBean("joramClient",
169                                 "type=User,name="+name+
170                                 "["+proxyId+"]");
171     } catch (Exception JavaDoc e) {
172       if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
173         JoramTracing.dbgClient.log(BasicLevel.DEBUG,
174                                    "unregisterMBean",e);
175     }
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    */

188   public void setDMQ(DeadMQueue dmq) throws ConnectException JavaDoc, AdminException {
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    */

202   public void setThreshold(int thresh) throws ConnectException JavaDoc, AdminException {
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    */

214   public DeadMQueue getDMQ() throws ConnectException JavaDoc, AdminException {
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    */

234   public int getThreshold() throws ConnectException JavaDoc, AdminException {
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
246   /**
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 JavaDoc subName, int nbMaxMsg)
258     throws ConnectException JavaDoc, 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 JavaDoc subName)
274     throws ConnectException JavaDoc, 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   /**
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 JavaDoc {
297     GetSubscriptionsRep reply =
298       (GetSubscriptionsRep)AdminModule.doRequest(
299         new GetSubscriptions(proxyId));
300     String JavaDoc[] subNames = reply.getSubNames();
301     String JavaDoc[] 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
314   /** used by MBean jmx */
315   public List JavaDoc getSubscriptionList()
316     throws AdminException, ConnectException JavaDoc {
317     Vector JavaDoc list = new Vector JavaDoc();
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
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 JavaDoc subName)
341     throws AdminException, ConnectException JavaDoc {
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
352   public String JavaDoc getSubscriptionString(String JavaDoc subName)
353     throws AdminException, ConnectException JavaDoc {
354     Subscription sub = getSubscription(subName);
355     return sub.toString();
356   }
357
358   public String JavaDoc[] getMessageIds(String JavaDoc subName)
359     throws AdminException, ConnectException JavaDoc {
360     GetSubscriptionMessageIdsRep reply =
361       (GetSubscriptionMessageIdsRep)AdminModule.doRequest(
362         new GetSubscriptionMessageIds(proxyId, subName));
363     return reply.getMessageIds();
364   }
365
366   public Message readMessage(String JavaDoc subName,
367                              String JavaDoc msgId) throws AdminException, ConnectException JavaDoc, JMSException JavaDoc {
368     GetSubscriptionMessageRep reply =
369       (GetSubscriptionMessageRep) AdminModule.doRequest(
370         new GetSubscriptionMessage(proxyId,
371                                    subName,
372                                    msgId));
373     return Message.wrapMomMessage(null, reply.getMessage());
374   }
375
376   public void deleteMessage(
377     String JavaDoc subName,
378     String JavaDoc msgId)
379     throws AdminException, ConnectException JavaDoc {
380     AdminModule.doRequest(
381       new DeleteSubscriptionMessage(proxyId,
382                                     subName,
383                                     msgId));
384   }
385
386   public void clearSubscription(String JavaDoc subName)
387     throws AdminException, ConnectException JavaDoc {
388     AdminModule.doRequest(
389       new ClearSubscription(proxyId,
390                             subName));
391   }
392
393    
394   /** Returns the identifier of the user's proxy. */
395   public String JavaDoc getProxyId() {
396     return proxyId;
397   }
398
399   /** Sets the naming reference of a connection factory. */
400   public void toReference(Reference ref) throws NamingException {
401     ref.add(new StringRefAddr("user.name", name));
402     ref.add(new StringRefAddr("user.id", proxyId));
403   }
404
405   /** Restores the administered object from a naming reference. */
406   public void fromReference(Reference ref) throws NamingException {
407     name = (String JavaDoc) ref.get("user.name").getContent();
408     proxyId = (String JavaDoc) ref.get("user.id").getContent();
409   }
410
411   /**
412    * Codes an <code>User</code> instance as a Hashtable for travelling
413    * through the SOAP protocol.
414    */

415   public Hashtable JavaDoc code() {
416     Hashtable JavaDoc h = new Hashtable JavaDoc();
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    */

425   public void decode(Hashtable JavaDoc h) {
426     name = (String JavaDoc) h.get("name");
427     proxyId = (String JavaDoc) h.get("proxyId");
428   }
429 }
430
Popular Tags