DataConnection.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DataConnection.java,v 1.3 2005/10/10 17:01:00 rbair Exp $
  3.  *
  4.  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.dataset;
  22. import java.beans.PropertyChangeListener;
  23. import java.beans.PropertyChangeSupport;
  24. import java.io.IOException;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. /**
  28.  * Maintains a connection to some underlying data store. Generally used by
  29.  * a DataProvider to interact with the DataStore, however, it is not specifically
  30.  * necessary for a DataProvider to use a DataConnection.
  31.  *
  32.  * @author rbair
  33.  */
  34. public abstract class DataConnection {
  35. /**
  36.  * Logger used for log statements
  37.  */
  38. private static final Logger LOG = Logger.getLogger(DataConnection.class.getName());
  39. /**
  40.      * Helper used for notifying of bean property changes. In particular, this
  41.      * is used to notify of changes in the connection status
  42.      */
  43.     private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
  44.     /**
  45.      * Flag indicating the logical connection status of this DataConnection.
  46.      * It is possible for the DataConnection to be logically connected but
  47.      * physically disconnected (ie HTTP), or vice versa.
  48.      */
  49.     private boolean connected = false;
  50.     
  51.     /**
  52.      * Manages the connected state of this DataConnection. The connected
  53.      * state is this DataConnections "logical" connection status. The
  54.      * underlying connection may be disconnected, and yet the
  55.      * DataConnection may still be "logically" connected. For example, an
  56.      * HTTP based connection may not be persistent, but is always "logically"
  57.      * connected.
  58.      * <p/>
  59.      * It is possible for listeners to be attached to the DataConnection that
  60.      * can react to the connection status. For instance, a status bar icon might
  61.      * change to indicate its disconnected status. Or DataSets could be flushed
  62.      * of their data.
  63.      * @param b If true, opens any necessary connections to the source.
  64.      *          For instance, this could be used to open a connection to a
  65.      *          database, or to a URL. If false, then any current connections
  66.      *          are logically closed, and may be physically closed, depending
  67.      *          on the DataConnection implementation
  68.      * @throws IOException
  69.      */
  70.     public void setConnected(boolean b) {
  71.         if (b && !connected) {
  72.             try {
  73.              LOG.fine("Attempting to connect to the data store");
  74.                 connect();
  75.                 connected = true;
  76.                 LOG.fine("Connected to the data store, firing property change event");
  77.                 pcs.firePropertyChange("connected", false, true);
  78.             } catch (Exception e) {
  79.              LOG.log(Level.SEVERE, "Failed to connect to the data store.", e);
  80.              //no need to fire a property change event here, because connected was
  81.              //false when this method began
  82.                 connected = false;
  83.             }
  84.         } else if (!b && connected) {
  85.             try {
  86.              LOG.fine("Attempting to disconnect from the data store");
  87.                 disconnect();
  88.             } catch (Exception e) {
  89.              LOG.log(Level.WARNING, "Failed to physically disconnect from the data store, " +
  90.              "but will continue to logically disconnect from the data store.", e);
  91.                 e.printStackTrace();
  92.             } finally {
  93.                 connected = false;
  94.                 LOG.fine("Logically disconnected from the data store, firing property change event");
  95.                 pcs.firePropertyChange("connected", true, false);
  96.             }
  97.         }
  98.     }
  99.     
  100.     /**
  101.      * @return whether the DataStoreConnection is logically connected or not.
  102.      */
  103.     public boolean isConnected() {
  104.         return connected;
  105.     }
  106.     
  107.     /**
  108.      * Optional method to make a connection to the data store. This method is
  109.      * called whenever the connection state changes from !connected to
  110.      * connected. It is up to the child class implementation to manage the
  111.      * physical connection to the data store. This method may or may not be
  112.      * useful in that context.
  113.      * 
  114.      * @throws Exception
  115.      */
  116.     protected abstract void connect() throws Exception;
  117.     /**
  118.      * Optional method to disconnect from the data store. This method is called
  119.      * whenever the connection state changes from connected to !connected. It is
  120.      * up to the child class implementation to manage the physical connection to
  121.      * the data store. This method may or may not be useful in that context.
  122.      * 
  123.      * @throws Exception
  124.      */
  125.     protected abstract void disconnect() throws Exception;
  126.     
  127.     /**
  128.      * Adds a PropertyChangeListener to this class for any changes to bean 
  129.      * properties.
  130.      *
  131.      * @param listener The PropertyChangeListener to notify of changes to this 
  132.      * instance.
  133.      */
  134.     public void addPropertyChangeListener(PropertyChangeListener listener) {
  135.         pcs.addPropertyChangeListener(listener);
  136.     }
  137.    /**
  138.      * Adds a PropertyChangeListener to this class for specific property changes.
  139.      *
  140.      * @param propertyName The name of the property to listen to changes for.
  141.      * @param listener The PropertyChangeListener to notify of changes to this 
  142.      * instance.
  143.      */
  144.      public void addPropertyChangeListener(String propertyName,
  145.             PropertyChangeListener listener) {
  146.         pcs.addPropertyChangeListener(propertyName, listener);
  147.     }
  148.     
  149.     /**
  150.      * Stops notifying a specific listener of changes to a specific property.
  151.      *
  152.      * @param propertyName The name of the property to ignore from now on.
  153.      * @param listener The listener to stop receiving notifications.
  154.      */
  155.     public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
  156.      pcs.removePropertyChangeListener(propertyName, listener);
  157.     }
  158. }