Java Code Examples for org.eclipse.jetty.server.Server#getHandler()

The following examples show how to use org.eclipse.jetty.server.Server#getHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JettyStarter.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
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<>();
}
 
Example 2
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
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");
}
 
Example 3
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
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, '|'));
}
 
Example 4
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
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");
}
 
Example 5
Source File: JettyEmbedServer.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
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!");
    }
}
 
Example 6
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
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, '|'));
}
 
Example 7
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
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, '|'));
}
 
Example 8
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
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");
}
 
Example 9
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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");
}
 
Example 10
Source File: JettyServer.java    From DataLink with Apache License 2.0 5 votes vote down vote up
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.");
    }
}
 
Example 11
Source File: DevServer.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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");
}
 
Example 12
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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");
}
 
Example 13
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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");
}
 
Example 14
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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");
}
 
Example 15
Source File: JettyStarter.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 16
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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");
}
 
Example 17
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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");
}
 
Example 18
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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, '|'));
}
 
Example 19
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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");
}
 
Example 20
Source File: JettyFactory.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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, '|'));
}