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: OfflineConnection.java,v 1.3 2006/02/23 11:02:57 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.*;
53  import java.sql.Connection;
54  import java.util.Enumeration;
55  import javax.swing.*;
56  
57  import org.apache.commons.logging.Log;
58  import org.apache.commons.logging.LogFactory;
59  import org.exolab.jms.authentication.User;
60  import org.exolab.jms.config.Configuration;
61  import org.exolab.jms.config.DatabaseConfiguration;
62  import org.exolab.jms.persistence.DatabaseService;
63  import org.exolab.jms.persistence.PersistenceException;
64  import org.exolab.jms.service.ServiceException;
65  
66  
67  /***
68   * Connect directly to the Persistent store to retrieve information and perfrom
69   * updates.
70   * <p/>
71   * <P> Note: If the OpenJMSServer is active, this connection will fail, since it
72   * requires and exclusive lock on the database to avoid database corruption.
73   * Similarly, if this connection is active the OpenJMSServer cannot be started
74   * for the same reasons.
75   *
76   * @author <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
77   * @version $Revision: 1.3 $ $Date: 2006/02/23 11:02:57 $
78   * @see AbstractAdminConnection
79   * @see AdminMgr
80   */
81  public class OfflineConnection extends AbstractAdminConnection {
82  
83  
84      // The parent Gui
85      private Component _parent;
86  
87      /***
88       * The database service.
89       */
90      private DatabaseService _database;
91  
92      /***
93       * The logger.
94       */
95      private static final Log _log = LogFactory.getLog(OfflineConnection.class);
96  
97  
98      /***
99       * Connect to the RMIAdmin Server if in online mode, or open the database
100      * and update the data directly in offline mode.
101      *
102      * @param parent The component parent.
103      * @throws OfflineConnectionException When the database cannot be opened
104      */
105     public OfflineConnection(Component parent, Configuration config)
106             throws OfflineConnectionException {
107         try {
108             if (_instance == null) {
109                 _parent = parent;
110                 _database = new DatabaseService(config);
111                 _database.start();
112 
113                 DatabaseConfiguration dbconfig =
114                         config.getDatabaseConfiguration();
115 
116                 // determine the database type and instantiate the appropriate
117                 // database adapter
118                 if (dbconfig.getRdbmsDatabaseConfiguration() != null) {
119                     _database.getAdapter();
120                     _instance = this;
121                 }
122             } else {
123                 throw new OfflineConnectionException("Already connected");
124             }
125         } catch (Exception err) {
126             _log.error(err.getMessage(), err);
127             throw new OfflineConnectionException
128                     ("Database Error: " + err.getMessage());
129         }
130     }
131 
132     // implementation of AbstractAdminConnection.close
133     public void close() {
134         try {
135             _database.stop();
136         } catch (ServiceException exception) {
137             _log.error(exception, exception);
138         }
139         _instance = null;
140     }
141 
142     // implementation of AbstractAdminConnection.addDurableConsumer
143     public boolean addDurableConsumer(String topic, String name) {
144         boolean result = false;
145         try {
146             _database.begin();
147             Connection connection = _database.getConnection();
148             _database.getAdapter().addDurableConsumer(connection, topic,
149                     name);
150             _database.commit();
151             result = true;
152         } catch (PersistenceException exception) {
153             error("Failed to add durable consumer=" + name + " for topic="
154                     + topic, exception);
155         }
156 
157         return result;
158     }
159 
160     // implementation of AbstractAdminConnection.removeDurableConsumer
161     public boolean removeDurableConsumer(String name) {
162         boolean result = false;
163 
164         try {
165             _database.begin();
166             Connection connection = _database.getConnection();
167 
168             _database.getAdapter().removeDurableConsumer(connection, name);
169             _database.commit();
170             result = true;
171         } catch (PersistenceException exception) {
172             error("Failed to remove durable consumer=" + name, exception);
173         }
174         return result;
175     }
176 
177     // implementation of AbstractAdminConnection.unregisterConsumer
178     public boolean unregisterConsumer(String name) {
179         return false;
180     }
181 
182     // implementation of AbstractAdminConnection.isConnected
183     public boolean isConnected(String name) {
184         return false;
185     }
186 
187     // implementation of AbstractAdminConnection.getAllDestinations
188     public Enumeration getAllDestinations() {
189         Enumeration result = null;
190         try {
191             _database.begin();
192             Connection connection = _database.getConnection();
193             result = _database.getAdapter().getAllDestinations(connection);
194             _database.commit();
195         } catch (PersistenceException exception) {
196             error("Failed to get destinations", exception);
197         }
198 
199         return result;
200     }
201 
202     // implementation of AbstractAdminConnection.addDestination
203     public boolean addDestination(String destination, boolean isQueue) {
204         boolean success = false;
205 
206         try {
207             _database.begin();
208             Connection connection = _database.getConnection();
209 
210             _database.getAdapter().addDestination(connection,
211                     destination, isQueue);
212 
213             _database.commit();
214             success = true;
215         } catch (PersistenceException exception) {
216             error("Failed to add destination=" + destination, exception);
217         }
218 
219         return success;
220     }
221 
222     // implementation of AbstractAdminConnection.getDurableConsumerMessageCount
223     public int getDurableConsumerMessageCount(String topic, String name) {
224         int count = -1;
225 
226         try {
227             _database.begin();
228             Connection connection = _database.getConnection();
229 
230             count = _database.getAdapter().getDurableConsumerMessageCount(
231                     connection, topic, name);
232             _database.commit();
233         } catch (PersistenceException exception) {
234             error("Failed to get message count for topic=" + topic
235                     + ", name=" + name, exception);
236         }
237         return count;
238     }
239 
240     // implementation of AbstractAdminConnection.getQueueMessageCount
241     public int getQueueMessageCount(String queue) {
242         int count = -1;
243 
244         try {
245             _database.begin();
246             Connection connection = _database.getConnection();
247 
248             count = _database.getAdapter().getQueueMessageCount(connection,
249                     queue);
250             _database.commit();
251         } catch (PersistenceException exception) {
252             error("Failed to get message count for queue=" + queue, exception);
253         }
254 
255         return count;
256     }
257 
258     // implementation of AbstractAdminConnection.durableConsumerExists
259     public boolean durableConsumerExists(String name) {
260         boolean result = false;
261 
262         try {
263             _database.begin();
264             Connection connection = _database.getConnection();
265 
266             result = _database.getAdapter().durableConsumerExists(connection,
267                     name);
268             _database.commit();
269         } catch (PersistenceException exception) {
270             error("Failed to determine if consumer exists: " + name, exception);
271         }
272 
273         return result;
274     }
275 
276     // implementation of AbstractAdminConnection.getDurableConsumers
277     public Enumeration getDurableConsumers(String topic) {
278         Enumeration result = null;
279 
280         try {
281             _database.begin();
282             Connection connection = _database.getConnection();
283 
284             result = _database.getAdapter().getDurableConsumers(connection,
285                     topic);
286             _database.commit();
287         } catch (PersistenceException exception) {
288             error("Failed to retrieve durable consumers", exception);
289         }
290         return result;
291     }
292 
293     // implementation of AbstractAdminConnection.removeDestination
294     public boolean removeDestination(String destination) {
295         boolean result = false;
296 
297         try {
298             _database.begin();
299             Connection connection = _database.getConnection();
300 
301             _database.getAdapter().removeDestination(connection, destination);
302             _database.commit();
303             result = true;
304         } catch (PersistenceException exception) {
305             error("Failed to remove destination=" + destination, exception);
306         }
307 
308         return result;
309     }
310 
311     // implementation of AbstractAdminConnection.purgeMessages
312     public int purgeMessages() {
313         return _database.getAdapter().purgeMessages();
314     }
315 
316     // implementation of AbstractAdminConnection.stopServer
317     public void stopServer() {
318         JOptionPane.showMessageDialog(
319                 _parent, "Not available in offline mode",
320                 "Shutdown Error", JOptionPane.ERROR_MESSAGE);
321     }
322 
323     // implementation of AbstractAdminConnection.addUser
324     public boolean addUser(String username, String password) {
325         boolean success = false;
326 
327         try {
328             _database.begin();
329             Connection connection = _database.getConnection();
330 
331             _database.getAdapter().addUser(connection,
332                     new User(username, password));
333             _database.commit();
334             success = true;
335         } catch (PersistenceException exception) {
336             error("Failed to add user=" + username, exception);
337         }
338         return success;
339     }
340 
341     // implementation of AbstractAdminConnection.changePassord
342     public boolean changePassword(String username, String password) {
343 
344         boolean success = false;
345 
346         try {
347             _database.begin();
348             Connection connection = _database.getConnection();
349 
350             _database.getAdapter().updateUser(connection,
351                     new User(username, password));
352             _database.commit();
353             success = true;
354         } catch (PersistenceException exception) {
355             error("Failed to change password for user=" + username, exception);
356         }
357         return success;
358     }
359 
360     // implementation of AbstractAdminConnection.removeUser
361     public boolean removeUser(String username) {
362         boolean result = false;
363 
364         try {
365             _database.begin();
366             Connection connection = _database.getConnection();
367 
368             _database.getAdapter().removeUser(connection,
369                     new User(username, null));
370             _database.commit();
371             result = true;
372         } catch (PersistenceException exception) {
373             error("Failed to remove user=" + username, exception);
374         }
375 
376         return result;
377     }
378 
379     // implementation of AbstractAdminConnection.getAllUsers
380     public Enumeration getAllUsers() {
381         Enumeration result = null;
382 
383         try {
384             _database.begin();
385             Connection connection = _database.getConnection();
386 
387             result = _database.getAdapter().getAllUsers(connection);
388             _database.commit();
389         } catch (PersistenceException exception) {
390             rollback();
391         }
392 
393         return result;
394     }
395 
396     /***
397      * Helper to log an error and rollback.
398      *
399      * @param message   the error messge
400      * @param exception the exception
401      */
402     private void error(String message, PersistenceException exception) {
403         _log.error(message, exception);
404         rollback();
405     }
406 
407     /***
408      * Rollback the current transaction, logging any error.
409      */
410     private void rollback() {
411         try {
412             _database.rollback();
413         } catch (PersistenceException exception) {
414             _log.warn(exception, exception);
415         }
416     }
417 
418 }