/
RdfFeatureFactory.scala
126 lines (114 loc) · 4.53 KB
/
RdfFeatureFactory.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright © 2015-2019 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knora is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Knora. If not, see <http://www.gnu.org/licenses/>.
*/
package org.knora.webapi.messages.util.rdf
import org.knora.webapi.exceptions.AssertionException
import org.knora.webapi.feature.{FeatureFactory, FeatureFactoryConfig}
import org.knora.webapi.messages.util.rdf.jenaimpl._
import org.knora.webapi.messages.util.rdf.rdf4jimpl._
import org.knora.webapi.settings.KnoraSettingsImpl
/**
* A feature factory that creates RDF processing tools.
*/
object RdfFeatureFactory extends FeatureFactory {
/**
* The name of the feature toggle that enables the Jena implementation of the RDF façade.
*/
private val JENA_TOGGLE_NAME = "jena-rdf-library"
// Jena singletons.
private val jenaNodeFactory = new JenaNodeFactory
private val jenaModelFactory = new JenaModelFactory(jenaNodeFactory)
private val jenaFormatUtil = new JenaFormatUtil(modelFactory = jenaModelFactory, nodeFactory = jenaNodeFactory)
private var jenaShaclValidator: Option[JenaShaclValidator] = None
// RDF4J singletons.
private val rdf4jNodeFactory = new RDF4JNodeFactory
private val rdf4jModelFactory = new RDF4JModelFactory(rdf4jNodeFactory)
private val rdf4jFormatUtil = new RDF4JFormatUtil(modelFactory = rdf4jModelFactory, nodeFactory = rdf4jNodeFactory)
private var rdf4jShaclValidator: Option[RDF4JShaclValidator] = None
/**
* Initialises the [[RdfFeatureFactory]]. This method must be called once, on application startup.
*
* @param settings the application settings.
*/
def init(settings: KnoraSettingsImpl): Unit = {
// Construct the SHACL validators, which need the application settings.
this.synchronized {
jenaShaclValidator = Some(
new JenaShaclValidator(
baseDir = settings.shaclShapesDir,
rdfFormatUtil = jenaFormatUtil,
nodeFactory = jenaNodeFactory
)
)
rdf4jShaclValidator = Some(
new RDF4JShaclValidator(
baseDir = settings.shaclShapesDir,
rdfFormatUtil = rdf4jFormatUtil,
nodeFactory = rdf4jNodeFactory
)
)
}
}
/**
* Returns an [[RdfModelFactory]].
*
* @param featureFactoryConfig the feature factory configuration.
* @return an [[RdfModelFactory]].
*/
def getRdfModelFactory(featureFactoryConfig: FeatureFactoryConfig): RdfModelFactory = {
if (featureFactoryConfig.getToggle(JENA_TOGGLE_NAME).isEnabled) {
jenaModelFactory
} else {
rdf4jModelFactory
}
}
/**
* Returns an [[RdfNodeFactory]].
*
* @param featureFactoryConfig the feature factory configuration.
* @return an [[RdfNodeFactory]].
*/
def getRdfNodeFactory(featureFactoryConfig: FeatureFactoryConfig): RdfNodeFactory = {
if (featureFactoryConfig.getToggle(JENA_TOGGLE_NAME).isEnabled) {
jenaNodeFactory
} else {
rdf4jNodeFactory
}
}
/**
* Returns an [[RdfFormatUtil]].
*
* @param featureFactoryConfig the feature factory configuration.
* @return an [[RdfFormatUtil]].
*/
def getRdfFormatUtil(featureFactoryConfig: FeatureFactoryConfig): RdfFormatUtil = {
if (featureFactoryConfig.getToggle(JENA_TOGGLE_NAME).isEnabled) {
jenaFormatUtil
} else {
rdf4jFormatUtil
}
}
def getShaclValidator(featureFactoryConfig: FeatureFactoryConfig): ShaclValidator = {
def notInitialised: Nothing = throw AssertionException("RdfFeatureFactory has not been initialised")
if (featureFactoryConfig.getToggle(JENA_TOGGLE_NAME).isEnabled) {
jenaShaclValidator.getOrElse(notInitialised)
} else {
rdf4jShaclValidator.getOrElse(notInitialised)
}
}
}