This is an automated email from the ASF dual-hosted git repository. mgrigorov pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/wicket.git
commit e4c560dfc355c53fae945188bf9b5023ebd6c4a0 Author: Martin Tzvetanov Grigorov <mgrigo...@apache.org> AuthorDate: Thu Mar 26 09:14:23 2020 +0200 WICKET-6751 Minor improvements Add licence headers Check for null before using 'previous' page lock --- .../apache/wicket/page/DefaultPageLockManager.java | 56 +++++++++++++++------- .../org/apache/wicket/page/IPageLockManager.java | 18 ++++++- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/wicket-core/src/main/java/org/apache/wicket/page/DefaultPageLockManager.java b/wicket-core/src/main/java/org/apache/wicket/page/DefaultPageLockManager.java index 7db9a36..45fbd71 100644 --- a/wicket-core/src/main/java/org/apache/wicket/page/DefaultPageLockManager.java +++ b/wicket-core/src/main/java/org/apache/wicket/page/DefaultPageLockManager.java @@ -1,6 +1,21 @@ +/* + * 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.wicket.page; -import java.io.Serializable; import java.time.Duration; import java.time.Instant; import java.util.Iterator; @@ -11,22 +26,23 @@ import java.util.function.Supplier; import org.apache.wicket.Application; import org.apache.wicket.settings.ExceptionSettings; import org.apache.wicket.util.LazyInitializer; +import org.apache.wicket.util.lang.Args; import org.apache.wicket.util.lang.Threads; import org.apache.wicket.util.time.Durations; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Default {@link IPageLockManager} that that holds a map of locks in the current session. + * Default {@link IPageLockManager} that holds a map of locks in the current session. */ -public class DefaultPageLockManager implements IPageLockManager, Serializable { +public class DefaultPageLockManager implements IPageLockManager { private static final long serialVersionUID = 1L; private static final Logger logger = LoggerFactory.getLogger(DefaultPageLockManager.class); /** map of which pages are owned by which threads */ - private final Supplier<ConcurrentMap<Integer, PageAccessSynchronizer.PageLock>> locks = new LazyInitializer<ConcurrentMap<Integer, PageAccessSynchronizer.PageLock>>() + private final LazyInitializer<ConcurrentMap<Integer, PageAccessSynchronizer.PageLock>> locks = new LazyInitializer<>() { private static final long serialVersionUID = 1L; @@ -48,7 +64,7 @@ public class DefaultPageLockManager implements IPageLockManager, Serializable { */ public DefaultPageLockManager(Duration timeout) { - this.timeout = timeout; + this.timeout = Args.notNull(timeout, "timeout"); } private static long remaining(Instant start, Duration timeout) @@ -80,9 +96,9 @@ public class DefaultPageLockManager implements IPageLockManager, Serializable { PageAccessSynchronizer.PageLock previous = null; - Duration timeout = getTimeout(pageId); + Duration pageTimeout = getTimeout(pageId); - while (!locked && Durations.elapsedSince(start).compareTo(timeout) < 0) + while (!locked && Durations.elapsedSince(start).compareTo(pageTimeout) < 0) { if (isDebugEnabled) { @@ -100,7 +116,7 @@ public class DefaultPageLockManager implements IPageLockManager, Serializable { else { // wait for a lock to become available - long remaining = remaining(start, timeout); + long remaining = remaining(start, pageTimeout); if (remaining > 0) { previous.waitForRelease(remaining, isDebugEnabled); @@ -119,11 +135,11 @@ public class DefaultPageLockManager implements IPageLockManager, Serializable { { if (logger.isWarnEnabled()) { + final String previousThreadName = previous != null ? previous.getThread().getName() : "N/A"; logger.warn( "Thread '{}' failed to acquire lock to page with id '{}', attempted for {} out of allowed {}." + " The thread that holds the lock has name '{}'.", - thread.getName(), pageId, Duration.between(start, Instant.now()), timeout, - previous.getThread().getName()); + thread.getName(), pageId, Duration.between(start, Instant.now()), pageTimeout, previousThreadName); if (Application.exists()) { ExceptionSettings.ThreadDumpStrategy strategy = Application.get() @@ -135,7 +151,15 @@ public class DefaultPageLockManager implements IPageLockManager, Serializable { Threads.dumpAllThreads(logger); break; case THREAD_HOLDING_LOCK : - Threads.dumpSingleThread(logger, previous.getThread()); + final Thread previousThread = previous != null ? previous.getThread() : null; + if (previousThread != null) + { + Threads.dumpSingleThread(logger, previousThread); + } + else + { + logger.warn("Cannot dump the stack of the previous thread because it is not available."); + } break; case NO_THREADS : default : @@ -143,7 +167,7 @@ public class DefaultPageLockManager implements IPageLockManager, Serializable { } } } - throw new CouldNotLockPageException(pageId, thread.getName(), timeout); + throw new CouldNotLockPageException(pageId, thread.getName(), pageTimeout); } } @@ -162,18 +186,18 @@ public class DefaultPageLockManager implements IPageLockManager, Serializable { private void internalUnlockPages(final Integer pageId) { final Thread thread = Thread.currentThread(); - final Iterator<PageAccessSynchronizer.PageLock> locks = this.locks.get().values().iterator(); + final Iterator<PageAccessSynchronizer.PageLock> pageLockIterator = this.locks.get().values().iterator(); final boolean isDebugEnabled = logger.isDebugEnabled(); - while (locks.hasNext()) + while (pageLockIterator.hasNext()) { // remove all locks held by this thread if 'pageId' is not specified // otherwise just the lock for this 'pageId' - final PageAccessSynchronizer.PageLock lock = locks.next(); + final PageAccessSynchronizer.PageLock lock = pageLockIterator.next(); if ((pageId == null || pageId == lock.getPageId()) && lock.getThread() == thread) { - locks.remove(); + pageLockIterator.remove(); if (isDebugEnabled) { logger.debug("'{}' released lock to page with id '{}'", thread.getName(), diff --git a/wicket-core/src/main/java/org/apache/wicket/page/IPageLockManager.java b/wicket-core/src/main/java/org/apache/wicket/page/IPageLockManager.java index 45c70bd..63af9d8 100644 --- a/wicket-core/src/main/java/org/apache/wicket/page/IPageLockManager.java +++ b/wicket-core/src/main/java/org/apache/wicket/page/IPageLockManager.java @@ -1,3 +1,19 @@ +/* + * 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.wicket.page; import java.io.Serializable; @@ -6,7 +22,7 @@ import java.io.Serializable; * Lock manager for {@link PageAccessSynchronizer} responsible for locking and unlocking pages for * the duration of a request. */ -public interface IPageLockManager +public interface IPageLockManager extends Serializable { /**