Skip to content

Commit

Permalink
Introduce a new class - MultiThrowable - to report exceptions when mu…
Browse files Browse the repository at this point in the history
…ltiple actions are taken where each action may throw an exception but all actions are taken before any errors are reported. Use this new class when reporting multiple container (e.g. web application) failures during start.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1837133 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Jul 31, 2018
1 parent 146dbb3 commit 012f0bc
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 8 deletions.
18 changes: 10 additions & 8 deletions java/org/apache/catalina/core/ContainerBase.java
Expand Up @@ -64,6 +64,7 @@
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.MultiThrowable;
import org.apache.tomcat.util.res.StringManager;
import org.apache.tomcat.util.threads.InlineExecutorService;

Expand Down Expand Up @@ -943,31 +944,32 @@ protected synchronized void startInternal() throws LifecycleException {
results.add(startStopExecutor.submit(new StartChild(children[i])));
}

boolean fail = false;
MultiThrowable multiThrowable = new MultiThrowable();

for (Future<Void> result : results) {
try {
result.get();
} catch (Exception e) {
} catch (Throwable e) {
log.error(sm.getString("containerBase.threadedStartFailed"), e);
fail = true;
multiThrowable.add(e);
}

}
if (fail) {
throw new LifecycleException(
sm.getString("containerBase.threadedStartFailed"));
if (multiThrowable.size() > 0) {
throw new LifecycleException(sm.getString("containerBase.threadedStartFailed"),
multiThrowable.getThrowable());
}

// Start the Valves in our pipeline (including the basic), if any
if (pipeline instanceof Lifecycle)
if (pipeline instanceof Lifecycle) {
((Lifecycle) pipeline).start();
}


setState(LifecycleState.STARTING);

// Start our thread
threadStart();

}


Expand Down
97 changes: 97 additions & 0 deletions java/org/apache/tomcat/util/MultiThrowable.java
@@ -0,0 +1,97 @@
/*
* 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.tomcat.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Wraps a list of throwables as a single throwable. This is intended to be used
* when multiple actions are taken where each may throw an exception but all
* actions are taken before any errors are reported.
*/
public class MultiThrowable extends Throwable {

private static final long serialVersionUID = 1L;

private List<Throwable> throwables = new ArrayList<>();

/**
* Add a throwable to the list of wrapped throwables.
*
* @param t The throwable to add
*/
public void add(Throwable t) {
throwables.add(t);
}


/**
* @return A read-only list of the wrapped throwables.
*/
public List<Throwable> getThrowables() {
return Collections.unmodifiableList(throwables);
}


/**
* @return {@code null} if there are no wrapped throwables, the Throwable if
* there is a single wrapped throwable or the current instance of
* there are multiple wrapped throwables
*/
public Throwable getThrowable() {
if (size() == 0) {
return null;
} else if (size() == 1) {
return throwables.get(0);
} else {
return this;
}
}


/**
* @return The number of throwables currently wrapped by this instance.
*/
public int size() {
return throwables.size();
}


/**
* Overrides the default implementation to provide a concatenation of the
* messages associated with each of the wrapped throwables. Note that the
* format of the returned String is not guaranteed to be fixed and may
* change in a future release.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(super.toString());
sb.append(": ");
sb.append(size());
sb.append(" wrapped Throwables: ");
for (Throwable t : throwables) {
sb.append("[");
sb.append(t.getMessage());
sb.append("]");
}

return sb.toString();
}
}
65 changes: 65 additions & 0 deletions test/org/apache/catalina/startup/TestTomcat.java
Expand Up @@ -38,10 +38,14 @@
import org.apache.catalina.Context;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.authenticator.AuthenticatorBase;
import org.apache.catalina.connector.Request;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.ha.context.ReplicatedContext;
import org.apache.tomcat.util.MultiThrowable;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.descriptor.web.ContextEnvironment;
import org.apache.tomcat.util.descriptor.web.ContextResourceLink;
Expand Down Expand Up @@ -554,4 +558,65 @@ public void lifecycleEvent(LifecycleEvent event) {
used = true;
}
}


@Test
public void testBrokenWarOne() throws Exception {
Tomcat tomcat = getTomcatInstance();

StandardContext ctx = (StandardContext) tomcat.addContext("/a", null);
ctx.addValve(new BrokenAuthenticator());

try {
tomcat.start();
Assert.fail();
} catch (Throwable t) {
Assert.assertTrue(getRootCause(t) instanceof LifecycleException);
}
}


@Test
public void testBrokenWarTwo() throws Exception {
Tomcat tomcat = getTomcatInstance();

StandardContext ctxA = (StandardContext) tomcat.addContext("/a", null);
ctxA.addValve(new BrokenAuthenticator());
StandardContext ctxB = (StandardContext) tomcat.addContext("/b", null);
ctxB.addValve(new BrokenAuthenticator());

try {
tomcat.start();
Assert.fail();
} catch (Throwable t) {
Assert.assertTrue(getRootCause(t) instanceof MultiThrowable);
}
}


private static Throwable getRootCause(Throwable t) {
while (t.getCause() != null && t.getCause() != t) {
t = t.getCause();
}
return t;
}


private static class BrokenAuthenticator extends AuthenticatorBase {

@Override
protected boolean doAuthenticate(Request request, HttpServletResponse response) throws IOException {
return false;
}

@Override
protected String getAuthMethod() {
return null;
}

@Override
protected synchronized void startInternal() throws LifecycleException {
throw new LifecycleException("Deliberately Broken");
}
}
}
7 changes: 7 additions & 0 deletions webapps/docs/changelog.xml
Expand Up @@ -70,6 +70,13 @@
<bug>62527</bug>: Revert restriction of JNDI to the <code>java:</code>
namespace. (remm)
</fix>
<add>
Introduce a new class - <code>MultiThrowable</code> - to report
exceptions when multiple actions are taken where each action may throw
an exception but all actions are taken before any errors are reported.
Use this new class when reporting multiple container (e.g. web
application) failures during start. (markt)
</add>
</changelog>
</subsection>
<subsection name="Coyote">
Expand Down

0 comments on commit 012f0bc

Please sign in to comment.