Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #11270 - Improve XmlConfiguration reporting of Resource location during error #11345

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading