Skip to content

Method: unwrap(Class)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.rdf4j.connector.init;
19:
20: import cz.cvut.kbss.ontodriver.Wrapper;
21: import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
22: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
23: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
24: import org.apache.http.client.HttpClient;
25: import org.apache.http.client.utils.HttpClientUtils;
26: import org.eclipse.rdf4j.http.client.HttpClientSessionManager;
27: import org.eclipse.rdf4j.http.client.SharedHttpClientSessionManager;
28: import org.eclipse.rdf4j.model.ValueFactory;
29: import org.eclipse.rdf4j.repository.Repository;
30: import org.eclipse.rdf4j.repository.RepositoryConnection;
31: import org.eclipse.rdf4j.repository.RepositoryException;
32: import org.eclipse.rdf4j.repository.http.HTTPRepository;
33:
34: import java.io.File;
35:
36: /**
37: * Wrapper for RDF4J {@link HTTPRepository} allowing to set custom {@link HttpClient} for it to use.
38: */
39: public class RemoteRepositoryWrapper implements Repository, Wrapper {
40:
41: private final HTTPRepository delegate;
42: private HttpClient httpClient;
43:
44: public RemoteRepositoryWrapper(HTTPRepository delegate, DriverConfiguration configuration) {
45: this.delegate = delegate;
46: this.httpClient = HttpClientFactory.createHttpClient(configuration);
47: final HttpClientSessionManager sessionManager = delegate.getHttpClientSessionManager();
48: if (sessionManager instanceof SharedHttpClientSessionManager) {
49: ((SharedHttpClientSessionManager) sessionManager).setHttpClient(httpClient);
50: }
51: }
52:
53: @Override
54: public void setDataDir(File file) {
55: delegate.setDataDir(file);
56: }
57:
58: @Override
59: public File getDataDir() {
60: return delegate.getDataDir();
61: }
62:
63: @Override
64: public void init() throws RepositoryException {
65: delegate.init();
66: }
67:
68: @Override
69: public boolean isInitialized() {
70: return delegate.isInitialized();
71: }
72:
73: @Override
74: public void shutDown() throws RepositoryException {
75: delegate.shutDown();
76: if (httpClient != null) {
77: HttpClientUtils.closeQuietly(httpClient);
78: this.httpClient = null;
79: }
80: }
81:
82: @Override
83: public boolean isWritable() throws RepositoryException {
84: return delegate.isWritable();
85: }
86:
87: @Override
88: public RepositoryConnection getConnection() throws RepositoryException {
89: return delegate.getConnection();
90: }
91:
92: @Override
93: public ValueFactory getValueFactory() {
94: return delegate.getValueFactory();
95: }
96:
97: @Override
98: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
99:• if (cls.isAssignableFrom(delegate.getClass())) {
100: return cls.cast(delegate);
101: }
102: throw new Rdf4jDriverException("No instance of class " + cls + " found.");
103: }
104: }