View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2000-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: IpcJmsAdminConnection.java,v 1.10 2003/08/17 01:32:26 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * $Date       jimm    Created
47   */
48  
49  package org.exolab.jms.server.mipc;
50  
51  import java.io.Serializable;
52  import java.util.Vector;
53  
54  import javax.jms.JMSException;
55  import javax.jms.JMSSecurityException;
56  
57  import org.exolab.core.ipc.NotifierIfc;
58  import org.exolab.jms.server.AdminConnection;
59  import org.exolab.jms.server.AdminConnectionManager;
60  
61  
62  /***
63   * This is the server side receiver for admin requests.
64   *
65   * @version     $Revision: 1.10 $ $Date: 2003/08/17 01:32:26 $
66   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
67   * @see         org.exolab.jms.administration.mipc.IpcJmsAdminConnection
68   */
69  public class IpcJmsAdminConnection implements NotifierIfc {
70  
71      /***
72       * Default constructor.
73       *
74       */
75      public IpcJmsAdminConnection() {
76      }
77  
78  
79      /***
80       * A new request has been received.
81       * Carry out the request, and pass back any relevent data.
82       *
83       * @param ob The data received,
84       * @param id The id of the calling connection, not used by the client.
85       * @return Object Return any requested result. This must never be null.
86       *
87       */
88      public Serializable notify(Object ob, String id) {
89          Vector v = (Vector) ob;
90          String func = (String) v.get(1);
91          Serializable result = null;
92          String identifierId = (String) v.get(2);
93  
94          if (func.equals("createConnection")) {
95              result = createConnection(id, (String) v.get(3), (String) v.get(4));
96          } else if (func.equals("addDurableConsumer")) {
97              result = addDurableConsumer((String) v.get(3), (String) v.get(4),
98                  (String) v.get(2));
99          } else if (func.equals("removeDurableConsumer")) {
100             result = removeDurableConsumer((String) v.get(3),
101                 (String) v.get(2));
102         } else if (func.equals("durableConsumerExists")) {
103             result = durableConsumerExists((String) v.get(3),
104                 (String) v.get(2));
105         } else if (func.equals("getDurableConsumers")) {
106             result = getDurableConsumers((String) v.get(3), (String) v.get(2));
107         } else if (func.equals("unregisterConsumer")) {
108             result = unregisterConsumer((String) v.get(3), (String) v.get(2));
109         } else if (func.equals("isConnected")) {
110             result = isConnected((String) v.get(3), (String) v.get(2));
111         } else if (func.equals("getAllDestinations")) {
112             result = getAllDestinations((String) v.get(2));
113         } else if (func.equals("addDestination")) {
114             result = addDestination((String) v.get(3), (Boolean) v.get(4),
115                 (String) v.get(2));
116         } else if (func.equals("removeDestination")) {
117             result = removeDestination((String) v.get(3), (String) v.get(2));
118         } else if (func.equals("destinationExists")) {
119             result = destinationExists((String) v.get(3), (String) v.get(2));
120         } else if (func.equals("getDurableConsumerMessageCount")) {
121             result = getDurableConsumerMessageCount((String) v.get(3),
122                 (String) v.get(4), (String) v.get(2));
123         } else if (func.equals("getQueueMessageCount")) {
124             result = getQueueMessageCount((String) v.get(3), (String) v.get(2));
125         } else if (func.equals("stopServer")) {
126             result = stopServer((String) v.get(2));
127         } else if (func.equals("addUser")) {
128             result = addUser((String) v.get(3), (String) v.get(4),
129                 (String) v.get(2));
130         } else if (func.equals("changePassword")) {
131             result = changePassword((String) v.get(3), (String) v.get(4),
132                 (String) v.get(2));
133         } else if (func.equals("removeUser")) {
134             result = removeUser((String) v.get(3), (String) v.get(2));
135         } else if (func.equals("getAllUsers")) {
136             result = getAllUsers((String) v.get(2));
137         } else if (func.equals("purgeMessages")) {
138             result = purgeMessages((String) v.get(2));
139         } else {
140             String st = ("Unknown request received: " + func);
141             System.err.println(st);
142             result = pack(new Boolean(false), st);
143         }
144 
145         return result;
146     }
147 
148     /***
149      * The connection has been broken.
150      *
151      * @param id   the unique identifier of this connection.
152      *
153      */
154     public void disconnection(String id) {
155     }
156 
157     /***
158      * A new connection request has been made.
159      *
160      * @param ipcId The unique IPC id of this connection.
161      * @param username the client's username
162      * @param password the client's password
163      * @return Vector The result of the request.
164      */
165     private Vector createConnection(String ipcId,
166                                     String username,
167                                     String password) {
168         Vector result;
169 
170         try {
171             AdminConnection connection =
172                 AdminConnectionManager.instance().createConnection(
173                     ipcId, username, password);
174             result = pack(Boolean.TRUE, connection.getIdentifierId());
175         } catch (JMSException exception) {
176             result = pack(Boolean.FALSE, exception);
177         }
178 
179         return result;
180     }
181 
182     /***
183      * Add a durable consumer to for a topic
184      *
185      * @param topic         the name of the topic destination
186      * @param name          consumer name
187      * @param identifierId  the unique identifier for this connection
188      * @return Vector      The result of the operation
189      */
190     private Vector addDurableConsumer(String topic, String name,
191                                       String identifierId) {
192         boolean result;
193 
194         try {
195             result =
196                 getAdminConnection(identifierId).addDurableConsumer(topic, name);
197             return pack(Boolean.TRUE, new Boolean(result));
198         } catch (JMSException exception) {
199             return pack(Boolean.FALSE, exception);
200         }
201     }
202 
203     /***
204      * Remove the specified durable consumer
205      *
206      * @param name - the consumer name
207      * @param identifierId  the unique identifier for this connection
208      * @return Vector - the result of the operation
209      */
210     private Vector removeDurableConsumer(String name, String identifierId) {
211         boolean result;
212 
213         try {
214             result =
215                 getAdminConnection(identifierId).removeDurableConsumer(name);
216             return pack(Boolean.TRUE, new Boolean(result));
217         } catch (JMSException exception) {
218             return pack(Boolean.FALSE, exception);
219         }
220     }
221 
222     /***
223      * Determine whether the specified durable consumer exists
224      *
225      * @param name - the consumer name
226      * @param identifierId  the unique identifier for this connection
227      * @return Vector - the result of the operation
228      */
229     private Vector durableConsumerExists(String name, String identifierId) {
230         boolean result;
231 
232         try {
233             result =
234                 getAdminConnection(identifierId).durableConsumerExists(name);
235             return pack(Boolean.TRUE, new Boolean(result));
236         } catch (JMSException exception) {
237             return pack(Boolean.FALSE, exception);
238         }
239     }
240 
241     /***
242      * Return a list of durable consumers for the specified topic
243      *
244      * @param topic - the queue/topic name
245      * @param identifierId  the unique identifier for this connection
246      * @return Vector - the result of the operation
247      *
248      */
249     private Vector getDurableConsumers(String topic, String identifierId) {
250         Vector consumers;
251 
252         try {
253             consumers =
254                 getAdminConnection(identifierId).getDurableConsumers(topic);
255             return pack(Boolean.TRUE, consumers);
256         } catch (JMSException exception) {
257             return pack(Boolean.FALSE, exception);
258         }
259     }
260 
261     /***
262      * Deactivate the specified durable consumer
263      *
264      * @param name - the name of the durable consumer
265      * @param identifierId  the unique identifier for this connection
266      * @return Vector - the result of the operation
267      */
268     private Vector unregisterConsumer(String name, String identifierId) {
269         boolean result;
270 
271         try {
272             result = getAdminConnection(identifierId).unregisterConsumer(name);
273             return pack(Boolean.TRUE, new Boolean(result));
274         } catch (JMSException exception) {
275             return pack(Boolean.FALSE, exception);
276         }
277     }
278 
279     /***
280      * Check to see if the given consumer is currently connected to the
281      * OpenJMSServer. This is only valid when in online mode.
282      *
283      * @param name - the unique consumer identifier.
284      * @param identifierId  the unique identifier for this connection
285      * @return Vector - the result of the operation
286      */
287     private Vector isConnected(String name, String identifierId) {
288         boolean result;
289 
290         try {
291             result = getAdminConnection(identifierId).isConnected(name);
292             return pack(Boolean.TRUE, new Boolean(result));
293         } catch (JMSException exception) {
294             return pack(Boolean.FALSE, exception);
295         }
296     }
297 
298     /***
299      * Add a new destination
300      *
301      * @param destination - the queue/topic name
302      * @param queue - true if this is a queue, false is a topic
303      * @param identifierId  the unique identifier for this connection
304      * @return Vector - the result of the operation
305      */
306     private Vector addDestination(String destination, Boolean queue,
307                                   String identifierId) {
308         boolean result;
309 
310         try {
311             result =
312                 getAdminConnection(identifierId).addDestination(destination, queue);
313             return pack(Boolean.TRUE, new Boolean(result));
314         } catch (JMSException exception) {
315             return pack(Boolean.FALSE, exception);
316         }
317     }
318 
319     /***
320      * Destroy all references to a queue/topic. Remove all messages and
321      * Consumer registrations.
322      *
323      * @param destination - the queue/topic name
324      * @param identifierId  the unique identifier for this connection
325      * @return Vector - result of the operation
326      */
327     private Vector removeDestination(String destination, String identifierId) {
328         boolean result;
329 
330         try {
331             result =
332                 getAdminConnection(identifierId).removeDestination(destination);
333             return pack(Boolean.TRUE, new Boolean(result));
334         } catch (JMSException exception) {
335             return pack(Boolean.FALSE, exception);
336         }
337     }
338 
339     /***
340      * Check if the specified destination exists.
341      *
342      * @param name - the destination name to check
343      * @param identifierId  the unique identifier for this connection
344      * @return Vector - result of the operation
345      */
346     private Vector destinationExists(String name, String identifierId) {
347         boolean result;
348 
349         try {
350             result = getAdminConnection(identifierId).destinationExists(name);
351             return pack(Boolean.TRUE, new Boolean(result));
352         } catch (JMSException exception) {
353             return pack(Boolean.FALSE, exception);
354         }
355     }
356 
357     /***
358      * Get a list of all the destinations
359      *
360      * @param identifierId  the unique identifier for this connection
361      * @return Vector    The result of the operation
362      */
363     private Vector getAllDestinations(String identifierId) {
364         Vector result;
365 
366         try {
367             result = getAdminConnection(identifierId).getAllDestinations();
368             return pack(Boolean.TRUE, result);
369         } catch (JMSException exception) {
370             return pack(Boolean.FALSE, exception);
371         }
372     }
373 
374     /***
375      * Return the number of messages for a specific durable consumer
376      *
377      * @param topic         topic name
378      * @param name          consumer name
379      * @param identifierId  the unique identifier for this connection
380      * @return Vector       The result of the operation
381      */
382     private Vector getDurableConsumerMessageCount(String topic, String name,
383                                                   String identifierId) {
384         int result;
385 
386         try {
387             result = getAdminConnection(identifierId).
388                 getDurableConsumerMessageCount(topic, name);
389             return pack(Boolean.TRUE, new Integer(result));
390         } catch (JMSException exception) {
391             return pack(Boolean.FALSE, exception);
392         }
393     }
394 
395     /***
396      * Return the number of messages in the given queue
397      *
398      * @param queue             queue name
399      * @param identifierId  the unique identifier for this connection
400      * @return Vector           The result of the operation
401      */
402     private Vector getQueueMessageCount(String queue, String identifierId) {
403         int result;
404 
405         try {
406             result =
407                 getAdminConnection(identifierId).getQueueMessageCount(queue);
408             return pack(Boolean.TRUE, new Integer(result));
409         } catch (JMSException exception) {
410             return pack(Boolean.FALSE, exception);
411         }
412     }
413 
414     /***
415      * Remove all processed messages from the database.
416      *
417      * @param identifierId  the unique identifier for this connection
418      * @return Vector          The result of the operation
419      */
420     private Vector purgeMessages(String identifierId) {
421         int result;
422 
423         try {
424             result = getAdminConnection(identifierId).purgeMessages();
425             return pack(Boolean.TRUE, new Integer(result));
426         } catch (JMSException exception) {
427             return pack(Boolean.FALSE, exception);
428         }
429     }
430 
431     /***
432      * Send a stop request to the OpenJMSServer.
433      *
434      * @param identifierId  the unique identifier for this connection
435      * @return Vector              The result of the operation
436      */
437     private Vector stopServer(String identifierId) {
438         try {
439             getAdminConnection(identifierId).stopServer();
440             return pack(Boolean.TRUE, Boolean.TRUE);
441         } catch (JMSException exception) {
442             return pack(Boolean.FALSE, exception);
443         }
444     }
445 
446     /***
447      * Add a user with the specified name
448      *
449      * @param username    the users name
450      * @param password    the users password
451      * @param identifierId  the unique identifier for this connection
452      * @return Vector     The result of the operation
453      */
454     private Vector addUser(String username, String password,
455                            String identifierId) {
456         boolean result;
457 
458         try {
459             result =
460                 getAdminConnection(identifierId).addUser(username, password);
461             return pack(Boolean.TRUE, new Boolean(result));
462         } catch (JMSException exception) {
463             return pack(Boolean.FALSE, exception);
464         }
465     }
466 
467     /***
468      * Change password for the specified user
469      *
470      * @param username    the users name
471      * @param password    the users password
472      * @param identifierId  the unique identifier for this connection
473      * @return Vector     The result of the operation
474      */
475     private Vector changePassword(String username, String password,
476                                   String identifierId) {
477         boolean result;
478 
479         try {
480             result =
481                 getAdminConnection(identifierId).changePassword(username, password);
482             return pack(Boolean.TRUE, new Boolean(result));
483         } catch (JMSException exception) {
484             return pack(Boolean.FALSE, exception);
485         }
486     }
487 
488     /***
489      * Remove the specified user
490      *
491      * @param username    the users name
492      * @param identifierId  the unique identifier for this connection
493      * @return Vector      the result of the operation
494      */
495     private Vector removeUser(String username, String identifierId) {
496         boolean result;
497 
498         try {
499             result = getAdminConnection(identifierId).removeUser(username);
500             return pack(Boolean.TRUE, new Boolean(result));
501         } catch (JMSException exception) {
502             return pack(Boolean.FALSE, exception);
503         }
504     }
505 
506     /***
507      * Return a list of all registered users.
508      *
509      * @param identifierId  the unique identifier for this connection
510      * @return Vector         The result of the operation
511      */
512     private Vector getAllUsers(String identifierId) {
513         Vector result;
514 
515         try {
516             result = getAdminConnection(identifierId).getAllUsers();
517             return pack(Boolean.TRUE, result);
518         } catch (JMSException exception) {
519             return pack(Boolean.FALSE, exception);
520         }
521     }
522 
523     /***
524      * Pack all the data that is required by the server in a vector.
525      * Set the size of the vector to be exactly the right size for efficiency.
526      *
527      * @param success Boolean indicating success or failure of request.
528      * @param ob The Object being returned.
529      * @return Vector The vector containing all the data.
530      *
531      */
532     private Vector pack(Boolean success, Object ob) {
533         Vector v = new Vector(2);
534         v.add(success);
535         v.add(ob);
536         return v;
537     }
538 
539     /***
540      * Get the AdminConnection for this connection.
541      *
542      * @param identifierId the unique id for this connection
543      * @return AdminConnection the connection
544      * @throws JMSSecurityException if the connection not exist
545      */
546     private AdminConnection getAdminConnection(String identifierId)
547         throws JMSSecurityException {
548 
549         AdminConnection connection =
550             AdminConnectionManager.instance().getConnection(identifierId);
551 
552         if (connection == null) {
553             throw new JMSSecurityException("Not authorized for this operation");
554         }
555 
556         return connection;
557     }
558 }