Skip to content

Commit

Permalink
Formatting in scanners
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 2e2f15f commit ec2685f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 71 deletions.
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, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,29 +17,35 @@

package com.sun.enterprise.deployment.annotation.impl;

import com.sun.enterprise.deployment.ApplicationClientDescriptor;
import java.net.URISyntaxException;
import java.util.*;
import org.glassfish.apf.impl.AnnotationUtils;
import org.glassfish.api.deployment.archive.ReadableArchive;
import org.glassfish.hk2.classmodel.reflect.*;

import org.glassfish.internal.deployment.AnnotationTypesProvider;
import org.jvnet.hk2.annotations.Optional;
import org.jvnet.hk2.annotations.Service;
import org.glassfish.hk2.api.PerLookup;
import com.sun.enterprise.deploy.shared.FileArchive;
import com.sun.enterprise.deployment.ApplicationClientDescriptor;
import com.sun.enterprise.deployment.deploy.shared.InputJarArchive;

import com.sun.enterprise.deployment.deploy.shared.MultiReadableArchive;

import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;

import org.glassfish.apf.impl.AnnotationUtils;
import org.glassfish.api.deployment.archive.ReadableArchive;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.hk2.classmodel.reflect.AnnotatedElement;
import org.glassfish.hk2.classmodel.reflect.AnnotationType;
import org.glassfish.hk2.classmodel.reflect.Member;
import org.glassfish.hk2.classmodel.reflect.Parser;
import org.glassfish.hk2.classmodel.reflect.ParsingContext;
import org.glassfish.hk2.classmodel.reflect.Type;
import org.glassfish.internal.deployment.AnnotationTypesProvider;
import org.jvnet.hk2.annotations.Optional;
import org.jvnet.hk2.annotations.Service;

