Issue #3743 - Using only Location based XmlConfiguration in Jetty itself
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
parent
0bc88ec286
commit
6686083462
|
@ -36,9 +36,8 @@ public class FileServerXml
|
||||||
{
|
{
|
||||||
public static void main( String[] args ) throws Exception
|
public static void main( String[] args ) throws Exception
|
||||||
{
|
{
|
||||||
Resource fileserverXml = Resource.newSystemResource("fileserver.xml");
|
Resource fileServerXml = Resource.newSystemResource("fileserver.xml");
|
||||||
XmlConfiguration configuration = new XmlConfiguration(
|
XmlConfiguration configuration = new XmlConfiguration(fileServerXml);
|
||||||
fileserverXml.getInputStream());
|
|
||||||
Server server = (Server) configuration.configure();
|
Server server = (Server) configuration.configure();
|
||||||
server.start();
|
server.start();
|
||||||
server.join();
|
server.join();
|
||||||
|
|
|
@ -91,7 +91,7 @@ public class GlobalWebappConfigBinding implements AppLifeCycle.Binding
|
||||||
|
|
||||||
if (globalContextSettings.exists())
|
if (globalContextSettings.exists())
|
||||||
{
|
{
|
||||||
XmlConfiguration jettyXmlConfig = new XmlConfiguration(globalContextSettings.getInputStream());
|
XmlConfiguration jettyXmlConfig = new XmlConfiguration(globalContextSettings);
|
||||||
Resource resource = Resource.newResource(app.getOriginId());
|
Resource resource = Resource.newResource(app.getOriginId());
|
||||||
app.getDeploymentManager().scope(jettyXmlConfig,resource);
|
app.getDeploymentManager().scope(jettyXmlConfig,resource);
|
||||||
jettyXmlConfig.configure(context);
|
jettyXmlConfig.configure(context);
|
||||||
|
|
|
@ -160,7 +160,7 @@ public abstract class AbstractContextProvider extends AbstractLifeCycle implemen
|
||||||
{
|
{
|
||||||
Thread.currentThread().setContextClassLoader(classLoader);
|
Thread.currentThread().setContextClassLoader(classLoader);
|
||||||
|
|
||||||
XmlConfiguration xmlConfiguration = new XmlConfiguration(res.getInputStream());
|
XmlConfiguration xmlConfiguration = new XmlConfiguration(res);
|
||||||
HashMap properties = new HashMap();
|
HashMap properties = new HashMap();
|
||||||
//put the server instance in
|
//put the server instance in
|
||||||
properties.put("Server", getServerInstanceWrapper().getServer());
|
properties.put("Server", getServerInstanceWrapper().getServer());
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.osgi.boot.internal.serverfactory;
|
package org.eclipse.jetty.osgi.boot.internal.serverfactory;
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -142,10 +141,10 @@ public class ServerInstanceWrapper
|
||||||
|
|
||||||
for (URL jettyConfiguration : jettyConfigurations)
|
for (URL jettyConfiguration : jettyConfigurations)
|
||||||
{
|
{
|
||||||
try(InputStream in = jettyConfiguration.openStream())
|
try
|
||||||
{
|
{
|
||||||
// Execute a Jetty configuration file
|
// Execute a Jetty configuration file
|
||||||
XmlConfiguration config = new XmlConfiguration(in);
|
XmlConfiguration config = new XmlConfiguration(jettyConfiguration);
|
||||||
|
|
||||||
config.getIdMap().putAll(id_map);
|
config.getIdMap().putAll(id_map);
|
||||||
config.getProperties().putAll(properties);
|
config.getProperties().putAll(properties);
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.websocket.client;
|
package org.eclipse.jetty.websocket.client;
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
import org.eclipse.jetty.client.HttpClient;
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
|
@ -36,9 +35,9 @@ class XmlBasedHttpClientProvider
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try (InputStream in = resource.openStream())
|
try
|
||||||
{
|
{
|
||||||
XmlConfiguration configuration = new XmlConfiguration(in);
|
XmlConfiguration configuration = new XmlConfiguration(resource);
|
||||||
return (HttpClient) configuration.configure();
|
return (HttpClient) configuration.configure();
|
||||||
}
|
}
|
||||||
catch (Throwable t)
|
catch (Throwable t)
|
||||||
|
|
|
@ -20,6 +20,8 @@ package org.eclipse.jetty.xml;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.util.resource.Resource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A ConfigurationProcessor for non XmlConfiguration format files.
|
* A ConfigurationProcessor for non XmlConfiguration format files.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -31,8 +33,13 @@ import java.net.URL;
|
||||||
*/
|
*/
|
||||||
public interface ConfigurationProcessor
|
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;
|
Object configure( Object obj) throws Exception;
|
||||||
public Object configure() throws Exception;
|
Object configure() throws Exception;
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
@ -176,10 +177,30 @@ public class XmlConfiguration
|
||||||
|
|
||||||
private final Map<String, Object> _idMap = new HashMap<>();
|
private final Map<String, Object> _idMap = new HashMap<>();
|
||||||
private final Map<String, String> _propertyMap = new HashMap<>();
|
private final Map<String, String> _propertyMap = new HashMap<>();
|
||||||
private final URL _url;
|
private final Resource _location;
|
||||||
private final String _dtd;
|
private final String _dtd;
|
||||||
private ConfigurationProcessor _processor;
|
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.
|
* Reads and parses the XML configuration file.
|
||||||
*
|
*
|
||||||
|
@ -189,12 +210,19 @@ public class XmlConfiguration
|
||||||
*/
|
*/
|
||||||
public XmlConfiguration(URL configuration) throws SAXException, IOException
|
public XmlConfiguration(URL configuration) throws SAXException, IOException
|
||||||
{
|
{
|
||||||
synchronized (__parser)
|
this(Resource.newResource(configuration));
|
||||||
{
|
|
||||||
_url = configuration;
|
|
||||||
setConfig(__parser.parse(configuration.toString()));
|
|
||||||
_dtd = __parser.getDTD();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 "<Configure ....>" element.
|
* The String should start with a "<Configure ....>" element.
|
||||||
* @throws IOException if the configuration could not be read
|
* @throws IOException if the configuration could not be read
|
||||||
* @throws SAXException if the configuration could not be parsed
|
* @throws SAXException if the configuration could not be parsed
|
||||||
|
* @deprecated use Constructor which has location information
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public XmlConfiguration(String configuration) throws SAXException, IOException
|
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\">"
|
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));
|
InputSource source = new InputSource(new StringReader(configuration));
|
||||||
synchronized (__parser)
|
synchronized (__parser)
|
||||||
{
|
{
|
||||||
_url = null;
|
_location = null;
|
||||||
setConfig(__parser.parse(source));
|
setConfig(__parser.parse(source));
|
||||||
_dtd = __parser.getDTD();
|
_dtd = __parser.getDTD();
|
||||||
}
|
}
|
||||||
|
@ -224,13 +254,15 @@ public class XmlConfiguration
|
||||||
* @param configuration An input stream containing a complete configuration file
|
* @param configuration An input stream containing a complete configuration file
|
||||||
* @throws IOException if the configuration could not be read
|
* @throws IOException if the configuration could not be read
|
||||||
* @throws SAXException if the configuration could not be parsed
|
* @throws SAXException if the configuration could not be parsed
|
||||||
|
* @deprecated use Constructor which has location information
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public XmlConfiguration(InputStream configuration) throws SAXException, IOException
|
public XmlConfiguration(InputStream configuration) throws SAXException, IOException
|
||||||
{
|
{
|
||||||
InputSource source = new InputSource(configuration);
|
InputSource source = new InputSource(configuration);
|
||||||
synchronized (__parser)
|
synchronized (__parser)
|
||||||
{
|
{
|
||||||
_url = null;
|
_location = null;
|
||||||
setConfig(__parser.parse(source));
|
setConfig(__parser.parse(source));
|
||||||
_dtd = __parser.getDTD();
|
_dtd = __parser.getDTD();
|
||||||
}
|
}
|
||||||
|
@ -257,7 +289,7 @@ public class XmlConfiguration
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException("Unknown XML tag:" + config.getTag());
|
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 static class JettyXmlConfiguration implements ConfigurationProcessor
|
||||||
{
|
{
|
||||||
private String _url;
|
private String _location;
|
||||||
XmlParser.Node _root;
|
XmlParser.Node _root;
|
||||||
XmlConfiguration _configuration;
|
XmlConfiguration _configuration;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(Resource resource, XmlParser.Node root, XmlConfiguration configuration)
|
||||||
|
{
|
||||||
|
_location = resource == null ? null : resource.toString();
|
||||||
|
_root = root;
|
||||||
|
_configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(URL url, XmlParser.Node root, XmlConfiguration configuration)
|
public void init(URL url, XmlParser.Node root, XmlConfiguration configuration)
|
||||||
{
|
{
|
||||||
_url = url == null ? null : url.toString();
|
_location = url == null ? null : url.toString();
|
||||||
_root = root;
|
_root = root;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
@ -352,7 +392,7 @@ public class XmlConfiguration
|
||||||
if (oClass != null && !oClass.isInstance(obj))
|
if (oClass != null && !oClass.isInstance(obj))
|
||||||
{
|
{
|
||||||
String loaders = (oClass.getClassLoader() == obj.getClass().getClassLoader()) ? "" : "Object Class and type Class are from different loaders.";
|
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");
|
String id = _root.getAttribute("id");
|
||||||
if (id != null)
|
if (id != null)
|
||||||
|
@ -404,7 +444,7 @@ public class XmlConfiguration
|
||||||
}
|
}
|
||||||
catch (NoSuchMethodException x)
|
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)
|
if (id != null)
|
||||||
|
@ -496,12 +536,12 @@ public class XmlConfiguration
|
||||||
envObj(node);
|
envObj(node);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new IllegalStateException("Unknown tag: " + tag + " in " + _url);
|
throw new IllegalStateException("Unknown tag: " + tag + " in " + _location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
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;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -677,7 +717,7 @@ public class XmlConfiguration
|
||||||
{
|
{
|
||||||
Object result = constructor.newInstance(args);
|
Object result = constructor.newInstance(args);
|
||||||
if (constructor.getAnnotation(Deprecated.class) != null)
|
if (constructor.getAnnotation(Deprecated.class) != null)
|
||||||
LOG.warn("Deprecated constructor {} in {}", constructor, _url);
|
LOG.warn("Deprecated constructor {} in {}", constructor, _location);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -685,7 +725,7 @@ public class XmlConfiguration
|
||||||
{
|
{
|
||||||
Object result = method.invoke(obj, args);
|
Object result = method.invoke(obj, args);
|
||||||
if (method.getAnnotation(Deprecated.class) != null)
|
if (method.getAnnotation(Deprecated.class) != null)
|
||||||
LOG.warn("Deprecated method {} in {}", method, _url);
|
LOG.warn("Deprecated method {} in {}", method, _location);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -693,7 +733,7 @@ public class XmlConfiguration
|
||||||
{
|
{
|
||||||
Object result = field.get(object);
|
Object result = field.get(object);
|
||||||
if (field.getAnnotation(Deprecated.class) != null)
|
if (field.getAnnotation(Deprecated.class) != null)
|
||||||
LOG.warn("Deprecated field {} in {}", field, _url);
|
LOG.warn("Deprecated field {} in {}", field, _location);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -701,7 +741,7 @@ public class XmlConfiguration
|
||||||
{
|
{
|
||||||
field.set(obj, arg);
|
field.set(obj, arg);
|
||||||
if (field.getAnnotation(Deprecated.class) != null)
|
if (field.getAnnotation(Deprecated.class) != null)
|
||||||
LOG.warn("Deprecated field {} in {}", field, _url);
|
LOG.warn("Deprecated field {} in {}", field, _location);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -18,12 +18,10 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.xml;
|
package org.eclipse.jetty.xml;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -627,7 +625,7 @@ public class XmlConfigurationTest
|
||||||
@Test
|
@Test
|
||||||
public void testArgumentsGetIgnoredMissingDTD() throws Exception
|
public void testArgumentsGetIgnoredMissingDTD() throws Exception
|
||||||
{
|
{
|
||||||
XmlConfiguration xmlConfiguration = new XmlConfiguration(new ByteArrayInputStream(("" +
|
XmlConfiguration xmlConfiguration = new XmlConfiguration("" +
|
||||||
"<Configure class=\"org.eclipse.jetty.xml.AnnotatedTestConfiguration\">" +
|
"<Configure class=\"org.eclipse.jetty.xml.AnnotatedTestConfiguration\">" +
|
||||||
" <Arg>arg1</Arg> " +
|
" <Arg>arg1</Arg> " +
|
||||||
" <Arg>arg2</Arg> " +
|
" <Arg>arg2</Arg> " +
|
||||||
|
@ -639,7 +637,7 @@ public class XmlConfigurationTest
|
||||||
" <Arg>arg3</Arg>\n" +
|
" <Arg>arg3</Arg>\n" +
|
||||||
" </New>" +
|
" </New>" +
|
||||||
" </Set>" +
|
" </Set>" +
|
||||||
"</Configure>").getBytes(StandardCharsets.ISO_8859_1)));
|
"</Configure>");
|
||||||
// XmlConfiguration xmlConfiguration = new XmlConfiguration(url);
|
// XmlConfiguration xmlConfiguration = new XmlConfiguration(url);
|
||||||
|
|
||||||
AnnotatedTestConfiguration atc = (AnnotatedTestConfiguration)xmlConfiguration.configure();
|
AnnotatedTestConfiguration atc = (AnnotatedTestConfiguration)xmlConfiguration.configure();
|
||||||
|
@ -655,7 +653,7 @@ public class XmlConfigurationTest
|
||||||
@Test
|
@Test
|
||||||
public void testSetGetIgnoredMissingDTD() throws Exception
|
public void testSetGetIgnoredMissingDTD() throws Exception
|
||||||
{
|
{
|
||||||
XmlConfiguration xmlConfiguration = new XmlConfiguration(new ByteArrayInputStream(("" +
|
XmlConfiguration xmlConfiguration = new XmlConfiguration("" +
|
||||||
"<Configure class=\"org.eclipse.jetty.xml.DefaultTestConfiguration\">" +
|
"<Configure class=\"org.eclipse.jetty.xml.DefaultTestConfiguration\">" +
|
||||||
" <Set name=\"first\">arg1</Set> " +
|
" <Set name=\"first\">arg1</Set> " +
|
||||||
" <Set name=\"second\">arg2</Set> " +
|
" <Set name=\"second\">arg2</Set> " +
|
||||||
|
@ -667,7 +665,7 @@ public class XmlConfigurationTest
|
||||||
" <Set name=\"third\">arg3</Set> " +
|
" <Set name=\"third\">arg3</Set> " +
|
||||||
" </New>" +
|
" </New>" +
|
||||||
" </Set>" +
|
" </Set>" +
|
||||||
"</Configure>").getBytes(StandardCharsets.UTF_8)));
|
"</Configure>");
|
||||||
// XmlConfiguration xmlConfiguration = new XmlConfiguration(url);
|
// XmlConfiguration xmlConfiguration = new XmlConfiguration(url);
|
||||||
|
|
||||||
DefaultTestConfiguration atc = (DefaultTestConfiguration)xmlConfiguration.configure();
|
DefaultTestConfiguration atc = (DefaultTestConfiguration)xmlConfiguration.configure();
|
||||||
|
|
Loading…
Reference in New Issue