001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.managers;
007:
008: import java.sql.Connection;
009: import java.sql.SQLException;
010: import java.util.Collection;
011: import java.util.HashMap;
012: import java.util.Map;
013: import java.util.concurrent.ConcurrentHashMap;
014:
015: import org.openrdf.model.Namespace;
016: import org.openrdf.model.impl.NamespaceImpl;
017: import org.openrdf.sail.rdbms.exceptions.RdbmsException;
018: import org.openrdf.sail.rdbms.schema.NamespacesTable;
019:
020: /**
021: * Manages the namespace prefixes.
022: *
023: * @author James Leigh
024: *
025: */
026: public class NamespaceManager {
027: private Map<String, NamespaceImpl> byNamespace = new ConcurrentHashMap<String, NamespaceImpl>();
028: private Map<String, NamespaceImpl> byPrefix = new ConcurrentHashMap<String, NamespaceImpl>();
029: private Connection conn;
030: private NamespacesTable table;
031:
032: public void setConnection(Connection conn) {
033: this .conn = conn;
034: }
035:
036: public void setNamespacesTable(NamespacesTable table) {
037: this .table = table;
038: }
039:
040: public void close() throws RdbmsException {
041: try {
042: conn.close();
043: } catch (SQLException e) {
044: throw new RdbmsException(e);
045: }
046: }
047:
048: public void initialize() throws RdbmsException {
049: try {
050: load();
051: } catch (SQLException e) {
052: throw new RdbmsException(e);
053: }
054: }
055:
056: public NamespaceImpl findNamespace(String namespace)
057: throws RdbmsException {
058: if (namespace == null)
059: return null;
060: if (byNamespace.containsKey(namespace))
061: return byNamespace.get(namespace);
062: try {
063: return create(namespace);
064: } catch (SQLException e) {
065: throw new RdbmsException(e);
066: }
067: }
068:
069: private void load() throws SQLException {
070: Map<String, NamespaceImpl> map = new HashMap<String, NamespaceImpl>();
071: Map<String, NamespaceImpl> prefixes = new HashMap<String, NamespaceImpl>();
072: for (Object[] row : table.selectAll()) {
073: String prefix = (String) row[0];
074: String namespace = (String) row[1];
075: NamespaceImpl ns = new NamespaceImpl(prefix, namespace);
076: map.put(namespace, ns);
077: if (prefix != null) {
078: prefixes.put(prefix, ns);
079: }
080: }
081: byNamespace.putAll(map);
082: byPrefix.clear();
083: byPrefix.putAll(prefixes);
084: }
085:
086: private synchronized NamespaceImpl create(String namespace)
087: throws SQLException {
088: if (byNamespace.containsKey(namespace))
089: return byNamespace.get(namespace);
090: table.insert(null, namespace);
091: NamespaceImpl ns = new NamespaceImpl(null, namespace);
092: byNamespace.put(ns.getName(), ns);
093: return ns;
094: }
095:
096: public void setPrefix(String prefix, String name)
097: throws RdbmsException {
098: NamespaceImpl ns = findNamespace(name);
099: try {
100: table.updatePrefix(prefix, name);
101: } catch (SQLException e) {
102: throw new RdbmsException(e);
103: }
104: ns.setPrefix(prefix);
105: byPrefix.put(prefix, ns);
106: }
107:
108: public NamespaceImpl findByPrefix(String prefix) {
109: return byPrefix.get(prefix);
110: }
111:
112: public void removePrefix(String prefix) throws RdbmsException {
113: NamespaceImpl ns = findByPrefix(prefix);
114: if (ns == null)
115: return;
116: try {
117: table.updatePrefix(prefix, ns.getName());
118: } catch (SQLException e) {
119: throw new RdbmsException(e);
120: }
121: ns.setPrefix(null);
122: byPrefix.remove(prefix);
123: }
124:
125: public Collection<? extends Namespace> getNamespacesWithPrefix()
126: throws RdbmsException {
127: return byPrefix.values();
128: }
129:
130: public void clearPrefixes() throws RdbmsException {
131: try {
132: table.clearPrefixes();
133: load();
134: } catch (SQLException e) {
135: throw new RdbmsException(e);
136: }
137: }
138:
139: }
|