Skip to content

Commit

Permalink
ResourceAnnotationControl - moved copy pasted code
Browse files Browse the repository at this point in the history
- all classes used the same method.

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Mar 19, 2023
1 parent c1bdc9c commit e6bef17
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 716 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* 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 All @@ -17,40 +17,26 @@

package com.sun.enterprise.connectors.deployment.annotation.handlers;

import com.sun.enterprise.deployment.AdministeredObjectDefinitionDescriptor;
import com.sun.enterprise.deployment.MetadataSource;
import com.sun.enterprise.deployment.ResourceDescriptor;
import com.sun.enterprise.deployment.annotation.context.ResourceContainerContext;
import com.sun.enterprise.deployment.annotation.handlers.AbstractResourceHandler;
import com.sun.enterprise.deployment.annotation.handlers.ResourceAnnotationControl;

import jakarta.resource.AdministeredObjectDefinition;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;

import org.glassfish.apf.AnnotationHandlerFor;
import org.glassfish.apf.AnnotationInfo;
import org.glassfish.apf.AnnotationProcessorException;
import org.glassfish.apf.HandlerProcessingResult;
import org.glassfish.deployment.common.JavaEEResourceType;
import org.glassfish.deployment.common.RootDeploymentDescriptor;
import org.jvnet.hk2.annotations.Service;

import com.sun.enterprise.deployment.AdministeredObjectDefinitionDescriptor;
import com.sun.enterprise.deployment.EjbBundleDescriptor;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.MetadataSource;
import com.sun.enterprise.deployment.ResourceDescriptor;
import com.sun.enterprise.deployment.WebBundleDescriptor;
import com.sun.enterprise.deployment.annotation.context.EjbBundleContext;
import com.sun.enterprise.deployment.annotation.context.EjbContext;
import com.sun.enterprise.deployment.annotation.context.EjbInterceptorContext;
import com.sun.enterprise.deployment.annotation.context.ResourceContainerContext;
import com.sun.enterprise.deployment.annotation.context.WebBundleContext;
import com.sun.enterprise.deployment.annotation.context.WebComponentContext;
import com.sun.enterprise.deployment.annotation.context.WebComponentsContext;
import com.sun.enterprise.deployment.annotation.handlers.AbstractResourceHandler;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.AroundTimeout;
import jakarta.interceptor.Interceptors;
import jakarta.resource.AdministeredObjectDefinition;

