WIP Environments
This commit is contained in:
parent
0a32147e89
commit
789856d814
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
* <Configure id="Server" class="org.eclipse.jetty.server.Server">
|
||||
* <New class="org.eclipse.jetty.xml.EnvironmentBuilder">
|
||||
* <Arg name="name">ee10</Arg>
|
||||
* <Call name="putId">
|
||||
* <Arg>Server</Arg>
|
||||
* <Arg><Ref id="Server"/></Arg>
|
||||
* </Call>
|
||||
* <Put name="propertyA">valueA</Put>
|
||||
* <Put name="propertyB">valueB</Put>
|
||||
* <Put name="propertyC">valueC</Put>
|
||||
*
|
||||
* <Call name="addClassPath">
|
||||
* <Arg>
|
||||
* <Array type="String">
|
||||
* <Item>lib/feature/foo.jar</Item>
|
||||
* <Item>lib/feature/bar.jar</Item>
|
||||
* </Array>
|
||||
* </Arg>
|
||||
* </Call>
|
||||
*
|
||||
* <Call name="addXml">
|
||||
* <Arg>
|
||||
* <Array type="String">
|
||||
* <Item>etc/foo.xml</Item>
|
||||
* <Item>etc/bar.jar</Item>
|
||||
* </Array>
|
||||
* </Arg>
|
||||
* </Call>
|
||||
*
|
||||
* <Call id="EnvironmentEE10" name="build"/>
|
||||
* </New>
|
||||
* <Call name="addEnvironment">
|
||||
* <Arg><Ref id="EnvironmentEE10"/></Arg>
|
||||
* </Call>
|
||||
* </Configure>
|
||||
* </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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue