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.17 2002/04/08 13:19:42 marin 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  
39      //Oracle
40      names.put(
41        "gate.persist.OracleDataStore",
42        "OracleDataStore: Oracle-specific RDBMS storage over JDBC"
43      );
44  
45  
46      //Postgres
47      names.put(
48        "gate.persist.PostgresDataStore",
49        "PostgresDataStore: PostgreSQL-specific RDBMS storage over JDBC"
50      );
51  
52      //filesystem
53      names.put(
54        "gate.persist.SerialDataStore",
55        "SerialDataStore: file-based storage using Java serialisation"
56      );
57  
58  
59  
60      return names;
61    } // getDataStoreClassNames()
62  
63    /**
64     * Adds the specified element to this set if it is not already
65     * present. Overriden here for event registration code.
66     */
67    public boolean add(Object o) {
68      return super.add(o);
69    } // add
70  
71    /**
72     * Removes the given element from this set if it is present.
73     * Overriden here for event registration code.
74     */
75    public boolean remove(Object o) {
76      boolean res = super.remove(o);
77      if(res) {
78        fireDatastoreClosed(
79          new CreoleEvent((DataStore)o, CreoleEvent.DATASTORE_CLOSED)
80        );
81        removeSecurityData((DataStore) o);
82      }
83      return res;
84    } // remove
85  
86    /**
87     * Removes all of the elements from this set.
88     * Overriden here for event registration code.
89     */
90    public void clear() {
91      Set datastores = new HashSet(this);
92      super.clear();
93  
94      Iterator iter = datastores.iterator();
95      while(iter.hasNext()) {
96        fireDatastoreClosed(
97          new CreoleEvent((DataStore) iter.next(), CreoleEvent.DATASTORE_CLOSED)
98        );
99      } // while
100   }   // clear()
101 
102   /** Configuration data such as driver names. */
103   private static Map configData = new HashMap();
104 
105   /** Get the configuration data map. */
106   public static Map getConfigData() { return configData; }
107 
108   /**
109    * Adds configuration data (e.g. from <TT>gate.xml</TT> files) to
110    * the register. New key/value pairs are added to the existing
111    * set (this will overwrite existing pairs whose keys match new ones).
112    */
113   public static void addConfig(Map configData) {
114     DataStoreRegister.configData.putAll(configData);
115   } // addConfig
116 
117   /** A hashmap from datastore to security data (current user and group)*/
118   private static Map securityData = new HashMap();
119 
120   /**
121    * Returns the security data for this datastore
122    */
123   public static Map getSecurityData(DataStore ds) {
124     return (Map) securityData.get(ds);
125   } //
126 
127   /**
128    * Adds security data for this datastore
129    */
130   public static void addSecurityData(DataStore ds, Map secData){
131     DataStoreRegister.securityData.put(ds, secData);
132   }
133 
134   /**
135    * Removes the security data for this datastore
136    */
137   public static void removeSecurityData(DataStore ds){
138     DataStoreRegister.securityData.remove(ds);
139   }
140 
141   /**
142    * Removes a previously registered {@link gate.event.CreoleListener}
143    * from the list of listeners for this DataStoreRegister.
144    * Normally the only listener that is registered with the DataStoreRegister
145    * is the {@link CreoleRegister} which can be obtained through
146    * {@link Gate#getCreoleRegister()}
147    */
148   public synchronized void removeCreoleListener(CreoleListener l) {
149     if (creoleListeners != null && creoleListeners.contains(l)) {
150       Vector v = (Vector) creoleListeners.clone();
151       v.removeElement(l);
152       creoleListeners = v;
153     }
154   } // removeCreoleListener(CreoleListener l)
155 
156   /**
157    * Registers a new {@link gate.event.CreoleListener} with this
158    * DataStoreRegister. Normally the only listener that is
159    * registered with the DataStoreRegister is the {@link CreoleRegister}
160    * which can be obtained through {@link Gate#getCreoleRegister()}
161    */
162   public synchronized void addCreoleListener(CreoleListener l) {
163     Vector v =
164       creoleListeners ==
165         null ? new Vector(2) : (Vector) creoleListeners.clone();
166     if (!v.contains(l)) {
167       v.addElement(l);
168       creoleListeners = v;
169     }// if
170   }// addCreoleListener(CreoleListener l)
171 
172   /**
173    * Notifies all registered {@link gate.event.CreoleListener}s that a
174    * {@link DataStore} has been opened. Normally the only listener that is
175    * registered with the DataStoreRegister is the {@link CreoleRegister}
176    * which can be obtained through {@link Gate#getCreoleRegister()}
177    */
178   protected void fireDatastoreOpened(CreoleEvent e) {
179     if (creoleListeners != null) {
180       Vector listeners = creoleListeners;
181       int count = listeners.size();
182       for (int i = 0; i < count; i++) {
183         ((CreoleListener) listeners.elementAt(i)).datastoreOpened(e);
184       } // for
185     }   // if
186   }     // fireDatastoreOpened(CreoleEvent e)
187 
188   /**
189    * Notifies all registered {@link gate.event.CreoleListener}s that a new
190    * {@link DataStore} has been created. Normally the only listener that is
191    * registered with the DataStoreRegister is the {@link CreoleRegister}
192    * which can be obtained through {@link Gate#getCreoleRegister()}
193    */
194   protected void fireDatastoreCreated(CreoleEvent e) {
195     if (creoleListeners != null ) {
196       Vector listeners = creoleListeners;
197       int count = listeners.size();
198       for (int i = 0; i < count; i++) {
199         ((CreoleListener) listeners.elementAt(i)).datastoreCreated(e);
200       } // for
201     }   // if
202   } // fireDatastoreCreated(CreoleEvent e)
203 
204   /**
205    * Notifies all registered {@link gate.event.CreoleListener}s that a
206    * {@link DataStore} has been closed. Normally the only listener that is
207    * registered with the DataStoreRegister is the {@link CreoleRegister}
208    * which can be obtained through {@link Gate#getCreoleRegister()}
209    */
210   protected void fireDatastoreClosed(CreoleEvent e) {
211     if (creoleListeners != null) {
212       Vector listeners = creoleListeners;
213       int count = listeners.size();
214       for (int i = 0; i < count; i++) {
215         ((CreoleListener) listeners.elementAt(i)).datastoreClosed(e);
216       } // for
217     }   // if
218   } // fireDatastoreClosed(CreoleEvent e)
219 
220   /** */
221   private transient Vector creoleListeners;
222 
223 } // class DataStoreRegister
224