/**
* @author Dapeng Hu
Expand All @@ -59,6 +45,9 @@
@AnnotationHandlerFor(AdministeredObjectDefinition.class)
public class AdministeredObjectDefinitionHandler extends AbstractResourceHandler {

private static final ResourceAnnotationControl CTRL = new ResourceAnnotationControl(
AdministeredObjectDefinition.class);

public AdministeredObjectDefinitionHandler() {
}

Expand All @@ -69,127 +58,33 @@ protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, Resour
return processAnnotation(adminObjectDefnAn, ainfo, rcContexts);
}

protected HandlerProcessingResult processAnnotation(AdministeredObjectDefinition adminObjectDefnAn, AnnotationInfo aiInfo,
ResourceContainerContext[] rcContexts)
throws AnnotationProcessorException {

Class annotatedClass = (Class)aiInfo.getAnnotatedElement();
protected HandlerProcessingResult processAnnotation(AdministeredObjectDefinition adminObjectDefnAn,
AnnotationInfo aiInfo, ResourceContainerContext[] rcContexts) throws AnnotationProcessorException {
Class<?> annotatedClass = (Class<?>) aiInfo.getAnnotatedElement();
Annotation[] annotations = annotatedClass.getAnnotations();
boolean warClass = isAWebComponentClass(annotations);
boolean ejbClass = isAEjbComponentClass(annotations);

for(ResourceContainerContext context : rcContexts){
if (!canProcessAnnotation(annotatedClass, ejbClass, warClass, context)){
for (ResourceContainerContext context : rcContexts) {
if (!CTRL.canProcessAnnotation(annotatedClass, ejbClass, warClass, context)) {
return getDefaultProcessedResult();
}
Set<ResourceDescriptor> aodDescs = context.getResourceDescriptors(JavaEEResourceType.AODD);
AdministeredObjectDefinitionDescriptor desc = createDescriptor(adminObjectDefnAn);
if(isDefinitionAlreadyPresent(aodDescs, desc)){
if (isDefinitionAlreadyPresent(aodDescs, desc)) {
merge(aodDescs, adminObjectDefnAn);
}else{
} else {
context.addResourceDescriptor(desc);
}
}
return getDefaultProcessedResult();
}

/**
* To take care of the case where an ejb is provided in a .war and
* annotation processor will process this class twice (once for ejb and
* once for web-bundle-context, which is a bug).<br>
* This method helps to overcome the issue, partially.<br>
* Checks whether both the annotated class and the context are either ejb or web.
*
* @param annotatedClass annotated-class
* @param ejbClass indicates whether the class is an ejb-class
* @param warClass indicates whether the class is an web-class
* @param context resource-container-context
* @return boolean indicates whether the annotation can be processed.
*/
private boolean canProcessAnnotation(Class annotatedClass, boolean ejbClass, boolean warClass,
ResourceContainerContext context) {
if (ejbClass) {
if (!(context instanceof EjbBundleContext ||
context instanceof EjbContext ||
context instanceof EjbInterceptorContext )) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Ignoring @AdministeredObjectDefinition annotation processing as the class is " +
"an EJB class and context is not one of EJBContext");
}
return false;
}
} else if (context instanceof EjbBundleContext) {
EjbBundleContext ejbContext = (EjbBundleContext) context;
EjbBundleDescriptor ejbBundleDescriptor = ejbContext.getDescriptor();
EjbDescriptor[] ejbDescriptor = ejbBundleDescriptor.getEjbByClassName(annotatedClass.getName());
if (ejbDescriptor == null || ejbDescriptor.length == 0) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Ignoring @AdministeredObjectDefinition annotation processing as the class " +
"[ " + annotatedClass + " ] is " +
"not an EJB class and the context is EJBContext");
}
return false;
}
} else if (warClass) {
if (!(context instanceof WebBundleContext || context instanceof WebComponentsContext
|| context instanceof WebComponentContext )) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Ignoring @AdministeredObjectDefinition annotation processing as the class is " +
"an Web class and context is not one of WebContext");
}
return false;
}
} else if (context instanceof WebBundleContext) {
WebBundleContext webBundleContext = (WebBundleContext) context;
WebBundleDescriptor webBundleDescriptor = webBundleContext.getDescriptor();
Collection<RootDeploymentDescriptor> extDesc = webBundleDescriptor.getExtensionsDescriptors();
for(RootDeploymentDescriptor desc : extDesc){
if(desc instanceof EjbBundleDescriptor){
EjbBundleDescriptor ejbBundleDesc = (EjbBundleDescriptor)desc;
EjbDescriptor[] ejbDescs = ejbBundleDesc.getEjbByClassName(annotatedClass.getName());
if(ejbDescs != null && ejbDescs.length > 0){
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Ignoring @AdministeredObjectDefinition annotation processing as the class " +
"[ " + annotatedClass + " ] is " +
"not an Web class and the context is WebContext");
}
return false;
}else if(ejbBundleDesc.getInterceptorByClassName(annotatedClass.getName()) != null){
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Ignoring @AdministeredObjectDefinition annotation processing " +
"as the class " +
"[ " + annotatedClass + " ] is " +
"not an Web class and the context is WebContext");
}
return false;
}else{
Method[] methods = annotatedClass.getDeclaredMethods();
for(Method method : methods){
Annotation annotations[] = method.getAnnotations();
for(Annotation annotation : annotations){
if(annotation.annotationType().equals(AroundInvoke.class) ||
annotation.annotationType().equals(AroundTimeout.class) ||
annotation.annotationType().equals(Interceptors.class)) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Ignoring @AdministeredObjectDefinition annotation processing " +
"as the class " +
"[ " + annotatedClass + " ] is " +
"not an Web class, an interceptor and the context is WebContext");
}
return false;
}
}
}
}
}
}
}
return true;
}


private boolean isDefinitionAlreadyPresent(Set<ResourceDescriptor> descriptors,
AdministeredObjectDefinitionDescriptor desc) {
AdministeredObjectDefinitionDescriptor desc) {
boolean result = false ;
for(ResourceDescriptor descriptor : descriptors){
if(descriptor.equals(desc)){
Expand All @@ -207,20 +102,20 @@ private void merge(Set<ResourceDescriptor> aodDescs, AdministeredObjectDefinitio
if (desc.getName().equals(defn.name())) {

if (desc.getDescription() == null) {
if (defn.description() != null && !defn.description().equals("")) {
if (defn.description() != null && !defn.description().isEmpty()) {
desc.setDescription(defn.description());
}
}

if (desc.getInterfaceName() == null || desc.getInterfaceName().equals("")) {
if (desc.getInterfaceName() == null || desc.getInterfaceName().isEmpty()) {
desc.setInterfaceName(defn.interfaceName());
}

if (desc.getClassName() == null || desc.getClassName().equals("")) {
if (desc.getClassName() == null || desc.getClassName().isEmpty()) {
desc.setClassName(defn.className());
}

if (desc.getResourceAdapter() == null || desc.getResourceAdapter().equals("")) {
if (desc.getResourceAdapter() == null || desc.getResourceAdapter().isEmpty()) {
desc.setResourceAdapter(defn.resourceAdapter());
}

Expand All @@ -229,7 +124,7 @@ private void merge(Set<ResourceDescriptor> aodDescs, AdministeredObjectDefinitio

if (defnProperties.length > 0) {
for (String property : defnProperties) {
int index = property.indexOf("=");
int index = property.indexOf('=');
// found "=" and not at start or end of string
if (index > 0 && index < property.length() - 1) {
String name = property.substring(0, index).trim();
Expand Down
Loading

0 comments on commit e6bef17

Please sign in to comment.