Skip to content

Commit

Permalink
bugfix: Jax-JS Json-B injection retrives the correct BeanManager base…
Browse files Browse the repository at this point in the history
…d on the actual type of the injection point desired
  • Loading branch information
lprimak committed Mar 7, 2025
1 parent 3335617 commit 534ac92
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
5 changes: 5 additions & 0 deletions appserver/web/weld-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<include>**/faces-config.xml</include>
<include>**/com.sun.faces.spi.FacesConfigResourceProvider</include>
<include>**/jakarta.enterprise.inject.spi.Extension</include>
<include>**/org.glassfish.jersey.internal.spi.*</include>
</includes>
</resource>
</resources>
Expand Down Expand Up @@ -200,6 +201,10 @@
<groupId>fish.payara.server.internal.transaction</groupId>
<artifactId>jta</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>jakarta.json.bind</groupId>
<artifactId>jakarta.json.bind-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.xml.ws</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2024] [Payara Foundation and/or its affiliates]
// Portions Copyright [2024-2025] [Payara Foundation and/or its affiliates]

package org.glassfish.weld;

Expand All @@ -55,6 +55,7 @@
import jakarta.enterprise.inject.spi.CDIProvider;
import java.util.Map;
import java.util.Set;
import static org.glassfish.weld.JaxRSJsonContextResolver.currentType;

/**
* @author <a href="mailto:[email protected]">JJ Snyder</a>
Expand Down Expand Up @@ -103,6 +104,15 @@ protected BeanManagerImpl unsatisfiedBeanManager(String callerClassName) {

return super.unsatisfiedBeanManager(callerClassName);
}

@Override
protected String getCallingClassName() {
if (currentType.get() != null) {
return currentType.get().getName();
} else {
return super.getCallingClassName();
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2025] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/main/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.weld;

import jakarta.annotation.Priority;
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import jakarta.ws.rs.ConstrainedTo;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.FeatureContext;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.ext.ContextResolver;
import org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static jakarta.ws.rs.RuntimeType.SERVER;
import static org.glassfish.jersey.internal.spi.AutoDiscoverable.DEFAULT_PRIORITY;

/**
* Works in conjunction with {@link org.glassfish.weld.GlassFishWeldProvider} to provide the
* correct {@link Jsonb} instance based on the type of Jax-RS resource class being processed.
*
* This includes creating {@link Jsonb} instances that contains correct {@link jakarta.enterprise.inject.spi.BeanManager}
*/
@ConstrainedTo(SERVER)
@Priority(DEFAULT_PRIORITY)
@Produces(MediaType.APPLICATION_JSON)
public class JaxRSJsonContextResolver implements ContextResolver<Jsonb>, ForcedAutoDiscoverable {
private final Map<Class<?>, Jsonb> jsonbMap = new ConcurrentHashMap<>();
static final ThreadLocal<Class<?>> currentType = new ThreadLocal<>();

@Override
public void configure(FeatureContext context) {
context.register(JaxRSJsonContextResolver.class);
}

@Override
public Jsonb getContext(Class<?> type) {
return jsonbMap.computeIfAbsent(type, classLoader -> {
currentType.set(type);
try {
return JsonbBuilder.create();
} finally {
currentType.remove();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.glassfish.weld.JaxRSJsonContextResolver

0 comments on commit 534ac92

Please sign in to comment.