1   /*
2    *  DataStoreRegister.java
3    *
4    *  Copyright (c) 1998-2001, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Hamish Cunningham, 23/Jan/2001
12   *
13   *  $Id: DataStoreRegister.java,v 1.16 2001/11/08 14:29:46 hamish Exp $
14   */
15  
16  package gate;
17  
18  import java.util.*;
19  import java.net.*;
20  
21  import gate.util.*;
22  import gate.persist.*;
23  import gate.event.*;
24  
25  /** Records all the open DataStores.
26    */
27  public class DataStoreRegister extends HashSet {
28  
29    /** All the DataStore classes available. This is a map of class name
30     *  to descriptive text.
31     */
32    public static Map getDataStoreClassNames() {
33      Map names = new HashMap();
34  
35  // no plugability here at present.... at some future point there should
36  // be a capability to add new data store classes via creole.xml metadata
37  // and resource jars
38      names.put(
39        "gate.persist.SerialDataStore",
40        "SerialDataStore: file-based storage using Java serialisation"
41      );
42  //    names.put(
43  //      "gate.persist.JdbcDataStore",
44  //      "JdbcDataStore: generic RDBMS storage over JDBC"
45  //    );
46      names.put(
47        "gate.persist.OracleDataStore",
48        "OracleDataStore: Oracle-specific RDBMS storage over JDBC"
49      );
50  
51      return names;
52    } // getDataStoreClassNames()
53  
54    /**
55     * Adds the specified element to this set if it is not already
56     * present. Overriden here for event registration code.
57     */
58    public boolean add(Object o) {
59      return super.add(o);
60    } // add
61  
62    /**
63     * Removes the given element from this set if it is present.
64     * Overriden here for event registration code.
65     */
66    public boolean remove(Object o) {
67      boolean res = super.remove(o);
68      if(res) {
69        fireDatastoreClosed(
70          new CreoleEvent((DataStore)o, CreoleEvent.DATASTORE_CLOSED)
71        );
72        removeSecurityData((DataStore) o);
73      }
74      return res;
75    } // remove
76  
77    /**
78     * Removes all of the elements from this set.
79     * Overriden here for event registration code.
80     */
81    public void clear() {
82      Set datastores = new HashSet(this);
83      super.clear();
84  
85      Iterator iter = datastores.iterator();
86      while(iter.hasNext()) {
87        fireDatastoreClosed(
88          new CreoleEvent((DataStore) iter.next(), CreoleEvent.DATASTORE_CLOSED)
89        );
90      } // while
91    }   // clear()
92  
93    /** Configuration data such as driver names. */
94    private static Map configData = new HashMap();
95  
96    /** Get the configuration data map. */
97    public static Map getConfigData() { return configData; }
98  
99    /**
100    * Adds configuration data (e.g. from <TT>gate.xml</TT> files) to
101    * the register. New key/value pairs are added to the existing
102    * set (this will overwrite existing pairs whose keys match new ones).
103    */
104   public static void addConfig(Map configData) {
105     DataStoreRegister.configData.putAll(configData);
106   } // addConfig
107 
108   /** A hashmap from datastore to security data (current user and group)*/
109   private static Map securityData = new HashMap();
110 
111   /**
112    * Returns the security data for this datastore
113    */
114   public static Map getSecurityData(DataStore ds) {
115     return (Map) securityData.get(ds);
116   } //
117 
118   /**
119    * Adds security data for this datastore
120    */
121   public static void addSecurityData(DataStore ds, Map secData){
122     DataStoreRegister.securityData.put(ds, secData);
123   }
124 
125   /**
126    * Removes the security data for this datastore
127    */
128   public static void removeSecurityData(DataStore ds){
129     DataStoreRegister.securityData.remove(ds);
130   }
131 
132   /**
133    * Removes a previously registered {@link gate.event.CreoleListener}
134    * from the list of listeners for this DataStoreRegister.
135    * Normally the only listener that is registered with the DataStoreRegister
136    * is the {@link CreoleRegister} which can be obtained through
137    * {@link Gate#getCreoleRegister()}
138    */
139   public synchronized void removeCreoleListener(CreoleListener l) {
140     if (creoleListeners != null && creoleListeners.contains(l)) {
141       Vector v = (Vector) creoleListeners.clone();
142       v.removeElement(l);
143       creoleListeners = v;
144     }
145   } // removeCreoleListener(CreoleListener l)
146 
147   /**
148    * Registers a new {@link gate.event.CreoleListener} with this
149    * DataStoreRegister. Normally the only listener that is
150    * registered with the DataStoreRegister is the {@link CreoleRegister}
151    * which can be obtained through {@link Gate#getCreoleRegister()}
152    */
153   public synchronized void addCreoleListener(CreoleListener l) {
154     Vector v =
155       creoleListeners ==
156         null ? new Vector(2) : (Vector) creoleListeners.clone();
157     if (!v.contains(l)) {
158       v.addElement(l);
159       creoleListeners = v;
160     }// if
161   }// addCreoleListener(CreoleListener l)
162 
163   /**
164    * Notifies all registered {@link gate.event.CreoleListener}s that a
165    * {@link DataStore} has been opened. Normally the only listener that is
166    * registered with the DataStoreRegister is the {@link CreoleRegister}
167    * which can be obtained through {@link Gate#getCreoleRegister()}
168    */
169   protected void fireDatastoreOpened(CreoleEvent e) {
170     if (creoleListeners != null) {
171       Vector listeners = creoleListeners;
172       int count = listeners.size();
173       for (int i = 0; i < count; i++) {
174         ((CreoleListener) listeners.elementAt(i)).datastoreOpened(e);
175       } // for
176     }   // if
177   }     // fireDatastoreOpened(CreoleEvent e)
178 
179   /**
180    * Notifies all registered {@link gate.event.CreoleListener}s that a new
181    * {@link DataStore} has been created. Normally the only listener that is
182    * registered with the DataStoreRegister is the {@link CreoleRegister}
183    * which can be obtained through {@link Gate#getCreoleRegister()}
184    */
185   protected void fireDatastoreCreated(CreoleEvent e) {
186     if (creoleListeners != null ) {
187       Vector listeners = creoleListeners;
188       int count = listeners.size();
189       for (int i = 0; i < count; i++) {
190         ((CreoleListener) listeners.elementAt(i)).datastoreCreated(e);
191       } // for
192     }   // if
193   } // fireDatastoreCreated(CreoleEvent e)
194 
195   /**
196    * Notifies all registered {@link gate.event.CreoleListener}s that a
197    * {@link DataStore} has been closed. Normally the only listener that is
198    * registered with the DataStoreRegister is the {@link CreoleRegister}
199    * which can be obtained through {@link Gate#getCreoleRegister()}
200    */
201   protected void fireDatastoreClosed(CreoleEvent e) {
202     if (creoleListeners != null) {
203       Vector listeners = creoleListeners;
204       int count = listeners.size();
205       for (int i = 0; i < count; i++) {
206         ((CreoleListener) listeners.elementAt(i)).datastoreClosed(e);
207       } // for
208     }   // if
209   } // fireDatastoreClosed(CreoleEvent e)
210 
211   /** */
212   private transient Vector creoleListeners;
213 
214 } // class DataStoreRegister
215