Issue #3743 - Using only Location based XmlConfiguration in Jetty itself

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-06-06 11:55:01 -05:00
parent 0bc88ec286
commit 6686083462
8 changed files with 82 additions and 40 deletions

View File

@ -36,9 +36,8 @@ public class FileServerXml
{
public static void main( String[] args ) throws Exception
{
Resource fileserverXml = Resource.newSystemResource("fileserver.xml");
XmlConfiguration configuration = new XmlConfiguration(
fileserverXml.getInputStream());
Resource fileServerXml = Resource.newSystemResource("fileserver.xml");
XmlConfiguration configuration = new XmlConfiguration(fileServerXml);
Server server = (Server) configuration.configure();
server.start();
server.join();

View File

@ -91,7 +91,7 @@ public class GlobalWebappConfigBinding implements AppLifeCycle.Binding
if (globalContextSettings.exists())
{
XmlConfiguration jettyXmlConfig = new XmlConfiguration(globalContextSettings.getInputStream());
XmlConfiguration jettyXmlConfig = new XmlConfiguration(globalContextSettings);
Resource resource = Resource.newResource(app.getOriginId());
app.getDeploymentManager().scope(jettyXmlConfig,resource);
jettyXmlConfig.configure(context);

View File

@ -160,7 +160,7 @@ public abstract class AbstractContextProvider extends AbstractLifeCycle implemen
{
Thread.currentThread().setContextClassLoader(classLoader);
XmlConfiguration xmlConfiguration = new XmlConfiguration(res.getInputStream());
XmlConfiguration xmlConfiguration = new XmlConfiguration(res);
HashMap properties = new HashMap();
//put the server instance in
properties.put("Server", getServerInstanceWrapper().getServer());

View File

@ -18,7 +18,6 @@
package org.eclipse.jetty.osgi.boot.internal.serverfactory;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
@ -142,10 +141,10 @@ public class ServerInstanceWrapper
for (URL jettyConfiguration : jettyConfigurations)
{
try(InputStream in = jettyConfiguration.openStream())
try
{
// Execute a Jetty configuration file
XmlConfiguration config = new XmlConfiguration(in);
XmlConfiguration config = new XmlConfiguration(jettyConfiguration);
config.getIdMap().putAll(id_map);
config.getProperties().putAll(properties);

View File

@ -18,7 +18,6 @@
package org.eclipse.jetty.websocket.client;
import java.io.InputStream;
import java.net.URL;
import org.eclipse.jetty.client.HttpClient;
@ -36,9 +35,9 @@ class XmlBasedHttpClientProvider
return null;
}
try (InputStream in = resource.openStream())
try
{
XmlConfiguration configuration = new XmlConfiguration(in);
XmlConfiguration configuration = new XmlConfiguration(resource);
return (HttpClient) configuration.configure();
}
catch (Throwable t)

View File

@ -20,6 +20,8 @@ package org.eclipse.jetty.xml;
import java.net.URL;
import org.eclipse.jetty.util.resource.Resource;
/**
* A ConfigurationProcessor for non XmlConfiguration format files.
* <p>
@ -31,8 +33,13 @@ import java.net.URL;
*/
public interface ConfigurationProcessor
{
public void init(URL url, XmlParser.Node root, XmlConfiguration configuration);
/**
* @deprecated use {@link #init(Resource, XmlParser.Node, XmlConfiguration)} instead
*/
@Deprecated
void init(URL url, XmlParser.Node root, XmlConfiguration configuration);
void init(Resource resource, XmlParser.Node root, XmlConfiguration configuration);
public Object configure( Object obj) throws Exception;
public Object configure() throws Exception;
Object configure( Object obj) throws Exception;
Object configure() throws Exception;
}

View File

@ -30,6 +30,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.file.Path;
@ -176,10 +177,30 @@ public class XmlConfiguration
private final Map<String, Object> _idMap = new HashMap<>();
private final Map<String, String> _propertyMap = new HashMap<>();
private final URL _url;
private final Resource _location;
private final String _dtd;
private ConfigurationProcessor _processor;
/**
* 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
*/
public XmlConfiguration(Resource resource) throws SAXException, IOException
{
synchronized (__parser)
{
_location = resource;
try(InputStream inputStream = resource.getInputStream())
{
setConfig(__parser.parse(inputStream));
}
_dtd = __parser.getDTD();
}
}
/**
* Reads and parses the XML configuration file.
*
@ -189,12 +210,19 @@ public class XmlConfiguration
*/
public XmlConfiguration(URL configuration) throws SAXException, IOException
{
synchronized (__parser)
{
_url = configuration;
setConfig(__parser.parse(configuration.toString()));
_dtd = __parser.getDTD();
}
this(Resource.newResource(configuration));
}
/**
* Reads and parses the XML configuration file.
*
* @param configuration the URI and location of the XML configuration
* @throws IOException if the configuration could not be read
* @throws SAXException if the configuration could not be parsed
*/
public XmlConfiguration(URI configuration) throws SAXException, IOException
{
this(Resource.newResource(configuration));
}
/**
@ -204,7 +232,9 @@ public class XmlConfiguration
* The String should start with a "&lt;Configure ....&gt;" element.
* @throws IOException if the configuration could not be read
* @throws SAXException if the configuration could not be parsed
* @deprecated use Constructor which has location information
*/
@Deprecated
public XmlConfiguration(String configuration) throws SAXException, IOException
{
configuration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE Configure PUBLIC \"-//Jetty//Configure//EN\" \"http://eclipse.org/jetty/configure.dtd\">"
@ -212,7 +242,7 @@ public class XmlConfiguration
InputSource source = new InputSource(new StringReader(configuration));
synchronized (__parser)
{
_url = null;
_location = null;
setConfig(__parser.parse(source));
_dtd = __parser.getDTD();
}
@ -224,13 +254,15 @@ public class XmlConfiguration
* @param configuration An input stream containing a complete configuration file
* @throws IOException if the configuration could not be read
* @throws SAXException if the configuration could not be parsed
* @deprecated use Constructor which has location information
*/
@Deprecated
public XmlConfiguration(InputStream configuration) throws SAXException, IOException
{
InputSource source = new InputSource(configuration);
synchronized (__parser)
{
_url = null;
_location = null;
setConfig(__parser.parse(source));
_dtd = __parser.getDTD();
}
@ -257,7 +289,7 @@ public class XmlConfiguration
{
throw new IllegalArgumentException("Unknown XML tag:" + config.getTag());
}
_processor.init(_url, config, this);
_processor.init(_location, config, this);
}
/**
@ -332,14 +364,22 @@ public class XmlConfiguration
private static class JettyXmlConfiguration implements ConfigurationProcessor
{
private String _url;
private String _location;
XmlParser.Node _root;
XmlConfiguration _configuration;
@Override
public void init(Resource resource, XmlParser.Node root, XmlConfiguration configuration)
{
_location = resource == null ? null : resource.toString();
_root = root;
_configuration = configuration;
}
@Override
public void init(URL url, XmlParser.Node root, XmlConfiguration configuration)
{
_url = url == null ? null : url.toString();
_location = url == null ? null : url.toString();
_root = root;
_configuration = configuration;
}
@ -352,7 +392,7 @@ public class XmlConfiguration
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 " + _url);
throw new IllegalArgumentException("Object of class '" + obj.getClass().getCanonicalName() + "' is not of type '" + oClass.getCanonicalName() + "'. " + loaders + " in " + _location);
}
String id = _root.getAttribute("id");
if (id != null)
@ -404,7 +444,7 @@ public class XmlConfiguration
}
catch (NoSuchMethodException x)
{
throw new IllegalStateException(String.format("No constructor %s(%s,%s) in %s", oClass, arguments, namedArgMap, _url));
throw new IllegalStateException(String.format("No constructor %s(%s,%s) in %s", oClass, arguments, namedArgMap, _location));
}
}
if (id != null)
@ -496,12 +536,12 @@ public class XmlConfiguration
envObj(node);
break;
default:
throw new IllegalStateException("Unknown tag: " + tag + " in " + _url);
throw new IllegalStateException("Unknown tag: " + tag + " in " + _location);
}
}
catch (Exception e)
{
LOG.warn("Config error at " + node, e.toString() + " in " + _url);
LOG.warn("Config error at " + node, e.toString() + " in " + _location);
throw e;
}
}
@ -677,7 +717,7 @@ public class XmlConfiguration
{
Object result = constructor.newInstance(args);
if (constructor.getAnnotation(Deprecated.class) != null)
LOG.warn("Deprecated constructor {} in {}", constructor, _url);
LOG.warn("Deprecated constructor {} in {}", constructor, _location);
return result;
}
@ -685,7 +725,7 @@ public class XmlConfiguration
{
Object result = method.invoke(obj, args);
if (method.getAnnotation(Deprecated.class) != null)
LOG.warn("Deprecated method {} in {}", method, _url);
LOG.warn("Deprecated method {} in {}", method, _location);
return result;
}
@ -693,7 +733,7 @@ public class XmlConfiguration
{
Object result = field.get(object);
if (field.getAnnotation(Deprecated.class) != null)
LOG.warn("Deprecated field {} in {}", field, _url);
LOG.warn("Deprecated field {} in {}", field, _location);
return result;
}
@ -701,7 +741,7 @@ public class XmlConfiguration
{
field.set(obj, arg);
if (field.getAnnotation(Deprecated.class) != null)
LOG.warn("Deprecated field {} in {}", field, _url);
LOG.warn("Deprecated field {} in {}", field, _location);
}
/**

View File

@ -18,12 +18,10 @@
package org.eclipse.jetty.xml;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -627,7 +625,7 @@ public class XmlConfigurationTest
@Test
public void testArgumentsGetIgnoredMissingDTD() throws Exception
{
XmlConfiguration xmlConfiguration = new XmlConfiguration(new ByteArrayInputStream(("" +
XmlConfiguration xmlConfiguration = new XmlConfiguration("" +
"<Configure class=\"org.eclipse.jetty.xml.AnnotatedTestConfiguration\">" +
" <Arg>arg1</Arg> " +
" <Arg>arg2</Arg> " +
@ -639,7 +637,7 @@ public class XmlConfigurationTest
" <Arg>arg3</Arg>\n" +
" </New>" +
" </Set>" +
"</Configure>").getBytes(StandardCharsets.ISO_8859_1)));
"</Configure>");
// XmlConfiguration xmlConfiguration = new XmlConfiguration(url);
AnnotatedTestConfiguration atc = (AnnotatedTestConfiguration)xmlConfiguration.configure();
@ -655,7 +653,7 @@ public class XmlConfigurationTest
@Test
public void testSetGetIgnoredMissingDTD() throws Exception
{
XmlConfiguration xmlConfiguration = new XmlConfiguration(new ByteArrayInputStream(("" +
XmlConfiguration xmlConfiguration = new XmlConfiguration("" +
"<Configure class=\"org.eclipse.jetty.xml.DefaultTestConfiguration\">" +
" <Set name=\"first\">arg1</Set> " +
" <Set name=\"second\">arg2</Set> " +
@ -667,7 +665,7 @@ public class XmlConfigurationTest
" <Set name=\"third\">arg3</Set> " +
" </New>" +
" </Set>" +
"</Configure>").getBytes(StandardCharsets.UTF_8)));
"</Configure>");
// XmlConfiguration xmlConfiguration = new XmlConfiguration(url);
DefaultTestConfiguration atc = (DefaultTestConfiguration)xmlConfiguration.configure();