001: /*
002: This source file is part of Smyle, a database library.
003: For up-to-date information, see http://www.drjava.de/smyle
004: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: For full license text, see doc/license/lgpl.txt in this distribution
021: */
022:
023: package drjava.smyle.core;
024:
025: import java.io.*;
026: import java.util.*;
027: import java.lang.ref.*;
028: import drjava.smyle.*;
029:
030: public class StoreRegistry {
031: private static final HashMap<String, WeakReference<DiskStore>> stores = new HashMap<String, WeakReference<DiskStore>>();
032:
033: public static synchronized Store createEmptyStore(File dir,
034: Class cls) {
035: DiskStore store = findStore(dir);
036: if (store != null)
037: throw new BadUseException(
038: "Store is already open, can't create");
039: else {
040: FileSystemDisk disk = new FileSystemDisk(dir, false);
041: disk.deleteEverything();
042: store = createStore(disk, cls, false);
043: addStore(dir, store);
044: return store;
045: }
046: }
047:
048: public static synchronized Store openStore(File dir, Class cls,
049: boolean readOnly) {
050: DiskStore store = findStore(dir);
051: if (store != null) {
052: store.addReference();
053: return store;
054: } else {
055: store = createStore(new FileSystemDisk(dir, readOnly), cls,
056: readOnly);
057: addStore(dir, store);
058: return store;
059: }
060: }
061:
062: static private DiskStore createStore(Disk disk, Class cls,
063: boolean readOnly) {
064: if (cls == null)
065: return new DiskStore(disk, readOnly);
066: try {
067: return (DiskStore) cls.getConstructor(
068: new Class[] { Disk.class, Boolean.TYPE })
069: .newInstance(
070: new Object[] {
071: disk,
072: readOnly ? Boolean.TRUE
073: : Boolean.FALSE });
074: } catch (Exception e) {
075: e.printStackTrace();
076: throw new SmyleIOException(e.toString());
077: }
078: }
079:
080: static synchronized void removeStore(DiskStore store) {
081: Collection<WeakReference<DiskStore>> c = stores.values();
082: for (Iterator<WeakReference<DiskStore>> i = c.iterator(); i
083: .hasNext();) {
084: WeakReference<DiskStore> ref = i.next();
085: if (ref.get() == store) {
086: i.remove();
087: break;
088: }
089: }
090: }
091:
092: static void addStore(File dir, DiskStore store) {
093: store.enableIndexing();
094: try {
095: stores.put(dir.getCanonicalPath(),
096: new WeakReference<DiskStore>(store));
097: } catch (IOException e) {
098: throw new SmyleIOException(e);
099: }
100: }
101:
102: static DiskStore findStore(File dir) {
103: try {
104: String path = dir.getCanonicalPath();
105: WeakReference<DiskStore> ref = stores.get(path);
106: if (ref == null)
107: return null;
108: DiskStore store = ref.get();
109: if (store == null)
110: stores.remove(path);
111: return store;
112: } catch (IOException e) {
113: throw new SmyleIOException(e);
114: }
115: }
116: }
|