source: vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/FacetDescriptionServiceImpl.java @ 6248

Last change on this file since 6248 was 6248, checked in by Twan Goosen, 9 years ago

'title' attribute now added to actual html elements, not volatile wicket containers

Reduced some logging :)

File size: 3.8 KB
Line 
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 */
17package eu.clarin.cmdi.vlo.service.impl;
18
19import com.google.common.base.Strings;
20import com.sun.jersey.client.impl.CopyOnWriteHashMap;
21import eu.clarin.cmdi.vlo.config.VloConfig;
22import eu.clarin.cmdi.vlo.facets.FacetConcept;
23import eu.clarin.cmdi.vlo.facets.FacetConcepts;
24import eu.clarin.cmdi.vlo.facets.FacetConceptsMarshaller;
25import eu.clarin.cmdi.vlo.service.FacetDescriptionService;
26import java.io.File;
27import java.net.URI;
28import java.util.Map;
29import javax.annotation.PostConstruct;
30import javax.xml.bind.JAXBException;
31import javax.xml.transform.Source;
32import javax.xml.transform.stream.StreamSource;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36/**
37 *
38 * @author Twan Goosen <twan.goosen@mpi.nl>
39 */
40public 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}
Note: See TracBrowser for help on using the repository browser.