You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2026/09/16 07:09:18 UTC
(tomcat) branch main updated (af8c930545 -> cf892f9dbd)
This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git
from af8c930545 Add missing JMX operations
new 6fe265aded Fix various concurrency issues around adding and removing Realms
new cf892f9dbd Add test cases written by Claude / Opus 5
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
java/org/apache/catalina/realm/CombinedRealm.java | 198 ++++++++---
.../apache/catalina/realm/LocalStrings.properties | 3 +
.../apache/catalina/realm/TestCombinedRealm.java | 392 +++++++++++++++++++++
webapps/docs/changelog.xml | 9 +
4 files changed, 548 insertions(+), 54 deletions(-)
create mode 100644 test/org/apache/catalina/realm/TestCombinedRealm.java
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
(tomcat) 01/02: Fix various concurrency issues around adding and removing Realms
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 6fe265aded6705fb63478082e706ee46fac79f65
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Sep 16 08:08:41 2026 +0100
Fix various concurrency issues around adding and removing Realms
This makes it much safer to add and/or remove nested realms at runtime.
This is a further follow-on to "Add removeRealm, matching addRealm"
---
java/org/apache/catalina/realm/CombinedRealm.java | 198 +++++++++++++++------
.../apache/catalina/realm/LocalStrings.properties | 3 +
webapps/docs/changelog.xml | 9 +
3 files changed, 156 insertions(+), 54 deletions(-)
diff --git a/java/org/apache/catalina/realm/CombinedRealm.java b/java/org/apache/catalina/realm/CombinedRealm.java
index 78b7793018..19c1516fce 100644
--- a/java/org/apache/catalina/realm/CombinedRealm.java
+++ b/java/org/apache/catalina/realm/CombinedRealm.java
@@ -18,9 +18,10 @@ package org.apache.catalina.realm;
import java.security.Principal;
import java.security.cert.X509Certificate;
-import java.util.ArrayList;
-import java.util.Iterator;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
import javax.management.ObjectName;
@@ -40,71 +41,126 @@ import org.ietf.jgss.GSSName;
/**
* Realm implementation that contains one or more realms. Authentication is attempted for each realm in the order they
- * were configured. If any realm authenticates the user then the authentication succeeds. When combining realms
- * usernames should be unique across all combined realms.
+ * were configured. If any realm authenticates the user then the authentication succeeds. When combining realms user
+ * names should be unique across all combined realms.
+ * <p>
+ * For the typical usage (create realms, add realms to container, start container), each realm will be registered with
+ * JMX and {@link #getRealmPath()} will match the JMX object name. For simple changes (remove a realm, add a new realm)
+ * {@link #getRealmPath()} and the JMX object name will remain synchronized. For more unusual changes such as moving the
+ * realm to a new container, the JMX object names and {@link #getRealmPath()} are very likely to end up out of sync. If
+ * there are naming conflicts it is possible for realms that are still in use to not be registered in JMX.
*/
public class CombinedRealm extends RealmBase {
+ private static final Log log = LogFactory.getLog(CombinedRealm.class);
+
/**
- * Default constructor for CombinedRealm.
+ * The list of Realms contained by this Realm.
*/
- public CombinedRealm() {
- }
+ protected final List<Realm> realms = new CopyOnWriteArrayList<>();
+
+ private final Set<Realm> realmsToDestroy = new HashSet<>();
+
+ private int nextRealmIndex = 0;
- private static final Log log = LogFactory.getLog(CombinedRealm.class);
/**
- * The list of Realms contained by this Realm.
+ * Default constructor for CombinedRealm.
*/
- protected final List<Realm> realms = new ArrayList<>();
+ public CombinedRealm() {
+ }
+
/**
* Add a realm to the list of realms that will be used to authenticate users.
*
- * @param theRealm realm which should be wrapped by the combined realm
+ * @param realm Realm which should be added to the combined realm
*/
- public void addRealm(Realm theRealm) {
- realms.add(theRealm);
+ public synchronized void addRealm(Realm realm) {
+
+ if (realms.contains(realm)) {
+ if (log.isDebugEnabled()) {
+ log.debug(sm.getString("combinedRealm.addRealmDuplicate", realm));
+ }
+ return;
+ }
+
+ boolean addRealm = true;
- if (log.isDebugEnabled()) {
- log.debug(sm.getString("combinedRealm.addRealm", theRealm.getClass().getName(),
- Integer.toString(realms.size())));
+ setSubRealmPath(realm);
+
+ if (getState().isAvailable()) {
+ addRealm = startRealm(realm);
+ }
+ if (addRealm) {
+ nextRealmIndex++;
+ realms.add(realm);
+ // In case the Realm has been removed and then re-added
+ realmsToDestroy.remove(realm);
+ if (log.isDebugEnabled()) {
+ log.debug(sm.getString("combinedRealm.addRealm", realm.getClass().getName(),
+ Integer.toString(realms.size())));
+ }
+ } else {
+ destroyRealm(realm);
+ }
+ }
+
+
+ private boolean startRealm(Realm realm) {
+ realm.setContainer(getContainer());
+ if (realm instanceof Lifecycle) {
+ try {
+ ((Lifecycle) realm).start();
+ } catch (LifecycleException e) {
+ log.error(sm.getString("combinedRealm.realmStartFail", realm.getClass().getName()), e);
+ return false;
+ }
}
+ return true;
}
/**
- * Remove a realm from the list of realms that are used to authenticate
- * users.
+ * Remove a realm from the list of realms that are used to authenticate users.
*
- * @param theRealm realm which should no longer be wrapped by the combined
- * realm
+ * @param realm Realm which should be removed from the combined realm
*
- * @return {@code true} if the realm was present and removed,
- * {@code false} otherwise
+ * @return {@code true} if the realm was present and removed, {@code false} otherwise
*/
- public boolean removeRealm(Realm theRealm) {
- boolean removed = realms.remove(theRealm);
-
- if (removed && log.isDebugEnabled()) {
- log.debug(sm.getString("combinedRealm.removeRealm", theRealm.getClass().getName(),
- Integer.toString(realms.size())));
+ public synchronized boolean removeRealm(Realm realm) {
+ boolean removed = realms.remove(realm);
+
+ if (removed) {
+ if (getState().isAvailable()) {
+ /*
+ * This realm could be being used in an authenticate() call. Delay destroying it until after the
+ * combined realm has stopped.
+ */
+ realmsToDestroy.add(realm);
+ } else {
+ destroyRealm(realm);
+ }
+ if (log.isDebugEnabled()) {
+ log.debug(sm.getString("combinedRealm.removeRealm", realm.getClass().getName(),
+ Integer.toString(realms.size())));
+ }
}
-
return removed;
}
/**
- * Returns the JMX ObjectNames of the realms that this realm is wrapping.
- * Entries for realms that do not implement LifecycleMBeanBase will be null.
+ * Returns the JMX ObjectNames of the realms that this realm is wrapping. Entries for realms that do not implement
+ * LifecycleMBeanBase will be null.
*
* @return the array of realm ObjectNames, which may contain null entries
*/
public ObjectName[] getRealms() {
- ObjectName[] result = new ObjectName[realms.size()];
+ Realm[] realmsSnapshot = getNestedRealms();
+ ObjectName[] result = new ObjectName[realmsSnapshot.length];
int i = 0;
- for (Realm realm : realms) {
+ for (Realm realm : realmsSnapshot) {
if (realm instanceof LifecycleMBeanBase) {
result[i] = ((LifecycleMBeanBase) realm).getObjectName();
}
@@ -206,35 +262,45 @@ public class CombinedRealm extends RealmBase {
@Override
public void setContainer(Container container) {
- int i = 0;
for (Realm realm : realms) {
- // Set the realmPath for JMX naming
- if (realm instanceof RealmBase) {
- ((RealmBase) realm).setRealmPath(getRealmPath() + "/realm" + Integer.toString(i));
- }
// Set the container for sub-realms. Mainly so logging works.
realm.setContainer(container);
- i++;
}
super.setContainer(container);
}
+ /**
+ * {@inheritDoc}
+ * <p>
+ * Calling this method will also (re)set the paths for all of the nested realms. If a nested realm has been removed
+ * this will result in the remaining nested realms being re-numbered which may create an inconsistency between the
+ * nested realm's path and its JMX registration (if any).
+ */
+ @Override
+ public synchronized void setRealmPath(String theRealmPath) {
+ super.setRealmPath(theRealmPath);
+ nextRealmIndex = 0;
+ for (Realm realm : realms) {
+ setSubRealmPath(realm);
+ nextRealmIndex++;
+ }
+ }
+
+
+ private void setSubRealmPath(Realm realm) {
+ if (realm instanceof RealmBase) {
+ ((RealmBase) realm).setRealmPath(getRealmPath() + "/realm" + Integer.toString(nextRealmIndex));
+ }
+ }
+
+
@Override
protected void startInternal() throws LifecycleException {
// Start 'sub-realms' then this one
- Iterator<Realm> iter = realms.iterator();
-
- while (iter.hasNext()) {
- Realm realm = iter.next();
- if (realm instanceof Lifecycle) {
- try {
- ((Lifecycle) realm).start();
- } catch (LifecycleException e) {
- // If realm doesn't start can't authenticate against it
- iter.remove();
- log.error(sm.getString("combinedRealm.realmStartFail", realm.getClass().getName()), e);
- }
+ for (Realm realm : realms) {
+ if (!startRealm(realm)) {
+ removeRealm(realm);
}
}
@@ -253,7 +319,11 @@ public class CombinedRealm extends RealmBase {
super.stopInternal();
for (Realm realm : realms) {
if (realm instanceof Lifecycle) {
- ((Lifecycle) realm).stop();
+ try {
+ ((Lifecycle) realm).stop();
+ } catch (LifecycleException e) {
+ log.error(sm.getString("combinedRealm.realmStopFail", realm.getClass().getName()), e);
+ }
}
}
}
@@ -265,11 +335,31 @@ public class CombinedRealm extends RealmBase {
@Override
protected void destroyInternal() throws LifecycleException {
for (Realm realm : realms) {
- if (realm instanceof Lifecycle) {
+ destroyRealm(realm);
+ }
+ super.destroyInternal();
+
+ for (Realm realm : realmsToDestroy) {
+ destroyRealm(realm);
+ }
+ }
+
+
+ private void destroyRealm(Realm realm) {
+ if (realm instanceof Lifecycle) {
+ if (((Lifecycle) realm).getState().isAvailable()) {
+ try {
+ ((Lifecycle) realm).stop();
+ } catch (LifecycleException e) {
+ log.error(sm.getString("combinedRealm.realmStopFail", realm.getClass().getName()), e);
+ }
+ }
+ try {
((Lifecycle) realm).destroy();
+ } catch (LifecycleException e) {
+ log.error(sm.getString("combinedRealm.realmDestroyFail", realm.getClass().getName()), e);
}
}
- super.destroyInternal();
}
diff --git a/java/org/apache/catalina/realm/LocalStrings.properties b/java/org/apache/catalina/realm/LocalStrings.properties
index 6800265e3a..f84e69b447 100644
--- a/java/org/apache/catalina/realm/LocalStrings.properties
+++ b/java/org/apache/catalina/realm/LocalStrings.properties
@@ -14,12 +14,15 @@
# limitations under the License.
combinedRealm.addRealm=Add [{0}] realm, making a total of [{1}] realms
+combinedRealm.addRealmDuplicate=The [{0}] realm has already been added to this combined realm
combinedRealm.authFail=Failed to authenticate user [{0}] with realm [{1}]
combinedRealm.authStart=Attempting to authenticate user [{0}] with realm [{1}]
combinedRealm.authSuccess=Authenticated user [{0}] with realm [{1}]
combinedRealm.getPassword=The getPassword() method should never be called
combinedRealm.getPrincipal=The getPrincipal() method should never be called
+combinedRealm.realmDestroyFail=Failed to destroy [{0}] realm
combinedRealm.realmStartFail=Failed to start [{0}] realm
+combinedRealm.realmStopFail=Failed to stop [{0}] realm
combinedRealm.removeRealm=Remove [{0}] realm, leaving a total of [{1}] realms
combinedRealm.setCredentialHandler=A CredentialHandler was set on an instance of the CombinedRealm (or a sub-class of CombinedRealm). CombinedRealm doesn't use a configured CredentialHandler. Is this a configuration error?
combinedRealm.unexpectedMethod=An unexpected call was made to a method on the combined realm
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index d567477601..a13ce985fa 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -246,6 +246,15 @@
class loader until after the web application class loader has started.
(markt)
</fix>
+ <fix>
+ Fix various concurrency issues around adding and removing
+ <code>Realm</code>s from a <code>CombinedRealm</code>, in particular
+ doing so while the <code>CombinedRealm</code> is in use. Note that once
+ a <code>Realm</code> instance has been removed from a
+ <code>CombinedRealm</code> instance at run-time, the removed
+ <code>Realm</code> instance may not be reused apart from re-adding it to
+ the same <code>CombinedRealm</code>. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Coyote">
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
(tomcat) 02/02: Add test cases written by Claude / Opus 5
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit cf892f9dbded18cc5ee3ddf656ebf36051179772
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Sep 16 08:09:12 2026 +0100
Add test cases written by Claude / Opus 5
---
.../apache/catalina/realm/TestCombinedRealm.java | 392 +++++++++++++++++++++
1 file changed, 392 insertions(+)
diff --git a/test/org/apache/catalina/realm/TestCombinedRealm.java b/test/org/apache/catalina/realm/TestCombinedRealm.java
new file mode 100644
index 0000000000..de3803bfd0
--- /dev/null
+++ b/test/org/apache/catalina/realm/TestCombinedRealm.java
@@ -0,0 +1,392 @@
+/*
+ * 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.realm;
+
+import java.security.Principal;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.LifecycleState;
+import org.apache.catalina.Realm;
+import org.apache.catalina.startup.TesterMapRealm;
+import org.apache.tomcat.unittest.TesterContext;
+
+public class TestCombinedRealm {
+
+ private static final String USER_NAME = "user";
+ private static final String PASSWORD = "password";
+
+
+ /**
+ * A nested Realm that fails to start. It also tracks whether it is used for authentication so tests can confirm
+ * that a Realm that failed to start is not used.
+ */
+ private static final class FailToStartRealm extends RealmBase {
+
+ private volatile boolean authenticateCalled = false;
+
+ @Override
+ protected void startInternal() throws LifecycleException {
+ throw new LifecycleException("Deliberate failure to start");
+ }
+
+ @Override
+ public Principal authenticate(String username, String credentials) {
+ authenticateCalled = true;
+ return null;
+ }
+
+ private boolean isAuthenticateCalled() {
+ return authenticateCalled;
+ }
+
+ @Override
+ protected String getPassword(String username) {
+ return null;
+ }
+
+ @Override
+ protected Principal getPrincipal(String username) {
+ return null;
+ }
+ }
+
+
+ /**
+ * A nested Realm that authenticates a single user and counts the calls made to stop() and destroy() so tests can
+ * confirm a Realm is cleaned up exactly once.
+ */
+ private static final class CountingRealm extends RealmBase {
+
+ private final String username;
+ private final String password;
+ private int stopCount = 0;
+ private int destroyCount = 0;
+
+ private CountingRealm(String username, String password) {
+ this.username = username;
+ this.password = password;
+ setCredentialHandler(new MessageDigestCredentialHandler());
+ }
+
+ @Override
+ protected void stopInternal() throws LifecycleException {
+ stopCount++;
+ super.stopInternal();
+ }
+
+ @Override
+ protected void destroyInternal() throws LifecycleException {
+ destroyCount++;
+ super.destroyInternal();
+ }
+
+ private int getStopCount() {
+ return stopCount;
+ }
+
+ private int getDestroyCount() {
+ return destroyCount;
+ }
+
+ @Override
+ protected String getPassword(String username) {
+ if (this.username.equals(username)) {
+ return password;
+ }
+ return null;
+ }
+
+ @Override
+ protected Principal getPrincipal(String username) {
+ if (this.username.equals(username)) {
+ return new GenericPrincipal(username, null);
+ }
+ return null;
+ }
+ }
+
+
+ private static TesterMapRealm createNestedRealm() {
+ TesterMapRealm realm = new TesterMapRealm();
+ realm.setCredentialHandler(new MessageDigestCredentialHandler());
+ realm.addUser(USER_NAME, PASSWORD);
+ return realm;
+ }
+
+
+ private static CombinedRealm createCombinedRealm(Realm... nestedRealms) {
+ Context context = new TesterContext();
+ CombinedRealm combinedRealm = new CombinedRealm();
+ combinedRealm.setContainer(context);
+ for (Realm nestedRealm : nestedRealms) {
+ combinedRealm.addRealm(nestedRealm);
+ }
+ return combinedRealm;
+ }
+
+
+ /*
+ * A nested Realm that fails to start must be removed from the combined Realm since it cannot be used to
+ * authenticate users. The combined Realm must still start.
+ */
+ @Test
+ public void testNestedRealmFailsToStart() throws Exception {
+ TesterMapRealm good = createNestedRealm();
+ FailToStartRealm bad = new FailToStartRealm();
+
+ CombinedRealm combinedRealm = createCombinedRealm(good, bad);
+ Assert.assertEquals(2, combinedRealm.getNestedRealms().length);
+
+ combinedRealm.start();
+
+ // The combined Realm must not fail because one nested Realm failed
+ Assert.assertEquals(LifecycleState.STARTED, combinedRealm.getState());
+
+ // The Realm that failed to start must have been removed
+ Realm[] nestedRealms = combinedRealm.getNestedRealms();
+ Assert.assertEquals(1, nestedRealms.length);
+ Assert.assertSame(good, nestedRealms[0]);
+
+ // The Realm that failed to start must have been cleaned up
+ Assert.assertEquals(LifecycleState.DESTROYED, bad.getState());
+ }
+
+
+ /*
+ * A nested Realm that fails to start must not be used to authenticate users. The Realm that fails to start is added
+ * first so that it would be the first Realm used for authentication if it had not been removed.
+ */
+ @Test
+ public void testNestedRealmThatFailsToStartIsNotUsed() throws Exception {
+ FailToStartRealm bad = new FailToStartRealm();
+ TesterMapRealm good = createNestedRealm();
+
+ CombinedRealm combinedRealm = createCombinedRealm(bad, good);
+ combinedRealm.start();
+
+ // Authentication must still work via the Realm that started
+ Assert.assertNotNull(combinedRealm.authenticate(USER_NAME, PASSWORD));
+ Assert.assertNull(combinedRealm.authenticate(USER_NAME, "wrong"));
+
+ Assert.assertFalse(bad.isAuthenticateCalled());
+ }
+
+
+ /*
+ * If every nested Realm fails to start, the combined Realm must still start but must not authenticate any user.
+ */
+ @Test
+ public void testAllNestedRealmsFailToStart() throws Exception {
+ FailToStartRealm bad = new FailToStartRealm();
+
+ CombinedRealm combinedRealm = createCombinedRealm(bad);
+ combinedRealm.start();
+
+ Assert.assertEquals(LifecycleState.STARTED, combinedRealm.getState());
+ Assert.assertEquals(0, combinedRealm.getNestedRealms().length);
+ Assert.assertNull(combinedRealm.authenticate(USER_NAME, PASSWORD));
+ Assert.assertFalse(bad.isAuthenticateCalled());
+ }
+
+
+ /*
+ * A Realm removed while the combined Realm is running stops being used for authentication immediately but must not
+ * be destroyed until the combined Realm is destroyed since it may be in use by a concurrent authenticate() call.
+ */
+ @Test
+ public void testRemoveRealmWhileRunningDefersCleanUp() throws Exception {
+ CountingRealm retained = new CountingRealm("retained", PASSWORD);
+ CountingRealm removed = new CountingRealm("removed", PASSWORD);
+
+ CombinedRealm combinedRealm = createCombinedRealm(retained, removed);
+ combinedRealm.start();
+ Assert.assertNotNull(combinedRealm.authenticate("removed", PASSWORD));
+
+ Assert.assertTrue(combinedRealm.removeRealm(removed));
+
+ // No longer used for authentication
+ Assert.assertEquals(1, combinedRealm.getNestedRealms().length);
+ Assert.assertSame(retained, combinedRealm.getNestedRealms()[0]);
+ Assert.assertNull(combinedRealm.authenticate("removed", PASSWORD));
+ Assert.assertNotNull(combinedRealm.authenticate("retained", PASSWORD));
+
+ // Clean-up must have been deferred
+ Assert.assertEquals(LifecycleState.STARTED, removed.getState());
+ Assert.assertEquals(0, removed.getStopCount());
+ Assert.assertEquals(0, removed.getDestroyCount());
+
+ // Stopping the combined Realm only stops the Realms it still contains
+ combinedRealm.stop();
+ Assert.assertEquals(LifecycleState.STOPPED, retained.getState());
+ Assert.assertEquals(LifecycleState.STARTED, removed.getState());
+
+ // Destroying the combined Realm must clean up both, each exactly once
+ combinedRealm.destroy();
+ Assert.assertEquals(LifecycleState.DESTROYED, retained.getState());
+ Assert.assertEquals(LifecycleState.DESTROYED, removed.getState());
+ Assert.assertEquals(1, retained.getStopCount());
+ Assert.assertEquals(1, retained.getDestroyCount());
+ Assert.assertEquals(1, removed.getStopCount());
+ Assert.assertEquals(1, removed.getDestroyCount());
+ }
+
+
+ /*
+ * A Realm removed when the combined Realm is not running can be cleaned up immediately since it cannot be in use.
+ */
+ @Test
+ public void testRemoveRealmWhileStoppedCleansUpImmediately() throws Exception {
+ CountingRealm removed = new CountingRealm("removed", PASSWORD);
+
+ CombinedRealm combinedRealm = createCombinedRealm(removed);
+ combinedRealm.start();
+ combinedRealm.stop();
+ Assert.assertEquals(LifecycleState.STOPPED, removed.getState());
+
+ Assert.assertTrue(combinedRealm.removeRealm(removed));
+
+ // Clean-up must not have been deferred
+ Assert.assertEquals(LifecycleState.DESTROYED, removed.getState());
+ Assert.assertEquals(1, removed.getStopCount());
+ Assert.assertEquals(1, removed.getDestroyCount());
+
+ combinedRealm.destroy();
+
+ // Still exactly once
+ Assert.assertEquals(1, removed.getStopCount());
+ Assert.assertEquals(1, removed.getDestroyCount());
+ }
+
+
+ /*
+ * A Realm that is removed and then re-added must be used for authentication again and must not be cleaned up as a
+ * result of the earlier removal.
+ */
+ @Test
+ public void testRemoveAndReAddRealm() throws Exception {
+ CountingRealm realm = new CountingRealm("removed", PASSWORD);
+
+ CombinedRealm combinedRealm = createCombinedRealm(realm);
+ combinedRealm.start();
+
+ Assert.assertTrue(combinedRealm.removeRealm(realm));
+ Assert.assertEquals(0, combinedRealm.getNestedRealms().length);
+
+ combinedRealm.addRealm(realm);
+ Assert.assertEquals(1, combinedRealm.getNestedRealms().length);
+ Assert.assertNotNull(combinedRealm.authenticate("removed", PASSWORD));
+
+ // The pending clean-up from the removal must have been cancelled so the
+ // Realm is only cleaned up once, as a Realm the combined Realm contains
+ combinedRealm.stop();
+ combinedRealm.destroy();
+ Assert.assertEquals(LifecycleState.DESTROYED, realm.getState());
+ Assert.assertEquals(1, realm.getStopCount());
+ Assert.assertEquals(1, realm.getDestroyCount());
+ }
+
+
+ /*
+ * The realm path is used to construct the JMX object name so every nested Realm must be allocated a unique realm
+ * path. If two nested Realms share a realm path, registering the second un-registers the first leaving a Realm
+ * that is in use but not visible in JMX.
+ */
+ private static void assertDistinctRealmPaths(RealmBase... realms) {
+ Set<String> realmPaths = new HashSet<>();
+ for (RealmBase realm : realms) {
+ Assert.assertTrue("Duplicate realm path [" + realm.getRealmPath() + "]",
+ realmPaths.add(realm.getRealmPath()));
+ }
+ }
+
+
+ /*
+ * A combined Realm may be nested inside another combined Realm. The nested Realms of the inner combined Realm must
+ * still be allocated unique realm paths.
+ */
+ @Test
+ public void testNestedCombinedRealmPaths() {
+ // Mimics the digester: nested Realms are added to the Realm that contains
+ // them and the container is set once the outermost Realm is complete.
+ CombinedRealm outer = new CombinedRealm();
+ CombinedRealm inner = new CombinedRealm();
+ TesterMapRealm innerFirst = createNestedRealm();
+ TesterMapRealm innerSecond = createNestedRealm();
+ TesterMapRealm outerSecond = createNestedRealm();
+
+ inner.addRealm(innerFirst);
+ inner.addRealm(innerSecond);
+ outer.addRealm(inner);
+ outer.addRealm(outerSecond);
+ outer.setContainer(new TesterContext());
+
+ Assert.assertEquals("/realm0/realm0", inner.getRealmPath());
+ Assert.assertEquals("/realm0/realm1", outerSecond.getRealmPath());
+ Assert.assertEquals("/realm0/realm0/realm0", innerFirst.getRealmPath());
+ Assert.assertEquals("/realm0/realm0/realm1", innerSecond.getRealmPath());
+
+ assertDistinctRealmPaths(inner, outerSecond, innerFirst, innerSecond);
+ }
+
+
+ /*
+ * Changing the realm path of a combined Realm must renumber the nested Realms so they remain unique and must leave
+ * the next index such that a Realm added afterwards does not collide with an existing Realm.
+ */
+ @Test
+ public void testSetRealmPathRenumbersNestedRealms() {
+ TesterMapRealm first = createNestedRealm();
+ TesterMapRealm second = createNestedRealm();
+ CombinedRealm combinedRealm = createCombinedRealm(first, second);
+
+ combinedRealm.setRealmPath("/changed");
+
+ Assert.assertEquals("/changed/realm0", first.getRealmPath());
+ Assert.assertEquals("/changed/realm1", second.getRealmPath());
+
+ TesterMapRealm third = createNestedRealm();
+ combinedRealm.addRealm(third);
+ Assert.assertEquals("/changed/realm2", third.getRealmPath());
+
+ assertDistinctRealmPaths(first, second, third);
+ }
+
+
+ /*
+ * A realm path must not be re-used when a Realm is removed and another Realm added since the removed Realm may
+ * still be registered with JMX.
+ */
+ @Test
+ public void testRealmPathsAreNotReUsed() {
+ TesterMapRealm removed = createNestedRealm();
+ TesterMapRealm retained = createNestedRealm();
+ CombinedRealm combinedRealm = createCombinedRealm(removed, retained);
+
+ Assert.assertTrue(combinedRealm.removeRealm(removed));
+
+ TesterMapRealm added = createNestedRealm();
+ combinedRealm.addRealm(added);
+
+ assertDistinctRealmPaths(retained, added);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org