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: OnlineConnection.java,v 1.3 2003/08/24 07:09:29 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.Component;
53  import java.util.Enumeration;
54  import java.util.Vector;
55  
56  import javax.swing.JOptionPane;
57  
58  import org.exolab.jms.administration.AdminConnectionFactory;
59  import org.exolab.jms.administration.JmsAdminServerIfc;
60  import org.exolab.jms.config.ConfigHelper;
61  import org.exolab.jms.config.ConfigurationManager;
62  import org.exolab.jms.config.Connector;
63  import org.exolab.jms.config.SecurityConfiguration;
64  import org.exolab.jms.config.types.SchemeType;
65  
66  
67  /***
68   * Connects to the OpenJMSServer for all updates and requests.
69   *
70   * <P>Note: The OpenJMSServer must be active and in a running state for this
71   * type of connection to succeed.
72   *
73   * @version     $Revision: 1.3 $ $Date: 2003/08/24 07:09:29 $
74   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
75   */
76  public class OnlineConnection extends AbstractAdminConnection {
77  
78      // The connection to the OpenJMS server.
79      private JmsAdminServerIfc _admin = null;
80  
81      // The parent Gui
82      private Component _parent;
83  
84      // The connection object type
85      private String _connectionType;
86  
87  
88      /***
89       * Connect to the Admin Server
90       *
91       * @exception OnlineConnectionException When online connection fails.
92       *
93       */
94      public OnlineConnection(Component parent) throws OnlineConnectionException {
95          String username = "anonymous";
96          String password = "anonymous";
97  
98          try {
99              if (_instance == null) {
100                 Connector connector = ConfigurationManager.getConnector();
101                 SchemeType scheme = connector.getScheme();
102                 String url = ConfigHelper.getAdminURL(
103                     scheme, ConfigurationManager.getConfig());
104 
105                 SecurityConfiguration config =
106                     ConfigurationManager.getConfig().getSecurityConfiguration();
107                 if (config.getSecurityEnabled()) {
108                     CreateLogonDialog.instance().displayCreateLogon();
109 
110                     if (CreateLogonDialog.instance().isConfirmed()) {
111                         username = CreateLogonDialog.instance().getName();
112                         password = CreateLogonDialog.instance().getPassword();
113                     }
114                 }
115 
116                 //Perform logon
117                 _admin = AdminConnectionFactory.create(url, username, password);
118 
119                 _parent = parent;
120                 _instance = this;
121             } else {
122                 throw new org.exolab.jms.tools.admin.OnlineConnectionException("Already connected");
123             }
124         } catch (Exception err) {
125             throw new org.exolab.jms.tools.admin.OnlineConnectionException("Failed to connect: " + err.toString());
126         }
127     }
128 
129     /***
130      * Connect to the Admin Server, special constructor to be able to stop the server
131      *
132      * @exception OnlineConnectionException When online connection fails.
133      *
134      */
135     public OnlineConnection(String username, String password) throws OnlineConnectionException {
136         try {
137             if (_instance == null) {
138                 Connector connector = ConfigurationManager.getConnector();
139                 SchemeType scheme = connector.getScheme();
140                 String url = ConfigHelper.getAdminURL(
141                     scheme, ConfigurationManager.getConfig());
142 
143                 //Perform logon
144                 _admin = AdminConnectionFactory.create(url, username, password);
145 
146                 _instance = this;
147             } else {
148                 throw new org.exolab.jms.tools.admin.OnlineConnectionException("Already connected");
149             }
150         } catch (Exception err) {
151             throw new org.exolab.jms.tools.admin.OnlineConnectionException("Failed to connect: " + err.toString());
152         }
153     }
154 
155     /***
156      * Display the error in a JOptionPane.
157      *
158      * @param err The Error to display.
159      * @param st The string to use as a title on the JOptionPane.
160      *
161      */
162     private void displayError(Exception err, String st) {
163         JOptionPane.showMessageDialog
164             (_parent, st + "\n" + err, st, JOptionPane.ERROR_MESSAGE);
165     }
166 
167     // implementation of AbstractAdminConnection.close
168     public void close() {
169         _admin.close();
170         _instance = null;
171         _admin = null;
172     }
173 
174     // implementation of AbstractAdminConnection.addDurableConsumer
175     public boolean addDurableConsumer(String topic, String name) {
176         try {
177             return _admin.addDurableConsumer(topic, name);
178         } catch (javax.jms.JMSException error) {
179             if (error.getLinkedException() != null) {
180                 error.getLinkedException().printStackTrace();
181             }
182             displayError(error, "Failed to add consumer");
183             return false;
184         } catch (Exception err) {
185             displayError(err, "Failed to add consumer " + name);
186             return false;
187         }
188     }
189 
190     // implementation of AbstractAdminConnection.unregisterConsumer
191     public boolean removeDurableConsumer(String name) {
192         try {
193             return _admin.removeDurableConsumer(name);
194         } catch (Exception err) {
195             displayError(err, "Failed to remove consumer " + name);
196             return false;
197         }
198     }
199 
200     // implementation of AbstractAdminConnection.unregisterConsumer
201     public boolean unregisterConsumer(String name) {
202         try {
203             return _admin.unregisterConsumer(name);
204         } catch (Exception err) {
205             displayError(err, "Failed to De-Activate consumer " + name);
206             return false;
207         }
208     }
209 
210     // implementation of AbstractAdminConnection.isConnected
211     public boolean isConnected(String name) {
212         try {
213             return _admin.isConnected(name);
214         } catch (Exception err) {
215             return false;
216         }
217     }
218 
219     // implementation of AbstractAdminConnection.getAllDestinations
220     public Enumeration getAllDestinations() {
221         try {
222             Vector v = _admin.getAllDestinations();
223             Enumeration e = null;
224             if (v != null) {
225                 e = v.elements();
226             }
227             return e;
228         } catch (Exception err) {
229             displayError(err, "Failed to getAllQueueTopics");
230             return null;
231         }
232     }
233 
234     // implementation of AbstractAdminConnection.addDestination
235     public boolean addDestination(String destination, boolean isQueue) {
236         try {
237             return _admin.addDestination(destination, new Boolean(isQueue));
238         } catch (Exception err) {
239             displayError(err, "Failed to add destination");
240             return false;
241         }
242     }
243 
244     // implementation of AbstractAdminConnection.getDurableConsumerMessageCount
245     public int getDurableConsumerMessageCount(String topic, String name) {
246         try {
247             return _admin.getDurableConsumerMessageCount(topic, name);
248         } catch (Exception err) {
249             displayError(err, "Failed in getDurableConsumerMessageCount");
250             return -1;
251         }
252     }
253 
254     // implementation of AbstractAdminConnection.getQueueMessageCount
255     public int getQueueMessageCount(String queue) {
256         try {
257             return _admin.getQueueMessageCount(queue);
258         } catch (Exception err) {
259             displayError(err, "Failed in getQueueMessageCount");
260             return -1;
261         }
262     }
263 
264     // implementation of AbstractAdminConnection.durableConsumerExists
265     public boolean durableConsumerExists(String name) {
266         try {
267             return _admin.durableConsumerExists(name);
268         } catch (Exception err) {
269             displayError(err, "Failed in durableConsumerExists");
270         }
271 
272         return false;
273     }
274 
275     // implementation of AbstractAdminConnection.getDurableConsumers
276     public Enumeration getDurableConsumers(String topic) {
277         try {
278             Vector v = _admin.getDurableConsumers(topic);
279             Enumeration e = null;
280             if (v != null) {
281                 e = v.elements();
282             }
283             return e;
284         } catch (Exception err) {
285             displayError(err, "Failed in getDurableConsumers");
286             return null;
287         }
288     }
289 
290     // implementation of AbstractAdminConnection.removeDestination
291     public boolean removeDestination(String destination) {
292         try {
293             return _admin.removeDestination(destination);
294         } catch (Exception err) {
295             displayError(err, "Failed to destroy destination");
296             return false;
297         }
298     }
299 
300     // implementation of AbstractAdminConnection.purgeMessages
301     public int purgeMessages() {
302         int result = -1;
303         try {
304             result = _admin.purgeMessages();
305         } catch (Exception err) {
306             displayError(err, "Failed to purge messages from database");
307         }
308 
309         return result;
310     }
311 
312     // implementation of AbstractAdminConnection.stopServer
313     public void stopServer() {
314         try {
315             if (_admin == null) {
316                 JOptionPane.showMessageDialog
317                     (_parent, "Must connect with online mode \nto "
318                     + "shutdown server", "Shutdown Error",
319                         JOptionPane.ERROR_MESSAGE);
320             } else {
321                 _admin.stopServer();
322                 _instance = null;
323                 _admin = null;
324             }
325         } catch (Exception err) {
326             displayError(err, "Failed to Stop server");
327         }
328     }
329 
330     // implementation of AbstractAdminConnection.addUser
331     public boolean addUser(String username, String password) {
332         try {
333             return _admin.addUser(username, password);
334         } catch (javax.jms.JMSException error) {
335             if (error.getLinkedException() != null) {
336                 error.getLinkedException().printStackTrace();
337             }
338             displayError(error, "Failed to add user");
339             return false;
340         } catch (Exception err) {
341             displayError(err, "Failed to add user " + username);
342             return false;
343         }
344     }
345 
346     // implementation of AbstractAdminConnection.changePassord
347     public boolean changePassword(String username, String password) {
348         try {
349             return _admin.changePassword(username, password);
350         } catch (javax.jms.JMSException error) {
351             if (error.getLinkedException() != null) {
352                 error.getLinkedException().printStackTrace();
353             }
354             displayError(error, "Failed to change password");
355             return false;
356         } catch (Exception err) {
357             displayError(err, "Failed to change password for user " + username);
358             return false;
359         }
360     }
361 
362     // implementation of AbstractAdminConnection.removeUser
363     public boolean removeUser(String username) {
364         try {
365             return _admin.removeUser(username);
366         } catch (javax.jms.JMSException error) {
367             if (error.getLinkedException() != null) {
368                 error.getLinkedException().printStackTrace();
369             }
370             displayError(error, "Failed to remove user");
371             return false;
372         } catch (Exception err) {
373             displayError(err, "Failed to remove user " + username);
374             return false;
375         }
376     }
377 
378     // implementation of AbstractAdminConnection.getAllUsers
379     public Enumeration getAllUsers() {
380         try {
381             Vector v = _admin.getAllUsers();
382             Enumeration e = null;
383             if (v != null) {
384                 e = v.elements();
385             }
386             return e;
387         } catch (Exception err) {
388             displayError(err, "Failed to getAllUsers");
389             return null;
390         }
391     }
392 
393 } // End OnlineConnection