/**
* Implementation of the Scanner interface for AppClient
* <p>
Expand All @@ -49,50 +56,57 @@
* @author Shing Wai Chan
* @author tjquinn
*/
@Service(name="car")
@Service(name = "car")
@PerLookup
public class AppClientScanner extends ModuleScanner<ApplicationClientDescriptor> {

@Inject
@Named("EJB") @Optional
@Named("EJB")
@Optional
protected AnnotationTypesProvider ejbProvider;

@Override
public void process(ReadableArchive archive, ApplicationClientDescriptor bundleDesc, ClassLoader classLoader, Parser parser) throws IOException {
public void process(ReadableArchive archive, ApplicationClientDescriptor bundleDesc, ClassLoader classLoader,
Parser parser) throws IOException {
setParser(parser);
doProcess(archive, bundleDesc, classLoader);
completeProcess(bundleDesc, archive);
calculateResults(bundleDesc);
}

public void process(File archiveFile, ApplicationClientDescriptor bundleDesc, ClassLoader classLoader) throws IOException {

@Override
public void process(File archiveFile, ApplicationClientDescriptor bundleDesc, ClassLoader classLoader)
throws IOException {
/*
* This variant should not be invoked, but we need to have it here to
* satisfy the interface contract. For this app client scanner, its
* satisfy the interface contract. For this app client scanner, its
* own process(ReadableArchive...) method will be invoked rather than
* the one implemented at ModuleScanner. This is to allow the app
* client one to support InputJarArchives as well as FileArchives. This
* the one implemented at ModuleScanner. This is to allow the app
* client one to support InputJarArchives as well as FileArchives. This
* is important because the ACC deals with JARs directly rather than
* expanding them into directories.
*/
throw new UnsupportedOperationException("Not supported.");
}


/**
* This scanner will scan the given main class for annotation processing.
* The archiveFile and libJarFiles correspond to classpath.
*
* @param archiveFile
* @param desc
* @param classLoader
*/
private void doProcess(ReadableArchive archive, ApplicationClientDescriptor desc,
ClassLoader classLoader) throws IOException {
private void doProcess(ReadableArchive archive, ApplicationClientDescriptor desc, ClassLoader classLoader)
throws IOException {
if (AnnotationUtils.getLogger().isLoggable(Level.FINE)) {
AnnotationUtils.getLogger().fine("archiveFile is " + archive.getURI().toASCIIString());
AnnotationUtils.getLogger().fine("classLoader is " + classLoader);
}

//always add main class
// always add main class
String mainClassName = desc.getMainClassName();
addScanClassName(mainClassName);

Expand All @@ -102,35 +116,36 @@ private void doProcess(ReadableArchive archive, ApplicationClientDescriptor desc
addScanClassName(desc.getCallbackHandler());
}

if (archive instanceof FileArchive) {
addScanDirectory(new File(archive.getURI()));
} else if (archive instanceof InputJarArchive) {
/*
* This is during deployment, so use the faster code path using
* the File object.
*/
URI uriToAdd = archive.getURI();
addScanJar(scanJar(uriToAdd));
} else if (archive instanceof MultiReadableArchive) {
/*
* During app client launches, scan the developer's archive
* which is in slot #1, not the facade archive which is in
* slot #0. Also, use URIs instead of File objects because
* during Java Web Start launches we don't have access to
* File objects.
*/
addScanURI(scanURI(((MultiReadableArchive) archive).getURI(1)));
}
if (archive instanceof FileArchive) {
addScanDirectory(new File(archive.getURI()));
} else if (archive instanceof InputJarArchive) {
/*
* This is during deployment, so use the faster code path using
* the File object.
*/
URI uriToAdd = archive.getURI();
addScanJar(scanJar(uriToAdd));
} else if (archive instanceof MultiReadableArchive) {
/*
* During app client launches, scan the developer's archive
* which is in slot #1, not the facade archive which is in
* slot #0. Also, use URIs instead of File objects because
* during Java Web Start launches we don't have access to
* File objects.
*/
addScanURI(scanURI(((MultiReadableArchive) archive).getURI(1)));
}

this.classLoader = classLoader;
this.archiveFile = null; // = archive;
this.archiveFile = null;
}


private File scanJar(URI uriToAdd) {
return new File(uriToAdd);
}


private URI scanURI(URI uriToAdd) throws IOException {
if (uriToAdd.getScheme().equals("jar")) {
try {
Expand All @@ -142,37 +157,39 @@ private URI scanURI(URI uriToAdd) throws IOException {
return uriToAdd;
}


/**
* Overriding to handle the case where EJB class is mistakenly packaged inside an appclient jar.
* Instead of throwing an error which might raise backward compatiability issues, a cleaner way is to
* just skip the annotation processing for them.
* Instead of throwing an error which might raise backward compatiability issues, a cleaner way
* is to just skip the annotation processing for them.
*/
@Override
protected void calculateResults(ApplicationClientDescriptor bundleDesc) {
super.calculateResults(bundleDesc);

Class[] ejbAnnotations;
if (ejbProvider != null)
Class<?>[] ejbAnnotations;
if (ejbProvider != null) {
ejbAnnotations = ejbProvider.getAnnotationTypes();
else
} else {
ejbAnnotations = new Class[] {jakarta.ejb.Stateful.class, jakarta.ejb.Stateless.class,
jakarta.ejb.MessageDriven.class, jakarta.ejb.Singleton.class};
Set<String> toBeRemoved = new HashSet<String>();
jakarta.ejb.MessageDriven.class, jakarta.ejb.Singleton.class};
}
Set<String> toBeRemoved = new HashSet<>();
ParsingContext context = classParser.getContext();
for (Class ejbAnnotation: ejbAnnotations) {
for (Class<?> ejbAnnotation : ejbAnnotations) {
Type type = context.getTypes().getBy(ejbAnnotation.getName());
if (type != null && type instanceof AnnotationType) {
AnnotationType at = (AnnotationType) type;
for (AnnotatedElement ae : at.allAnnotatedTypes()) {
Type t = (ae instanceof Member?((Member) ae).getDeclaringType():(Type) ae);
Type t = (ae instanceof Member ? ((Member) ae).getDeclaringType() : (Type) ae);
if (t.wasDefinedIn(scannedURI)) {
toBeRemoved.add(t.getName());
}
}
}
}

for (String element: toBeRemoved) {
for (String element : toBeRemoved) {
entries.remove(element);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
*/
public abstract class ModuleScanner<T> extends JavaEEScanner implements Scanner<T> {

private static final int DEFAULT_ENTRY_BUFFER_SIZE = 8192;

private static final Logger LOG = DOLUtils.getDefaultLogger();

@LogMessageInfo(
Expand Down Expand Up @@ -106,7 +104,7 @@ public abstract class ModuleScanner<T> extends JavaEEScanner implements Scanner<
private static ExecutorService executorService;

@Inject
DefaultAnnotationScanner defaultScanner;
private DefaultAnnotationScanner defaultScanner;

protected File archiveFile;
protected ClassLoader classLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,21 @@

import com.sun.enterprise.deployment.ConnectorDescriptor;

import java.io.IOException;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.logging.Level;

import org.glassfish.apf.impl.AnnotationUtils;
import org.jvnet.hk2.annotations.Service;

import org.glassfish.hk2.api.PerLookup;
import org.jvnet.hk2.annotations.Service;


@Service(name="rar")
@Service(name = "rar")
@PerLookup
public class RarScanner extends ModuleScanner<ConnectorDescriptor>{
public class RarScanner extends ModuleScanner<ConnectorDescriptor> {

public void process(File archiveFile, ConnectorDescriptor desc,
ClassLoader classLoader) throws IOException {
@Override
public void process(File archiveFile, ConnectorDescriptor desc, ClassLoader classLoader) throws IOException {
if (AnnotationUtils.getLogger().isLoggable(Level.FINE)) {
AnnotationUtils.getLogger().fine("archiveFile is " + archiveFile);
AnnotationUtils.getLogger().fine("classLoader is " + classLoader);
Expand All @@ -45,19 +43,14 @@ public void process(File archiveFile, ConnectorDescriptor desc,
addScanDirectory(archiveFile);

// add top level jars for scanning
File[] jarFiles = archiveFile.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return (pathname.isFile() &&
pathname.getAbsolutePath().endsWith(".jar"));
}
});

FileFilter filter = pathname -> pathname.isFile() && pathname.getAbsolutePath().endsWith(".jar");
File[] jarFiles = archiveFile.listFiles(filter);
if (jarFiles != null && jarFiles.length > 0) {
for (File jarFile : jarFiles) {
addScanJar(jarFile);
}
}
}else{
} else {
AnnotationUtils.getLogger().fine("RARScanner : not a directory : " + archiveFile.getName());
}
}
Expand Down

0 comments on commit ec2685f

Please sign in to comment.