Skip to content

Commit

Permalink
Fixes #11270 - Improve XmlConfiguration reporting of Resource locatio…
Browse files Browse the repository at this point in the history
…n during error (#11345)

Now catching and rethrowing XmlConfigurationException with details about the XML file location.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
Co-authored-by: Simone Bordet <simone.bordet@gmail.com>
  • Loading branch information
joakime and sbordet authored Feb 27, 2024
1 parent 1e6240e commit 2803f5a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

import org.eclipse.jetty.util.ConcurrentPool;
import org.eclipse.jetty.util.ExceptionUtil;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.Pool;
Expand Down Expand Up @@ -252,8 +253,9 @@ public XmlParser getXmlParser()
* Reads and parses the XML configuration file.
*
* @param resource the Resource to the XML configuration
* @throws IOException if the configuration could not be read
* @throws SAXException if the configuration could not be parsed
* @throws IOException not thrown anymore (kept for signature backwards compat)
* @throws SAXException not thrown anymore (kept for signature backwards compat)
* @throws XmlConfigurationException if configuration was not able to loaded from XML provided
*/
public XmlConfiguration(Resource resource) throws SAXException, IOException
{
Expand All @@ -266,8 +268,9 @@ public XmlConfiguration(Resource resource) throws SAXException, IOException
* @param resource the Resource to the XML configuration
* @param idMap Map of objects with IDs
* @param properties Map of properties
* @throws IOException if the configuration could not be read
* @throws SAXException if the configuration could not be parsed
* @throws IOException not thrown anymore (kept for signature backwards compat)
* @throws SAXException not thrown anymore (kept for signature backwards compat)
* @throws XmlConfigurationException if configuration was not able to loaded from XML provided
*/
public XmlConfiguration(Resource resource, Map<String, Object> idMap, Map<String, String> properties) throws SAXException, IOException
{
Expand All @@ -280,21 +283,21 @@ public XmlConfiguration(Resource resource, Map<String, Object> idMap, Map<String
_idMap = idMap == null ? new HashMap<>() : idMap;
_propertyMap = properties == null ? new HashMap<>() : properties;
}
catch (Throwable t)
{
throw new XmlConfigurationException("Bad Jetty XML configuration in " + this, t);
}
finally
{
if (parser instanceof Closeable)
((Closeable)parser).close();
if (parser instanceof Closeable closeable)
IO.close(closeable);
}
}

@Override
public String toString()
{
if (_location == null)
{
return "UNKNOWN-LOCATION";
}
return _location.toString();
return Objects.toString(_location, "UNKNOWN-LOCATION");
}

private void setConfig(XmlParser.Node config)
Expand All @@ -312,7 +315,7 @@ else if (PROCESSOR_FACTORIES != null)
break;
}
if (_processor == null)
throw new IllegalStateException("Unknown configuration type: " + config.getTag() + " in " + this);
throw new IllegalStateException("Unknown configuration type: " + config.getTag());
}
else
{
Expand Down Expand Up @@ -429,7 +432,7 @@ public Object configure(Object obj) throws Exception
if (oClass != null && !oClass.isInstance(obj))
{
String loaders = (oClass.getClassLoader() == obj.getClass().getClassLoader()) ? "" : "Object Class and type Class are from different loaders.";
throw new IllegalArgumentException("Object of class '" + obj.getClass().getCanonicalName() + "' is not of type '" + oClass.getCanonicalName() + "'. " + loaders + " in " + _configuration);
throw new IllegalArgumentException("Object of class '" + obj.getClass().getCanonicalName() + "' is not of type '" + oClass.getCanonicalName() + "'. " + loaders);
}
String id = _root.getAttribute("id");
if (id != null)
Expand All @@ -438,7 +441,7 @@ public Object configure(Object obj) throws Exception
AttrOrElementNode aoeNode = new AttrOrElementNode(obj, _root, "Id", "Class", "Arg");
// The Object already existed, if it has <Arg> nodes, warn about them not being used.
aoeNode.getNodes("Arg")
.forEach((node) -> LOG.warn("Ignored arg {} in {}", node, this._configuration._location));
.forEach((node) -> LOG.warn("Ignored arg {} in {}", node, _configuration));
configure(obj, _root, aoeNode.getNext());
return obj;
}
Expand All @@ -465,14 +468,14 @@ public Object configure() throws Exception
}
catch (NoSuchMethodException x)
{
throw new IllegalStateException(String.format("No matching constructor %s in %s", oClass, _configuration));
throw new IllegalStateException("No matching constructor " + oClass);
}
}
else
{
// The Object already existed, if it has <Arg> nodes, warn about them not being used.
aoeNode.getNodes("Arg")
.forEach((node) -> LOG.warn("Ignored arg {} in {}", node, this._configuration._location));
.forEach((node) -> LOG.warn("Ignored arg {} in {}", node, _configuration));
}

_configuration.initializeDefaults(obj);
Expand Down Expand Up @@ -550,7 +553,7 @@ public void configure(Object obj, XmlParser.Node cfg, int i) throws Exception
envObj(node);
break;
default:
throw new IllegalStateException("Unknown tag: " + tag + " in " + _configuration);
throw new IllegalStateException("Unknown tag: " + tag);
}
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.xml;

public class XmlConfigurationException extends IllegalStateException
{
public XmlConfigurationException(String s)
{
super(s);
}

public XmlConfigurationException(String message, Throwable cause)
{
super(message, cause);
}
}

0 comments on commit 2803f5a

Please sign in to comment.