org.eclipse.jetty.webapp.JettyWebXmlConfiguration Java Examples

The following examples show how to use org.eclipse.jetty.webapp.JettyWebXmlConfiguration. 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: JettyServer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and configures a new Jetty instance.
 *
 * @param props the configuration
 */
public JettyServer(final NiFiProperties props) {
    final QueuedThreadPool threadPool = new QueuedThreadPool(props.getWebThreads());
    threadPool.setName("NiFi Web Server");

    // create the server
    this.server = new Server(threadPool);
    this.props = props;

    // enable the annotation based configuration to ensure the jsp container is initialized properly
    final Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
    classlist.addBefore(JettyWebXmlConfiguration.class.getName(), AnnotationConfiguration.class.getName());

    // configure server
    configureConnectors(server);

    // load wars from the nar working directories
    loadWars(locateNarWorkingDirectories());
}
 
Example #2
Source File: JettyServer.java    From nifi-registry with Apache License 2.0 6 votes vote down vote up
public JettyServer(final NiFiRegistryProperties properties, final CryptoKeyProvider cryptoKeyProvider, final String docsLocation) {
    final QueuedThreadPool threadPool = new QueuedThreadPool(properties.getWebThreads());
    threadPool.setName("NiFi Registry Web Server");

    this.properties = properties;
    this.masterKeyProvider = cryptoKeyProvider;
    this.docsLocation = docsLocation;
    this.server = new Server(threadPool);

    // enable the annotation based configuration to ensure the jsp container is initialized properly
    final Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
    classlist.addBefore(JettyWebXmlConfiguration.class.getName(), AnnotationConfiguration.class.getName());

    try {
        configureConnectors();
        loadWars();
    } catch (final Throwable t) {
        startUpFailure(t);
    }
}
 
Example #3
Source File: Jetty9Server.java    From gocd with Apache License 2.0 6 votes vote down vote up
private WebAppContext createWebAppContext() {
    webAppContext = new WebAppContext();
    webAppContext.setDefaultsDescriptor(GoWebXmlConfiguration.configuration(getWarFile()));

    webAppContext.setConfigurationClasses(new String[]{
            WebInfConfiguration.class.getCanonicalName(),
            WebXmlConfiguration.class.getCanonicalName(),
            JettyWebXmlConfiguration.class.getCanonicalName()
    });
    webAppContext.setContextPath(systemEnvironment.getWebappContextPath());

    // delegate all logging to parent classloader to avoid initialization of loggers in multiple classloaders
    webAppContext.getSystemClasspathPattern().add("org.apache.log4j.");
    webAppContext.getSystemClasspathPattern().add("org.slf4j.");
    webAppContext.getSystemClasspathPattern().add("org.apache.commons.logging.");

    webAppContext.setWar(getWarFile());
    webAppContext.setParentLoaderPriority(systemEnvironment.getParentLoaderPriority());
    return webAppContext;
}
 
Example #4
Source File: Jetty9ServerTest.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddWebAppContextHandler() throws Exception {
    jetty9Server.configure();
    jetty9Server.startHandlers();

    WebAppContext webAppContext = (WebAppContext) getLoadedHandlers().get(WebAppContext.class);

    assertThat(webAppContext, instanceOf(WebAppContext.class));
    List<String> configClasses = new ArrayList<>(Arrays.asList(webAppContext.getConfigurationClasses()));
    assertThat(configClasses.contains(WebInfConfiguration.class.getCanonicalName()), is(true));
    assertThat(configClasses.contains(WebXmlConfiguration.class.getCanonicalName()), is(true));
    assertThat(configClasses.contains(JettyWebXmlConfiguration.class.getCanonicalName()), is(true));
    assertThat(webAppContext.getContextPath(), is("context"));
    assertThat(webAppContext.getWar(), is("cruise.war"));
    assertThat(webAppContext.isParentLoaderPriority(), is(true));
    assertThat(webAppContext.getDefaultsDescriptor(), is("jar:file:cruise.war!/WEB-INF/webdefault.xml"));
}
 
Example #5
Source File: Main.java    From openscoring with GNU Affero General Public License v3.0 6 votes vote down vote up
private Server createServer(InetSocketAddress address) throws Exception {
	Server server = new Server(address);

	Configuration.ClassList classList = Configuration.ClassList.setServerDefault(server);
	classList.addBefore(JettyWebXmlConfiguration.class.getName(), AnnotationConfiguration.class.getName());

	ContextHandlerCollection handlerCollection = new ContextHandlerCollection();

	Handler applicationHandler = createApplicationHandler();
	handlerCollection.addHandler(applicationHandler);

	Handler adminConsoleHandler = createAdminConsoleHandler();
	if(adminConsoleHandler != null){
		handlerCollection.addHandler(adminConsoleHandler);
	}

	server.setHandler(handlerCollection);

	return server;
}
 
