下面列出了org.eclipse.jetty.server.Server#getHandler ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private List<String> startServer(Server server, String schedulerHost, int restPort, String httpProtocol) {
try {
if (server.getHandler() == null) {
logger.info("SCHEDULER_HOME/dist/war folder is empty, nothing is deployed");
} else {
server.start();
if (server.isStarted()) {
return printDeployedApplications(server, schedulerHost, restPort, httpProtocol);
} else {
logger.error("Failed to start web applications");
System.exit(1);
}
}
} catch (BindException bindException) {
logger.error("Failed to start web applications. Port " + restPort + " is already used", bindException);
System.exit(2);
} catch (Exception e) {
logger.error("Failed to start web applications", e);
System.exit(3);
}
return new ArrayList<>();
}
public static void initWebAppContext(Server server, int type) throws Exception {
System.out.println("[INFO] Application loading");
WebAppContext webAppContext = (WebAppContext) server.getHandler();
webAppContext.setContextPath(CONTEXT);
webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH);
webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH);
if (IDE_INTELLIJ == type) {
webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH);
supportJspAndSetTldJarNames(server, TLD_JAR_NAMES);
} else {
webAppContext.setParentLoaderPriority(true);
}
System.out.println("[INFO] Application loaded");
}
public static void initWebAppContext(Server server, int type) {
System.out.println("[INFO] Application loading");
WebAppContext webAppContext = (WebAppContext) server.getHandler();
webAppContext.setContextPath(CONTEXT);
webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH);
webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH);
if (IDE_INTELLIJ == type) {
webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH);
supportJspAndSetTldJarNames(server, TLD_JAR_NAMES);
} else {
webAppContext.setParentLoaderPriority(true);
}
System.out.println("[INFO] Application loaded");
}
public void start() throws Exception {
Resource configXml = Resource.newSystemResource(config);
XmlConfiguration configuration = new XmlConfiguration(configXml.getInputStream());
server = (Server) configuration.configure();
// Integer port = getPort();
// if (port != null && port > 0) {
// Connector[] connectors = server.getConnectors();
// for (Connector connector : connectors) {
// connector.setPort(port);
// }
// }
Handler handler = server.getHandler();
if (handler != null && handler instanceof WebAppContext) {
WebAppContext webAppContext = (WebAppContext) handler;
webAppContext.setResourceBase(JettyEmbedServer.class.getResource("/webapp").toString());
}
server.start();
if (logger.isInfoEnabled()) {
logger.info("##Jetty Embed Server is startup!");
}
}
public static void supportJspAndSetTldJarNames(Server server, String... jarNames) {
WebAppContext context = (WebAppContext) server.getHandler();
// This webapp will use jsps and jstl. We need to enable the
// AnnotationConfiguration in
// order to correctly set up the jsp container
org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList
.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration");
// Set the ContainerIncludeJarPattern so that jetty examines these container-path
// jars for
// tlds, web-fragments etc.
// If you omit the jar that contains the jstl .tlds, the jsp engine will scan for
// them
// instead.
ArrayList jarNameExprssions = Lists.newArrayList(".*/[^/]*servlet-api-[^/]*\\.jar$",
".*/javax.servlet.jsp.jstl-.*\\.jar$", ".*/[^/]*taglibs.*\\.jar$");
for (String jarName : jarNames) {
jarNameExprssions.add(".*/" + jarName + "-[^/]*\\.jar$");
}
context.setAttribute("org.eclipse.jetty.io.github.dunwu.javaee.server.webapp.ContainerIncludeJarPattern",
StringUtils.join(jarNameExprssions, '|'));
}
public static void initWebAppContext(Server server, int type) {
System.out.println("[INFO] Application loading");
WebAppContext webAppContext = (WebAppContext) server.getHandler();
webAppContext.setContextPath(CONTEXT);
webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH);
webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH);
if (IDE_INTELLIJ == type) {
webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH);
supportJspAndSetTldJarNames(server, TLD_JAR_NAMES);
} else {
webAppContext.setParentLoaderPriority(true);
}
System.out.println("[INFO] Application loaded");
}
public static void supportJspAndSetTldJarNames(Server server, String... jarNames) {
WebAppContext context = (WebAppContext) server.getHandler();
// This webapp will use jsps and jstl. We need to enable the
// AnnotationConfiguration in
// order to correctly set up the jsp container
org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList
.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration");
// Set the ContainerIncludeJarPattern so that jetty examines these container-path
// jars for
// tlds, web-fragments etc.
// If you omit the jar that contains the jstl .tlds, the jsp engine will scan for
// them
// instead.
ArrayList jarNameExprssions = Lists.newArrayList(".*/[^/]*servlet-api-[^/]*\\.jar$",
".*/javax.servlet.jsp.jstl-.*\\.jar$", ".*/[^/]*taglibs.*\\.jar$");
for (String jarName : jarNames) {
jarNameExprssions.add(".*/" + jarName + "-[^/]*\\.jar$");
}
context.setAttribute("org.eclipse.jetty.io.github.dunwu.javaee.server.webapp.ContainerIncludeJarPattern",
StringUtils.join(jarNameExprssions, '|'));
}
public static void supportJspAndSetTldJarNames(Server server, String... jarNames) {
WebAppContext context = (WebAppContext) server.getHandler();
// This webapp will use jsps and jstl. We need to enable the
// AnnotationConfiguration in
// order to correctly set up the jsp container
org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList
.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration");
// Set the ContainerIncludeJarPattern so that jetty examines these container-path
// jars for
// tlds, web-fragments etc.
// If you omit the jar that contains the jstl .tlds, the jsp engine will scan for
// them
// instead.
ArrayList jarNameExprssions = Lists.newArrayList(".*/[^/]*servlet-api-[^/]*\\.jar$",
".*/javax.servlet.jsp.jstl-.*\\.jar$", ".*/[^/]*taglibs.*\\.jar$");
for (String jarName : jarNames) {
jarNameExprssions.add(".*/" + jarName + "-[^/]*\\.jar$");
}
context.setAttribute("org.eclipse.jetty.io.github.dunwu.javaee.server.webapp.ContainerIncludeJarPattern",
StringUtils.join(jarNameExprssions, '|'));
}
public void startup() throws Exception {
Resource configXml = Resource.newSystemResource(DEFAULT_JETTY_CONFIG);
XmlConfiguration configuration = new XmlConfiguration(configXml.getInputStream());
server = (Server) configuration.configure();
Integer port = config.getHttpPort();
if (port != null && port > 0) {
Connector[] connectors = server.getConnectors();
for (Connector connector : connectors) {
if (connector instanceof AbstractNetworkConnector) {
((AbstractNetworkConnector) connector).setPort(port);
}
}
}
Handler handler = server.getHandler();
if (handler != null && handler instanceof WebAppContext) {
WebAppContext webAppContext = (WebAppContext) handler;
String webAppPath = System.getProperty("webapp.conf");
logger.info("Web App Path is " + webAppPath);
if (StringUtils.isBlank(webAppPath)) {
webAppContext.setResourceBase(JettyServer.class.getResource("/webapp").toString());
} else {
webAppContext.setResourceBase(webAppPath);
}
}
server.start();
if (logger.isInfoEnabled()) {
logger.info("##Jetty Embed Server is started.");
}
}
public static void supportJspAndSetTldJarNames(Server server, String... jarNames) {
WebAppContext context = (WebAppContext) server.getHandler();
// This webapp will use jsps and jstl. We need to enable the
// AnnotationConfiguration in
// order to correctly set up the jsp container
org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList
.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration");
// Set the ContainerIncludeJarPattern so that jetty examines these container-path
// jars for
// tlds, web-fragments etc.
// If you omit the jar that contains the jstl .tlds, the jsp engine will scan for
// them
// instead.
List<String> list = new ArrayList<>();
list.add(".*/[^/]*servlet-api-[^/]*\\.jar$");
list.add(".*/javax.servlet.jsp.jstl-.*\\.jar$");
list.add(".*/[^/]*taglibs.*\\.jar$");
for (String jarName : jarNames) {
String str = ".*/" + jarName + "-[^/]*\\.jar$";
list.add(str);
}
context.setAttribute("org.eclipse.jetty.io.github.dunwu.javaee.server.webapp.ContainerIncludeJarPattern",
StringUtils.join(list, '|'));
}
public static void reloadWebAppContext(Server server) throws Exception {
WebAppContext webAppContext = (WebAppContext) server.getHandler();
System.out.println("[INFO] Application reloading");
webAppContext.stop();
WebAppClassLoader classLoader = new WebAppClassLoader(webAppContext);
classLoader.addClassPath(getAbsolutePath() + "target/classes");
classLoader.addClassPath(getAbsolutePath() + "target/test-classes");
webAppContext.setClassLoader(classLoader);
webAppContext.start();
System.out.println("[INFO] Application reloaded");
}
public static void supportJspAndSetTldJarNames(Server server, String... jarNames) {
WebAppContext context = (WebAppContext) server.getHandler();
// This webapp will use jsps and jstl. We need to enable the
// AnnotationConfiguration in
// order to correctly set up the jsp container
org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList
.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration");
// Set the ContainerIncludeJarPattern so that jetty examines these container-path
// jars for
// tlds, web-fragments etc.
// If you omit the jar that contains the jstl .tlds, the jsp engine will scan for
// them
// instead.
List<String> list = new ArrayList<>();
list.add(".*/[^/]*servlet-api-[^/]*\\.jar$");
list.add(".*/javax.servlet.jsp.jstl-.*\\.jar$");
list.add(".*/[^/]*taglibs.*\\.jar$");
for (String jarName : jarNames) {
String str = ".*/" + jarName + "-[^/]*\\.jar$";
list.add(str);
}
context.setAttribute("org.eclipse.jetty.io.github.dunwu.javaee.server.webapp.ContainerIncludeJarPattern",
StringUtils.join(list, '|'));
}
public static void reloadWebAppContext(Server server) throws Exception {
WebAppContext webAppContext = (WebAppContext) server.getHandler();
System.out.println("[INFO] Application reloading");
webAppContext.stop();
WebAppClassLoader classLoader = new WebAppClassLoader(webAppContext);
classLoader.addClassPath(getAbsolutePath() + "target/classes");
classLoader.addClassPath(getAbsolutePath() + "target/test-classes");
webAppContext.setClassLoader(classLoader);
webAppContext.start();
System.out.println("[INFO] Application reloaded");
}
public static void reloadWebAppContext(Server server) throws Exception {
WebAppContext webAppContext = (WebAppContext) server.getHandler();
System.out.println("[INFO] Application reloading");
webAppContext.stop();
WebAppClassLoader classLoader = new WebAppClassLoader(webAppContext);
classLoader.addClassPath(getAbsolutePath() + "target/classes");
classLoader.addClassPath(getAbsolutePath() + "target/test-classes");
webAppContext.setClassLoader(classLoader);
webAppContext.start();
System.out.println("[INFO] Application reloaded");
}
private List<String> printDeployedApplications(Server server, String schedulerHost, int restPort,
String httpProtocol) {
HandlerList handlerList = (HandlerList) server.getHandler();
ArrayList<String> applicationsUrls = new ArrayList<>();
if (handlerList.getHandlers() != null) {
for (Handler handler : handlerList.getHandlers()) {
if (!(handler instanceof WebAppContext)) {
continue;
}
WebAppContext webAppContext = (WebAppContext) handler;
Throwable startException = webAppContext.getUnavailableException();
if (startException == null) {
if (!"/".equals(webAppContext.getContextPath())) {
String applicationUrl = getApplicationUrl(httpProtocol, schedulerHost, restPort, webAppContext);
applicationsUrls.add(applicationUrl);
logger.info("The web application " + webAppContext.getContextPath() + " created on " +
applicationUrl);
}
} else {
logger.warn("Failed to start context " + webAppContext.getContextPath(), startException);
}
}
logger.info("*** Get started at " + httpProtocol + "://" + schedulerHost + ":" + restPort + " ***");
}
return applicationsUrls;
}
public static void reloadWebAppContext(Server server) throws Exception {
WebAppContext webAppContext = (WebAppContext) server.getHandler();
System.out.println("[INFO] Application reloading");
webAppContext.stop();
WebAppClassLoader classLoader = new WebAppClassLoader(webAppContext);
classLoader.addClassPath(getAbsolutePath() + "target/classes");
classLoader.addClassPath(getAbsolutePath() + "target/test-classes");
webAppContext.setClassLoader(classLoader);
webAppContext.start();
System.out.println("[INFO] Application reloaded");
}
public static void reloadWebAppContext(Server server) throws Exception {
WebAppContext webAppContext = (WebAppContext) server.getHandler();
System.out.println("[INFO] Application reloading");
webAppContext.stop();
WebAppClassLoader classLoader = new WebAppClassLoader(webAppContext);
classLoader.addClassPath(getAbsolutePath() + "target/classes");
classLoader.addClassPath(getAbsolutePath() + "target/test-classes");
webAppContext.setClassLoader(classLoader);
webAppContext.start();
System.out.println("[INFO] Application reloaded");
}
public static void reloadWebAppContext(Server server) throws Exception {
WebAppContext webAppContext = (WebAppContext) server.getHandler();
System.out.println("[INFO] Application reloading");
webAppContext.stop();
WebAppClassLoader classLoader = new WebAppClassLoader(webAppContext);
classLoader.addClassPath(getAbsolutePath() + "target/classes");
classLoader.addClassPath(getAbsolutePath() + "target/test-classes");
webAppContext.setClassLoader(classLoader);
webAppContext.start();
System.out.println("[INFO] Application reloaded");
}
private static void reloadWebAppContext(Server server) throws Exception {
WebAppContext webAppContext = (WebAppContext) server.getHandler();
System.out.println("[INFO] Application reloading");
webAppContext.stop();
WebAppClassLoader classLoader = new WebAppClassLoader(webAppContext);
classLoader.addClassPath(getAbsolutePath() + "target/classes");
classLoader.addClassPath(getAbsolutePath() + "target/test-classes");
webAppContext.setClassLoader(classLoader);
webAppContext.start();
System.out.println("[INFO] Application reloaded");
}
public static void reloadWebAppContext(Server server) throws Exception {
WebAppContext webAppContext = (WebAppContext) server.getHandler();
System.out.println("[INFO] Application reloading");
webAppContext.stop();
WebAppClassLoader classLoader = new WebAppClassLoader(webAppContext);
classLoader.addClassPath(getAbsolutePath() + "target/classes");
classLoader.addClassPath(getAbsolutePath() + "target/test-classes");
webAppContext.setClassLoader(classLoader);
webAppContext.start();
System.out.println("[INFO] Application reloaded");
}