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

openmrs / openmrs-core / 44617254895

19 Nov 2026 09:17PM UTC coverage: 69.102% (-0.2%) from 69.299%
44617254895

Pull #6841

github

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

24 of 28 new or added lines in 2 files covered. (85.71%)

88 existing lines in 14 files now uncovered.

26833 of 38831 relevant lines covered (69.1%)

0.69 hits per line

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

88.89
/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
        // explicit no-op: Servlet 3.1 containers (Tomcat 8.5, Jetty 9.4) have no default implementation
45
        @Override
46
        public void sessionDestroyed(HttpSessionEvent se) {
NEW
47
        }
×
48

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

61
        /**
62
         * Sets the timeout, in minutes, applied to each new session. Blank or invalid values leave the
63
         * servlet container's default in place.
64
         *
65
         * @param value the timeout in minutes, may be null
66
         */
67
        static void setSessionTimeout(String value) {
68
                if (!StringUtils.hasText(value)) {
1 ✔
69
                        sessionTimeoutInMinutes = null;
1 ✔
70
                        return;
1 ✔
71
                }
72
                int minutes;
73
                try {
74
                        minutes = Integer.parseInt(value.trim());
1 ✔
75
                } catch (NumberFormatException e) {
1 ✔
76
                        minutes = -1;
1 ✔
77
                }
1 ✔
78
                if (minutes <= 0 || minutes > Integer.MAX_VALUE / 60) {
1 ✔
79
                        sessionTimeoutInMinutes = null;
1 ✔
80
                        log.warn("Ignoring invalid {} property value '{}'; it must be a positive number of minutes",
1 ✔
81
                            OpenmrsConstants.SESSION_TIMEOUT_PROPERTY, value);
82
                        return;
1 ✔
83
                }
84
                sessionTimeoutInMinutes = minutes;
1 ✔
85
                log.info("Using a session timeout of {} minute(s) from the {} property", minutes,
1 ✔
86
                    OpenmrsConstants.SESSION_TIMEOUT_PROPERTY);
87
        }
1 ✔
88
}
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