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 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: OpenJMSServer.java,v 1.1 2004/11/26 01:51:15 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * $Date	    jimm    Created
47   */
48  
49  
50  package org.exolab.jms.tools.admin;
51  
52  import java.awt.Rectangle;
53  import java.awt.event.ActionEvent;
54  import java.awt.event.ActionListener;
55  import java.util.Enumeration;
56  
57  import javax.swing.JMenuItem;
58  import javax.swing.JOptionPane;
59  import javax.swing.JPopupMenu;
60  import javax.swing.JTree;
61  import javax.swing.tree.DefaultMutableTreeNode;
62  import javax.swing.tree.DefaultTreeModel;
63  
64  import org.exolab.jms.client.JmsDestination;
65  import org.exolab.jms.client.JmsQueue;
66  import org.exolab.jms.client.JmsTopic;
67  
68  
69  /***
70   * This class controls all dispay characteristics and menus related to an
71   * OpenJMSServer. Currently only add queue/topic is supported.
72   *
73   * @version     $Revision: 1.1 $ $Date: 2004/11/26 01:51:15 $
74   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
75   * @see         OpenJMSConsumer
76   * @see         AdminMgr
77   * @see         QueryDialog
78   */
79  public class OpenJMSServer extends DefaultMutableTreeNode {
80  
81      // The server name.
82      private String serverName_;
83  
84      //  A reference to the tree this node belongs to.
85      static private JTree tree_ = null;
86  
87      // A flag indicating if the menu has been created yet.
88      static private boolean commandsCreated_ = false;
89  
90      // The popup menu for all queue/topics
91      static private JPopupMenu commands_ = null;
92  
93      /***
94       * The OpenJMS server connection. Currently there is only one
95       * OpenJMSServer connection at a time.
96       *
97       * @param serverName The name of this server
98       * @param tree The parent tree this root node belongs to.
99       *
100      */
101     public OpenJMSServer(String serverName, JTree tree) {
102         serverName_ = serverName;
103 
104         if (!commandsCreated_) {
105             tree_ = tree;
106 
107             // construct the top level folders
108             //add(new OpenJMSConsumerFolder(tree_));
109             //add(new OpenJMSQueueFolder(tree_));
110             //add(new OpenJMSTopicFolder(tree_));
111 
112             createCommands();
113             commandsCreated_ = true;
114         }
115     }
116 
117     /***
118      * Create the menu for all servers and set up the Action events for
119      * each menu item. Since menus are shared, the callbacks called are
120      * static. Once a menu is slected, the slected node can be determined
121      * from the parent object.
122      *
123      */
124     protected void createCommands() {
125         commands_ = new JPopupMenu();
126         JMenuItem m = new JMenuItem("Add Queue");
127         m.addActionListener(new ActionListener() {
128 
129             public void actionPerformed(ActionEvent evt) {
130                 addQueue();
131             }
132         }
133         );
134         commands_.add(m);
135 
136         m = new JMenuItem("Add Topic");
137         m.addActionListener(new ActionListener() {
138 
139             public void actionPerformed(ActionEvent evt) {
140                 addTopic();
141             }
142         }
143         );
144         commands_.add(m);
145 
146         m = new JMenuItem("Add User");
147         m.addActionListener(new ActionListener() {
148 
149             public void actionPerformed(ActionEvent evt) {
150                 addUser();
151             }
152         }
153         );
154         commands_.add(m);
155 
156         m = new JMenuItem("Purge Messages");
157         m.addActionListener(new ActionListener() {
158 
159             public void actionPerformed(ActionEvent evt) {
160                 purgeMessages();
161             }
162         }
163         );
164         commands_.add(m);
165     }
166 
167     /***
168      * Determine all known OpenJMSServers. For the moment only the offline
169      * mode is supported, which is basically opening up the database directly.
170      *
171      * @param tree The parent tree this root node belongs to.
172      *
173      */
174     static public DefaultTreeModel createServerList(JTree tree) {
175         // todo try and connect to all known servers.
176         // for the moment just add one.
177         OpenJMSServer server = new OpenJMSServer("OpenJMSServer", tree);
178         return new DefaultTreeModel(server);
179     }
180 
181     /***
182      * Get all queue/topics from the database for this JMS server and display
183      * them as children of the root node.
184      *
185      */
186     public void displayConnections() {
187         Enumeration e = AbstractAdminConnection.instance().getAllDestinations();
188         if (e != null) {
189             while (e.hasMoreElements()) {
190                 JmsDestination destination = (JmsDestination) e.nextElement();
191                 if (destination instanceof JmsQueue) {
192                     add(new OpenJMSQueue(destination.getName(), tree_));
193                 } else if (destination instanceof JmsTopic) {
194                     add(new OpenJMSTopic(destination.getName(), tree_));
195                 }
196             }
197         }
198         //Users
199         e = AbstractAdminConnection.instance().getAllUsers();
200         if (e != null) {
201             while (e.hasMoreElements()) {
202                 add(new OpenJMSUser(e.nextElement().toString(), tree_));
203             }
204         }
205         refresh();
206     }
207 
208     /***
209      * Children are allowed for all servers
210      *
211      * @return boolean Always returns true.
212      *
213      */
214     public boolean getAllowsChildren() {
215         return true;
216     }
217 
218     /***
219      * This node has been right clicked. The locations of this node is given
220      * by the loc object. Use this location to popup the server message
221      * menu.
222      *
223      * @param The location of this Consumer node.
224      *
225      */
226     public void displayCommands(Rectangle loc) {
227         double x;
228         double y;
229 
230         x = loc.getX();
231         y = loc.getY();
232         y += loc.getHeight();
233 
234         commands_.show(tree_, (int) x, (int) y);
235     }
236 
237     /***
238      * The unique name of this server
239      *
240      * @return String the server name.
241      *
242      */
243     public String toString() {
244         return serverName_;
245     }
246 
247     /***
248      * This node has changed. Inform the parent tree that it needs to be
249      * re-drawn.
250      *
251      */
252     public void refresh() {
253         DefaultTreeModel model = (DefaultTreeModel) tree_.getModel();
254         model.nodeStructureChanged((DefaultMutableTreeNode) this);
255     }
256 
257     /***
258      * Get the particular instance of the server that has been selected.
259      *
260      * @return OpenJMSServer the instance selected.
261      *
262      */
263     static private OpenJMSServer getInstanceSelected() {
264         Object loc = tree_.getLastSelectedPathComponent();
265         return (OpenJMSServer) loc;
266     }
267 
268     /***
269      * A new queue is being added for this server. Popup a add queue
270      * destination dialog, to collect relevent information, then update the
271      * database. If the database update is successful, add the queue node as a
272      * a child of this server node, and refresh.
273      */
274     static private void addQueue() {
275         OpenJMSServer This = getInstanceSelected();
276         CreateQueueDialog.instance().displayCreateQueue();
277 
278         if (CreateQueueDialog.instance().isConfirmed()) {
279             if (AbstractAdminConnection.instance().addDestination(
280                 CreateQueueDialog.instance().getName(), true)) {
281 
282                 This.add(new OpenJMSQueue(
283                     CreateQueueDialog.instance().getName(), tree_));
284                 This.refresh();
285             } else {
286                 JOptionPane.showMessageDialog
287                     (tree_, "Queue already exists", "Create Error",
288                         JOptionPane.ERROR_MESSAGE);
289             }
290         }
291     }
292 
293     /***
294      * A new queue is being added for this server. Popup a add queue
295      * destination dialog, to collect relevent information, then update the
296      * database. If the database update is successful, add the queue node as a
297      * a child of this server node, and refresh.
298      */
299     static private void addUser() {
300         OpenJMSServer This = getInstanceSelected();
301         CreateUserDialog.instance().displayCreateUser();
302 
303         if (CreateUserDialog.instance().isConfirmed()) {
304             if (AbstractAdminConnection.instance().addUser(
305                 CreateUserDialog.instance().getName(),
306                 CreateUserDialog.instance().getPassword())) {
307 
308                 This.add(new OpenJMSUser(
309                     CreateUserDialog.instance().getName(), tree_));
310                 This.refresh();
311             } else {
312                 JOptionPane.showMessageDialog
313                     (tree_, "User already exists", "Create Error",
314                         JOptionPane.ERROR_MESSAGE);
315             }
316         }
317     }
318 
319     /***
320      * A new topic is being added for this server. Popup a add topic
321      * destination dialog, to collect relevent information, then update the
322      * database. If the database update is successful, add the topic node as a
323      * a child of this server node, and refresh.
324      */
325     static private void addTopic() {
326         OpenJMSServer This = getInstanceSelected();
327         CreateTopicDialog.instance().displayCreateTopic();
328 
329         if (CreateTopicDialog.instance().isConfirmed()) {
330             if (AbstractAdminConnection.instance().addDestination(
331                 CreateTopicDialog.instance().getName(), false)) {
332 
333                 This.add(new OpenJMSTopic(
334                     CreateTopicDialog.instance().getName(), tree_));
335                 This.refresh();
336             } else {
337                 JOptionPane.showMessageDialog
338                     (tree_, "Topic already exists", "Create Error",
339                         JOptionPane.ERROR_MESSAGE);
340             }
341         }
342     }
343 
344     /***
345      * Purge all processed messages from the databse.
346      */
347     static private void purgeMessages() {
348         QueryDialog.instance().display
349             ("Are you sure you want to purge all\n processed messages.");
350         if (org.exolab.jms.tools.admin.QueryDialog.instance().isConfirmed()) {
351             int count = AbstractAdminConnection.instance().purgeMessages();
352             JOptionPane.showMessageDialog
353                 (tree_, count + " messages were purged.", "Info",
354                     JOptionPane.ERROR_MESSAGE);
355         } else {
356             JOptionPane.showMessageDialog
357                 (tree_, "Purge Messages Aborted.", "Purge Error",
358                     JOptionPane.ERROR_MESSAGE);
359         }
360     }
361 } // End ServerList