Skip to content

Commit

Permalink
PAYARA-2775 MP Metrics support for @stateless JAX-RS resource (#2830)
Browse files Browse the repository at this point in the history
* @Intercepted Bean<?> support in EJB interceptor

* create EnhancedAnnotatedType using BeanManager API

* Add annotation to constructor & param using ASM Class writer

* unused import removed
  • Loading branch information
jGauravGupta authored and smillidge committed Jun 14, 2018
1 parent 3a7caed commit 8821add
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
//Portions Copyright [2016] [Payara Foundation]
//Portions Copyright [2016-2018] [Payara Foundation and/or its affiliates]
package com.sun.ejb.codegen;

import org.glassfish.hk2.external.org.objectweb.asm.*;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.ReflectPermission;
import java.security.AccessController;
Expand Down Expand Up @@ -97,7 +98,7 @@ public Class generateSerializableSubclass()
Type.getType(Serializable.class).getInternalName()
};

tv.visit(V1_1, ACC_PUBLIC,
tv.visit(V1_8, ACC_PUBLIC,
subclassInternalName, null,
Type.getType(baseClass).getInternalName(), interfaces);

Expand All @@ -109,12 +110,12 @@ public Class generateSerializableSubclass()
Constructor[] ctors = baseClass.getConstructors();
Constructor ctorWithParams = null;
for(Constructor ctor : ctors) {
if(ctor.getParameterTypes().length == 0) {
if (ctor.getParameterTypes().length == 0) {
ctorWithParams = null; //exists the no-arg ctor, use it
break;
} else if(ctorWithParams == null) {
ctorWithParams = ctor;
}
ctorWithParams = ctor;
}
}

int numArgsToPass = 1; // default is 1 to just handle 'this'
Expand All @@ -137,6 +138,18 @@ public Class generateSerializableSubclass()
ctorv.visitInsn(RETURN);
ctorv.visitMaxs(numArgsToPass, numArgsToPass);

if (ctorWithParams != null) {
for (Annotation annotation : ctorWithParams.getAnnotations()) {
ctorv.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true);
}

for (int i = 0; i < ctorWithParams.getParameterTypes().length; i++) {
for (Annotation parameterAnnotation : ctorWithParams.getParameterAnnotations()[i]) {
ctorv.visitParameterAnnotation(i, Type.getDescriptor(parameterAnnotation.annotationType()), true);
}
}
}

MethodVisitor cv = cw.visitMethod(ACC_PRIVATE, "writeObject", "(Ljava/io/ObjectOutputStream;)V", null, new String[] { "java/io/IOException" });
cv.visitVarInsn(ALOAD, 0);
cv.visitVarInsn(ALOAD, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public class MetricsInterceptor {
private MetricsResolver resolver;

private Bean<?> bean;

@Inject
private MetricsInterceptor(MetricRegistry registry, MetricsResolver resolver, @Intercepted Bean<?> bean) {
public MetricsInterceptor(MetricRegistry registry, MetricsResolver resolver, @Intercepted Bean<?> bean) {
this.registry = registry;
this.resolver = resolver;
this.bean = bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.manager.BeanManagerImpl;

@Service
@Rank(10)
Expand Down Expand Up @@ -392,7 +394,7 @@ public <T> T createInterceptorInstance(Class<T> interceptorClass,
BeanDeploymentArchive bda = getBDAForBeanClass(topLevelBundleDesc, ejb.getEjbClassName());

WeldBootstrap bootstrap = weldDeployer.getBootstrapForApp(ejb.getEjbBundleDescriptor().getApplication());
WeldManager beanManager = bootstrap.getManager(bda);
BeanManagerImpl beanManager = bootstrap.getManager(bda);

org.jboss.weld.ejb.spi.EjbDescriptor<T> ejbDesc = beanManager.getEjbDescriptor( ejb.getName());

Expand Down Expand Up @@ -420,30 +422,20 @@ public <T> T createInterceptorInstance(Class<T> interceptorClass,

// Check to see if the interceptor was defined as a Bean.
// This can happen when using @Interceptors to define the interceptors.
Set<Bean<?>> availableBeans = beanManager.getBeans( interceptorClass);
if ( availableBeans != null && !availableBeans.isEmpty()) {
Set<Bean<?>> availableBeans = beanManager.getBeans(interceptorClass);
Bean<?> interceptorBean;
if (availableBeans != null && !availableBeans.isEmpty()) {
// using the ejb's creationalContext so we don't have to do any cleanup.
// the cleanup will be handled by weld when it clean's up the ejb.
Bean<?> interceptorBean = beanManager.resolve( availableBeans );
Object instance = beanManager.getReference(interceptorBean, interceptorClass, creationalContext );
return ( T ) instance;
}

// There are other interceptors like SessionBeanInterceptor that are
// defined via code and they are not beans.
// Cannot use the ejb's creationalContext.
creationalContext = beanManager.createCreationalContext(null);

AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(interceptorClass);
InjectionTarget<T> it =
beanManager.getInjectionTargetFactory(annotatedType).createInterceptorInjectionTarget();
T interceptorInstance = it.produce(creationalContext);
it.inject(interceptorInstance, creationalContext);

// make sure the interceptor's cdi objects get cleaned up when the ejb is cleaned up.
ejbContext.addDependentContext( new JCDIInjectionContextImpl<>( it, creationalContext, interceptorInstance ) );

return interceptorInstance;
interceptorBean = beanManager.resolve(availableBeans);
} else {
AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(interceptorClass);
BeanAttributes<T> attributes = beanManager.createBeanAttributes(annotatedType);
EnhancedAnnotatedType<T> enhancedAnnotatedType = beanManager.createEnhancedAnnotatedType(interceptorClass);
interceptorBean = InterceptorImpl.of(attributes, enhancedAnnotatedType, beanManager);
}
Object instance = beanManager.getReference(interceptorBean, interceptorClass, creationalContext);
return (T) instance;
}

@Override
Expand Down

0 comments on commit 8821add

Please sign in to comment.