• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 44614243903

19 Nov 2026 08:59PM UTC coverage: 69.114% (-0.2%) from 69.299%
44614243903

Pull #6841

github

web-flow
Merge 5b767c47c into 57615988d
Pull Request #6841: TRUNK-6868: Allow the session timeout to be configured (2.8.x)

24 of 27 new or added lines in 2 files covered. (88.89%)

89 existing lines in 15 files now uncovered.

26837 of 38830 relevant lines covered (69.11%)

0.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.31
/web/src/main/java/org/openmrs/web/SessionTimeoutHttpSessionListener.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.web;
11

12
import javax.servlet.http.HttpSessionEvent;
13
import javax.servlet.http.HttpSessionListener;
14

15
import org.openmrs.util.ConfigUtil;
16
import org.openmrs.util.OpenmrsConstants;
17
import org.slf4j.Logger;
18
import org.slf4j.LoggerFactory;
19
import org.springframework.stereotype.Component;
20
import org.springframework.util.StringUtils;
21

22
/**
23
 * Applies the configured session timeout to each new session. The value is loaded once at startup,
24
 * after modules start, and kept statically so it survives Spring context refreshes.
25
 *
26
 * @see OpenmrsConstants#SESSION_TIMEOUT_PROPERTY
27
 * @since 2.8.11
28
 */
29
@Component
30
public class SessionTimeoutHttpSessionListener implements HttpSessionListener {
1 ✔
31

32
        private static final Logger log = LoggerFactory.getLogger(SessionTimeoutHttpSessionListener.class);
1 ✔
33

34
        private static volatile Integer sessionTimeoutInMinutes = null;
1 ✔
35

36
        @Override
37
        public void sessionCreated(HttpSessionEvent se) {
38
                Integer timeout = sessionTimeoutInMinutes;
1 ✔
39
                if (timeout != null) {
1 ✔
40
                        se.getSession().setMaxInactiveInterval(timeout * 60);
1 ✔
41
                }
42
        }
1 ✔
43

44
        /**
45
         * Reads the session timeout from the system, runtime, or global properties. Never throws, so it
46
         * cannot fail startup.
47
         */
48
        public static void loadSessionTimeout() {
49
                try {
50
                        setSessionTimeout(ConfigUtil.getProperty(OpenmrsConstants.SESSION_TIMEOUT_PROPERTY));
1 ✔
NEW
51
                } catch (Exception e) {
×
NEW
52
                        log.warn("Unable to load the {} property", OpenmrsConstants.SESSION_TIMEOUT_PROPERTY, e);
×
53
                }
1 ✔
54
        }
1 ✔
55

56
        /**
57
         * Sets the timeout, in minutes, applied to each new session. Blank or invalid values leave the
58
         * servlet container's default in place.
59
         *
60
         * @param value the timeout in minutes, may be null
61
         */
62
        static void setSessionTimeout(String value) {
63
                if (!StringUtils.hasText(value)) {
1 ✔
64
                        sessionTimeoutInMinutes = null;
1 ✔
65
                        return;
1 ✔
66
                }
67
                int minutes;
68
                try {
69
                        minutes = Integer.parseInt(value.trim());
1 ✔
70
                } catch (NumberFormatException e) {
1 ✔
71
                        minutes = -1;
1 ✔
72
                }
1 ✔
73
                if (minutes <= 0 || minutes > Integer.MAX_VALUE / 60) {
1 ✔
74
                        sessionTimeoutInMinutes = null;
1 ✔
75
                        log.warn("Ignoring invalid {} property value '{}'; it must be a positive number of minutes",
1 ✔
76
                            OpenmrsConstants.SESSION_TIMEOUT_PROPERTY, value);
77
                        return;
1 ✔
78
                }
79
                sessionTimeoutInMinutes = minutes;
1 ✔
80
                log.info("Using a session timeout of {} minute(s) from the {} property", minutes,
1 ✔
81
                    OpenmrsConstants.SESSION_TIMEOUT_PROPERTY);
82
        }
1 ✔
83
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc