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 2003/08/17 01:32:27 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.sql.Connection;
54  import java.util.Enumeration;
55  import java.util.Hashtable;
56  
57  import javax.naming.Context;
58  import javax.naming.InitialContext;
59  import javax.swing.JFileChooser;
60  import javax.swing.JOptionPane;
61  import javax.transaction.TransactionManager;
62  
63  import org.exolab.jms.authentication.User;
64  import org.exolab.jms.client.JmsDestination;
65  import org.exolab.jms.client.JmsQueue;
66  import org.exolab.jms.client.JmsTopic;
67  import org.exolab.jms.config.Configuration;
68  import org.exolab.jms.config.ConfigurationManager;
69  import org.exolab.jms.config.DatabaseConfiguration;
70  import org.exolab.jms.persistence.DatabaseService;
71  import org.exolab.jms.persistence.PersistenceException;
72  
73  
74  /***
75   * Connect directly to the Persistent store to retrieve information and perfrom
76   * updates.
77   *
78   * <P> Note: If the OpenJMSServer is active, this connection will fail, since
79   * it requires and exclusive lock on the database to avoid database corruption.
80   * Similarly, if this connection is active the OpenJMSServer cannot be started
81   * for the same reasons.
82   *
83   * @version     $Revision: 1.3 $ $Date: 2003/08/17 01:32:27 $
84   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
85   * @see		AbstractAdminConnection
86   * @see		AdminMgr
87   */
88  public class OfflineConnection extends AbstractAdminConnection {
89  
90      // The parent Gui
91      private Component _parent;
92  
93      // The deafult JNDI context.
94      private Context _context = null;
95  
96      /***
97       * Connect to the RMIAdmin Server if in online mode, or open
98       * the database and update the data directly in offline mode.
99       *
100      * @param online Indicates if we are connecting in online or offline mode.
101      * @param parent The component parent.
102      * @throws IllegalStateException if {@link ConfigurationManager} has not
103      * been initialised
104      * @throws OfflineConnectionException When the database cannot be opened
105      */
106     public OfflineConnection(Component parent)
107         throws OfflineConnectionException {
108         try {
109             if (_instance == null) {
110                 _parent = parent;
111 
112                 Configuration config = ConfigurationManager.getConfig();
113 
114                 DatabaseConfiguration dbconfig =
115                     config.getDatabaseConfiguration();
116 
117                 // determine the database type and instantiate the appropriate
118                 // database adapter
119                 if (dbconfig.getRdbmsDatabaseConfiguration() != null) {
120                     DatabaseService.instance().getAdapter();
121                     _instance = this;
122                 } else {
123                     JFileChooser chooser = new JFileChooser(".");
124                     chooser.setDialogTitle
125                         ("Select OpenJMS Database to connect to");
126                     chooser.setFileFilter(new DatabaseFilter());
127                     int returnVal = chooser.showOpenDialog(parent);
128 
129                     if (returnVal == JFileChooser.APPROVE_OPTION) {
130                         DatabaseService.instance().getAdapter();
131                         _instance = this;
132                     }
133                 }
134             } else {
135                 throw new org.exolab.jms.tools.admin.OfflineConnectionException("Already connected");
136             }
137         } catch (Exception err) {
138             throw new org.exolab.jms.tools.admin.OfflineConnectionException
139                 ("Database Error: " + err.getMessage());
140         }
141     }
142 
143     // implementation of AbstractAdminConnection.close
144     public void close() {
145         DatabaseService.getAdapter().close();
146         _instance = null;
147     }
148 
149     // implementation of AbstractAdminConnection.addDurableConsumer
150     public boolean addDurableConsumer(String topic, String name) {
151         boolean result = false;
152         Connection connection = null;
153 
154         try {
155             connection = DatabaseService.getConnection();
156             DatabaseService.getAdapter().addDurableConsumer(connection, topic,
157                 name);
158             connection.commit();
159             result = true;
160 
161         } catch (PersistenceException exception) {
162             try {
163                 connection.rollback();
164             } catch (Exception nested) {
165                 // ignore
166             }
167         } catch (Exception exception) {
168             // ignore for the moment
169         } finally {
170             if (connection != null) {
171                 try {
172                     connection.close();
173                 } catch (Exception nested) {
174                     // ignore
175                 }
176             }
177         }
178 
179         return result;
180     }
181 
182     // implementation of AbstractAdminConnection.removeDurableConsumer
183     public boolean removeDurableConsumer(String name) {
184         boolean result = false;
185         Connection connection = null;
186 
187         try {
188             connection = DatabaseService.getConnection();
189 
190             DatabaseService.getAdapter().removeDurableConsumer(connection, name);
191             connection.commit();
192             result = true;
193 
194         } catch (PersistenceException exception) {
195             try {
196                 connection.rollback();
197             } catch (Exception nested) {
198                 // ignore
199             }
200         } catch (Exception exception) {
201             // ignore for the moment
202         } finally {
203             if (connection != null) {
204                 try {
205                     connection.close();
206                 } catch (Exception nested) {
207                     // ignore
208                 }
209             }
210         }
211 
212         return result;
213     }
214 
215     // implementation of AbstractAdminConnection.unregisterConsumer
216     public boolean unregisterConsumer(String name) {
217         return false;
218     }
219 
220     // implementation of AbstractAdminConnection.isConnected
221     public boolean isConnected(String name) {
222         return false;
223     }
224 
225     // implementation of AbstractAdminConnection.getAllDestinations
226     public Enumeration getAllDestinations() {
227         Enumeration result = null;
228         Connection connection = null;
229 
230         try {
231             connection = DatabaseService.getConnection();
232 
233             result = DatabaseService.getAdapter().getAllDestinations(
234                 connection);
235             connection.commit();
236         } catch (PersistenceException exception) {
237             try {
238                 connection.rollback();
239             } catch (Exception nested) {
240                 // ignore
241             }
242         } catch (Exception exception) {
243             // ignore for the moment
244         } finally {
245             if (connection != null) {
246                 try {
247                     connection.close();
248                 } catch (Exception nested) {
249                     // ignore
250                 }
251             }
252         }
253 
254         return result;
255     }
256 
257     // implementation of AbstractAdminConnection.addDestination
258     public boolean addDestination(String destination, boolean isQueue) {
259         Connection connection = null;
260         boolean success = false;
261 
262         try {
263             connection = DatabaseService.getConnection();
264 
265             DatabaseService.getAdapter().addDestination(connection,
266                 destination, isQueue);
267             if (_context == null) {
268                 // connect to the JNDI server and get a reference to
269                 // root context
270                 Hashtable props = new Hashtable();
271                 props.put(Context.INITIAL_CONTEXT_FACTORY,
272                     "org.exolab.jms.jndi.intravm.IntravmJndiServer");
273                 _context = new InitialContext(props);
274             }
275 
276             Object ob = (isQueue ? (Object) (new JmsQueue(destination)) :
277                 (Object) (new JmsTopic(destination)));
278 
279             if (ob instanceof JmsQueue) {
280                 ((JmsDestination) ob).setPersistent(true);
281             }
282 
283             _context.rebind(destination, ob);
284             connection.commit();
285             success = true;
286         } catch (PersistenceException exception) {
287             System.err.println("Failed to add destination " + destination +
288                 " b/c " + exception.toString());
289             try {
290                 connection.rollback();
291             } catch (Exception nested) {
292                 // ignore
293             }
294         } catch (javax.naming.NamingException err) {
295             System.err.println("Failed to add " + destination +
296                 " in JNDI context");
297             try {
298                 connection.rollback();
299             } catch (Exception nested) {
300                 // ignore
301             }
302         } catch (Exception exception) {
303             // ignore for the moment
304         } finally {
305             if (connection != null) {
306                 try {
307                     connection.close();
308                 } catch (Exception nested) {
309                     // ignore
310                 }
311             }
312         }
313 
314         return success;
315     }
316 
317     // implementation of AbstractAdminConnection.getDurableConsumerMessageCount
318     public int getDurableConsumerMessageCount(String topic, String name) {
319         int count = -1;
320         Connection connection = null;
321 
322         try {
323             connection = DatabaseService.getConnection();
324 
325             count = DatabaseService.getAdapter().getDurableConsumerMessageCount(
326                 connection, topic, name);
327             connection.commit();
328         } catch (PersistenceException exception) {
329             System.err.println("Failed to get message count for " + topic +
330                 " b/c " + exception.toString());
331             try {
332                 connection.rollback();
333             } catch (Exception nested) {
334                 // ignore
335             }
336         } catch (Exception exception) {
337             // ignore for the moment
338         } finally {
339             if (connection != null) {
340                 try {
341                     connection.close();
342                 } catch (Exception nested) {
343                     // ignore
344                 }
345             }
346         }
347 
348         return count;
349     }
350 
351     // implementation of AbstractAdminConnection.getQueueMessageCount
352     public int getQueueMessageCount(String queue) {
353         int count = -1;
354         Connection connection = null;
355 
356         try {
357             connection = DatabaseService.getConnection();
358 
359             count = DatabaseService.getAdapter().getQueueMessageCount(
360                 connection, queue);
361             connection.commit();
362         } catch (PersistenceException exception) {
363             System.err.println("Failed to get message count for " + queue +
364                 " b/c " + exception.toString());
365             try {
366                 connection.rollback();
367             } catch (Exception nested) {
368                 // ignore
369             }
370         } catch (Exception exception) {
371             // ignore for the moment
372         } finally {
373             if (connection != null) {
374                 try {
375                     connection.close();
376                 } catch (Exception nested) {
377                     // ignore
378                 }
379             }
380         }
381 
382         return count;
383     }
384 
385     // implementation of AbstractAdminConnection.durableConsumerExists
386     public boolean durableConsumerExists(String name) {
387         boolean result = false;
388         Connection connection = null;
389 
390         try {
391             connection = DatabaseService.getConnection();
392 
393             result = DatabaseService.getAdapter().durableConsumerExists(
394                 connection, name);
395             connection.commit();
396         } catch (PersistenceException exception) {
397             System.err.println("Failed on consumer exists for " + name +
398                 " b/c " + exception.toString());
399             try {
400                 connection.rollback();
401             } catch (Exception nested) {
402                 // ignore
403             }
404         } catch (Exception exception) {
405             // ignore for the moment
406         } finally {
407             if (connection != null) {
408                 try {
409                     connection.close();
410                 } catch (Exception nested) {
411                     // ignore
412                 }
413             }
414         }
415 
416         return result;
417     }
418 
419     // implementation of AbstractAdminConnection.getDurableConsumers
420     public Enumeration getDurableConsumers(String topic) {
421         Enumeration result = null;
422         Connection connection = null;
423 
424         try {
425             connection = DatabaseService.getConnection();
426 
427             result = DatabaseService.getAdapter().getDurableConsumers(
428                 connection, topic);
429             connection.commit();
430         } catch (PersistenceException exception) {
431             System.err.println("Failed on getDurableConsumers for " + topic +
432                 " b/c " + exception.toString());
433             try {
434                 connection.rollback();
435             } catch (Exception nested) {
436                 // ignore
437             }
438         } catch (Exception exception) {
439             // ignore for the moment
440         } finally {
441             if (connection != null) {
442                 try {
443                     connection.close();
444                 } catch (Exception nested) {
445                     // ignore
446                 }
447             }
448         }
449 
450         return result;
451     }
452 
453     // implementation of AbstractAdminConnection.removeDestination
454     public boolean removeDestination(String destination) {
455         boolean result = false;
456         Connection connection = null;
457 
458         try {
459             connection = DatabaseService.getConnection();
460 
461             DatabaseService.getAdapter().removeDestination(connection,
462                 destination);
463             if (_context == null) {
464                 // connect to the JNDI server and get a reference to
465                 // root context
466                 Hashtable props = new Hashtable();
467                 props.put(Context.INITIAL_CONTEXT_FACTORY,
468                     "org.exolab.jms.jndi.intravm.IntravmJndiServer");
469                 _context = new InitialContext(props);
470             }
471             _context.unbind(destination);
472             connection.commit();
473             result = true;
474         } catch (PersistenceException exception) {
475             System.err.println("Failed on getDurableConsumers for " +
476                 destination + " b/c " + exception.toString());
477             try {
478                 connection.rollback();
479             } catch (Exception nested) {
480                 // ignore
481             }
482         } catch (javax.naming.NamingException err) {
483             System.err.println("Failed to removeDestination " + destination +
484                 " b/c" + err.toString());
485             try {
486                 connection.rollback();
487             } catch (Exception nested) {
488                 // ignore
489             }
490         } catch (Exception exception) {
491             // ignore for the moment
492         } finally {
493             if (connection != null) {
494                 try {
495                     connection.close();
496                 } catch (Exception nested) {
497                     // ignore
498                 }
499             }
500         }
501 
502         return result;
503     }
504 
505     // implementation of AbstractAdminConnection.purgeMessages
506     public int purgeMessages() {
507         return DatabaseService.getAdapter().purgeMessages();
508     }
509 
510     // implementation of AbstractAdminConnection.stopServer
511     public void stopServer() {
512         JOptionPane.showMessageDialog
513             (_parent, "Not available in offline mode",
514                 "Shutdown Error", JOptionPane.ERROR_MESSAGE);
515     }
516 
517     // implementation of AbstractAdminConnection.addUser
518     public boolean addUser(String username, String password) {
519         Connection connection = null;
520         boolean success = false;
521 
522         try {
523             connection = DatabaseService.getConnection();
524 
525             DatabaseService.getAdapter().addUser(connection,
526                 new User(username, password));
527             connection.commit();
528             success = true;
529         } catch (PersistenceException exception) {
530             System.err.println("Failed to add user " + username +
531                 exception.toString());
532             try {
533                 connection.rollback();
534             } catch (Exception nested) {
535                 // ignore
536             }
537         } catch (Exception exception) {
538             // ignore for the moment
539         } finally {
540             if (connection != null) {
541                 try {
542                     connection.close();
543                 } catch (Exception nested) {
544                     // ignore
545                 }
546             }
547         }
548 
549         return success;
550     }
551 
552     // implementation of AbstractAdminConnection.changePassord
553     public boolean changePassword(String username, String password) {
554         Connection connection = null;
555         boolean success = false;
556 
557         try {
558             connection = DatabaseService.getConnection();
559 
560             DatabaseService.getAdapter().updateUser(connection,
561                 new User(username, password));
562             connection.commit();
563             success = true;
564         } catch (PersistenceException exception) {
565             System.err.println("Failed to add user " + username +
566                 exception.toString());
567             try {
568                 connection.rollback();
569             } catch (Exception nested) {
570                 // ignore
571             }
572         } catch (Exception exception) {
573             // ignore for the moment
574         } finally {
575             if (connection != null) {
576                 try {
577                     connection.close();
578                 } catch (Exception nested) {
579                     // ignore
580                 }
581             }
582         }
583 
584         return success;
585     }
586 
587     // implementation of AbstractAdminConnection.removeUser
588     public boolean removeUser(String username) {
589         boolean result = false;
590         Connection connection = null;
591 
592         try {
593             connection = DatabaseService.getConnection();
594 
595             DatabaseService.getAdapter().removeUser(connection,
596                 new User(username, null));
597             connection.commit();
598             result = true;
599         } catch (PersistenceException exception) {
600             System.err.println("Failed on remove user for " +
601                 username + exception.toString());
602             try {
603                 connection.rollback();
604             } catch (Exception nested) {
605                 // ignore
606             }
607         } catch (Exception exception) {
608             // ignore for the moment
609         } finally {
610             if (connection != null) {
611                 try {
612                     connection.close();
613                 } catch (Exception nested) {
614                     // ignore
615                 }
616             }
617         }
618 
619         return result;
620     }
621 
622     // implementation of AbstractAdminConnection.getAllUsers
623     public Enumeration getAllUsers() {
624         Enumeration result = null;
625         Connection connection = null;
626 
627         try {
628             connection = DatabaseService.getConnection();
629 
630             result = DatabaseService.getAdapter().getAllUsers(
631                 connection);
632             connection.commit();
633         } catch (PersistenceException exception) {
634             System.err.println("Failed on getAllUsers "
635                 + exception.toString());
636             try {
637                 connection.rollback();
638             } catch (Exception nested) {
639                 // ignore
640             }
641         } catch (Exception exception) {
642             // ignore for the moment
643         } finally {
644             if (connection != null) {
645                 try {
646                     connection.close();
647                 } catch (Exception nested) {
648                     // ignore
649                 }
650             }
651         }
652 
653         return result;
654     }
655 } //-- OfflineConnection