Debian Codegraph @

tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/ClassLoaderServlet.java

raw
1 //
2 // ========================================================================
3 // Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package com.acme.test;
20
21 import java.io.PrintWriter;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.net.URL;
25 import java.security.CodeSource;
26 import java.security.ProtectionDomain;
27 import javax.servlet.ServletException;
28 import javax.servlet.annotation.WebServlet;
29 import javax.servlet.http.HttpServlet;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.eclipse.jetty.util.IO;
34
35 @WebServlet(urlPatterns = "/classloader")
36 public class ClassLoaderServlet extends HttpServlet
37 {
38 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
39 throws ServletException
40 {
41 try
42 {
43 PrintWriter writer = resp.getWriter();
44 writer.println("<html>");
45 writer.println("<HEAD><link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheet.css\"/></HEAD>");
46 writer.println("<body>");
47 writer.println("<h1>ClassLoader Isolation Test</h1>");
48
49 Class<?> webappIO = IO.class;
50 URI webappURI = getLocationOfClass(webappIO);
51 String webappVersion = webappIO.getPackage().getImplementationVersion();
52 Class<?> serverIO = req.getServletContext().getClass().getClassLoader().loadClass("org.eclipse.jetty.util.IO");
53 URI serverURI = getLocationOfClass(serverIO);
54 String serverVersion = serverIO.getPackage().getImplementationVersion();
55
56 writer.printf("<p>Webapp loaded <code>org.eclipse.jetty.util.IO</code>(%s) from %s%n", webappVersion, webappURI);
57 writer.printf("<br/>Server loaded <code>org.eclipse.jetty.util.IO</code>(%s) from %s%n", serverVersion, serverURI);
58 if (webappVersion.equals(serverVersion))
59 writer.println("<br/><b>Version Result: <span class=\"fail\">FAIL</span></b>");
60 else
61 writer.println("<br/><b>Version Result: <span class=\"pass\">PASS</span></b>");
62 if (webappURI.equals(serverURI))
63 writer.println("<br/><b>URI Result: <span class=\"fail\">FAIL</span></b></p>");
64 else
65 writer.println("<br/><b>URI Result: <span class=\"pass\">PASS</span></b></p>");
66
67 writer.println("</body>");
68 writer.println("</html>");
69 writer.flush();
70 writer.close();
71 }
72 catch (Exception e)
73 {
74 throw new ServletException(e);
75 }
76 }
77
78 public static URI getLocationOfClass(Class<?> clazz)
79 {
80 try
81 {
82 ProtectionDomain domain = clazz.getProtectionDomain();
83 if (domain != null)
84 {
85 CodeSource source = domain.getCodeSource();
86 if (source != null)
87 {
88 URL location = source.getLocation();
89
90 if (location != null)
91 return location.toURI();
92 }
93 }
94
95 String resourceName = clazz.getName().replace('.', '/') + ".class";
96 ClassLoader loader = clazz.getClassLoader();
97 URL url = (loader == null ? ClassLoader.getSystemClassLoader() : loader).getResource(resourceName);
98 if (url != null)
99 {
100 return getJarSource(url.toURI());
101 }
102 }
103 catch (URISyntaxException e)
104 {
105 throw new RuntimeException(e);
106 }
107 return null;
108 }
109
110 public static URI getJarSource(URI uri)
111 {
112 try
113 {
114 if (!"jar".equals(uri.getScheme()))
115 return uri;
116 // Get SSP (retaining encoded form)
117 String s = uri.getRawSchemeSpecificPart();
118 int bangSlash = s.indexOf("!/");
119 if (bangSlash >= 0)
120 s = s.substring(0, bangSlash);
121 return new URI(s);
122 }
123 catch (URISyntaxException e)
124 {
125 throw new IllegalArgumentException(e);
126 }
127 }
128 }