Debian Codegraph @

jetty-ee9/jetty-ee9-demos/jetty-ee9-demo-spec/jetty-ee9-demo-spec-webapp/src/main/java/org/example/test/ClassLoaderServlet.java

raw
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 org.example.test;
15
16 import java.io.PrintWriter;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.net.URL;
20 import java.security.CodeSource;
21 import java.security.ProtectionDomain;
22
23 import jakarta.servlet.ServletException;
24 import jakarta.servlet.annotation.WebServlet;
25 import jakarta.servlet.http.HttpServlet;
26 import jakarta.servlet.http.HttpServletRequest;
27 import jakarta.servlet.http.HttpServletResponse;
28
29 @WebServlet(urlPatterns = "/classloader")
30 public class ClassLoaderServlet extends HttpServlet
31 {
32 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
33 throws ServletException
34 {
35 try
36 {
37 PrintWriter writer = resp.getWriter();
38 writer.println("<html>");
39 writer.println("<head><link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheet.css\"/></head>");
40 writer.println("<body>");
41 writer.println("<h1>ClassLoader Isolation Test</h1>");
42
43 // TODO uncomment the following once 9.4.19 is released with a fix for #3726
44 /*
45 Class<?> webappIO = IO.class;
46 URI webappURI = getLocationOfClass(webappIO);
47 String webappVersion = webappIO.getPackage().getImplementationVersion();
48 Class<?> serverIO = req.getServletContext().getClass().getClassLoader().loadClass("org.eclipse.jetty.util.IO");
49 URI serverURI = getLocationOfClass(serverIO);
50 String serverVersion = serverIO.getPackage().getImplementationVersion();
51
52 writer.printf("<p>Webapp loaded <code>org.eclipse.jetty.util.IO</code>(%s) from %s%n",webappVersion,webappURI);
53 writer.printf("<br/>Server loaded <code>org.eclipse.jetty.util.IO</code>(%s) from %s%n",serverVersion, serverURI);
54 if (webappVersion.equals(serverVersion))
55 writer.println("<br/><b>Version Result: <span class=\"fail\">FAIL</span></b>");
56 else
57 writer.println("<br/><b>Version Result: <span class=\"pass\">PASS</span></b>");
58 if (webappURI.equals(serverURI))
59 writer.println("<br/><b>URI Result: <span class=\"fail\">FAIL</span></b></p>");
60 else
61 writer.println("<br/><b>URI Result: <span class=\"pass\">PASS</span></b></p>");
62 */
63
64 writer.println("</body>");
65 writer.println("</html>");
66 writer.flush();
67 writer.close();
68 }
69 catch (Exception e)
70 {
71 throw new ServletException(e);
72 }
73 }
74
75 public static URI getLocationOfClass(Class<?> clazz)
76 {
77 try
78 {
79 ProtectionDomain domain = clazz.getProtectionDomain();
80 if (domain != null)
81 {
82 CodeSource source = domain.getCodeSource();
83 if (source != null)
84 {
85 URL location = source.getLocation();
86
87 if (location != null)
88 return location.toURI();
89 }
90 }
91
92 String resourceName = clazz.getName().replace('.', '/') + ".class";
93 ClassLoader loader = clazz.getClassLoader();
94 URL url = (loader == null ? ClassLoader.getSystemClassLoader() : loader).getResource(resourceName);
95 if (url != null)
96 {
97 return getJarSource(url.toURI());
98 }
99 }
100 catch (URISyntaxException e)
101 {
102 throw new RuntimeException(e);
103 }
104 return null;
105 }
106
107 public static URI getJarSource(URI uri)
108 {
109 try
110 {
111 if (!"jar".equals(uri.getScheme()))
112 return uri;
113 // Get SSP (retaining encoded form)
114 String s = uri.getRawSchemeSpecificPart();
115 int bangSlash = s.indexOf("!/");
116 if (bangSlash >= 0)
117 s = s.substring(0, bangSlash);
118 return new URI(s);
119 }
120 catch (URISyntaxException e)
121 {
122 throw new IllegalArgumentException(e);
123 }
124 }
125 }