-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebSocket Next: method arguments injection
- resolves #39224
- Loading branch information
Showing
12 changed files
with
533 additions
and
53 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
...rver/deployment/src/main/java/io/quarkus/websockets/next/deployment/ArgumentProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package io.quarkus.websockets.next.deployment; | ||
|
||
import java.util.Set; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.MethodParameterInfo; | ||
import org.jboss.jandex.Type; | ||
|
||
import io.quarkus.gizmo.BytecodeCreator; | ||
import io.quarkus.gizmo.ResultHandle; | ||
import io.quarkus.websockets.next.OnClose; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.WebSocketConnection; | ||
import io.quarkus.websockets.next.deployment.WebSocketEndpointBuildItem.Callback; | ||
|
||
/** | ||
* Provides arguments for method parameters of a callback method declared on a WebSocket endpoint. | ||
*/ | ||
interface ArgumentProvider { | ||
|
||
/** | ||
* | ||
* @param parameterContext | ||
* @return {@code true} if this provider matches the given parameter context, {@code false} otherwise | ||
*/ | ||
boolean matches(ParameterContext parameterContext); | ||
|
||
/** | ||
* | ||
* @param parameterContext | ||
* @param callbackContext | ||
* @return the result handle to be passed as an argument to a callback method | ||
*/ | ||
ResultHandle get(ParameterContext parameterContext, CallbackContext callbackContext); | ||
|
||
static final int DEFAULT_PRIORITY = 0; | ||
|
||
/** | ||
* | ||
* @return the priority | ||
*/ | ||
default int priotity() { | ||
return DEFAULT_PRIORITY; | ||
} | ||
|
||
interface ParameterContext { | ||
|
||
/** | ||
* | ||
* @return the callback | ||
*/ | ||
Callback callback(); | ||
|
||
/** | ||
* | ||
* @return the Java method parameter | ||
*/ | ||
MethodParameterInfo parameter(); | ||
|
||
/** | ||
* | ||
* @return the set of annotations, potentially transformed | ||
*/ | ||
Set<AnnotationInstance> paramAnnotations(); | ||
|
||
} | ||
|
||
interface CallbackContext { | ||
|
||
/** | ||
* | ||
* @return the bytecode | ||
*/ | ||
BytecodeCreator bytecode(); | ||
|
||
/** | ||
* Obtains the message directly in the bytecode. | ||
* | ||
* @return the message object or {@code null} for {@link OnOpen} and {@link OnClose} callbacks | ||
*/ | ||
ResultHandle message(); | ||
|
||
/** | ||
* Attempts to decode the message directly in the bytecode. | ||
* | ||
* @param parameterType | ||
* @return the decoded message object or {@code null} for {@link OnOpen} and {@link OnClose} callbacks | ||
*/ | ||
ResultHandle decodeMessage(Type parameterType); | ||
|
||
/** | ||
* Obtains the current connection directly in the bytecode. | ||
* | ||
* @return the current {@link WebSocketConnection}, never {@code null} | ||
*/ | ||
ResultHandle connection(); | ||
|
||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...oyment/src/main/java/io/quarkus/websockets/next/deployment/ArgumentProviderBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.websockets.next.deployment; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
final class ArgumentProviderBuildItem extends MultiBuildItem { | ||
|
||
private final ArgumentProvider provider; | ||
|
||
ArgumentProviderBuildItem(ArgumentProvider provider) { | ||
this.provider = provider; | ||
} | ||
|
||
ArgumentProvider getProvider() { | ||
return provider; | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...yment/src/main/java/io/quarkus/websockets/next/deployment/ArgumentProvidersBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.quarkus.websockets.next.deployment; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import io.quarkus.builder.item.SimpleBuildItem; | ||
import io.quarkus.websockets.next.deployment.ArgumentProvider.ParameterContext; | ||
|
||
final class ArgumentProvidersBuildItem extends SimpleBuildItem { | ||
|
||
final List<ArgumentProvider> sortedProviders; | ||
|
||
ArgumentProvidersBuildItem(List<ArgumentProvider> injectors) { | ||
this.sortedProviders = injectors; | ||
} | ||
|
||
/** | ||
* | ||
* @param context | ||
* @return the first matching provider or {@code null} | ||
*/ | ||
ArgumentProvider findMatching(ParameterContext context) { | ||
for (ArgumentProvider provider : sortedProviders) { | ||
if (provider.matches(context)) { | ||
return provider; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* | ||
* @param context | ||
* @return all matching providers, never {@code null} | ||
*/ | ||
List<ArgumentProvider> findAllMatching(ParameterContext context) { | ||
List<ArgumentProvider> matching = new ArrayList<>(); | ||
for (ArgumentProvider provider : sortedProviders) { | ||
if (provider.matches(context)) { | ||
matching.add(provider); | ||
} | ||
} | ||
return matching; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...yment/src/main/java/io/quarkus/websockets/next/deployment/ConnectionArgumentProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.websockets.next.deployment; | ||
|
||
import io.quarkus.gizmo.ResultHandle; | ||
|
||
class ConnectionArgumentProvider implements ArgumentProvider { | ||
|
||
@Override | ||
public boolean matches(ParameterContext parameterContext) { | ||
return parameterContext.parameter().type().name().equals(WebSocketDotNames.WEB_SOCKET_CONNECTION); | ||
} | ||
|
||
@Override | ||
public ResultHandle get(ParameterContext parameterContext, CallbackContext callbackContext) { | ||
return callbackContext.connection(); | ||
} | ||
|
||
@Override | ||
public int priotity() { | ||
return DEFAULT_PRIORITY + 1; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...ployment/src/main/java/io/quarkus/websockets/next/deployment/MessageArgumentProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.websockets.next.deployment; | ||
|
||
import io.quarkus.gizmo.ResultHandle; | ||
|
||
class MessageArgumentProvider implements ArgumentProvider { | ||
|
||
@Override | ||
public boolean matches(ParameterContext parameterContext) { | ||
return parameterContext.callback().acceptsMessage() && parameterContext.paramAnnotations().isEmpty(); | ||
} | ||
|
||
@Override | ||
public ResultHandle get(ParameterContext parameterContext, CallbackContext callbackContext) { | ||
return callbackContext.decodeMessage(parameterContext.parameter().type()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.