001package ca.uhn.fhir.rest.server.interceptor;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.util.ClasspathUtil;
025import com.fasterxml.jackson.databind.ObjectMapper;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import java.io.IOException;
030import java.io.StringReader;
031import java.util.Properties;
032
033public class ConfigLoader {
034
035        private static final Logger ourLog = LoggerFactory.getLogger(ConfigLoader.class);
036        public static final String CLASSPATH = "classpath:";
037
038        public static String loadResourceContent(String theResourcePath) {
039                if(theResourcePath.startsWith(CLASSPATH)) {
040                        theResourcePath = theResourcePath.substring(CLASSPATH.length());
041                }
042                return ClasspathUtil.loadResource(theResourcePath);
043        }
044
045        public static Properties loadProperties(String theResourcePath) {
046                String propsString = loadResourceContent(theResourcePath);
047                Properties props = new Properties();
048                try {
049                        props.load(new StringReader(propsString));
050                } catch (IOException e) {
051                        throw new RuntimeException(Msg.code(324) + String.format("Unable to load properties at %s", theResourcePath), e);
052                }
053                return props;
054        }
055
056        public static <T> T loadJson(String theResourcePath, Class<T> theModelClass) {
057                ObjectMapper mapper = new ObjectMapper();
058                try {
059                        return mapper.readValue(loadResourceContent(theResourcePath), theModelClass);
060                } catch (Exception e) {
061                        throw new RuntimeException(Msg.code(325) + String.format("Unable to parse resource at %s", theResourcePath), e);
062                }
063        }
064
065}