Improve multiversion support
This commit is contained in:
222
src/main/java/ca/uhn/fhir/jpa/starter/HapiProperties.java
Normal file
222
src/main/java/ca/uhn/fhir/jpa/starter/HapiProperties.java
Normal file
@@ -0,0 +1,222 @@
|
||||
package ca.uhn.fhir.jpa.starter;
|
||||
|
||||
import ca.uhn.fhir.context.ConfigurationException;
|
||||
import ca.uhn.fhir.context.FhirVersionEnum;
|
||||
import ca.uhn.fhir.rest.api.EncodingEnum;
|
||||
import ca.uhn.fhir.rest.server.ETagSupportEnum;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
public class HapiProperties {
|
||||
public static final String SERVER_ADDRESS = "server_address";
|
||||
public static final String DEFAULT_PRETTY_PRINT = "default_pretty_print";
|
||||
public static final String MAX_PAGE_SIZE = "max_page_size";
|
||||
public static final String DEFAULT_PAGE_SIZE = "default_page_size";
|
||||
public static final String LOGGER_NAME = "logger.name";
|
||||
public static final String LOGGER_FORMAT = "logger.format";
|
||||
public static final String ALLOW_EXTERNAL_REFERENCES = "allow_external_references";
|
||||
public static final String ALLOW_MULTIPLE_DELETE = "allow_multiple_delete";
|
||||
public static final String DATASOURCE_PASSWORD = "datasource.password";
|
||||
public static final String DATASOURCE_USERNAME = "datasource.username";
|
||||
public static final String DATASOURCE_URL = "datasource.url";
|
||||
public static final String DATASOURCE_DRIVER = "datasource.driver";
|
||||
public static final String LOGGER_LOG_EXCEPTIONS = "logger.log_exceptions";
|
||||
public static final String LOGGER_ERROR_FORMAT = "logger.error_format";
|
||||
public static final String PERSISTENCE_UNIT_NAME = "persistence_unit_name";
|
||||
public static final String SERVER_BASE = "server.base";
|
||||
public static final String TEST_PORT = "test.port";
|
||||
public static final String SERVER_NAME = "server.name";
|
||||
public static final String SERVER_ID = "server.id";
|
||||
private static final String HAPI_PROPERTIES = "hapi.properties";
|
||||
static final String FHIR_VERSION = "fhir_version";
|
||||
private static final String DEFAULT_ENCODING = "default_encoding";
|
||||
private static final String ETAG_SUPPORT = "etag_support";
|
||||
private static Properties properties;
|
||||
|
||||
/**
|
||||
* Force the configuration to be reloaded
|
||||
*/
|
||||
public static void forceReload() {
|
||||
properties = null;
|
||||
getProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is mostly here for unit tests. Use the actual properties file
|
||||
* to set values
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public static void setProperty(String theKey, String theValue) {
|
||||
getProperties().setProperty(theKey, theValue);
|
||||
}
|
||||
|
||||
public static Properties getProperties() {
|
||||
if (properties == null) {
|
||||
// Load the configurable properties file
|
||||
try (InputStream in = HapiProperties.class.getClassLoader().getResourceAsStream(HAPI_PROPERTIES)){
|
||||
HapiProperties.properties = new Properties();
|
||||
HapiProperties.properties.load(in);
|
||||
} catch (Exception e) {
|
||||
throw new ConfigurationException("Could not load HAPI properties", e);
|
||||
}
|
||||
}
|
||||
|
||||
return properties;
|
||||
}
|
||||
|
||||
private static String getProperty(String propertyName) {
|
||||
Properties properties = HapiProperties.getProperties();
|
||||
|
||||
if (properties != null) {
|
||||
return properties.getProperty(propertyName);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String getProperty(String propertyName, String defaultValue) {
|
||||
Properties properties = HapiProperties.getProperties();
|
||||
|
||||
if (properties != null) {
|
||||
String value = properties.getProperty(propertyName);
|
||||
|
||||
if (value != null && value.length() > 0) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
private static Boolean getBooleanProperty(String propertyName, Boolean defaultValue) {
|
||||
String value = HapiProperties.getProperty(propertyName);
|
||||
|
||||
if (value == null || value.length() == 0) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
private static Integer getIntegerProperty(String propertyName, Integer defaultValue) {
|
||||
String value = HapiProperties.getProperty(propertyName);
|
||||
|
||||
if (value == null || value.length() == 0) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return Integer.parseInt(value);
|
||||
}
|
||||
|
||||
public static FhirVersionEnum getFhirVersion() {
|
||||
String fhirVersionString = HapiProperties.getProperty(FHIR_VERSION);
|
||||
|
||||
if (fhirVersionString != null && fhirVersionString.length() > 0) {
|
||||
return FhirVersionEnum.valueOf(fhirVersionString);
|
||||
}
|
||||
|
||||
return FhirVersionEnum.DSTU3;
|
||||
}
|
||||
|
||||
public static ETagSupportEnum getEtagSupport() {
|
||||
String etagSupportString = HapiProperties.getProperty(ETAG_SUPPORT);
|
||||
|
||||
if (etagSupportString != null && etagSupportString.length() > 0) {
|
||||
return ETagSupportEnum.valueOf(etagSupportString);
|
||||
}
|
||||
|
||||
return ETagSupportEnum.ENABLED;
|
||||
}
|
||||
|
||||
public static EncodingEnum getDefaultEncoding() {
|
||||
String defaultEncodingString = HapiProperties.getProperty(DEFAULT_ENCODING);
|
||||
|
||||
if (defaultEncodingString != null && defaultEncodingString.length() > 0) {
|
||||
return EncodingEnum.valueOf(defaultEncodingString);
|
||||
}
|
||||
|
||||
return EncodingEnum.JSON;
|
||||
}
|
||||
|
||||
public static Boolean getDefaultPrettyPrint() {
|
||||
return HapiProperties.getBooleanProperty(DEFAULT_PRETTY_PRINT, true);
|
||||
}
|
||||
|
||||
public static String getServerAddress() {
|
||||
return HapiProperties.getProperty(SERVER_ADDRESS);
|
||||
}
|
||||
|
||||
public static Integer getDefaultPageSize() {
|
||||
return HapiProperties.getIntegerProperty(DEFAULT_PAGE_SIZE, 20);
|
||||
}
|
||||
|
||||
public static Integer getMaximumPageSize() {
|
||||
return HapiProperties.getIntegerProperty(MAX_PAGE_SIZE, 200);
|
||||
}
|
||||
|
||||
public static String getPersistenceUnitName() {
|
||||
return HapiProperties.getProperty(PERSISTENCE_UNIT_NAME, "HAPI_PU");
|
||||
}
|
||||
|
||||
public static String getLoggerName() {
|
||||
return HapiProperties.getProperty(LOGGER_NAME, "fhirtest.access");
|
||||
}
|
||||
|
||||
public static String getLoggerFormat() {
|
||||
return HapiProperties.getProperty(LOGGER_FORMAT, "Path[${servletPath}] Source[${requestHeader.x-forwarded-for}] Operation[${operationType} ${operationName} ${idOrResourceName}] UA[${requestHeader.user-agent}] Params[${requestParameters}] ResponseEncoding[${responseEncodingNoDefault}]");
|
||||
}
|
||||
|
||||
public static String getLoggerErrorFormat() {
|
||||
return HapiProperties.getProperty(LOGGER_ERROR_FORMAT, "ERROR - ${requestVerb} ${requestUrl}");
|
||||
}
|
||||
|
||||
public static Boolean getLoggerLogExceptions() {
|
||||
return HapiProperties.getBooleanProperty(LOGGER_LOG_EXCEPTIONS, true);
|
||||
}
|
||||
|
||||
public static String getDataSourceDriver() {
|
||||
return HapiProperties.getProperty(DATASOURCE_DRIVER, "org.apache.derby.jdbc.EmbeddedDriver");
|
||||
}
|
||||
|
||||
public static String getDataSourceUrl() {
|
||||
return HapiProperties.getProperty(DATASOURCE_URL, "jdbc:derby:directory:target/jpaserver_derby_files;create=true");
|
||||
}
|
||||
|
||||
public static String getDataSourceUsername() {
|
||||
return HapiProperties.getProperty(DATASOURCE_USERNAME);
|
||||
}
|
||||
|
||||
public static String getDataSourcePassword() {
|
||||
return HapiProperties.getProperty(DATASOURCE_PASSWORD);
|
||||
}
|
||||
|
||||
public static Boolean getAllowMultipleDelete() {
|
||||
return HapiProperties.getBooleanProperty(ALLOW_MULTIPLE_DELETE, true);
|
||||
}
|
||||
|
||||
public static Boolean getAllowExternalReferences() {
|
||||
return HapiProperties.getBooleanProperty(ALLOW_EXTERNAL_REFERENCES, true);
|
||||
}
|
||||
|
||||
public static Boolean getExpungeEnabled() {
|
||||
return HapiProperties.getBooleanProperty("expunge_enabled", true);
|
||||
}
|
||||
|
||||
public static Integer getTestPort() {
|
||||
return HapiProperties.getIntegerProperty(TEST_PORT, 0);
|
||||
}
|
||||
|
||||
public static String getServerBase() {
|
||||
return HapiProperties.getProperty(SERVER_BASE, "/fhir");
|
||||
}
|
||||
|
||||
public static String getServerName() {
|
||||
return HapiProperties.getProperty(SERVER_NAME, "Local Tester");
|
||||
}
|
||||
|
||||
public static String getServerId() {
|
||||
return HapiProperties.getProperty(SERVER_ID, "home");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user