WIP Environments

This commit is contained in:
Greg Wilkins 2022-05-03 17:00:48 +02:00
parent 0a32147e89
commit 789856d814
3 changed files with 213 additions and 63 deletions

View File

@ -16,10 +16,7 @@ package org.eclipse.jetty.server;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
@ -27,6 +24,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
@ -61,7 +59,7 @@ import org.eclipse.jetty.util.thread.ThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Server extends Handler.Wrapper implements Attributes, Environment.Factory
public class Server extends Handler.Wrapper implements Attributes
{
private static final Logger LOG = LoggerFactory.getLogger(Server.class);
private static final String __serverInfo = "jetty/" + Server.getVersion();
@ -143,13 +141,21 @@ public class Server extends Handler.Wrapper implements Attributes, Environment.F
return _serverContext;
}
@Override
public java.util.Collection<Environment> getEnvironments()
{
return _environments.values();
}
@Override
public void addEnvironment(Environment environment)
{
Objects.requireNonNull(environment);
String key = environment.getName();
if (StringUtil.isBlank(key))
throw new IllegalArgumentException("No environment name");
key = key.toLowerCase();
_environments.put(key, environment);
}
public Environment getEnvironment(String name)
{
String key = StringUtil.isBlank(name) ? "server" : name.toLowerCase();
@ -157,14 +163,7 @@ public class Server extends Handler.Wrapper implements Attributes, Environment.F
Environment environment = _environments.get(key);
if (environment != null)
return environment;
environment = new NamedEnvironment(name);
Environment existing = _environments.putIfAbsent(key, environment);
if (existing != null)
return existing;
addBean(environment);
return environment;
throw new RuntimeException("Unknown environment: " + name);
}
@Override
@ -796,24 +795,18 @@ public class Server extends Handler.Wrapper implements Attributes, Environment.F
{
return Server.class.getClassLoader();
}
@Override
public void addClassPath(Path path)
{
throw new UnsupportedOperationException();
}
}
private class NamedEnvironment extends Attributes.Layer implements Environment, Dumpable
{
private final String _name;
private final EnvClassLoader _classLoader;
private final ClassLoader _classLoader;
private NamedEnvironment(String name)
private NamedEnvironment(String name, ClassLoader classLoader)
{
super(Server.this);
_name = name;
_classLoader = new EnvClassLoader(name, Server.class.getClassLoader());
_classLoader = classLoader;
}
@Override
@ -828,34 +821,6 @@ public class Server extends Handler.Wrapper implements Attributes, Environment.F
return _classLoader;
}
@Override
public void addClassPath(Path path)
{
try
{
_classLoader.addURL(path.toUri().toURL());
}
catch (MalformedURLException e)
{
throw new RuntimeException(e);
}
}
// TODO replace with PathClassLoader?
private static class EnvClassLoader extends URLClassLoader
{
public EnvClassLoader(String name, ClassLoader parent)
{
super(name, new URL[0], parent);
}
@Override
protected void addURL(URL url)
{
super.addURL(url);
}
}
@Override
public void dump(Appendable out, String indent) throws IOException
{

View File

@ -13,9 +13,6 @@
package org.eclipse.jetty.util.component;
import java.nio.file.Path;
import java.util.Collection;
import org.eclipse.jetty.util.Attributes;
public interface Environment extends Attributes
@ -24,8 +21,6 @@ public interface Environment extends Attributes
ClassLoader getClassLoader();
void addClassPath(Path path);
default void run(Runnable runnable)
{
ClassLoader old = Thread.currentThread().getContextClassLoader();
@ -39,11 +34,4 @@ public interface Environment extends Attributes
Thread.currentThread().setContextClassLoader(old);
}
}
interface Factory
{
Collection<Environment> getEnvironments();
Environment getEnvironment(String name);
}
}

View File

