Skip to content

Commit 0ddd167

Browse files
committed
Introduce ee10-webapp-isolated-logging example
1 parent 63ef963 commit 0ddd167

10 files changed

Lines changed: 322 additions & 0 deletions

File tree

embedded/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [`ee10-uber-jar/`](ee10-uber-jar/) - Building a uber-jar with all dependencies integrated for Jetty Server
2424
* [`ee10-uber-war/`](ee10-uber-war/) - Building an uber-WAR that can run as a deployed WebApp/WAR or as a standalone uber-jar with all dependencies integrated for Jetty Server.
2525
* [`ee10-webapp-context/`](ee10-webapp-context/) - Setup an EE10 `WebAppContext` from a File System or ClassPath
26+
* [`ee10-webapp-isolated-logging/`](ee10-webapp-isolated-logging/) - Setup an EE10 `WebAppContext` that has a different logging library than the server.
2627
* [`ee10-websocket-jakarta-api/`](ee10-websocket-jakarta-api/) - Using `jakarta.websocket` API from `ServletContextHandler`
2728
* [`ee10-websocket-jetty-api/`](ee10-websocket-jetty-api/) - Using Jetty WebSocket API from `ServletContextHandler`
2829
* [`file-server/`](file-server/) - Serving static files with `ResourceHandler`
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.eclipse.jetty.examples.embedded</groupId>
6+
<artifactId>ee10-webapp-isolated-logging</artifactId>
7+
<version>12.0.x</version>
8+
</parent>
9+
<artifactId>ee10-server</artifactId>
10+
<version>12.0.x</version>
11+
<packaging>jar</packaging>
12+
<name>Jetty Examples :: Jetty 12.0.x :: Embedded :: EE10 WebApp Isolated Logging :: Server</name>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.eclipse.jetty</groupId>
17+
<artifactId>jetty-server</artifactId>
18+
<version>${jetty.version}</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>org.eclipse.jetty</groupId>
23+
<artifactId>jetty-slf4j-impl</artifactId>
24+
<version>${jetty.version}</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.eclipse.jetty.ee10</groupId>
29+
<artifactId>jetty-ee10-annotations</artifactId>
30+
<version>${jetty.version}</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.eclipse.jetty.ee10</groupId>
35+
<artifactId>jetty-ee10-webapp</artifactId>
36+
<version>${jetty.version}</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.eclipse.jetty.examples.embedded</groupId>
41+
<artifactId>ee10-webapp-using-logback</artifactId>
42+
<version>${project.version}</version>
43+
<type>war</type>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-dependency-plugin</artifactId>
52+
<version>3.8.1</version>
53+
<executions>
54+
<execution>
55+
<id>copy-wars</id>
56+
<goals>
57+
<goal>copy-dependencies</goal>
58+
</goals>
59+
<phase>generate-resources</phase>
60+
<configuration>
61+
<includeArtifactIds>ee10-webapp-using-logback</includeArtifactIds>
62+
<includeScope>runtime</includeScope>
63+
<includeTypes>war</includeTypes>
64+
<overWriteSnapshots>true</overWriteSnapshots>
65+
<overWriteReleases>true</overWriteReleases>
66+
<stripVersion>true</stripVersion>
67+
<outputDirectory>${project.build.directory}/webapps</outputDirectory>
68+
</configuration>
69+
</execution>
70+
</executions>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.eclipse.jetty.LEVEL=INFO
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.eclipse.jetty.examples.embedded</groupId>
6+
<artifactId>ee10-webapp-isolated-logging</artifactId>
7+
<version>12.0.x</version>
8+
</parent>
9+
<artifactId>ee10-webapp-using-logback</artifactId>
10+
<version>12.0.x</version>
11+
<packaging>war</packaging>
12+
<name>Jetty Examples :: Jetty 12.0.x :: Embedded :: EE10 WebApp Isolated Logging :: WebApp using Logback</name>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>ch.qos.logback</groupId>
17+
<artifactId>logback-classic</artifactId>
18+
<version>1.5.18</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>jakarta.servlet</groupId>
22+
<artifactId>jakarta.servlet-api</artifactId>
23+
<version>6.0.0</version>
24+
<scope>provided</scope>
25+
</dependency>
26+
</dependencies>
27+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.IOException;
17+
import java.io.PrintWriter;
18+
import java.util.Collections;
19+
import java.util.List;
20+
21+
import jakarta.servlet.ServletException;
22+
import jakarta.servlet.annotation.WebServlet;
23+
import jakarta.servlet.http.HttpServlet;
24+
import jakarta.servlet.http.HttpServletRequest;
25+
import jakarta.servlet.http.HttpServletResponse;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
@WebServlet(urlPatterns = "/echo/*")
30+
public class EchoServlet extends HttpServlet
31+
{
32+
private static final Logger LOG = LoggerFactory.getLogger(EchoServlet.class);
33+
34+
@Override
35+
public void init() throws ServletException
36+
{
37+
LOG.info("init()");
38+
super.init();
39+
}
40+
41+
@Override
42+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
43+
{
44+
LOG.info("doGet() - {}", req.getRequestURL());
45+
46+
resp.setStatus(200);
47+
PrintWriter out = resp.getWriter();
48+
out.printf("GET of %s%n", req.getRequestURL());
49+
for (String headerName : Collections.list(req.getHeaderNames()))
50+
{
51+
List<String> values = Collections.list(req.getHeaders(headerName));
52+
out.printf("Request Header [%s] = %s%n", headerName,
53+
String.join(", ", values));
54+
}
55+
}
56+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.IOException;
17+
18+
import jakarta.servlet.Filter;
19+
import jakarta.servlet.FilterChain;
20+
import jakarta.servlet.ServletException;
21+
import jakarta.servlet.ServletRequest;
22+
import jakarta.servlet.ServletResponse;
23+
import jakarta.servlet.annotation.WebFilter;
24+
import jakarta.servlet.http.HttpServletRequest;
25+
import jakarta.servlet.http.HttpServletResponse;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
@WebFilter(urlPatterns = "/*")
30+
public class LoggingFilter implements Filter
31+
{
32+
private static final Logger LOG = LoggerFactory.getLogger(LoggingFilter.class);
33+
34+
@Override
35+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
36+
{
37+
HttpServletRequest httpReq = (HttpServletRequest)request;
38+
HttpServletResponse httpResp = (HttpServletResponse)response;
39+
40+
LOG.info("Before doFilter()");
41+
chain.doFilter(httpReq, httpResp);
42+
LOG.info("After doFilter()");
43+
}
44+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>[webapp] %d %-5level [%thread] %logger{0}: %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<root level="debug">
9+
<appender-ref ref="STDOUT" />
10+
</root>
11+
</configuration>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.eclipse.jetty.examples.embedded</groupId>
6+
<artifactId>jetty-embedded-examples</artifactId>
7+
<version>12.0.x</version>
8+
</parent>
9+
<artifactId>ee10-webapp-isolated-logging</artifactId>
10+
<version>12.0.x</version>
11+
<packaging>pom</packaging>
12+
<name>Jetty Examples :: Jetty 12.0.x :: Embedded :: EE10 WebApp Isolated Logging</name>
13+
14+
<modules>
15+
<module>ee10-server</module>
16+
<module>ee10-webapp-using-logback</module>
17+
</modules>
18+
</project>

embedded/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>ee10-uber-jar</module>
3737
<module>ee10-uber-war</module>
3838
<module>ee10-webapp-context</module>
39+
<module>ee10-webapp-isolated-logging</module>
3940
<module>ee10-websocket-jakarta-api</module>
4041
<module>ee10-websocket-jetty-api</module>
4142
<module>file-server</module>

0 commit comments

Comments
 (0)