001/*-
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2024 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 =
031                        "TEST_SYSTEM_PROP_VALIDATION_RESOURCE_CACHES_MS";
032
033        static final String UNIT_TEST_CAPTURE_STACK = "unit_test_capture_stack";
034        static final String STACKFILTER_PATTERN_PROP = "log.stackfilter.pattern";
035        static final String HAPI_CLIENT_KEEPRESPONSES = "hapi.client.keepresponses";
036        static final String TEST_MODE = "test";
037        static final String UNIT_TEST_MODE = "unit_test_mode";
038        static final long DEFAULT_TEST_SYSTEM_PROP_VALIDATION_RESOURCE_CACHES_MS = 10 * DateUtils.MILLIS_PER_SECOND;
039        static final String PREVENT_INVALIDATING_CONDITIONAL_MATCH_CRITERIA =
040                        "hapi.storage.prevent_invalidating_conditional_match_criteria";
041
042        private HapiSystemProperties() {}
043
044        /**
045         * This property is used by unit tests - do not rely on it in production code
046         * as it may change at any time. If you want to capture responses in a reliable
047         * way in your own code, just use client interceptors
048         */
049        public static void enableHapiClientKeepResponses() {
050                System.setProperty(HAPI_CLIENT_KEEPRESPONSES, Boolean.TRUE.toString());
051        }
052
053        public static void disableHapiClientKeepResponses() {
054                System.clearProperty(HAPI_CLIENT_KEEPRESPONSES);
055        }
056
057        /**
058         * This property is used by unit tests - do not rely on it in production code
059         * as it may change at any time. If you want to capture responses in a reliable
060         * way in your own code, just use client interceptors
061         */
062        public static boolean isHapiClientKeepResponsesEnabled() {
063                return (Boolean.parseBoolean(System.getProperty(HAPI_CLIENT_KEEPRESPONSES)));
064        }
065
066        /**
067         * This property gets used in the logback.xml file.
068         * It causes logged stack traces to skip a number of packages that are
069         * just noise.
070         */
071        public static void setStackFilterPattern(String thePattern) {
072                System.setProperty(STACKFILTER_PATTERN_PROP, thePattern);
073        }
074
075        /**
076         * Set the validation resource cache expireAfterWrite timeout in milliseconds
077         *
078         * @param theMillis
079         */
080        public static void setTestValidationResourceCachesMs(long theMillis) {
081                System.setProperty(TEST_SYSTEM_PROP_VALIDATION_RESOURCE_CACHES_MS, "" + theMillis);
082        }
083
084        /**
085         * Get the validation resource cache expireAfterWrite timeout in milliseconds.  If it has not been set, the default
086         * value is 10 seconds.
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.
109         */
110        public static void enableUnitTestMode() {
111                System.setProperty(UNIT_TEST_MODE, Boolean.TRUE.toString());
112        }
113
114        public static void disableUnitTestMode() {
115                System.setProperty(UNIT_TEST_MODE, Boolean.FALSE.toString());
116        }
117
118        public static boolean isUnitTestModeEnabled() {
119                return Boolean.parseBoolean(System.getProperty(UNIT_TEST_MODE));
120        }
121
122        /**
123         * This property prevents stack traces from getting truncated and includes the full stack trace in failed search responses.
124         */
125        public static void enableUnitTestCaptureStack() {
126                System.setProperty(UNIT_TEST_CAPTURE_STACK, Boolean.TRUE.toString());
127        }
128
129        public static void disableUnitTestCaptureStack() {
130                System.clearProperty(HapiSystemProperties.UNIT_TEST_CAPTURE_STACK);
131        }
132
133        public static boolean isUnitTestCaptureStackEnabled() {
134                return Boolean.parseBoolean(System.getProperty(HapiSystemProperties.UNIT_TEST_CAPTURE_STACK));
135        }
136
137        public static boolean isDisableStatusBasedReindex() {
138                return Boolean.parseBoolean(System.getProperty(DISABLE_STATUS_BASED_REINDEX));
139        }
140
141        public static void disableStatusBasedReindex() {
142                System.setProperty(DISABLE_STATUS_BASED_REINDEX, Boolean.TRUE.toString());
143        }
144
145        /**
146         * This property sets {@link JpaStorageSettings#setStatusBasedReindexingDisabled(Boolean)} to true when the system starts up.
147         */
148        public static void enableStatusBasedReindex() {
149                System.clearProperty(DISABLE_STATUS_BASED_REINDEX);
150        }
151
152        /**
153         * This property is used to suppress the logging of the HAPI FHIR version on startup.
154         */
155        // TODO KHS use this in cdr
156        public static void enableSuppressHapiFhirVersionLog() {
157                System.setProperty(SUPPRESS_HAPI_FHIR_VERSION_LOG, Boolean.TRUE.toString());
158        }
159
160        public static boolean isSuppressHapiFhirVersionLogEnabled() {
161                return Boolean.parseBoolean(System.getProperty(SUPPRESS_HAPI_FHIR_VERSION_LOG));
162        }
163
164        public static boolean isPreventInvalidatingConditionalMatchCriteria() {
165                return Boolean.parseBoolean(System.getProperty(
166                                HapiSystemProperties.PREVENT_INVALIDATING_CONDITIONAL_MATCH_CRITERIA, Boolean.FALSE.toString()));
167        }
168}