DataStoreRegister.java
001 /*
002  *  DataStoreRegister.java
003  *
004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Hamish Cunningham, 23/Jan/2001
013  *
014  *  $Id: DataStoreRegister.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015  */
016 
017 package gate;
018 
019 import java.util.*;
020 
021 import gate.event.CreoleEvent;
022 import gate.event.CreoleListener;
023 
024 /**
025  * Records all the open DataStores.
026  */
027 public class DataStoreRegister extends HashSet {
028   private static final long serialVersionUID = 1L;
029 
030   /**
031    * All the DataStore classes available. This is a map of class name to
032    * descriptive text.
033    */
034   public static Map<String,String> getDataStoreClassNames() {
035     Map<String,String> names = new HashMap<String,String>();
036 
037     // TODO: no plugability here at present.... at some future point there should
038     // be a capability to add new data store classes via creole.xml metadata
039     // and resource jars
040 
041     // Oracle - hide it as it is obsolete
042 //    names.put("gate.persist.OracleDataStore", "OracleDataStore: Oracle-specific RDBMS storage over JDBC");
043 
044     // Postgres - hide it as it is obsolete
045 //    names.put("gate.persist.PostgresDataStore", "PostgresDataStore: PostgreSQL-specific RDBMS storage over JDBC");
046 
047     // filesystem
048     names.put("gate.persist.SerialDataStore""SerialDataStore: file-based storage using Java serialisation");
049 
050     names.put("gate.persist.LuceneDataStoreImpl""Lucene Based Searchable DataStore");
051     
052     // docservice
053     try {
054       if (Class.forName("gleam.docservice.gate.DocServiceDataStore", true, Gate.getClassLoader()) != null) {
055         names.put("gleam.docservice.gate.DocServiceDataStore",
056             "SAFE DocService DataStore");
057       }
058     catch (ClassNotFoundException e) {
059     }
060     
061     return names;
062   // getDataStoreClassNames()
063 
064   /**
065    * Adds the specified element to this set if it is not already present.
066    * Overriden here for event registration code.
067    */
068   public boolean add(Object o) {
069     return super.add(o);
070   // add
071 
072   /**
073    * Removes the given element from this set if it is present. Overriden here
074    * for event registration code.
075    */
076   public boolean remove(Object o) {
077     boolean res = super.remove(o);
078     if (res) {
079       fireDatastoreClosed(new CreoleEvent((DataStoreo, CreoleEvent.DATASTORE_CLOSED));
080       removeSecurityData((DataStoreo);
081     }
082     return res;
083   // remove
084 
085   /**
086    * Removes all of the elements from this set. Overriden here for event
087    * registration code.
088    */
089   public void clear() {
090     Set datastores = new HashSet(this);
091     super.clear();
092 
093     Iterator iter = datastores.iterator();
094     while (iter.hasNext()) {
095       fireDatastoreClosed(new CreoleEvent((DataStoreiter.next(), CreoleEvent.DATASTORE_CLOSED));
096     // while
097   // clear()
098 
099   /** Configuration data such as driver names. */
100   private static Map configData = new HashMap();
101 
102   /** Get the configuration data map. */
103   public static Map getConfigData() {
104     return configData;
105   }
106 
107   /**
108    * Adds configuration data (e.g. from <TT>gate.xml</TT> files) to the
109    * register. New key/value pairs are added to the existing set (this will
110    * overwrite existing pairs whose keys match new ones).
111    */
112   public static void addConfig(Map configData) {
113     DataStoreRegister.configData.putAll(configData);
114   // addConfig
115 
116   /** A hashmap from datastore to security data (current user and group) */
117   private static Map securityData = new HashMap();
118 
119   /**
120    * Returns the security data for this datastore
121    */
122   public static Map getSecurityData(DataStore ds) {
123     return (MapsecurityData.get(ds);
124   //
125 
126   /**
127    * Adds security data for this datastore
128    */
129   public static void addSecurityData(DataStore ds, Map secData) {
130     DataStoreRegister.securityData.put(ds, secData);
131   }
132 
133   /**
134    * Removes the security data for this datastore
135    */
136   public static void removeSecurityData(DataStore ds) {
137     DataStoreRegister.securityData.remove(ds);
138   }
139 
140   /**
141    * Removes a previously registered {@link gate.event.CreoleListener} from the
142    * list of listeners for this DataStoreRegister. Normally the only listener
143    * that is registered with the DataStoreRegister is the {@link CreoleRegister}
144    * which can be obtained through {@link Gate#getCreoleRegister()}
145    */
146   public synchronized void removeCreoleListener(CreoleListener l) {
147     if (creoleListeners != null && creoleListeners.contains(l)) {
148       Vector v = (VectorcreoleListeners.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 registered with the
157    * DataStoreRegister is the {@link CreoleRegister} which can be obtained
158    * through {@link Gate#getCreoleRegister()}
159    */
160   public synchronized void addCreoleListener(CreoleListener l) {
161     Vector v = creoleListeners == null new Vector(2(VectorcreoleListeners.clone();
162     if (!v.contains(l)) {
163       v.addElement(l);
164       creoleListeners = v;
165     }// if
166   }// addCreoleListener(CreoleListener l)
167 
168   /**
169    * Notifies all registered {@link gate.event.CreoleListener}s that a
170    {@link DataStore} has been opened. Normally the only listener that is
171    * registered with the DataStoreRegister is the {@link CreoleRegister} which
172    * can be obtained through {@link Gate#getCreoleRegister()}
173    */
174   protected void fireDatastoreOpened(CreoleEvent e) {
175     if (creoleListeners != null) {
176       Vector listeners = creoleListeners;
177       int count = listeners.size();
178       for (int i = 0; i < count; i++) {
179         ((CreoleListenerlisteners.elementAt(i)).datastoreOpened(e);
180       // for
181     // if
182   // fireDatastoreOpened(CreoleEvent e)
183 
184   /**
185    * Notifies all registered {@link gate.event.CreoleListener}s that a new
186    {@link DataStore} has been created. Normally the only listener that is
187    * registered with the DataStoreRegister is the {@link CreoleRegister} which
188    * can be obtained through {@link Gate#getCreoleRegister()}
189    */
190   protected void fireDatastoreCreated(CreoleEvent e) {
191     if (creoleListeners != null) {
192       Vector listeners = creoleListeners;
193       int count = listeners.size();
194       for (int i = 0; i < count; i++) {
195         ((CreoleListenerlisteners.elementAt(i)).datastoreCreated(e);
196       // for
197     // if
198   // fireDatastoreCreated(CreoleEvent e)
199 
200   /**
201    * Notifies all registered {@link gate.event.CreoleListener}s that a
202    {@link DataStore} has been closed. Normally the only listener that is
203    * registered with the DataStoreRegister is the {@link CreoleRegister} which
204    * can be obtained through {@link Gate#getCreoleRegister()}
205    */
206   protected void fireDatastoreClosed(CreoleEvent e) {
207     if (creoleListeners != null) {
208       Vector listeners = creoleListeners;
209       int count = listeners.size();
210       for (int i = 0; i < count; i++) {
211         ((CreoleListenerlisteners.elementAt(i)).datastoreClosed(e);
212       // for
213     // if
214   // fireDatastoreClosed(CreoleEvent e)
215 
216   /** */
217   private transient Vector creoleListeners;
218 
219 // class DataStoreRegister