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