/
XMLFileSettingsLoader.java
113 lines (106 loc) · 4.64 KB
/
XMLFileSettingsLoader.java
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
/*
* #%L
* Bitrepository Protocol
*
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program 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 General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package org.bitrepository.common.settings;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.bitrepository.common.JaxbHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
/**
* Loads settings as xml from the classpath.
*
*/
public class XMLFileSettingsLoader implements SettingsLoader {
/** The log for this class. */
private final Logger log = LoggerFactory.getLogger(getClass());
private String pathToSettingsFiles;
/** Directory seperator constant */
private static final String DIRECTORY_SEPERATOR = "/";
/** xml file extension constant */
private static final String XML_FILE_EXTENSION = ".xml";
/** xsd file extension constant */
private static final String XSD_FILE_EXTENSION = ".xsd";
/** xsd schema directory constant */
private static final String XSD_SCHEMA_DIR = "xsd/";
/**
* Creates a new loader
* @param pathToSettingsFiles The location of the files to load. The settings xml files are assume to be placed as
* ${CONFIGURATION_CLASS}.xml under this directory.
*
*/
public XMLFileSettingsLoader(String pathToSettingsFiles) {
this.pathToSettingsFiles = pathToSettingsFiles;
}
/**
* Loads the indicated settings for the specified collection.
* @param settingsClass The settings class identifying the type of settings requested.
* @param <T> The type of settings to return
* @return The loaded settings.
*/
public <T> T loadSettings(Class<T> settingsClass) {
StringBuilder fileLocationBuilder = new StringBuilder();
if (pathToSettingsFiles != null && !pathToSettingsFiles.equals("")) {
fileLocationBuilder.append(pathToSettingsFiles).append(DIRECTORY_SEPERATOR);
}
fileLocationBuilder.append(settingsClass.getSimpleName()).append(XML_FILE_EXTENSION);
String fileLocation = fileLocationBuilder.toString();
String schemaLocation = settingsClass.getSimpleName() + XSD_FILE_EXTENSION;
JaxbHelper jaxbHelper = new JaxbHelper(XSD_SCHEMA_DIR, schemaLocation);
InputStream configStreamLoad = null;
InputStream configStreamValidate = null;
try {
configStreamLoad = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileLocation);
configStreamValidate = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileLocation);
if (configStreamLoad == null) {
try {
configStreamLoad = new FileInputStream(fileLocation);
configStreamValidate = new FileInputStream(fileLocation);
} catch (FileNotFoundException e) {
throw new RuntimeException("Unable to load settings from " + fileLocation, e);
}
}
log.debug("Loading the settings file '" + fileLocation + "'.");
try {
jaxbHelper.validate(configStreamValidate);
return jaxbHelper.loadXml(settingsClass, configStreamLoad);
} catch (SAXException e) {
throw new RuntimeException("Unable to validate settings from " +
Thread.currentThread().getContextClassLoader().getResource(fileLocation), e);
} catch (Exception e) {
throw new RuntimeException("Unable to load settings from " + fileLocation, e);
}
} finally {
try {
if (configStreamLoad != null) configStreamLoad.close();
if (configStreamValidate != null) configStreamValidate.close();
} catch (IOException e) {
throw new RuntimeException("Unable to close inputstream", e);
}
}
}
}