KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > dataset > DataConnection


1 /*
2  * $Id: DataConnection.java,v 1.1 2005/02/27 00:24:55 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.dataset;
9
10 import java.beans.PropertyChangeListener JavaDoc;
11 import java.beans.PropertyChangeSupport JavaDoc;
12 import java.util.logging.Level JavaDoc;
13 import java.util.logging.Logger JavaDoc;
14
15 /**
16  * Maintains a connection to some underlying data store. Generally used by
17  * a DataProvider to interact with the DataStore, however, it is not specifically
18  * necessary for a DataProvider to use a DataConnection.
19  *
20  * @author rbair
21  */

22 public abstract class DataConnection {
23     /**
24      * Logger used for log statements
25      */

26     private static final Logger JavaDoc LOG = Logger.getLogger(DataConnection.class.getName());
27
28     /**
29      * Helper used for notifying of bean property changes. In particular, this
30      * is used to notify of changes in the connection status
31      */

32     private PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
33
34     /**
35      * Flag indicating the logical connection status of this DataConnection.
36      * It is possible for the DataConnection to be logically connected but
37      * physically disconnected (ie HTTP), or vice versa.
38      */

39     private boolean connected = false;
40     
41     /**
42      * Manages the connected state of this DataConnection. The connected
43      * state is this DataConnections "logical" connection status. The
44      * underlying connection may be disconnected, and yet the
45      * DataConnection may still be "logically" connected. For example, an
46      * HTTP based connection may not be persistent, but is always "logically"
47      * connected.
48      * <p/>
49      * It is possible for listeners to be attached to the DataConnection that
50      * can react to the connection status. For instance, a status bar icon might
51      * change to indicate its disconnected status. Or DataSets could be flushed
52      * of their data.
53      * @param b If true, opens any necessary connections to the source.
54      * For instance, this could be used to open a connection to a
55      * database, or to a URL. If false, then any current connections
56      * are logically closed, and may be physically closed, depending
57      * on the DataConnection implementation
58      * @throws IOException
59      */

60     public void setConnected(boolean b) {
61         if (b && !connected) {
62             try {
63                 LOG.fine("Attempting to connect to the data store");
64                 connect();
65                 connected = true;
66                 LOG.fine("Connected to the data store, firing property change event");
67                 pcs.firePropertyChange("connected", false, true);
68             } catch (Exception JavaDoc e) {
69                 LOG.log(Level.SEVERE, "Failed to connect to the data store.", e);
70                 //no need to fire a property change event here, because connected was
71
//false when this method began
72
connected = false;
73             }
74         } else if (!b && connected) {
75             try {
76                 LOG.fine("Attempting to disconnect from the data store");
77                 disconnect();
78             } catch (Exception JavaDoc e) {
79                 LOG.log(Level.WARNING, "Failed to physically disconnect from the data store, " +
80                         "but will continue to logically disconnect from the data store.", e);
81                 e.printStackTrace();
82             } finally {
83                 connected = false;
84                 LOG.fine("Logically disconnected from the data store, firing property change event");
85                 pcs.firePropertyChange("connected", true, false);
86             }
87         }
88     }
89     
90     /**
91      * @return whether the DataStoreConnection is logically connected or not.
92      */

93     public boolean isConnected() {
94         return connected;
95     }
96     
97     /**
98      * Optional method to make a connection to the data store. This method is
99      * called whenever the connection state changes from !connected to
100      * connected. It is up to the child class implementation to manage the
101      * physical connection to the data store. This method may or may not be
102      * useful in that context.
103      *
104      * @throws Exception
105      */

106     protected abstract void connect() throws Exception JavaDoc;
107
108     /**
109      * Optional method to disconnect from the data store. This method is called
110      * whenever the connection state changes from connected to !connected. It is
111      * up to the child class implementation to manage the physical connection to
112      * the data store. This method may or may not be useful in that context.
113      *
114      * @throws Exception
115      */

116     protected abstract void disconnect() throws Exception JavaDoc;
117     
118     /**
119      *
120      * @param listener
121      */

122     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
123         pcs.addPropertyChangeListener(listener);
124     }
125
126     /**
127      *
128      * @param propertyName
129      * @param listener
130      */

131     public void addPropertyChangeListener(String JavaDoc propertyName,
132             PropertyChangeListener JavaDoc listener) {
133         pcs.addPropertyChangeListener(propertyName, listener);
134     }
135     
136     public void removePropertyChangeListener(String JavaDoc propertyName, PropertyChangeListener JavaDoc listener) {
137         pcs.removePropertyChangeListener(propertyName, listener);
138     }
139
140 }
Popular Tags