Skip to content

Commit

Permalink
Merge pull request #905 from jansupol/2.2.prep3
Browse files Browse the repository at this point in the history
Merge 2.1 into 2.2
  • Loading branch information
jansupol authored Jul 31, 2024
2 parents eee19ca + 7cc3e84 commit 7c587a9
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class TyrusHttpUpgradeHandler implements HttpUpgradeHandler, ReadListener
* <p>
* The default value is 4194315 bytes, which correspond to 4M plus few bytes to frame headers.
* </p>
*
*/
public static final String FRAME_BUFFER_SIZE = "org.glassfish.tyrus.servlet.incoming-buffer-size";

Expand Down
56 changes: 44 additions & 12 deletions core/src/main/java/org/glassfish/tyrus/core/TyrusSession.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -36,6 +36,7 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -77,7 +78,7 @@ public class TyrusSession implements DistributedSession {
private final Map<String, String> pathParameters;
private final Principal userPrincipal;
private final Map<String, List<String>> requestParameterMap;
private final Object idleTimeoutLock = new Object();
private final ReentrantLock idleTimeoutLock = new ReentrantLock();
private final String id;
private final String connectionId;
private final Map<String, Object> userProperties;
Expand All @@ -94,6 +95,7 @@ public class TyrusSession implements DistributedSession {

private final Map<RemoteSession.DistributedMapKey, Object> distributedPropertyMap;
private final Map<String, Object> distributedUserProperties;
private final ReentrantLock handlerManagerLock = new ReentrantLock();

private volatile long maxIdleTimeout = 0;
private volatile ScheduledFuture<?> idleTimeoutFuture = null;
Expand Down Expand Up @@ -333,8 +335,11 @@ public WebSocketContainer getContainer() {
@Override
public void addMessageHandler(MessageHandler handler) {
checkConnectionState(State.CLOSED);
synchronized (handlerManager) {
handlerManagerLock.lock();
try {
handlerManager.addMessageHandler(handler);
} finally {
handlerManagerLock.unlock();
}
}

Expand All @@ -357,8 +362,11 @@ public void addMessageHandler(MessageHandler handler) {
@Override
public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler) {
checkConnectionState(State.CLOSED);
synchronized (handlerManager) {
handlerManagerLock.lock();
try {
handlerManager.addMessageHandler(clazz, handler);
} finally {
handlerManagerLock.unlock();
}
}

Expand All @@ -381,23 +389,32 @@ public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handle
@Override
public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Partial<T> handler) {
checkConnectionState(State.CLOSED);
synchronized (handlerManager) {
handlerManagerLock.lock();
try {
handlerManager.addMessageHandler(clazz, handler);
} finally {
handlerManagerLock.unlock();
}
}

@Override
public Set<MessageHandler> getMessageHandlers() {
synchronized (handlerManager) {
handlerManagerLock.lock();
try {
return handlerManager.getMessageHandlers();
} finally {
handlerManagerLock.unlock();
}
}

@Override
public void removeMessageHandler(MessageHandler handler) {
checkConnectionState(State.CLOSED);
synchronized (handlerManager) {
handlerManagerLock.lock();
try {
handlerManager.removeMessageHandler(handler);
} finally {
handlerManagerLock.unlock();
}
}

Expand Down Expand Up @@ -513,27 +530,36 @@ public void setHeartbeatInterval(long heartbeatInterval) {
}

void restartIdleTimeoutExecutor() {
synchronized (idleTimeoutLock) {
idleTimeoutLock.lock();
try {
cancelIdleTimeoutExecutor();
idleTimeoutFuture =
service.schedule(new IdleTimeoutCommand(), this.getMaxIdleTimeout(), TimeUnit.MILLISECONDS);
} finally {
idleTimeoutLock.unlock();
}
}

private void cancelIdleTimeoutExecutor() {
if (this.maxIdleTimeout < 1) {
synchronized (idleTimeoutLock) {
idleTimeoutLock.lock();
try {
if (idleTimeoutFuture != null) {
idleTimeoutFuture.cancel(true);
}
return;
} finally {
idleTimeoutLock.unlock();
}
}

synchronized (idleTimeoutLock) {
idleTimeoutLock.lock();
try {
if (idleTimeoutFuture != null) {
idleTimeoutFuture.cancel(false);
}
} finally {
idleTimeoutLock.unlock();
}
}

Expand Down Expand Up @@ -567,8 +593,11 @@ void notifyMessageHandlers(Object message, List<CoderWrapper<Decoder>> available
}

List<Map.Entry<Class<?>, MessageHandler>> orderedMessageHandlers;
synchronized (handlerManager) {
handlerManagerLock.lock();
try {
orderedMessageHandlers = handlerManager.getOrderedWholeMessageHandlers();
} finally {
handlerManagerLock.unlock();
}

for (CoderWrapper<Decoder> decoder : availableDecoders) {
Expand Down Expand Up @@ -602,8 +631,11 @@ void notifyMessageHandlers(Object message, List<CoderWrapper<Decoder>> available

<T> MessageHandler.Whole<T> getMessageHandler(Class<T> c) {
List<Map.Entry<Class<?>, MessageHandler>> orderedMessageHandlers;
synchronized (handlerManager) {
handlerManagerLock.lock();
try {
orderedMessageHandlers = handlerManager.getOrderedWholeMessageHandlers();
} finally {
handlerManagerLock.unlock();
}

for (Map.Entry<Class<?>, MessageHandler> entry : orderedMessageHandlers) {
Expand Down
40 changes: 36 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@

<contributors>
<contributor>
<name>Martin Matula</name>
<url>http://blog.alutam.com</url>
<name>Pavel Bucek</name>
</contributor>
<contributor>
<name>Pavel Bucek</name>
<name>Martin Matula</name>
<url>http://blog.alutam.com</url>
</contributor>
<contributor>
<name>Stepan Kopriva</name>
Expand Down Expand Up @@ -117,12 +117,13 @@
<servlet.api.version>6.1.0</servlet.api.version>
<spring.boot.version>2.6.7</spring.boot.version>


<java.version>11</java.version>

<maven.compiler.plugin.version>3.13.0</maven.compiler.plugin.version>
<maven-javadoc-plugin.version>3.8.0</maven-javadoc-plugin.version>
<maven.surefire.plugin.version>3.3.1</maven.surefire.plugin.version>
<maven.war.plugin.version>3.4.0</maven.war.plugin.version>
<cyclonedx.mvn.plugin.version>2.8.0</cyclonedx.mvn.plugin.version>

<api_package>jakarta.websocket</api_package>
<impl_namespace>org.glassfish</impl_namespace>
Expand Down Expand Up @@ -597,6 +598,37 @@
</plugins>
</build>
</profile>
<profile>
<id>sbom</id>
<activation>
<property>
<name>!skipSBOM</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.cyclonedx</groupId>
<artifactId>cyclonedx-maven-plugin</artifactId>
<version>${cyclonedx.mvn.plugin.version}</version>
<inherited>true</inherited>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>makeAggregateBom</goal>
</goals>
<configuration>
<!-- <schemaVersion>1.4</schemaVersion>-->
<projectType>framework</projectType>
<excludeTestProject>true</excludeTestProject>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<reporting>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://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: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.tyrus.test.artifacts;

import org.apache.maven.model.Dependency;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;

public class MultiReleaseTest {
private static final String s = "";
private static final File localRepository = MavenUtil.getLocalMavenRepository();
private static final Properties properties = MavenUtil.getMavenProperties();

@Test
public void testIsJdkMultiRelease() throws IOException, XmlPullParserException {
TestResult result = testJdkVersions("11", jdk11multiRelease(properties));
//Assertions.assertTrue(result.result(), "Some error occurred, see previous messages");
Assert.assertTrue("Some error occurred, see previous messages", result.result());
}

private static TestResult testJdkVersions(String version, DependencyPair... dependencies)
throws XmlPullParserException, IOException {
final TestResult result = new TestResult();
if (dependencies == null || dependencies.length == 0) {
System.out.append("No dependencies found for jdk ").println(version);
return result;
}

Stream<Dependency> deps = MavenUtil.streamTyrusJars();
List<File> files = MavenUtil.keepTyrusJars(deps, dependencies)
.map(dependency -> MavenUtil.getArtifactJar(localRepository, dependency, properties))
.collect(Collectors.toList());

//Assertions.assertEquals(dependencies.length, files.size(), "Some jdk " + version + " dependencies not found");
if (dependencies.length != files.size()) {
System.out.println("Expected:");
for (DependencyPair pair : dependencies) {
System.out.println(pair);
}
System.out.println("Resolved:");
for (File file : files) {
System.out.println(file.getName());
}
Assert.assertEquals("Some jdk " + version + " dependencies not found", dependencies.length, files.size());
}

for (File jar : files) {
JarFile jarFile = new JarFile(jar);
if (!jarFile.isMultiRelease()) {
result.exception().append("Not a multirelease jar ").append(jar.getName()).println("!");
}
ZipEntry versions = jarFile.getEntry("META-INF/versions/" + version);
System.out.append("Accessing META-INF/versions/").append(version).append(" of ").println(jar.getName());
if (versions == null) {
result.exception().append("No classes for JDK ").append(version).append(" for ").println(jar.getName());
}
result.ok().append("Classes for JDK ").append(version).append(" found for ").println(jar.getName());

Optional<JarEntry> file = jarFile.stream()
.filter(entry -> !entry.isDirectory())
.filter(entry -> !entry.getName().contains("versions"))
.filter(entry -> entry.getName().endsWith(".class"))
.findAny();
JarEntry jarEntry = file.get();
result.append(ClassVersionChecker.checkClassVersion(jarFile, jarEntry, properties));
}

// Verify that number of multirelease jars matches the expected dependencies
StringBuilder multi = new StringBuilder();
int multiCnt = 0;
List<File> allFiles = MavenUtil.streamTyrusJars()
.map(dependency -> MavenUtil.getArtifactJar(localRepository, dependency, properties))
.collect(Collectors.toList());
for (File jar : files) {
JarFile jarFile = new JarFile(jar);
if (jarFile.isMultiRelease()) {
multiCnt++;
multi.append("Multirelease jar ").append(jar.getName()).append('\n');
}
}
if (files.size() == multiCnt) {
result.ok().println("There is expected number of multirelease jars");
} else {
result.exception().println("There is unexpected number of multirelease jars:");
result.exception().append(multi).println("");
}

return result;
}

private static DependencyPair[] jdk11multiRelease(Properties properties) throws XmlPullParserException, IOException {
String tyrusVersion = MavenUtil.getTyrusVersion(properties);
if (tyrusVersion.startsWith("2.0")) {
return MavenUtil.streamTyrusJars()
.map(d -> new DependencyPair(d.getGroupId(), d.getArtifactId()))
.collect(Collectors.toList())
.toArray(new DependencyPair[0]);
}
return new DependencyPair[]{};
}

}

0 comments on commit 7c587a9

Please sign in to comment.