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: OfflineConnection.java,v 1.11 2003/08/17 01:32:23 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * $Date	    jimm    Created
47   */
48  
49  
50  package org.exolab.jms.jndiadministration;
51  
52  import java.awt.Component;
53  import java.util.Enumeration;
54  import java.util.Hashtable;
55  
56  import javax.naming.Context;
57  import javax.naming.InitialContext;
58  import javax.swing.JFileChooser;
59  import javax.swing.JOptionPane;
60  
61  import org.exolab.jms.config.Configuration;
62  import org.exolab.jms.config.ConfigurationManager;
63  import org.exolab.jms.config.DatabaseConfiguration;
64  import org.exolab.jms.persistence.DatabaseService;
65  
66  
67  /***
68   * Use the IntraVM Jndi Context.
69   *
70   * @version     $Revision: 1.11 $ $Date: 2003/08/17 01:32:23 $
71   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
72   * @see		AdminConnection
73   * @see		AdminMgr
74   */
75  public class OfflineConnection extends AdminConnection {
76  
77      // The parent Gui
78      private Component parent_;
79  
80      // The deafult JNDI context.
81      private Context context_ = null;
82  
83      /***
84       * Connect to the intravm Jndi Server.
85       *
86       * @param parent The component parent.
87       * @exception OfflineConnectionException When the database cannot be opened
88       *
89       */
90      public OfflineConnection(Component parent)
91          throws OfflineConnectionException {
92          try {
93              if (instance_ == null) {
94                  Configuration config = ConfigurationManager.getConfig();
95                  parent_ = parent;
96                  DatabaseConfiguration dbconfig =
97                      config.getDatabaseConfiguration();
98  
99                  if (dbconfig.getRdbmsDatabaseConfiguration() != null) {
100                     DatabaseService.getAdapter();
101                     instance_ = this;
102                 } else {
103                     JFileChooser chooser = new JFileChooser(".");
104                     chooser.setDialogTitle
105                         ("Select OpenJMS Database to connect to");
106                     chooser.setFileFilter(new DatabaseFilter());
107                     int returnVal = chooser.showOpenDialog(parent);
108 
109                     if (returnVal == JFileChooser.APPROVE_OPTION) {
110                         DatabaseService.getAdapter();
111                         instance_ = this;
112                     }
113                 }
114 
115                 if (instance_ != null) {
116                     if (context_ == null) {
117                         // connect to the JNDI server and get a
118                         // reference to root context
119                         Hashtable props = new Hashtable();
120                         props.put
121                             (Context.INITIAL_CONTEXT_FACTORY,
122                                 "org.exolab.jms.jndi.intravm.IntravmJndiServer");
123                         context_ = new InitialContext(props);
124                     }
125                 }
126             } else {
127                 throw new OfflineConnectionException("Already connected");
128             }
129         } catch (Exception err) {
130             throw new OfflineConnectionException
131                 ("Database Error: " + err.getMessage());
132         }
133     }
134 
135 
136     /***
137      * Close the database connection.
138      *
139      */
140     public void close() {
141         try {
142             context_.close();
143             DatabaseService.getAdapter().close();
144             instance_ = null;
145             context_ = null;
146         } catch (javax.naming.NamingException err) {
147             displayError(err, "Failed to close context");
148         }
149     }
150 
151 
152     /***
153      * Get an enumerated list of all the Contexts
154      *
155      * @return 	Enumeration The list of Contexts
156      *
157      */
158     public Enumeration getAllContexts(String name) {
159         try {
160             if (name == null) {
161                 name = "";
162             }
163 
164             return context_.list(name);
165         } catch (Exception err) {
166             System.err.println("Err in getAllContexts\n" + err);
167             return null;
168         }
169     }
170 
171 
172     /***
173      * Return the object associated with this context.
174      * if none exists return null.
175      *
176      * @param context The context name
177      * @return Object The object for this context.
178      *
179      */
180     public Object lookup(String context) {
181         try {
182             return context_.lookup(context);
183         } catch (Exception err) {
184             System.err.println("Failed to get Context " + context + "\n" +
185                 err);
186             return null;
187         }
188     }
189 
190 
191     /***
192      * Create a new context with the given name.
193      *
194      * @param name The new context name.
195      * @exception NamingException If the context cannot be created.
196      *
197      */
198     public void createContext(String name) throws javax.naming.NamingException {
199         context_.createSubcontext(name);
200     }
201 
202 
203     /***
204      * Destroy context with the given name.
205      *
206      * @param name The new context name.
207      * @exception NamingException If the context cannot be created.
208      *
209      */
210     public void destroyContext(String name) throws javax.naming.NamingException {
211         context_.unbind(name);
212     }
213 
214 
215     /***
216      * Rename context with the given name.
217      *
218      * @param oldName The old context name
219      * @param newName The new context name
220      * @exception NamingException If the context cannot be created.
221      *
222      */
223     public void renameContext(String oldName, String newName)
224         throws javax.naming.NamingException {
225         context_.rename(oldName, newName);
226     }
227 
228 
229     /***
230      * Rebind the context with the given object.
231      *
232      * @param name The context name
233      * @param ob The object to bind in this context
234      * @exception NamingException If the context cannot be created.
235      *
236      */
237     public void rebind(String name, Object ob)
238         throws javax.naming.NamingException {
239         context_.rebind(name, ob);
240     }
241 
242 
243     /***
244      * Display the error in a JOptionPane.
245      *
246      * @param err The Error to display.
247      * @param st The string to use as a title on the JOptionPane.
248      *
249      */
250     private void displayError(Exception err, String st) {
251         JOptionPane.showMessageDialog
252             (parent_, st + "\n" + err, st, JOptionPane.ERROR_MESSAGE);
253     }
254 
255 
256 } // End OfflineConnection