@ -0,0 +1,197 @@
//
// ========================================================================
// Copyright (c) 1995-2022 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;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.annotation.Name;
import org.eclipse.jetty.util.component.Environment;
import org.eclipse.jetty.util.resource.Resource;
/**
* A Builder of {@link Environment}s intended to be used in XML
* files generated by <code>start.jar</code>.
* <pre&gt;
* &lt;Configure id="Server" class="org.eclipse.jetty.server.Server"&gt;
* &lt;New class="org.eclipse.jetty.xml.EnvironmentBuilder"&gt;
* &lt;Arg name="name"&gt;ee10&lt;/Arg&gt;
* &lt;Call name="putId"&gt;
* &lt;Arg&gt;Server&lt;/Arg&gt;
* &lt;Arg&gt;&lt;Ref id="Server"/&gt;&lt;/Arg&gt;
* &lt;/Call&gt;
* &lt;Put name="propertyA"&gt;valueA&lt;/Put&gt;
* &lt;Put name="propertyB"&gt;valueB&lt;/Put&gt;
* &lt;Put name="propertyC"&gt;valueC&lt;/Put&gt;
*
* &lt;Call name="addClassPath"&gt;
* &lt;Arg&gt;
* &lt;Array type="String"&gt;
* &lt;Item&gt;lib/feature/foo.jar&lt;/Item&gt;
* &lt;Item&gt;lib/feature/bar.jar&lt;/Item&gt;
* &lt;/Array&gt;
* &lt;/Arg&gt;
* &lt;/Call&gt;
*
* &lt;Call name="addXml"&gt;
* &lt;Arg&gt;
* &lt;Array type="String"&gt;
* &lt;Item&gt;etc/foo.xml&lt;/Item&gt;
* &lt;Item&gt;etc/bar.jar&lt;/Item&gt;
* &lt;/Array&gt;
* &lt;/Arg&gt;
* &lt;/Call&gt;
*
* &lt;Call id="EnvironmentEE10" name="build"/&gt;
* &lt;/New&gt;
* &lt;Call name="addEnvironment"&gt;
* &lt;Arg&gt;&lt;Ref id="EnvironmentEE10"/&gt;&lt;/Arg&gt;
* &lt;/Call&gt;
* &lt;/Configure&gt;
* </pre>
*
*/
public class EnvironmentBuilder extends AbstractMap<String, String>
{
private final String _name;
private final String _environmentId;
private final Map<String, String> _properties = new HashMap<>();
private final List<Resource> _xmls = new ArrayList<>();
private final List<URL> _classpath = new ArrayList<>();
private final Map<String, Object> _idMap = new HashMap<>();
public EnvironmentBuilder(@Name("name") String name)
{
this(name, null);
}
public EnvironmentBuilder(@Name("name") String name,
@Name("envId") String envId)
{
System.getProperties().keySet().stream()
.map(String::valueOf)
.forEach(k -> _properties.put(k, System.getProperty(k)));
_name = name;
_environmentId = StringUtil.isBlank(envId) ? "Environment" : envId;
}
@Override
public Set<Entry<String, String>> entrySet()
{
return _properties.entrySet();
}
@Override
public String put(String property, String value)
{
return _properties.put(property, value);
}
public void putId(String id, Object value)
{
_idMap.put(id, value);
}
public void addXml(String... xmls)
{
try
{
for (String xml : xmls)
_xmls.add(Resource.newResource(xml));
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public void addClassPath(String... classPaths)
{
for (String classPath : classPaths)
{
try (Resource resource = Resource.newResource(classPath))
{
_classpath.add(resource.getURI().toURL());
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
public Environment build() throws Exception
{
if (StringUtil.isBlank(_name))
throw new IllegalStateException("Name not set");
Environment environment = new BuiltEnvironment(_name, _classpath);
_idMap.put(_environmentId, environment);
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(environment.getClassLoader());
try
{
// For all arguments, parse XMLs
XmlConfiguration last = null;
for (Resource xml : _xmls)
{
XmlConfiguration configuration = new XmlConfiguration(xml);
configuration.getIdMap().putAll(last == null ? _idMap : last.getIdMap());
configuration.getProperties().putAll(_properties);
configuration.configure();
last = configuration;
}
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
return environment;
}
private static class BuiltEnvironment extends Attributes.Mapped implements Environment
{
private final String _name;
private final URLClassLoader _classLoader;
public BuiltEnvironment(String name, List<URL> classpath)
{
_name = name;
_classLoader = new URLClassLoader(classpath.toArray(new URL[0]));
}
@Override
public String getName()
{
return _name;
}
@Override
public ClassLoader getClassLoader()
{
return _classLoader;
}
}
}