Skip to content

Commit

Permalink
Finishing AnnotatedElementHandler replacement
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Sep 18, 2022
1 parent 0a95c36 commit 2d5172b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;

import org.glassfish.apf.AnnotatedElementHandler;
import org.glassfish.apf.context.AnnotationContext;

/**
* This ClientContext implementation holds a top level reference
Expand Down Expand Up @@ -65,7 +66,7 @@ public AnnotatedElementHandler createContextForEjb() {
ejbDescs = this.getDescriptor().getEjbByClassName(ejbClassName);
}

AnnotatedElementHandler aeHandler = null;
AnnotationContext aeHandler = null;
if (ejbDescs != null && ejbDescs.length > 1) {
aeHandler = new EjbsContext(ejbDescs, ejbClass);
} else if (ejbDescs != null && ejbDescs.length == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;

import org.glassfish.apf.AnnotatedElementHandler;
import org.glassfish.apf.context.AnnotationContext;

/**
* This ClientContext implementation holds a top level reference
Expand Down Expand Up @@ -63,7 +64,7 @@ public AnnotatedElementHandler createContextForWeb() {
webComps = getDescriptor().getWebComponentByImplName(implClassName);
}

AnnotatedElementHandler aeHandler = null;
AnnotationContext aeHandler = null;
if (webComps != null && webComps.length > 1) {
aeHandler = new WebComponentsContext(webComps);
} else if (webComps != null && webComps.length == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.sun.enterprise.deployment.annotation.context.RarBundleContext;
import com.sun.enterprise.deployment.annotation.context.WebBundleContext;

import org.glassfish.apf.AnnotatedElementHandler;
import org.glassfish.apf.context.AnnotationContext;
import org.glassfish.deployment.common.RootDeploymentDescriptor;

/**
Expand All @@ -40,21 +40,21 @@ private AnnotatedElementHandlerFactory() {
}


public static AnnotatedElementHandler createAnnotatedElementHandler(RootDeploymentDescriptor bundleDesc) {
AnnotatedElementHandler aeHandler = null;
public static AnnotationContext createAnnotatedElementHandler(RootDeploymentDescriptor bundleDesc) {
if (bundleDesc instanceof EjbBundleDescriptor) {
EjbBundleDescriptor ejbBundleDesc = (EjbBundleDescriptor) bundleDesc;
aeHandler = new EjbBundleContext(ejbBundleDesc);
return new EjbBundleContext(ejbBundleDesc);
} else if (bundleDesc instanceof ApplicationClientDescriptor) {
ApplicationClientDescriptor appClientDesc = (ApplicationClientDescriptor) bundleDesc;
aeHandler = new AppClientContext(appClientDesc);
return new AppClientContext(appClientDesc);
} else if (bundleDesc instanceof WebBundleDescriptor) {
WebBundleDescriptor webBundleDesc = (WebBundleDescriptor) bundleDesc;
aeHandler = new WebBundleContext(webBundleDesc);
return new WebBundleContext(webBundleDesc);
} else if (bundleDesc instanceof ConnectorDescriptor) {
ConnectorDescriptor connectorDesc = (ConnectorDescriptor) bundleDesc;
aeHandler = new RarBundleContext(connectorDesc);
return new RarBundleContext(connectorDesc);
} else {
return null;
}
return aeHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
import org.jvnet.hk2.annotations.Service;

/**
* This factory is responsible for initializing a ready to use
* AnnotationProcessor.
* This factory is responsible for initializing a ready to use AnnotationProcessor.
*
* @author Shing Wai Chan
*/
Expand Down Expand Up @@ -164,5 +163,10 @@ public Class<? extends Annotation>[] getTypeDependencies() {
return getHandler().getTypeDependencies();
}


