Skip to content

Commit

Permalink
Merge branch 'jetty-9.4.x' of github.com:eclipse/jetty.project into j…
Browse files Browse the repository at this point in the history
…etty-9.4.x
  • Loading branch information
joakime committed Jun 10, 2019
2 parents 8b2728e + 5dbc0bd commit da4f116
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void processBinding(Node node, App app) throws Exception

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void configureContextHandler()
{
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -142,10 +141,10 @@ public static Server configure(Server server, List<URL> jettyConfigurations, Dic

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,32 @@ public class SpringConfigurationProcessor implements ConfigurationProcessor
private String _main;

@Override
public void init(URL url, XmlParser.Node config, XmlConfiguration configuration)
public void init(URL url, XmlParser.Node root, XmlConfiguration configuration)
{
// Moving back and forth between URL and File/FileSystem/Path/Resource is known to cause escaping issues.
init(org.eclipse.jetty.util.resource.Resource.newResource(url), root, configuration);
}

@Override
public void init(org.eclipse.jetty.util.resource.Resource jettyResource, XmlParser.Node config, XmlConfiguration configuration)
{
try
{
_configuration = configuration;

Resource resource = url != null
? new UrlResource(url)
: new ByteArrayResource(("" +
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\" \"http://www.springframework.org/dtd/spring-beans.dtd\">" +
config).getBytes(StandardCharsets.UTF_8));
Resource springResource;

if (jettyResource != null)
{
springResource = new UrlResource(jettyResource.getURI());
}
else
{
springResource = new ByteArrayResource(("" +
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\" \"http://www.springframework.org/dtd/spring-beans.dtd\">" +
config).getBytes(StandardCharsets.UTF_8));
}

_beanFactory = new DefaultListableBeanFactory()
{
Expand All @@ -92,7 +106,7 @@ protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrap
}
};

new XmlBeanDefinitionReader(_beanFactory).loadBeanDefinitions(resource);
new XmlBeanDefinitionReader(_beanFactory).loadBeanDefinitions(springResource);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package org.eclipse.jetty.websocket.client;

import java.io.InputStream;
import java.net.URL;

import org.eclipse.jetty.client.HttpClient;
Expand All @@ -36,9 +35,9 @@ public static HttpClient get(@SuppressWarnings("unused") WebSocketContainerScope
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

package org.eclipse.jetty.xml;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jetty.util.resource.Resource;

/**
* A ConfigurationProcessor for non XmlConfiguration format files.
* <p>
Expand All @@ -31,8 +34,33 @@
*/
public interface ConfigurationProcessor
{
public void init(URL url, XmlParser.Node root, XmlConfiguration configuration);

public Object configure( Object obj) throws Exception;
public Object configure() throws Exception;
/**
* @deprecated use {@link #init(Resource, XmlParser.Node, XmlConfiguration)} instead
*/
@Deprecated
void init(URL url, XmlParser.Node root, XmlConfiguration configuration);

/**
* Initialize a ConfigurationProcessor from provided Resource and XML
*
* @param resource the resource being read
* @param root the parsed XML root node for the resource
* @param configuration the configuration being used (typically for ref IDs)
*/
default void init(Resource resource, XmlParser.Node root, XmlConfiguration configuration)
{
// Moving back and forth between URL and File/FileSystem/Path/Resource is known to cause escaping issues.
try
{
init(resource.getURI().toURL(), root, configuration);
}
catch (MalformedURLException e)
{
throw new IllegalStateException("Unable to convert Resource to URL", e);
}
}

Object configure(Object obj) throws Exception;

Object configure() throws Exception;
}
87 changes: 63 additions & 24 deletions jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,45 +176,68 @@ public static String normalizeURI(String uri)

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 configuration the URL of the XML configuration
* @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(URL configuration) throws SAXException, IOException
public XmlConfiguration(Resource resource) throws SAXException, IOException
{
synchronized (__parser)
{
_url = configuration;
setConfig(__parser.parse(configuration.toString()));
_location = resource;
try(InputStream inputStream = resource.getInputStream())
{
setConfig(__parser.parse(inputStream));
}
_dtd = __parser.getDTD();
}
}

/**
* Reads and parses the XML configuration file.
*
* @param configuration the URL of the XML configuration
* @throws IOException if the configuration could not be read
* @throws SAXException if the configuration could not be parsed
* @deprecated use {@link XmlConfiguration(Resource)} instead due to escaping issues
*/
@Deprecated
public XmlConfiguration(URL configuration) throws SAXException, IOException
{
this(Resource.newResource(configuration));
}

/**
* Reads and parses the XML configuration string.
*
* @param configuration String of XML configuration commands excluding the normal XML preamble.
* 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\">"
configuration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<!DOCTYPE Configure PUBLIC \"-//Jetty//Configure//EN\" \"http://www.eclipse.org/jetty/configure_9_3.dtd\">"
+ configuration;
InputSource source = new InputSource(new StringReader(configuration));
synchronized (__parser)
try (StringReader reader = new StringReader(configuration))
{
_url = null;
setConfig(__parser.parse(source));
_dtd = __parser.getDTD();
InputSource source = new InputSource(reader);
synchronized (__parser)
{
_location = null;
setConfig(__parser.parse(source));
_dtd = __parser.getDTD();
}
}
}

Expand All @@ -224,18 +247,30 @@ public XmlConfiguration(String configuration) throws SAXException, IOException
* @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();
}
}

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

private void setConfig(XmlParser.Node config)
{
if ("Configure".equals(config.getTag()))
Expand All @@ -257,7 +292,7 @@ else if (__factoryLoader != null)
{
throw new IllegalArgumentException("Unknown XML tag:" + config.getTag());
}
_processor.init(_url, config, this);
_processor.init(_location, config, this);
}

/**
Expand Down Expand Up @@ -332,14 +367,18 @@ public void initializeDefaults(Object object)

private static class JettyXmlConfiguration implements ConfigurationProcessor
{
private String _url;
XmlParser.Node _root;
XmlConfiguration _configuration;

@Override
public void init(URL url, XmlParser.Node root, XmlConfiguration configuration)
{
_url = url == null ? null : url.toString();
// nobody calls this.
}

@Override
public void init(Resource resource, XmlParser.Node root, XmlConfiguration configuration)
{
_root = root;
_configuration = configuration;
}
Expand All @@ -352,7 +391,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 " + _url);
throw new IllegalArgumentException("Object of class '" + obj.getClass().getCanonicalName() + "' is not of type '" + oClass.getCanonicalName() + "'. " + loaders + " in " + _configuration);
}
String id = _root.getAttribute("id");
if (id != null)
Expand Down Expand Up @@ -404,7 +443,7 @@ public Object configure() throws Exception
}
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, _configuration));
}
}
if (id != null)
Expand Down Expand Up @@ -496,12 +535,12 @@ public void configure(Object obj, XmlParser.Node cfg, int i) throws Exception
envObj(node);
break;
default:
throw new IllegalStateException("Unknown tag: " + tag + " in " + _url);
throw new IllegalStateException("Unknown tag: " + tag + " in " + _configuration);
}
}
catch (Exception e)
{
LOG.warn("Config error at " + node, e.toString() + " in " + _url);
LOG.warn("Config error at " + node, e.toString() + " in " + _configuration);
throw e;
}
}
Expand Down Expand Up @@ -677,31 +716,31 @@ private Object invokeConstructor(Constructor<?> constructor, Object... args) thr
{
Object result = constructor.newInstance(args);
if (constructor.getAnnotation(Deprecated.class) != null)
LOG.warn("Deprecated constructor {} in {}", constructor, _url);
LOG.warn("Deprecated constructor {} in {}", constructor, _configuration);
return result;
}

private Object invokeMethod(Method method, Object obj, Object... args) throws IllegalAccessException, InvocationTargetException
{
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, _configuration);
return result;
}

private Object getField(Field field, Object object) throws IllegalAccessException
{
Object result = field.get(object);
if (field.getAnnotation(Deprecated.class) != null)
LOG.warn("Deprecated field {} in {}", field, _url);
LOG.warn("Deprecated field {} in {}", field, _configuration);
return result;
}

private void setField(Field field, Object obj, Object arg) throws IllegalAccessException
{
field.set(obj, arg);
if (field.getAnnotation(Deprecated.class) != null)
LOG.warn("Deprecated field {} in {}", field, _url);
LOG.warn("Deprecated field {} in {}", field, _configuration);
}

/**
Expand Down Expand Up @@ -1519,7 +1558,7 @@ private Object itemValue(Object obj, Object item) throws Exception
if ("Env".equals(tag))
return envObj(node);

LOG.warn("Unknown value tag: " + node, new Throwable());
LOG.warn("Unknown value tag: " + node + " in " + _configuration, new Throwable());
return null;
}

Expand Down

0 comments on commit da4f116

Please sign in to comment.