1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.session;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import jakarta.servlet.ServletContext;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Loader;
import org.apache.catalina.Session;
import org.apache.catalina.util.CustomObjectInputStream;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.ExceptionUtils;
/**
* Standard implementation of the <b>Manager</b> interface that provides simple session persistence across restarts of
* this component (such as when the entire server is shut down and restarted, or when a particular web application is
* reloaded).
* <p>
* <b>IMPLEMENTATION NOTE</b>: Correct behavior of session storing and reloading depends upon external calls to the
* <code>start()</code> and <code>stop()</code> methods of this class at the correct times.
*
* @author Craig R. McClanahan
*/
public class StandardManager extends ManagerBase {
private final Log log = LogFactory.getLog(StandardManager.class); // must not be static
// ----------------------------------------------------- Instance Variables
/**
* The descriptive name of this Manager implementation (for logging).
*/
protected static final String name = "StandardManager";
/**
* Path name of the disk file in which active sessions are saved when we stop, and from which these sessions are
* loaded when we start. A <code>null</code> value indicates that no persistence is desired. If this pathname is
* relative, it will be resolved against the temporary working directory provided by our context, available via the
* <code>jakarta.servlet.context.tempdir</code> context attribute.
*/
protected String pathname = null;
// ------------------------------------------------------------- Properties
@Override
public String getName() {
return name;
}
/**
* @return The session persistence pathname, if any.
*/
public String getPathname() {
return pathname;
}
/**
* Set the session persistence pathname to the specified value. If no persistence support is desired, set the
* pathname to <code>null</code>.
*
* @param pathname New session persistence pathname
*/
public void setPathname(String pathname) {
String oldPathname = this.pathname;
this.pathname = pathname;
support.firePropertyChange("pathname", oldPathname, this.pathname);
}
// --------------------------------------------------------- Public Methods
@Override
public void load() throws ClassNotFoundException, IOException {
if (log.isTraceEnabled()) {
log.trace("Start: Loading persisted sessions");
}
// Initialize our internal data structures
sessions.clear();
// Open an input stream to the specified pathname, if any
File file = file();
if (file == null) {
return;
}
if (log.isTraceEnabled()) {
log.trace(sm.getString("standardManager.loading", pathname));
}
Loader loader;
ClassLoader classLoader = null;
Log logger;
try (FileInputStream fis = new FileInputStream(file.getAbsolutePath());
BufferedInputStream bis = new BufferedInputStream(fis)) {
Context c = getContext();
loader = c.getLoader();
logger = c.getLogger();
if (loader != null) {
classLoader = loader.getClassLoader();
}
if (classLoader == null) {
classLoader = getClass().getClassLoader();
}
// Load the previously unloaded active sessions
synchronized (sessions) {
try (ObjectInputStream ois = new CustomObjectInputStream(bis, classLoader, logger,
getSessionAttributeValueClassNamePattern(), getWarnOnSessionAttributeFilterFailure())) {
Integer count = (Integer) ois.readObject();
int n = count.intValue();
if (log.isTraceEnabled()) {
log.trace("Loading " + n + " persisted sessions");
}
for (int i = 0; i < n; i++) {
StandardSession session = getNewSession();
session.readObjectData(ois);
session.setManager(this);
sessions.put(session.getIdInternal(), session);
session.activate();
if (!session.isValidInternal()) {
// If session is already invalid,
// expire session to prevent memory leak.
session.setValid(true);
session.expire();
}
}
} finally {
// Delete the persistent storage file
if (file.exists()) {
if (!file.delete()) {
log.warn(sm.getString("standardManager.deletePersistedFileFail", file));
}
}
}
}
} catch (FileNotFoundException e) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("standardManager.noFile", file.getAbsolutePath()));
}
return;
}
if (log.isTraceEnabled()) {
log.trace("Finish: Loading persisted sessions");
}
}
@Override
public void unload() throws IOException {
if (log.isTraceEnabled()) {
log.trace(sm.getString("standardManager.unloading.debug"));
}
if (sessions.isEmpty()) {
log.debug(sm.getString("standardManager.unloading.nosessions"));
return; // nothing to do
}
// Open an output stream to the specified pathname, if any
File file = file();
if (file == null) {
return;
}
if (log.isTraceEnabled()) {
log.trace(sm.getString("standardManager.unloading", pathname));
}
// Keep a note of sessions that are expired
List<StandardSession> list = new ArrayList<>();
try (FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
synchronized (sessions) {
if (log.isTraceEnabled()) {
log.trace("Unloading " + sessions.size() + " sessions");
}
// Write the number of active sessions, followed by the details
oos.writeObject(Integer.valueOf(sessions.size()));
for (Session s : sessions.values()) {
StandardSession session = (StandardSession) s;
list.add(session);
session.passivate();
session.writeObjectData(oos);
}
}
}
// Expire all the sessions we just wrote
if (log.isDebugEnabled()) {
log.debug(sm.getString("standardManager.expiringSessions", Integer.toString(list.size())));
}
for (StandardSession session : list) {
try {
session.expire(false);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
} finally {
session.recycle();
}
}
if (log.isTraceEnabled()) {
log.trace("Unloading complete");
}
}
/**
* Start this component and implement the requirements of
* {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
*
* @exception LifecycleException if this component detects a fatal error that prevents this component from being
* used
*/
@Override
protected void startInternal() throws LifecycleException {
super.startInternal();
// Load unloaded sessions, if any
try {
load();
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
log.error(sm.getString("standardManager.managerLoad"), t);
}
setState(LifecycleState.STARTING);
}
/**
* Stop this component and implement the requirements of
* {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
*
* @exception LifecycleException if this component detects a fatal error that prevents this component from being
* used
*/
@Override
protected void stopInternal() throws LifecycleException {
if (log.isTraceEnabled()) {
log.trace("Stopping");
}
setState(LifecycleState.STOPPING);
// Write out sessions
try {
unload();
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
log.error(sm.getString("standardManager.managerUnload"), t);
}
// Expire all active sessions
Session[] sessions = findSessions();
for (Session session : sessions) {
try {
if (session.isValid()) {
session.expire();
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
} finally {
// Measure against memory leaking if references to the session
// object are kept in a shared field somewhere
session.recycle();
}
}
// Require a new random number generator if we are restarted
super.stopInternal();
}
// ------------------------------------------------------ Protected Methods
/**
* Return a File object representing the pathname to our persistence file, if any.
*
* @return the file
*/
protected File file() {
if (pathname == null || pathname.isEmpty()) {
return null;
}
File file = new File(pathname);
if (!file.isAbsolute()) {
Context context = getContext();
ServletContext servletContext = context.getServletContext();
File tempdir = (File) servletContext.getAttribute(ServletContext.TEMPDIR);
if (tempdir != null) {
file = new File(tempdir, pathname);
}
}
return file;
}
}
|