Skip to content

Commit

Permalink
Jetty 10.0.x methodhandles lookup linkage (#4552)
Browse files Browse the repository at this point in the history
* use a lookup dedicated to the target class (webapp classloader) to avoid collision with server classloader

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>

* cleanup war test dependencies

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>

* remove empty lines

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>

* make it clear it is a bad websocket webapp only for testing purpose and add a good websocket webapp

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>

* no need anymore of this dependency

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>
  • Loading branch information
olamy authored Feb 9, 2020
1 parent e4c1984 commit abdb9f2
Show file tree
Hide file tree
Showing 16 changed files with 445 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,13 @@ private static MethodHandle mutatedInvoker(Class<?> targetClass, boolean throwOn
cTypes.add(arg.getType());
}
}
MethodType callingType = MethodType.methodType(method.getReturnType(), cTypes);

// Create low level MethodHandle
MethodHandles.Lookup lookup = MethodHandles.lookup();

try
{
MethodType callingType = MethodType.methodType(method.getReturnType(), cTypes);

// Create low level MethodHandle
MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(targetClass, MethodHandles.lookup());
// Low level invoker.
// We intentionally do not use lookup#unreflect() as that will incorrectly preserve
// the calling 'refc' type of where the method is declared, not the targetClass.
Expand Down
7 changes: 7 additions & 0 deletions tests/test-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@
<scope>test</scope>
<type>war</type>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>test-bad-websocket-webapp</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>war</type>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,11 @@ public void stop()
IO.close(reader);
}
}

public Queue<String> getLogs()
{
return logs;
}
}

public static class Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
Expand Down Expand Up @@ -401,4 +404,55 @@ public void testWebAppWithProxyAndJPMS() throws Exception
}
}
}

@ParameterizedTest
@ValueSource(strings = {
"",
"--jpms",
})
public void testSimpleWebAppWithWebsocket(String arg) throws Exception
{
String jettyVersion = System.getProperty("jettyVersion");
DistributionTester distribution = DistributionTester.Builder.newInstance()
.jettyVersion(jettyVersion)
.mavenLocalRepository(System.getProperty("mavenRepoPath"))
.build();
String[] args1 = {
"--create-startd",
"--approve-all-licenses",
"--add-to-start=resources,server,http,webapp,deploy,jsp,jmx,servlet,servlets,websocket"
};
try (DistributionTester.Run run1 = distribution.start(args1))
{
assertTrue(run1.awaitFor(5, TimeUnit.SECONDS));
assertEquals(0, run1.getExitValue());

File war = distribution.resolveArtifact("org.eclipse.jetty.tests:test-bad-websocket-webapp:war:" + jettyVersion);
distribution.installWarFile(war, "test1");
distribution.installWarFile(war, "test2");

int port = distribution.freePort();
String[] args2 = {
arg,
"jetty.http.port=" + port//,
//"jetty.server.dumpAfterStart=true"
};
try (DistributionTester.Run run2 = distribution.start(args2))
{
assertTrue(run2.awaitConsoleLogsFor("Started Server@", 10, TimeUnit.SECONDS));
assertFalse(run2.getLogs().stream().anyMatch(s -> s.contains("LinkageError")));

startHttpClient();
ContentResponse response = client.GET("http://localhost:" + port + "/test1/index.jsp");
assertEquals(HttpStatus.OK_200, response.getStatus());
assertThat(response.getContentAsString(), containsString("Hello"));
assertThat(response.getContentAsString(), not(containsString("<%")));

client.GET("http://localhost:" + port + "/test2/index.jsp");
assertEquals(HttpStatus.OK_200, response.getStatus());
assertThat(response.getContentAsString(), containsString("Hello"));
assertThat(response.getContentAsString(), not(containsString("<%")));
}
}
}
}
2 changes: 2 additions & 0 deletions tests/test-webapps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@
<module>test-cdi-common-webapp</module>
<module>test-weld-cdi-webapp</module>
<module>test-owb-cdi-webapp</module>
<module>test-websocket-webapp</module>
<module>test-bad-websocket-webapp</module>
</modules>
</project>
33 changes: 33 additions & 0 deletions tests/test-webapps/test-bad-websocket-webapp/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>test-webapps-parent</artifactId>
<version>10.0.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>test-bad-websocket-webapp</artifactId>
<packaging>war</packaging>

<name>Test :: Jetty Bad Websocket Simple Webapp</name>
<description>Contains wrong websocket which should fail at deployment</description>

<dependencies>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-javax-websocket-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-javax-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.tests.webapp.websocket.bad;

import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/badonclose/{arg}")
public class BadOnCloseServerEndpoint
{
private static String close = "";

@OnMessage
public String echo(String echo)
{
return close + echo;
}

@OnClose
public void onClose(Session session, @PathParam("arg") StringSequence sb)
{
close = sb.toString();
}

@OnError
public void onError(Session session, Throwable t)
throws IOException
{
String message = "Error happened:" + t.getMessage();
session.getBasicRemote().sendText(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.tests.webapp.websocket.bad;

import java.io.IOException;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/badonopen/{arg}")
public class BadOnOpenServerEndpoint
{
private static String open = "";

@OnMessage
public String echo(String echo)
{
return open + echo;
}

@OnOpen
public void onOpen(Session session, @PathParam("arg") StringSequence sb)
{
open = sb.toString();
}

@OnError
public void onError(Session session, Throwable t)
throws IOException
{
String message = "Error happened:" + t.getMessage();
session.getBasicRemote().sendText(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.tests.webapp.websocket.bad;

public class StringSequence
implements CharSequence
{
public String stringBuffer = "";

public StringSequence(String hold)
{
stringBuffer = hold;
}

public StringSequence()
{
}

@Override
public int length()
{
return stringBuffer.length();
}

@Override
public char charAt(int index)
{
return stringBuffer.charAt(index);
}

@Override
public CharSequence subSequence(int start, int end)
{
return stringBuffer.subSequence(start, end);
}

@Override
public String toString()
{
return stringBuffer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false"
version="3.1">

<display-name>Very Simple Bad Websocket Application</display-name>
</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
32 changes: 32 additions & 0 deletions tests/test-webapps/test-websocket-webapp/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>test-webapps-parent</artifactId>
<version>10.0.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>test-websocket-webapp</artifactId>
<packaging>war</packaging>

<name>Test :: Jetty Websocket Simple Webapp</name>

<dependencies>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-javax-websocket-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-javax-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Loading

0 comments on commit abdb9f2

Please sign in to comment.