View | Details | Raw Unified | Return to bug 56698
Collapse All | Expand All

(-)a/java/org/apache/catalina/session/PersistentManagerBase.java (+10 lines)
Lines 137-142 public abstract class PersistentManagerBase extends ManagerBase Link Here
137
     */
137
     */
138
    private static final String info = "PersistentManagerBase/1.1";
138
    private static final String info = "PersistentManagerBase/1.1";
139
139
140
    /**
141
     * Key of the note of a session in which the timestamp of last backup is stored.
142
     */
143
    private static final String PERSISTED_LAST_ACCESSED_TIME = "org.apache.catalina.session.PersistentManagerBase.persistedLastAccessedTime";
144
140
145
141
    /**
146
    /**
142
     * The descriptive name of this Manager implementation (for logging).
147
     * The descriptive name of this Manager implementation (for logging).
Lines 1021-1026 public abstract class PersistentManagerBase extends ManagerBase Link Here
1021
                synchronized (session) {
1026
                synchronized (session) {
1022
                    if (!session.isValid())
1027
                    if (!session.isValid())
1023
                        continue;
1028
                        continue;
1029
                    long lastAccessedTime = session.getLastAccessedTime();
1030
                    Long persistedLastAccessedTime = (Long) session.getNote(PERSISTED_LAST_ACCESSED_TIME);
1031
                    if (persistedLastAccessedTime != null && lastAccessedTime == persistedLastAccessedTime.longValue())
1032
                        continue;
1024
                    int timeIdle;
1033
                    int timeIdle;
1025
                    if (StandardSession.LAST_ACCESS_AT_START) {
1034
                    if (StandardSession.LAST_ACCESS_AT_START) {
1026
                        timeIdle = (int) ((timeNow - session.getLastAccessedTime()) / 1000L);
1035
                        timeIdle = (int) ((timeNow - session.getLastAccessedTime()) / 1000L);
Lines 1039-1044 public abstract class PersistentManagerBase extends ManagerBase Link Here
1039
                        } catch (IOException e) {
1048
                        } catch (IOException e) {
1040
                            // This is logged in writeSession()
1049
                            // This is logged in writeSession()
1041
                        }
1050
                        }
1051
                        session.setNote(PERSISTED_LAST_ACCESSED_TIME, Long.valueOf(lastAccessedTime));
1042
                    }
1052
                    }
1043
                }
1053
                }
1044
            }
1054
            }
(-)a/test/org/apache/catalina/session/TestPersistentManager.java (+191 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package org.apache.catalina.session;
18
19
import java.beans.PropertyChangeListener;
20
import java.io.IOException;
21
import java.util.ArrayList;
22
import java.util.Arrays;
23
import java.util.List;
24
25
import javax.servlet.ServletException;
26
import javax.servlet.http.HttpServlet;
27
import javax.servlet.http.HttpServletRequest;
28
import javax.servlet.http.HttpServletResponse;
29
30
import org.apache.catalina.Context;
31
import org.apache.catalina.LifecycleException;
32
import org.apache.catalina.Manager;
33
import org.apache.catalina.Session;
34
import org.apache.catalina.Store;
35
import org.apache.catalina.startup.Tomcat;
36
import org.apache.catalina.startup.TomcatBaseTest;
37
import org.junit.After;
38
import org.junit.Assert;
39
import org.junit.Before;
40
import org.junit.Test;
41
42
public class TestPersistentManager extends TomcatBaseTest {
43
44
    private final String ACTIVITY_CHECK = "org.apache.catalina.session.StandardSession.ACTIVITY_CHECK";
45
46
    private String oldActivityCheck;
47
48
    @Before
49
    public void setActivityCheck() {
50
        oldActivityCheck = System.setProperty(ACTIVITY_CHECK, "true");
51
    }
52
53
    @After
54
    public void resetActivityCheck() {
55
        if (oldActivityCheck != null) {
56
            System.setProperty(ACTIVITY_CHECK, oldActivityCheck);
57
        } else {
58
            System.clearProperty(ACTIVITY_CHECK);
59
        }
60
    }
61
62
    @Test
63
    public void backsUpOnce() throws IOException, LifecycleException {
64
65
        // Setup Tomcat instance
66
        Tomcat tomcat = getTomcatInstance();
67
        // Must have a real docBase - just use temp
68
        Context ctx = tomcat.addContext("",
69
                System.getProperty("java.io.tmpdir"));
70
71
        Tomcat.addServlet(ctx, "DummyServlet", new DummyServlet());
72
        ctx.addServletMapping("/dummy", "DummyServlet");
73
74
        PersistentManager manager = new PersistentManager();
75
        DummyStore store = new DummyStore();
76
77
        manager.setStore(store);
78
        manager.setMaxIdleBackup(0);
79
        manager.setDistributable(true);
80
        ctx.setManager(manager);
81
        tomcat.start();
82
        String sessionId = getUrl("http://localhost:" + getPort() + "/dummy")
83
                .toString();
84
        sleepABit();
85
86
        manager.processPersistenceChecks();
87
        Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds());
88
89
        // session was not accessed, so no save will be performed
90
        manager.processPersistenceChecks();
91
        Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds());
92
93
        // access session
94
        manager.findSession(sessionId).access();
95
        manager.findSession(sessionId).endAccess();
96
        sleepABit();
97
98
        // session was accessed, so it will be saved once again
99
        manager.processPersistenceChecks();
100
        Assert.assertEquals(Arrays.asList(sessionId, sessionId),
101
                store.getSavedIds());
102
103
        manager.processPersistenceChecks();
104
        Assert.assertEquals(Arrays.asList(sessionId, sessionId),
105
                store.getSavedIds());
106
    }
107
108
    private void sleepABit() {
109
        try {
110
            Thread.sleep(1000);
111
        } catch (InterruptedException e) {
112
            // ignore Interrupt
113
        }
114
    }
115
116
    private static class DummyServlet extends HttpServlet {
117
118
        private static final long serialVersionUID = -3696433049266123995L;
119
120
        @Override
121
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
122
                throws ServletException, IOException {
123
            resp.getWriter().print(req.getSession().getId());
124
        }
125
126
    }
127
128
    private static class DummyStore implements Store {
129
130
        private Manager manager;
131
        private List<String> savedIds = new ArrayList<String>();
132
133
        List<String> getSavedIds() {
134
            return savedIds;
135
        }
136
137
        @Override
138
        public String getInfo() {
139
            return "DummyStore/1.0";
140
        }
141
142
        @Override
143
        public Manager getManager() {
144
            return this.manager;
145
        }
146
147
        @Override
148
        public void setManager(Manager manager) {
149
            this.manager = manager;
150
        }
151
152
        @Override
153
        public int getSize() throws IOException {
154
            return 0;
155
        }
156
157
        @Override
158
        public void addPropertyChangeListener(PropertyChangeListener listener) {
159
        }
160
161
        @Override
162
        public String[] keys() throws IOException {
163
            return null;
164
        }
165
166
        @Override
167
        public Session load(String id) throws ClassNotFoundException,
168
                IOException {
169
            // TODO Auto-generated method stub
170
            return null;
171
        }
172
173
        @Override
174
        public void remove(String id) throws IOException {
175
        }
176
177
        @Override
178
        public void clear() throws IOException {
179
        }
180
181
        @Override
182
        public void removePropertyChangeListener(PropertyChangeListener listener) {
183
        }
184
185
        @Override
186
        public void save(Session session) throws IOException {
187
            savedIds.add(session.getId());
188
        }
189
190
    }
191
}

Return to bug 56698