如何以编程方式启动具有多个配置文件的码头服务器? [英] How can I programmatically start a jetty server with multiple configuration files?
本文介绍了如何以编程方式启动具有多个配置文件的码头服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
换句话说,这等同于:
java -jar start.jar等/jetty.xml jetty-plus.xml
java -jar start.jar etc/jetty.xml jetty-plus.xml
我正在使用Jetty 6.
I'm using Jetty 6.
推荐答案
Jetty 7,Jetty 8和Jetty 9可以实现. 你可以升级吗?
This is possible with Jetty 7, Jetty 8, and Jetty 9. Can you upgrade?
注意:码头6已于2010年停产.
Note: Jetty 6 was end of life'd back in 2010.
package jetty;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.xml.XmlConfiguration;
public class EmbeddedViaXml
{
public static void main(String[] args)
{
try
{
// The configuration files
List<URL> configs = new ArrayList<>();
configs.add(new File("jetty.xml").toURI().toURL());
configs.add(new File("jetty-plus.xml").toURI().toURL());
// The properties
Map<String, String> props = new HashMap<String, String>();
props.put("jetty.home",new File(System.getProperty("user.dir")).getCanonicalPath());
Server server = load(configs,props);
server.start();
server.join();
}
catch (Throwable t)
{
t.printStackTrace();
}
}
public static Server load(List<URL> xmlConfigUrls, Map<String, String> props) throws Exception
{
XmlConfiguration last = null;
// Hold list of configured objects
Object[] obj = new Object[xmlConfigUrls.size()];
// Configure everything
for (int i = 0; i < xmlConfigUrls.size(); i++)
{
URL configURL = xmlConfigUrls.get(i);
XmlConfiguration configuration = new XmlConfiguration(configURL);
if (last != null)
{
// Let configuration know about prior configured objects
configuration.getIdMap().putAll(last.getIdMap());
}
configuration.getProperties().putAll(props);
obj[i] = configuration.configure();
last = configuration;
}
// Find Server Instance.
Server foundServer = null;
int serverCount = 0;
for (int i = 0; i < xmlConfigUrls.size(); i++)
{
if (obj[i] instanceof Server)
{
if (obj[i].equals(foundServer))
{
// Identical server instance found
continue; // Skip
}
foundServer = (Server)obj[i];
serverCount++;
}
}
if (serverCount <= 0)
{
throw new IllegalStateException("Load failed to configure a " + Server.class.getName());
}
if (serverCount == 1)
{
return foundServer;
}
throw new IllegalStateException(String.format("Configured %d Servers, expected 1",serverCount));
}
}
这篇关于如何以编程方式启动具有多个配置文件的码头服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文