Example #6
Source File: Launcher.java    From electron-java-app with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    log.info("Server starting...");

    var server = new Server(8080);

    var context = new WebAppContext();
    context.setBaseResource(findWebRoot());
    context.addServlet(VaadinServlet.class, "/*");
    context.setContextPath("/");
    context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
            ".*vaadin/.*\\.jar|.*/classes/.*");
    context.setConfigurationDiscovered(true);
    context.getServletContext().setExtendedListenerTypes(true);
    context.addEventListener(new ServletContextListeners());
    server.setHandler(context);

    var sessions = new SessionHandler();
    context.setSessionHandler(sessions);

    var classList = Configuration.ClassList.setServerDefault(server);
    classList.addBefore(JettyWebXmlConfiguration.class.getName(), AnnotationConfiguration.class.getName());
    WebSocketServerContainerInitializer.initialize(context); // fixes IllegalStateException: Unable to configure jsr356 at that stage. ServerContainer is null

    try {
        server.start();
        server.join();
    } catch (Exception e) {
        log.error("Server error:\n", e);
    }

    log.info("Server stopped");
}
 
Example #7
Source File: EmbeddedJetty.java    From junit-servers with MIT License 4 votes vote down vote up
/**
 * Build web app context used to launch server.
 * May be override by subclasses.
 *
 * @throws Exception May be thrown by web app context initialization (will be wrapped later).
 */
private WebAppContext createdWebAppContext() throws Exception {
	final String path = configuration.getPath();
	final String webapp = configuration.getWebapp();
	final String classpath = configuration.getClasspath();
	final ClassLoader parentClassLoader = configuration.getParentClassLoader();
	final String overrideDescriptor = configuration.getOverrideDescriptor();
	final Resource baseResource = configuration.getBaseResource();
	final String containerJarPattern = configuration.getContainerJarPattern();
	final String webInfJarPattern = configuration.getWebInfJarPattern();

	final WebAppContext ctx = new WebAppContext();

	if (containerJarPattern != null) {
		log.debug("Setting jetty 'containerJarPattern' attribute: {}", containerJarPattern);
		ctx.setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, containerJarPattern);
	}
	else if (Java.isPostJdk9()) {
		// Fix to make TLD scanning works with Java >= 9
		log.debug("Setting default jetty 'containerJarPattern' for JRE >= 9: {}");
		ctx.setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, ".*\\.jar");
	}

	if (webInfJarPattern != null) {
		log.debug("Setting jetty 'WebInfJarPattern' attribute: {}", webInfJarPattern);
		ctx.setAttribute(WebInfConfiguration.WEBINF_JAR_PATTERN, webInfJarPattern);
	}

	final ClassLoader systemClassLoader = Thread.currentThread().getContextClassLoader();
	final ClassLoader classLoader;

	if (parentClassLoader != null) {
		log.debug("Overriding jetty parent classloader");
		classLoader = new CompositeClassLoader(parentClassLoader, systemClassLoader);
	}
	else {
		log.debug("Using current thread classloader as jetty parent classloader");
		classLoader = systemClassLoader;
	}

	log.debug("Set jetty classloader");
	ctx.setClassLoader(classLoader);

	log.debug("Set jetty context path to: {}", path);
	ctx.setContextPath(path);

	if (baseResource == null) {
		// use default base resource
		log.debug("Initializing default jetty base resource from: {}", webapp);
		ctx.setBaseResource(newResource(webapp));
	}
	else {
		log.debug("Initializing jetty base resource from: {}", baseResource);
		ctx.setBaseResource(baseResource);
	}

	if (overrideDescriptor != null) {
		log.debug("Set jetty descriptor: {}", overrideDescriptor);
		ctx.setOverrideDescriptor(overrideDescriptor);
	}

	log.debug("Initializing jetty configuration classes");
	ctx.setConfigurations(new Configuration[] {
		new WebInfConfiguration(),
		new WebXmlConfiguration(),
		new AnnotationConfiguration(),
		new JettyWebXmlConfiguration(),
		new MetaInfConfiguration(),
		new FragmentConfiguration()
	});

	if (isNotBlank(classpath)) {
		log.debug("Adding jetty container resource: {}", classpath);

		// Fix to scan Spring WebApplicationInitializer
		// This will add compiled classes to jetty classpath
		// See: http://stackoverflow.com/questions/13222071/spring-3-1-webapplicationinitializer-embedded-jetty-8-annotationconfiguration
		// And more precisely: http://stackoverflow.com/a/18449506/1215828
		final File classes = new File(classpath);
		final PathResource containerResources = new PathResource(classes.toURI());
		ctx.getMetaData().addContainerResource(containerResources);
	}

	ctx.setParentLoaderPriority(true);
	ctx.setWar(webapp);
	ctx.setServer(server);

	// Add server context
	server.setHandler(ctx);

	return ctx;
}
 
