001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.repository.config;
007:
008: import static org.openrdf.repository.config.RepositoryConfigSchema.REPOSITORYID;
009: import static org.openrdf.repository.config.RepositoryConfigSchema.REPOSITORY_CONTEXT;
010:
011: import java.util.LinkedHashSet;
012: import java.util.List;
013: import java.util.Set;
014:
015: import org.openrdf.model.Graph;
016: import org.openrdf.model.Literal;
017: import org.openrdf.model.Resource;
018: import org.openrdf.model.Statement;
019: import org.openrdf.model.ValueFactory;
020: import org.openrdf.model.impl.GraphImpl;
021: import org.openrdf.model.vocabulary.RDF;
022: import org.openrdf.repository.Repository;
023: import org.openrdf.repository.RepositoryConnection;
024: import org.openrdf.repository.RepositoryException;
025: import org.openrdf.repository.RepositoryResult;
026:
027: public class RepositoryConfigUtil {
028:
029: public static Set<String> getRepositoryIDs(Repository repository)
030: throws RepositoryException {
031: RepositoryConnection con = repository.getConnection();
032: try {
033: Set<String> idSet = new LinkedHashSet<String>();
034:
035: RepositoryResult<Statement> idStatementIter = con
036: .getStatements(null, REPOSITORYID, null, true);
037: try {
038: while (idStatementIter.hasNext()) {
039: Statement idStatement = idStatementIter.next();
040:
041: if (idStatement.getObject() instanceof Literal) {
042: Literal idLiteral = (Literal) idStatement
043: .getObject();
044: idSet.add(idLiteral.getLabel());
045: }
046: }
047: } finally {
048: idStatementIter.close();
049: }
050:
051: return idSet;
052: } finally {
053: con.close();
054: }
055: }
056:
057: /**
058: * Is configuration information for the specified repository ID present in
059: * the (system) repository?
060: *
061: * @param repository
062: * the repository to look in
063: * @param repositoryID
064: * the repositoryID to look for
065: * @return true if configurion information for the specified repository ID
066: * was found, false otherwise
067: * @throws RepositoryException
068: * if an error occurred while trying to retrieve information from the
069: * (system) repository
070: * @throws RepositoryConfigException
071: */
072: public static boolean hasRepositoryConfig(Repository repository,
073: String repositoryID) throws RepositoryException,
074: RepositoryConfigException {
075: RepositoryConnection con = repository.getConnection();
076: try {
077: return getIDStatement(con, repositoryID) != null;
078: } finally {
079: con.close();
080: }
081: }
082:
083: public static RepositoryConfig getRepositoryConfig(
084: Repository repository, String repositoryID)
085: throws RepositoryConfigException, RepositoryException {
086: RepositoryConnection con = repository.getConnection();
087: try {
088: Statement idStatement = getIDStatement(con, repositoryID);
089: if (idStatement == null) {
090: // No such config
091: return null;
092: }
093:
094: Resource repositoryNode = idStatement.getSubject();
095: Resource context = idStatement.getContext();
096:
097: if (context == null) {
098: throw new RepositoryException(
099: "No configuration context for repository "
100: + repositoryID);
101: }
102:
103: Graph contextGraph = new GraphImpl();
104: con.getStatements(null, null, null, true, context).addTo(
105: contextGraph);
106:
107: return RepositoryConfig
108: .create(contextGraph, repositoryNode);
109: } finally {
110: con.close();
111: }
112: }
113:
114: /**
115: * Update the specified Repository with the specified set of
116: * RepositoryConfigs. This will overwrite all existing configurations in the
117: * Repository that have a Repository ID occurring in these RepositoryConfigs.
118: *
119: * @param repository
120: * The Repository whose contents will be modified.
121: * @param configs
122: * The RepositoryConfigs that should be added to or updated in the
123: * Repository. The RepositoryConfig's ID may already occur in the
124: * Repository, in which case all previous configuration data for that
125: * Repository will be cleared before the RepositoryConfig is added.
126: * @throws RepositoryException
127: * When access to the Repository's RepositoryConnection causes a
128: * RepositoryException.
129: * @throws RepositoryConfigException
130: */
131: public static void updateRepositoryConfigs(Repository repository,
132: RepositoryConfig... configs) throws RepositoryException,
133: RepositoryConfigException {
134: RepositoryConnection con = repository.getConnection();
135:
136: try {
137: updateRepositoryConfigs(con, configs);
138: } finally {
139: con.close();
140: }
141: }
142:
143: /**
144: * Update the specified RepositoryConnection with the specified set of
145: * RepositoryConfigs. This will overwrite all existing configurations in the
146: * Repository that have a Repository ID occurring in these RepositoryConfigs.
147: *
148: * Note: this method does NOT commit the updates on the connection.
149: *
150: * @param con
151: * the repository connection to perform the update on
152: * @param configs
153: * The RepositoryConfigs that should be added to or updated in the
154: * Repository. The RepositoryConfig's ID may already occur in the
155: * Repository, in which case all previous configuration data for that
156: * Repository will be cleared before the RepositoryConfig is added.
157: *
158: * @throws RepositoryException
159: * @throws RepositoryConfigException
160: */
161: public static void updateRepositoryConfigs(
162: RepositoryConnection con, RepositoryConfig... configs)
163: throws RepositoryException, RepositoryConfigException {
164: ValueFactory vf = con.getRepository().getValueFactory();
165:
166: boolean wasAutoCommit = con.isAutoCommit();
167: con.setAutoCommit(false);
168:
169: for (RepositoryConfig config : configs) {
170: Resource context = getContext(con, config.getID());
171:
172: if (context != null) {
173: con.clear(context);
174: } else {
175: context = vf.createBNode();
176: }
177:
178: con.add(context, RDF.TYPE, REPOSITORY_CONTEXT);
179:
180: Graph graph = new GraphImpl(vf);
181: config.export(graph);
182: con.add(graph, context);
183: }
184:
185: con.setAutoCommit(wasAutoCommit);
186: }
187:
188: /**
189: * Removes one or more Repository configurations from a Repository. Nothing
190: * happens when this Repository does not contain configurations for these
191: * Repository IDs.
192: *
193: * @param repository
194: * The Repository to remove the configurations from.
195: * @param repositoryIDs
196: * The IDs of the Repositories whose configurations need to be
197: * removed.
198: * @throws RepositoryException
199: * Whenever access to the Repository's RepositoryConnection causes a
200: * RepositoryException.
201: * @throws RepositoryConfigException
202: */
203: public static boolean removeRepositoryConfigs(
204: Repository repository, String... repositoryIDs)
205: throws RepositoryException, RepositoryConfigException {
206: boolean changed = false;
207:
208: RepositoryConnection con = repository.getConnection();
209: try {
210: con.setAutoCommit(false);
211:
212: for (String id : repositoryIDs) {
213: Resource context = getContext(con, id);
214: if (context != null) {
215: con.clear(context);
216: con.remove(context, RDF.TYPE, REPOSITORY_CONTEXT);
217: changed = true;
218: }
219: }
220:
221: con.commit();
222: } finally {
223: con.close();
224: }
225:
226: return changed;
227: }
228:
229: public static Resource getContext(RepositoryConnection con,
230: String repositoryID) throws RepositoryException,
231: RepositoryConfigException {
232: Resource context = null;
233:
234: Statement idStatement = getIDStatement(con, repositoryID);
235: if (idStatement != null) {
236: context = idStatement.getContext();
237: }
238:
239: return context;
240: }
241:
242: private static Statement getIDStatement(RepositoryConnection con,
243: String repositoryID) throws RepositoryException,
244: RepositoryConfigException {
245: Literal idLiteral = con.getRepository().getValueFactory()
246: .createLiteral(repositoryID);
247: List<Statement> idStatementList = con.getStatements(null,
248: REPOSITORYID, idLiteral, true).asList();
249:
250: if (idStatementList.size() == 1) {
251: return idStatementList.get(0);
252: } else if (idStatementList.isEmpty()) {
253: return null;
254: } else {
255: throw new RepositoryConfigException(
256: "Multiple ID-statements for repository ID "
257: + repositoryID);
258: }
259: }
260: }
|