@Override
public String toString() {
return "LazyAnnotationHandler[\n" + descriptor + "\n " + handler + "\n]";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.glassfish.hk2.classmodel.reflect.Parser;
import org.glassfish.hk2.classmodel.reflect.ParsingContext;
import org.glassfish.hk2.classmodel.reflect.Type;
import org.glassfish.hk2.classmodel.reflect.Types;
import org.glassfish.logging.annotation.LogMessageInfo;

/**
Expand Down Expand Up @@ -149,19 +148,6 @@ protected void setParser(Parser parser) {
classParser = parser;
}


/**
* Scan the archive file and gather a list of classes
* that should be processed for anntoations
*
* @param archiveFile the archive file for scanning
* @param descriptor the bundle descriptor associated with this archive
* @param classLoader the classloader used to scan the annotation
* @throws IOException if the file cannot be read.
*/
@Override
protected abstract void process(File archiveFile, T descriptor, ClassLoader classLoader) throws IOException;

/**
* Performs all additional work after the "process" method has finished.
* <p>
Expand Down Expand Up @@ -346,12 +332,6 @@ protected void addLibraryJars(T bundleDesc, ReadableArchive moduleArchive) {
}


@Override
public Types getTypes() {
return classParser.getContext().getTypes();
}


protected synchronized ExecutorService getExecutorService() {
if (executorService != null) {
return executorService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.glassfish.apf.AnnotatedElementHandler;
import org.glassfish.apf.AnnotationProcessor;
import org.glassfish.apf.AnnotationProcessorException;
import org.glassfish.apf.ErrorHandler;
import org.glassfish.apf.ProcessingContext;
import org.glassfish.apf.ProcessingResult;
import org.glassfish.apf.ResultType;
import org.glassfish.apf.Scanner;
import org.glassfish.apf.context.AnnotationContext;
import org.glassfish.apf.impl.DefaultErrorHandler;
import org.glassfish.api.deployment.archive.Archive;
import org.glassfish.api.deployment.archive.ArchiveType;
Expand Down Expand Up @@ -564,8 +564,8 @@ protected ProcessingResult processAnnotations(T bundleDesc, ModuleScanner<T> sca
return null;
}

AnnotatedElementHandler aeHandler = AnnotatedElementHandlerFactory.createAnnotatedElementHandler(bundleDesc);
if (aeHandler == null) {
AnnotationContext annotationContext = AnnotatedElementHandlerFactory.createAnnotatedElementHandler(bundleDesc);
if (annotationContext == null) {
return null;
}

Expand Down Expand Up @@ -603,7 +603,7 @@ protected ProcessingResult processAnnotations(T bundleDesc, ModuleScanner<T> sca
ctx.setErrorHandler(annotationErrorHandler);
}
ctx.setProcessingInput(scanner);
ctx.pushHandler(aeHandler);
ctx.pushHandler(annotationContext);

// Make sure there is a classloader available on the descriptor during annotation
// processing.
Expand Down Expand Up @@ -671,7 +671,7 @@ public T readStandardDeploymentDescriptor(ReadableArchive archive)
*
* @param archive the archive
* @param descriptor the initialized deployment descriptor
* @link getRuntimeDeploymentDescriptorPath
* @see #getRuntimeDeploymentDescriptorPath(ReadableArchive)
*/
public void readRuntimeDeploymentDescriptor(ReadableArchive archive, T descriptor)
throws IOException, SAXException {
Expand All @@ -688,7 +688,7 @@ public void readRuntimeDeploymentDescriptor(ReadableArchive archive, T descripto
* @param archive the archive
* @param descriptor the initialized deployment descriptor
* @param warnIfMultipleDDs whether to log warnings if both the GlassFish and the legacy Sun descriptors are present
* @link getRuntimeDeploymentDescriptorPath
* @see #getRuntimeDeploymentDescriptorPath(ReadableArchive)
*/
public void readRuntimeDeploymentDescriptor(ReadableArchive archive, T descriptor, final boolean warnIfMultipleDDs)
throws IOException, SAXException {
Expand All @@ -701,10 +701,8 @@ public void readRuntimeDeploymentDescriptor(ReadableArchive archive, T descripto
DOLUtils.readRuntimeDeploymentDescriptor(getSortedConfigurationDDFiles(archive), archive, descriptor, this, warnIfMultipleDDs);
}

/*
* write the J2EE module represented by this instance to a new
* J2EE archive file
*
/**
* write the Jakarta EE module represented by this instance to a new Jakarta EE archive file
*/
public void write() throws IOException {
write(path);
Expand Down Expand Up @@ -939,12 +937,11 @@ public String getDeploymentDescriptorPath() {
* for a particular type of J2EE Archive
*/
public String getRuntimeDeploymentDescriptorPath(ReadableArchive archive) throws IOException {
DeploymentDescriptorFile ddFile = getConfigurationDDFile(archive);
if (ddFile != null) {
return ddFile.getDeploymentDescriptorPath();
} else {
DeploymentDescriptorFile<?> ddFile = getConfigurationDDFile(archive);
if (ddFile == null) {
return null;
}
return ddFile.getDeploymentDescriptorPath();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -38,11 +39,12 @@
/**
* This factory class is responsible for creating Archivists
*
* @author Jerome Dochez
* @author Jerome Dochez
*/
@Service
@Singleton
public class ArchivistFactory {

public final static String ARCHIVE_TYPE = "archiveType";
public final static String EXTENSION_ARCHIVE_TYPE = "extensionArchiveType";

Expand All @@ -51,44 +53,43 @@ public class ArchivistFactory {

public Archivist getArchivist(String archiveType, ClassLoader cl) {
Archivist result = getArchivist(archiveType);
if(result != null) {
if (result != null) {
result.setClassLoader(cl);
}
return result;
}

@SuppressWarnings("unchecked")
public Archivist getArchivist(String archiveType) {
ActiveDescriptor<Archivist> best = (ActiveDescriptor<Archivist>)
habitat.getBestDescriptor(new ArchivistFilter(archiveType, ARCHIVE_TYPE, Archivist.class));
if (best == null) return null;

public Archivist<?> getArchivist(String archiveType) {
ActiveDescriptor<Archivist<?>> best = (ActiveDescriptor<Archivist<?>>) habitat
.getBestDescriptor(new ArchivistFilter(archiveType, ARCHIVE_TYPE, Archivist.class));
if (best == null) {
return null;
}

return habitat.getServiceHandle(best).getService();
}

public Archivist getArchivist(ArchiveType moduleType) {

public Archivist<?> getArchivist(ArchiveType moduleType) {
return getArchivist(String.valueOf(moduleType));
}

@SuppressWarnings("unchecked")

public List<ExtensionsArchivist> getExtensionsArchivists(Collection<Sniffer> sniffers, ArchiveType moduleType) {
Set<String> containerTypes = new HashSet<String>();
Set<String> containerTypes = new HashSet<>();
for (Sniffer sniffer : sniffers) {
containerTypes.add(sniffer.getModuleType());
}
List<ExtensionsArchivist> archivists = new ArrayList<ExtensionsArchivist>();
List<ExtensionsArchivist> archivists = new ArrayList<>();
for (String containerType : containerTypes) {
List<ActiveDescriptor<?>> descriptors =
habitat.getDescriptors(
new ArchivistFilter(containerType, EXTENSION_ARCHIVE_TYPE, ExtensionsArchivist.class));
List<ActiveDescriptor<?>> descriptors = habitat
.getDescriptors(new ArchivistFilter(containerType, EXTENSION_ARCHIVE_TYPE, ExtensionsArchivist.class));

for (ActiveDescriptor<?> item : descriptors) {

ActiveDescriptor<ExtensionsArchivist> descriptor =
(ActiveDescriptor<ExtensionsArchivist>) item;

ServiceHandle<ExtensionsArchivist> handle = habitat.getServiceHandle(descriptor);
ExtensionsArchivist ea = handle.getService();
ActiveDescriptor<ExtensionsArchivist<?>> descriptor = (ActiveDescriptor<ExtensionsArchivist<?>>) item;
ServiceHandle<ExtensionsArchivist<?>> handle = habitat.getServiceHandle(descriptor);
ExtensionsArchivist<?> ea = handle.getService();
if (ea.supportsModuleType(moduleType)) {
archivists.add(ea);
}
Expand All @@ -98,6 +99,7 @@ public List<ExtensionsArchivist> getExtensionsArchivists(Collection<Sniffer> sni
}

private static class ArchivistFilter implements IndexedFilter {

private final String archiveType;
private final String metadataKey;
private final Class<?> index;
Expand All @@ -108,9 +110,7 @@ private ArchivistFilter(String archiveType, String metadataKey, Class<?> index)
this.index = index;
}

/* (non-Javadoc)
* @see org.glassfish.hk2.api.Filter#matches(org.glassfish.hk2.api.Descriptor)
*/

@Override
public boolean matches(Descriptor d) {
Map<String, List<String>> metadata = d.getMetadata();
Expand All @@ -123,23 +123,18 @@ public boolean matches(Descriptor d) {
return values.contains(archiveType);
}

/* (non-Javadoc)
* @see org.glassfish.hk2.api.IndexedFilter#getAdvertisedContract()
*/

@Override
public String getAdvertisedContract() {
return index.getName();
}

/* (non-Javadoc)
* @see org.glassfish.hk2.api.IndexedFilter#getName()
*/

@Override
public String getName() {
return null;
}

}


}

0 comments on commit 2d5172b

Please sign in to comment.