|
| 1 | +// |
| 2 | +// ======================================================================== |
| 3 | +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 | +// |
| 5 | +// This program and the accompanying materials are made available under the |
| 6 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 | +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 | +// ======================================================================== |
| 12 | +// |
| 13 | + |
| 14 | +package examples; |
| 15 | + |
| 16 | +import java.io.FileNotFoundException; |
| 17 | +import java.io.IOException; |
| 18 | +import java.net.URI; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | + |
| 22 | +import org.eclipse.jetty.ee10.webapp.WebAppContext; |
| 23 | +import org.eclipse.jetty.logging.JettyLoggingServiceProvider; |
| 24 | +import org.eclipse.jetty.server.Server; |
| 25 | +import org.eclipse.jetty.server.ServerConnector; |
| 26 | +import org.eclipse.jetty.server.handler.ContextHandlerCollection; |
| 27 | +import org.eclipse.jetty.util.TypeUtil; |
| 28 | +import org.eclipse.jetty.util.resource.Resource; |
| 29 | +import org.eclipse.jetty.util.resource.ResourceFactory; |
| 30 | + |
| 31 | +public class ServerMain |
| 32 | +{ |
| 33 | + public static void main(String[] args) throws Exception |
| 34 | + { |
| 35 | + int port = 8080; |
| 36 | + |
| 37 | + if (args.length >= 1) |
| 38 | + { |
| 39 | + port = Integer.parseInt(args[0]); |
| 40 | + } |
| 41 | + |
| 42 | + Server server = createServer(port); |
| 43 | + server.start(); |
| 44 | + server.join(); |
| 45 | + } |
| 46 | + |
| 47 | + public static Server createServer(int port) throws IOException |
| 48 | + { |
| 49 | + Path webapp = Path.of("target/webapps/ee10-webapp-using-logback.war").toAbsolutePath(); |
| 50 | + |
| 51 | + if (!Files.isRegularFile(webapp)) |
| 52 | + throw new FileNotFoundException("Unable to find required webapp: " + webapp); |
| 53 | + |
| 54 | + Server server = new Server(); |
| 55 | + ServerConnector connector = new ServerConnector(server); |
| 56 | + connector.setPort(port); |
| 57 | + server.addConnector(connector); |
| 58 | + |
| 59 | + ContextHandlerCollection contexts = new ContextHandlerCollection(); |
| 60 | + server.setHandler(contexts); |
| 61 | + |
| 62 | + WebAppContext context = new WebAppContext(); |
| 63 | + ResourceFactory resourceFactory = context.getResourceFactory(); |
| 64 | + Resource warResource = resourceFactory.newResource(webapp); |
| 65 | + context.setWarResource(warResource); |
| 66 | + // Allow discovery of Jakarta Servlet API (needed for annotation scanning) |
| 67 | + context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/jakarta.servlet-api-[^/]*\\.jar$"); |
| 68 | + |
| 69 | + // Important Note: |
| 70 | + // DO NOT use context.setParentLoaderPriority(true) - this will break WebApp ClassLoader Isolation |
| 71 | + |
| 72 | + /* Optional, not required to function properly. |
| 73 | + * Configure the HiddenClassMatcher of the WebApp to not see the Jetty server logging impl. |
| 74 | + * This is done to fix a warning seen. |
| 75 | + * SLF4J(E): A service provider failed to instantiate: |
| 76 | + * org.slf4j.spi.SLF4JServiceProvider: Provider org.eclipse.jetty.logging.JettyLoggingServiceProvider not found |
| 77 | + * Normal class loader isolation will prevent server classes from being seen. |
| 78 | + * However, resources are seen. What's happening is that the slf4j impl on the webapp sees |
| 79 | + * the service files, but is unable to load the class. |
| 80 | + * The following two lines will ban not just the classes of jetty-slf4j-impl, but the entire |
| 81 | + * jar from being seen on the classloader. |
| 82 | + */ |
| 83 | + URI serverLoggingLocation = TypeUtil.getLocationOfClass(JettyLoggingServiceProvider.class); |
| 84 | + context.getHiddenClassMatcher().add(serverLoggingLocation.toASCIIString()); |
| 85 | + |
| 86 | + contexts.addHandler(context); |
| 87 | + return server; |
| 88 | + } |
| 89 | +} |
0 commit comments