forked from eclipse-ee4j/jersey
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PAYARA-2491-Jersey-Bean-Validator-fails-to-inject-Weld-managed-beans
Also eclipse-ee4j#3801
- Loading branch information
Showing
5 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
...sfish/jersey/server/validation/internal/CompositeInjectingConstraintValidatorFactory.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,58 @@ | ||
/* | ||
* Copyright (c) 2018 Payara Foundation 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.jersey.server.validation.internal; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorFactory; | ||
import javax.ws.rs.container.ResourceContext; | ||
import javax.ws.rs.core.Context; | ||
|
||
/** | ||
* {@link ConstraintValidatorFactory} implementation that uses {@link InjectingConstraintValidatorFactory} | ||
* by default and fallbacks to {@link HibernateInjectingConstraintValidatorFactory} when the resource | ||
* cannot be found in resource context of Jersey. | ||
* | ||
* @author Mert Caliskan | ||
*/ | ||
public class CompositeInjectingConstraintValidatorFactory implements ConstraintValidatorFactory { | ||
|
||
@Context | ||
private ResourceContext resourceContext; | ||
|
||
private InjectingConstraintValidatorFactory jerseyVF; | ||
private HibernateInjectingConstraintValidatorFactory hibernateVF; | ||
|
||
@PostConstruct | ||
void postConstruct() { | ||
jerseyVF = resourceContext.getResource(InjectingConstraintValidatorFactory.class); | ||
hibernateVF = resourceContext.getResource(HibernateInjectingConstraintValidatorFactory.class); | ||
} | ||
|
||
@Override | ||
public <T extends ConstraintValidator<?, ?>> T getInstance(final Class<T> key) { | ||
T jerseyInstance = jerseyVF.getInstance(key); | ||
if (jerseyInstance == null) { | ||
return hibernateVF.getInstance(key); | ||
} | ||
return jerseyInstance; | ||
} | ||
|
||
@Override | ||
public void releaseInstance(final ConstraintValidator<?, ?> instance) { | ||
// NOOP | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...c/main/java/org/glassfish/jersey/server/validation/internal/DestructibleBeanInstance.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,63 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
// Portions Copyright [2018] [Payara Foundation and/or its affiliates] | ||
|
||
package org.glassfish.jersey.server.validation.internal; | ||
|
||
import javax.enterprise.context.spi.CreationalContext; | ||
import javax.enterprise.inject.spi.AnnotatedType; | ||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.enterprise.inject.spi.InjectionTarget; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
public class DestructibleBeanInstance<T> { | ||
private final T instance; | ||
private final InjectionTarget<T> injectionTarget; | ||
|
||
public DestructibleBeanInstance(BeanManager beanManager, Class<T> key) { | ||
this.injectionTarget = createInjectionTarget(beanManager, key); | ||
this.instance = createAndInjectBeans(beanManager, injectionTarget); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public DestructibleBeanInstance(BeanManager beanManager, T instance) { | ||
this.injectionTarget = createInjectionTarget(beanManager, (Class<T>) instance.getClass()); | ||
injectBeans(beanManager, beanManager.createCreationalContext(null), injectionTarget, instance); | ||
this.instance = instance; | ||
} | ||
|
||
public T getInstance() { | ||
return instance; | ||
} | ||
|
||
public void destroy() { | ||
injectionTarget.preDestroy(instance); | ||
injectionTarget.dispose(instance); | ||
} | ||
|
||
private InjectionTarget<T> createInjectionTarget(BeanManager beanManager, Class<T> type) { | ||
AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(type); | ||
return beanManager.createInjectionTarget(annotatedType); | ||
} | ||
|
||
private static <T> T createAndInjectBeans(BeanManager beanManager, InjectionTarget<T> injectionTarget) { | ||
CreationalContext<T> creationalContext = beanManager.createCreationalContext(null); | ||
|
||
T instance = injectionTarget.produce(creationalContext); | ||
injectBeans(beanManager, creationalContext, injectionTarget, instance); | ||
|
||
return instance; | ||
} | ||
|
||
private static <T> void injectBeans(BeanManager beanManager, CreationalContext<T> creationalContext, | ||
InjectionTarget<T> injectionTarget, T instance) { | ||
injectionTarget.inject(instance, creationalContext); | ||
injectionTarget.postConstruct(instance); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...sfish/jersey/server/validation/internal/HibernateInjectingConstraintValidatorFactory.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,52 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
// Portions Copyright [2018] [Payara Foundation and/or its affiliates] | ||
|
||
package org.glassfish.jersey.server.validation.internal; | ||
|
||
import org.glassfish.jersey.ext.cdi1x.internal.CdiUtil; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorFactory; | ||
import java.util.Collections; | ||
import java.util.IdentityHashMap; | ||
import java.util.Map; | ||
|
||
public class HibernateInjectingConstraintValidatorFactory implements ConstraintValidatorFactory { | ||
// TODO look for something with better performance (HF) | ||
private final Map<Object, DestructibleBeanInstance<?>> constraintValidatorMap = | ||
Collections.synchronizedMap(new IdentityHashMap<Object, DestructibleBeanInstance<?>>()); | ||
|
||
private BeanManager beanManager; | ||
|
||
@PostConstruct | ||
void postConstruct() { | ||
this.beanManager = CdiUtil.getBeanManager(); | ||
} | ||
|
||
@Override | ||
public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) { | ||
DestructibleBeanInstance<T> destructibleBeanInstance = new DestructibleBeanInstance<T>(beanManager, key); | ||
constraintValidatorMap.put(destructibleBeanInstance.getInstance(), destructibleBeanInstance); | ||
return destructibleBeanInstance.getInstance(); | ||
} | ||
|
||
@Override | ||
public void releaseInstance(ConstraintValidator<?, ?> instance) { | ||
DestructibleBeanInstance<?> destructibleBeanInstance = constraintValidatorMap.remove(instance); | ||
// HV-865 (Cleanup is multi threaded and instances can be removed by multiple threads. | ||
// Explicit null check is needed) | ||
if (destructibleBeanInstance != null) { | ||
destructibleBeanInstance.destroy(); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
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