001package ca.uhn.fhir.system;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2023 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 org.apache.commons.lang3.time.DateUtils;
024
025public final class HapiSystemProperties {
026        static final String SUPPRESS_HAPI_FHIR_VERSION_LOG = "suppress_hapi_fhir_version_log";
027        static final String DISABLE_STATUS_BASED_REINDEX = "disable_status_based_reindex";
028        /**
029         * This is provided for testing only! Use with caution as this property may change.
030         */
031        static final String TEST_SYSTEM_PROP_VALIDATION_RESOURCE_CACHES_MS = "TEST_SYSTEM_PROP_VALIDATION_RESOURCE_CACHES_MS";
032        static final String UNIT_TEST_CAPTURE_STACK = "unit_test_capture_stack";
033        static final String STACKFILTER_PATTERN_PROP = "log.stackfilter.pattern";
034        static final String HAPI_CLIENT_KEEPRESPONSES = "hapi.client.keepresponses";
035        static final String TEST_MODE = "test";
036        static final String UNIT_TEST_MODE = "unit_test_mode";
037        static final long DEFAULT_TEST_SYSTEM_PROP_VALIDATION_RESOURCE_CACHES_MS = 10 * DateUtils.MILLIS_PER_SECOND;
038
039        private HapiSystemProperties() {
040        }
041
042        /**
043         * This property is used by unit tests - do not rely on it in production code
044         * as it may change at any time. If you want to capture responses in a reliable
045         * way in your own code, just use client interceptors
046         */
047        public static void enableHapiClientKeepResponses() {
048                System.setProperty(HAPI_CLIENT_KEEPRESPONSES, Boolean.TRUE.toString());
049        }
050
051        public static void disableHapiClientKeepResponses() {
052                System.clearProperty(HAPI_CLIENT_KEEPRESPONSES);
053        }
054
055        /**
056         * This property is used by unit tests - do not rely on it in production code
057         * as it may change at any time. If you want to capture responses in a reliable
058         * way in your own code, just use client interceptors
059         */
060        public static boolean isHapiClientKeepResponsesEnabled() {
061                return (Boolean.parseBoolean(System.getProperty(HAPI_CLIENT_KEEPRESPONSES)));
062        }
063
064        /**
065         * This property gets used in the logback.xml file.
066         * It causes logged stack traces to skip a number of packages that are
067         * just noise.
068         */
069
070        public static void setStackFilterPattern(String thePattern) {
071                System.setProperty(STACKFILTER_PATTERN_PROP, thePattern);
072        }
073
074        /**
075         * Set the validation resource cache expireAfterWrite timeout in milliseconds
076         *
077         * @param theMillis
078         */
079        public static void setTestValidationResourceCachesMs(long theMillis) {
080                System.setProperty(TEST_SYSTEM_PROP_VALIDATION_RESOURCE_CACHES_MS, "" + theMillis);
081        }
082
083        /**
084         * Get the validation resource cache expireAfterWrite timeout in milliseconds.  If it has not been set, the default
085         * value is 10 seconds.
086         */
087
088        public static long getTestValidationResourceCachesMs() {
089                String property = System.getProperty(TEST_SYSTEM_PROP_VALIDATION_RESOURCE_CACHES_MS);
090                if (property == null) {
091                        return DEFAULT_TEST_SYSTEM_PROP_VALIDATION_RESOURCE_CACHES_MS;
092                }
093                return Long.parseLong(property);
094        }
095
096        /**
097         * When this property is primarily used to control application shutdown behavior
098         */
099        public static void enableTestMode() {
100                System.setProperty(TEST_MODE, Boolean.TRUE.toString());
101        }
102
103        public static boolean isTestModeEnabled() {
104                return Boolean.parseBoolean(System.getProperty(TEST_MODE));
105        }
106
107        /**
108         * This property is used to ensure unit test behaviour is deterministic.  It is also used to add extra logging for unit tests.
109         */
110        public static void enableUnitTestMode() {
111                System.setProperty(UNIT_TEST_MODE, Boolean.TRUE.toString());
112        }
113        public static void disableUnitTestMode() {
114                System.setProperty(UNIT_TEST_MODE, Boolean.FALSE.toString());
115        }
116
117        public static boolean isUnitTestModeEnabled() {
118                return Boolean.parseBoolean(System.getProperty(UNIT_TEST_MODE));
119        }
120
121        /**
122         * This property prevents stack traces from getting truncated and includes the full stack trace in failed search responses.
123         */
124        public static void enableUnitTestCaptureStack() {
125                System.setProperty(UNIT_TEST_CAPTURE_STACK, Boolean.TRUE.toString());
126        }
127
128        public static void disableUnitTestCaptureStack() {
129                System.clearProperty(HapiSystemProperties.UNIT_TEST_CAPTURE_STACK);
130        }
131
132        public static boolean isUnitTestCaptureStackEnabled() {
133                return Boolean.parseBoolean(System.getProperty(HapiSystemProperties.UNIT_TEST_CAPTURE_STACK));
134        }
135
136        public static boolean isDisableStatusBasedReindex() {
137                return Boolean.parseBoolean(System.getProperty(DISABLE_STATUS_BASED_REINDEX));
138        }
139
140        public static void disableStatusBasedReindex() {
141                System.setProperty(DISABLE_STATUS_BASED_REINDEX, Boolean.TRUE.toString());
142        }
143
144        /**
145         * This property sets {@link DaoConfig#setStatusBasedReindexingDisabled(Boolean)} to true when the system starts up.
146         */
147        public static void enableStatusBasedReindex() {
148                System.clearProperty(DISABLE_STATUS_BASED_REINDEX);
149        }
150
151        /**
152         * This property is used to suppress the logging of the HAPI FHIR version on startup.
153         */
154        // TODO KHS use this in cdr
155        public static void enableSuppressHapiFhirVersionLog() {
156                System.setProperty(SUPPRESS_HAPI_FHIR_VERSION_LOG, Boolean.TRUE.toString());
157        }
158
159        public static boolean isSuppressHapiFhirVersionLogEnabled() {
160                return Boolean.parseBoolean(System.getProperty(SUPPRESS_HAPI_FHIR_VERSION_LOG));
161        }
162
163}