Example #8
Source File: ClusterTest.java    From maven-framework-project with MIT License 4 votes vote down vote up
@Test
public void testCluster() throws Exception {
	
	String projectBaseDirectory = System.getProperty("user.dir");
	
	//
	// Create master node
	//
	Server masterServer = new Server(8080);

	WebAppContext masterContext = new WebAppContext();
	masterContext.setDescriptor(projectBaseDirectory + "/target/vaporware/WEB-INF/web.xml");
	masterContext.setResourceBase(projectBaseDirectory + "/target/vaporware");
	masterContext.setContextPath("/");
	masterContext.setConfigurations(
			new Configuration[] { 
					new WebInfConfiguration(),
					new WebXmlConfiguration(),
					new MetaInfConfiguration(), 
					new FragmentConfiguration(),
					new EnvConfiguration(),
					new PlusConfiguration(),
					new AnnotationConfiguration(), 
					new JettyWebXmlConfiguration(),
					new TagLibConfiguration()
			}
	);
	masterContext.setParentLoaderPriority(true);

	masterServer.setHandler(masterContext);
	masterServer.start();
	//masterServer.join();

	//
	// Create slave node
	//
	Server slaveServer = new Server(8181);

	WebAppContext slaveContext = new WebAppContext();
	slaveContext.setDescriptor(projectBaseDirectory + "/target/vaporware/WEB-INF/web-slave.xml");
	slaveContext.setResourceBase(projectBaseDirectory + "/target/vaporware");
	slaveContext.setContextPath("/");
	slaveContext.setConfigurations(
			new Configuration[] { 
					new WebInfConfiguration(),
					new WebXmlConfiguration(),
					new MetaInfConfiguration(), 
					new FragmentConfiguration(),
					new EnvConfiguration(),
					new PlusConfiguration(),
					new AnnotationConfiguration(), 
					new JettyWebXmlConfiguration(),
					new TagLibConfiguration()
			}
	);
	slaveContext.setParentLoaderPriority(true);

	slaveServer.setHandler(slaveContext);
	slaveServer.start();
	//slaveServer.join();
	
	// Try to let the user terminate the Jetty server instances gracefully.  This won't work in an environment like Eclipse, if 
	// console input can't be received.  However, even in that that case you will be able to kill the Maven process without 
	// a Java process lingering around (as would be the case if you used "Sever.join()" to pause execution of this thread).
	System.out.println("PRESS <ENTER> TO HALT SERVERS (or kill the Maven process)...");
	Scanner scanner = new Scanner(System.in);
	String line = scanner.nextLine();
	System.out.println(line);
	scanner.close();
	masterServer.stop();
	slaveServer.stop();
	System.out.println("Servers halted");
	
}
 
Example #9
Source File: JettyServer.java    From nifi with Apache License 2.0 4 votes vote down vote up
public JettyServer(final NiFiProperties props, final Set<Bundle> bundles) {
    final QueuedThreadPool threadPool = new QueuedThreadPool(props.getWebThreads());
    threadPool.setName("NiFi Web Server");

    // create the server
    this.server = new Server(threadPool);
    this.props = props;

    // enable the annotation based configuration to ensure the jsp container is initialized properly
    final Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
    classlist.addBefore(JettyWebXmlConfiguration.class.getName(), AnnotationConfiguration.class.getName());

    // configure server
    configureConnectors(server);

    // load wars from the bundle
    final Handler warHandlers = loadInitialWars(bundles);

    final HandlerList allHandlers = new HandlerList();

    // Only restrict the host header if running in HTTPS mode
    if (props.isHTTPSConfigured()) {
        // Create a handler for the host header and add it to the server
        HostHeaderHandler hostHeaderHandler = new HostHeaderHandler(props);
        logger.info("Created HostHeaderHandler [" + hostHeaderHandler.toString() + "]");

        // Add this before the WAR handlers
        allHandlers.addHandler(hostHeaderHandler);
    } else {
        logger.info("Running in HTTP mode; host headers not restricted");
    }


    final ContextHandlerCollection contextHandlers = new ContextHandlerCollection();
    contextHandlers.addHandler(warHandlers);
    allHandlers.addHandler(contextHandlers);
    server.setHandler(allHandlers);

    deploymentManager = new DeploymentManager();
    deploymentManager.setContextAttribute(CONTAINER_INCLUDE_PATTERN_KEY, CONTAINER_INCLUDE_PATTERN_VALUE);
    deploymentManager.setContexts(contextHandlers);
    server.addBean(deploymentManager);
}