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