1 | /* |
---|
2 | * Copyright (C) 2015 CLARIN |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | package eu.clarin.cmdi.vlo.service.impl; |
---|
18 | |
---|
19 | import com.google.common.base.Strings; |
---|
20 | import com.sun.jersey.client.impl.CopyOnWriteHashMap; |
---|
21 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
22 | import eu.clarin.cmdi.vlo.facets.FacetConcept; |
---|
23 | import eu.clarin.cmdi.vlo.facets.FacetConcepts; |
---|
24 | import eu.clarin.cmdi.vlo.facets.FacetConceptsMarshaller; |
---|
25 | import eu.clarin.cmdi.vlo.service.FacetDescriptionService; |
---|
26 | import java.io.File; |
---|
27 | import java.net.URI; |
---|
28 | import java.util.Map; |
---|
29 | import javax.annotation.PostConstruct; |
---|
30 | import javax.xml.bind.JAXBException; |
---|
31 | import javax.xml.transform.Source; |
---|
32 | import javax.xml.transform.stream.StreamSource; |
---|
33 | import org.slf4j.Logger; |
---|
34 | import org.slf4j.LoggerFactory; |
---|
35 | |
---|
36 | /** |
---|
37 | * |
---|
38 | * @author Twan Goosen <twan.goosen@mpi.nl> |
---|
39 | */ |
---|
40 | public class FacetDescriptionServiceImpl implements FacetDescriptionService { |
---|
41 | |
---|
42 | private final static Logger logger = LoggerFactory.getLogger(FacetDescriptionServiceImpl.class); |
---|
43 | |
---|
44 | private final Map<String, String> descriptions = new CopyOnWriteHashMap<>(); |
---|
45 | private final VloConfig config; |
---|
46 | private final FacetConceptsMarshaller marshaller; |
---|
47 | |
---|
48 | public FacetDescriptionServiceImpl(FacetConceptsMarshaller marshaller, VloConfig vloConfig) { |
---|
49 | this.marshaller = marshaller; |
---|
50 | this.config = vloConfig; |
---|
51 | } |
---|
52 | |
---|
53 | @PostConstruct |
---|
54 | protected void init() throws JAXBException { |
---|
55 | final Source streamSource = getFacetConceptsSource(); |
---|
56 | final FacetConcepts facetConcepts = marshaller.unmarshal(streamSource); |
---|
57 | for (FacetConcept concept : facetConcepts.getFacetConcept()) { |
---|
58 | if (concept.getDescription() != null) { |
---|
59 | logger.debug("Found facet definition '{}'", concept.getName()); |
---|
60 | descriptions.put(concept.getName(), concept.getDescription()); |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | private Source getFacetConceptsSource() { |
---|
66 | final String facetConceptsFile = config.getFacetConceptsFile(); |
---|
67 | |
---|
68 | if (Strings.isNullOrEmpty(facetConceptsFile)) { |
---|
69 | logger.info("No facet concepts file configured. Reading default definitions from packaged file."); |
---|
70 | return new StreamSource(getClass().getResourceAsStream(VloConfig.DEFAULT_FACET_CONCEPTS_RESOURCE_FILE)); |
---|
71 | } else { |
---|
72 | final URI facetsFile = resolveFacetsFile(facetConceptsFile); |
---|
73 | logger.info("Reading facet definitions from {}", facetsFile); |
---|
74 | return new StreamSource(facetsFile.toString()); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | private URI resolveFacetsFile(final String facetConceptsFile) { |
---|
79 | final URI configLocation = config.getConfigLocation(); |
---|
80 | if (configLocation == null) { |
---|
81 | return new File(facetConceptsFile).toURI(); |
---|
82 | } else if ("jar".equals(configLocation.getScheme())) { |
---|
83 | // some trickery to replace URI pointing inside JAR (fingers crossed) |
---|
84 | final String jarUri = configLocation.toString().replaceAll("!.*$", "!" + facetConceptsFile); |
---|
85 | return URI.create(jarUri); |
---|
86 | } else { |
---|
87 | // resolve against config |
---|
88 | return configLocation.resolve(facetConceptsFile); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | @Override |
---|
93 | public String getDescription(String facetName) { |
---|
94 | return descriptions.get(facetName); |
---|
95 | } |
---|
96 | |
---|
97 | } |
---|