Skip to content

Commit

Permalink
Archivists updated
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 ac1d5be commit 251927e
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ private AppClientArchivist completeInit(final AppClientArchivist arch) {
private AppClientArchivist getArchivist(final ClassLoader classLoader) throws IOException {
if (archivist == null) {
ArchivistFactory af = Util.getArchivistFactory();

archivist = completeInit((AppClientArchivist) af.getArchivist("car"));
}
archivist.setClassLoader(classLoader);
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) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -19,16 +20,18 @@
import com.sun.enterprise.deployment.ApplicationClientDescriptor;
import com.sun.enterprise.deployment.archivist.AppClientArchivist;
import com.sun.enterprise.deployment.archivist.ExtensionsArchivist;

import jakarta.inject.Inject;

import java.io.IOException;
import java.util.ArrayList;

import org.glassfish.api.deployment.archive.ReadableArchive;
import org.glassfish.hk2.api.IterableProvider;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.hk2.api.PostConstruct;
import org.jvnet.hk2.annotations.Optional;

import org.jvnet.hk2.annotations.Service;
import jakarta.inject.Inject;
import org.glassfish.hk2.api.PerLookup;
import org.xml.sax.SAXException;

/**
Expand All @@ -52,17 +55,21 @@
@PerLookup
public class ACCAppClientArchivist extends AppClientArchivist implements PostConstruct {

@Inject @Optional
IterableProvider<ExtensionsArchivist> allExtensionArchivists;
@Inject
@Optional
private IterableProvider<ExtensionsArchivist<?>> allExtensionArchivists;

@Override
public void readRuntimeDeploymentDescriptor(ReadableArchive archive, ApplicationClientDescriptor descriptor) throws IOException, SAXException {
public void readRuntimeDeploymentDescriptor(ReadableArchive archive, ApplicationClientDescriptor descriptor)
throws IOException, SAXException {
super.readRuntimeDeploymentDescriptor(archive, descriptor, false);
}


@Override
public void postConstruct() {
extensionsArchivists = new ArrayList<ExtensionsArchivist>();
for (ExtensionsArchivist extensionArchivist : allExtensionArchivists) {
extensionsArchivists = new ArrayList<>();
for (ExtensionsArchivist<?> extensionArchivist : allExtensionArchivists) {
if (extensionArchivist.supportsModuleType(getModuleType())) {
extensionsArchivists.add(extensionArchivist);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -272,8 +273,8 @@ private HandlerProcessingResult processAnnotations(ProcessingContext ctx, Annota
private void process(ProcessingContext ctx, AnnotationInfo element, HandlerProcessingResultImpl result)
throws AnnotationProcessorException {
Annotation annotation = element.getAnnotation();
LOG.log(Level.FINER, "Annotation : {0} delegate = {1}",
new Object[] {annotation.annotationType().getName(), delegate});
LOG.log(Level.FINER, "Processing annotation: {0} delegate = {1}",
new Object[] {element, delegate});
result.addResult(annotation.annotationType(), ResultType.UNPROCESSED);

// we ignore all java.* annotations
Expand All @@ -284,11 +285,10 @@ private void process(ProcessingContext ctx, AnnotationInfo element, HandlerProce

List<AnnotationHandler> annotationHandlers = handlers.get(annotation.annotationType().getName());
if (annotationHandlers == null) {
if (delegate != null) {
delegate.process(ctx, element, result);
if (delegate == null) {
LOG.log(Level.FINER, "No handler defined for {0}", annotation);
} else {
ctx.getErrorHandler()
.fine(new AnnotationProcessorException("No handler defined for " + annotation.annotationType()));
delegate.process(ctx, element, result);
}
return;
}
Expand All @@ -299,6 +299,9 @@ private void process(ProcessingContext ctx, AnnotationInfo element, HandlerProce
// (if present on the annotated element) before itself.
// do this check and process those annotations first.
Class<? extends Annotation>[] dependencies = handler.getTypeDependencies();
if (LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "Dependencies of handler " + handler + ", " + Arrays.toString(dependencies));
}
if (dependencies != null) {
AnnotatedElement ae = element.getAnnotatedElement();
for (Class<? extends Annotation> annotationType : dependencies) {
Expand All @@ -314,8 +317,7 @@ private void process(ProcessingContext ctx, AnnotationInfo element, HandlerProce
}
}

// at this point, all annotation that I declared depending on
// are processed
// at this point, all annotation that I declared depending on are processed
HandlerProcessingResult processingResult = null;
try {
processingResult = handler.processAnnotation(element);
Expand Down Expand Up @@ -401,7 +403,7 @@ public AnnotationHandler getAnnotationHandler(Class<? extends Annotation> type)
*/
@Override
public AnnotatedElement getLastAnnotatedElement(ElementType type) {
for (int i=annotatedElements.size();i!=0;i--) {
for (int i = annotatedElements.size(); i != 0; i--) {
StackElement e = annotatedElements.get(i - 1);
if (e.getElementType().equals(type)) {
return e.getAnnotatedElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public boolean readModulesDescriptors(Application app, ReadableArchive appArchiv
newArchivist.setRuntimeXMLValidationLevel(this.getRuntimeXMLValidationLevel());
newArchivist.setAnnotationProcessingRequested(annotationProcessingRequested);

BundleDescriptor descriptor = null;
BundleDescriptor bundleDescriptor = null;
try (ReadableArchive embeddedArchive = appArchive.getSubArchive(aModule.getArchiveUri())) {
if (embeddedArchive == null) {
throw new IllegalArgumentException(localStrings.getLocalString("enterprise.deployment.nosuchmodule",
Expand All @@ -553,19 +553,19 @@ public boolean readModulesDescriptors(Application app, ReadableArchive appArchiv
DOLUtils.setExtensionArchivistForSubArchivist(habitat, embeddedArchive, aModule, app, newArchivist);
if (aModule.getAlternateDescriptor() == null) {
// open the subarchive to get the deployment descriptor...
descriptor = newArchivist.open(embeddedArchive, app);
bundleDescriptor = newArchivist.open(embeddedArchive, app);
} else {
// the module use alternate deployement descriptor, ignore the
// DDs in the archive.
try (InputStream is = appArchive.getEntry(aModule.getAlternateDescriptor())) {
DeploymentDescriptorFile ddFile = newArchivist.getStandardDDFile();
DeploymentDescriptorFile<?> ddFile = newArchivist.getStandardDDFile();
ddFile.setXMLValidation(newArchivist.getXMLValidation());
ddFile.setXMLValidationLevel(newArchivist.getXMLValidationLevel());
if (appArchive.getURI() != null) {
ddFile.setErrorReportingString(appArchive.getURI().getSchemeSpecificPart());
}
descriptor = (BundleDescriptor) ddFile.read(is);
descriptor.setApplication(app);
bundleDescriptor = (BundleDescriptor) ddFile.read(is);
bundleDescriptor.setApplication(app);
}

// TODO : JD need to be revisited for EAR files with Alternative descriptors,
Expand All @@ -574,19 +574,19 @@ public boolean readModulesDescriptors(Application app, ReadableArchive appArchiv
List<ExtensionsArchivist> extensionsArchivists = newArchivist.getExtensionArchivists();
if (extensionsArchivists != null) {
for (ExtensionsArchivist extension : extensionsArchivists) {
Object rdd = extension.open(newArchivist, embeddedArchive, descriptor);
Object rdd = extension.open(newArchivist, embeddedArchive, bundleDescriptor);
if (rdd instanceof RootDeploymentDescriptor) {
extensions.put(extension, (RootDeploymentDescriptor) rdd);
}
}
}
newArchivist.postStandardDDsRead(descriptor, embeddedArchive, extensions);
newArchivist.readAnnotations(embeddedArchive, descriptor, extensions);
newArchivist.postAnnotationProcess(descriptor, embeddedArchive);
newArchivist.postOpen(descriptor, embeddedArchive);
newArchivist.postStandardDDsRead(bundleDescriptor, embeddedArchive, extensions);
newArchivist.readAnnotations(embeddedArchive, bundleDescriptor, extensions);
newArchivist.postAnnotationProcess(bundleDescriptor, embeddedArchive);
newArchivist.postOpen(bundleDescriptor, embeddedArchive);
// now reads the runtime deployment descriptor...
if (isHandlingRuntimeInfo()) {
DOLUtils.readAlternativeRuntimeDescriptor(appArchive, embeddedArchive, newArchivist, descriptor,
DOLUtils.readAlternativeRuntimeDescriptor(appArchive, embeddedArchive, newArchivist, bundleDescriptor,
aModule.getAlternateDescriptor());
// read extensions runtime deployment descriptors if any
for (Map.Entry<ExtensionsArchivist, RootDeploymentDescriptor> extension : extensions.entrySet()) {
Expand All @@ -600,20 +600,20 @@ public boolean readModulesDescriptors(Application app, ReadableArchive appArchiv
}
} // else
}
if (descriptor == null) {
if (bundleDescriptor == null) {
// display a message only if we had a handle on the sub archive
return false;
}
descriptor.getModuleDescriptor().setArchiveUri(aModule.getArchiveUri());
aModule.setModuleName(descriptor.getModuleDescriptor().getModuleName());
aModule.setDescriptor(descriptor);
descriptor.setApplication(app);
bundleDescriptor.getModuleDescriptor().setArchiveUri(aModule.getArchiveUri());
aModule.setModuleName(bundleDescriptor.getModuleDescriptor().getModuleName());
aModule.setDescriptor(bundleDescriptor);
bundleDescriptor.setApplication(app);
aModule.setManifest(newArchivist.getManifest());
// for optional application.xml case, set the
// context root as module name for web modules
if (!appArchive.exists("META-INF/application.xml")) {
if (aModule.getModuleType().equals(DOLUtils.warType())) {
WebBundleDescriptor wbd = (WebBundleDescriptor) descriptor;
WebBundleDescriptor wbd = (WebBundleDescriptor) bundleDescriptor;
if (wbd.getContextRoot() != null && !wbd.getContextRoot().isEmpty()) {
aModule.setContextRoot(wbd.getContextRoot());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
@Service
public class ApplicationFactory {

protected static final Logger logger = DOLUtils.getDefaultLogger();
private static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(Archivist.class);

@Inject
ArchiveFactory archiveFactory;

Expand All @@ -58,11 +61,6 @@ public class ApplicationFactory {
@Inject
DasConfig dasConfig;

protected static final Logger logger =
DOLUtils.getDefaultLogger();

// resources...
private static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(Archivist.class);

/**
* Open a jar file and return an application object for the modules contained
Expand All @@ -72,9 +70,7 @@ public class ApplicationFactory {
* @param jarFile the archive file
* @return the application object
*/
public Application openArchive(URI jarFile, String archiveType)
throws IOException, SAXException {

public Application openArchive(URI jarFile, String archiveType) throws IOException, SAXException {
return openArchive(jarFile, archiveType, false);
}

Expand All @@ -89,8 +85,7 @@ public Application openArchive(URI jarFile, String archiveType)
* @return the application object
*/
public Application openArchive(Archivist archivist, URI jarFile, boolean handleRuntimeInfo)
throws IOException, SAXException {

throws IOException, SAXException {
// never read the runtime deployment descriptor before the
// module type is found and the application object created

Expand All @@ -110,13 +105,12 @@ public Application openArchive(Archivist archivist, URI jarFile, boolean handleR
* @param handleRuntimeInfo true to read configuration deployment descriptors
* @return the application object
*/

public Application openArchive(Archivist archivist, ReadableArchive in, boolean handleRuntimeInfo)
throws IOException, SAXException {

throws IOException, SAXException {
return openArchive(in.getURI().getSchemeSpecificPart(), archivist, in, handleRuntimeInfo);
}


/**
* Open a jar file and return an application object for the modules contained
* in the archive. If the archive is a standalone module, this API will
Expand Down Expand Up @@ -148,7 +142,7 @@ public Application openArchive(String appName, Archivist archivist, ReadableArch
new Object[]{in.getURI()}));
return null;
}
ModuleDescriptor newModule = archivist.createModuleDescriptor(descriptor);
ModuleDescriptor<BundleDescriptor> newModule = archivist.createModuleDescriptor(descriptor);
newModule.setArchiveUri(in.getURI().getSchemeSpecificPart());
application = Application.createVirtualApplication(appName,newModule);
}
Expand All @@ -161,22 +155,22 @@ public Application openArchive(String appName, Archivist archivist, ReadableArch
}

// validate
application.setClassLoader(archivist.getClassLoader());
application.visit(new ApplicationValidator());

application.setClassLoader(archivist.getClassLoader());
application.visit(new ApplicationValidator());

return application;

}


/**
* This method creates an Application object from reading the
* standard deployment descriptor.
*
* @param archive the archive for the application
*/
public Application createApplicationFromStandardDD(
ReadableArchive archive, String archiveType) throws IOException, SAXException {
Archivist archivist = archivistFactory.getArchivist(archiveType, null);
public Application createApplicationFromStandardDD(ReadableArchive archive, String archiveType)
throws IOException, SAXException {
Archivist<BundleDescriptor> archivist = (Archivist<BundleDescriptor>) archivistFactory.getArchivist(archiveType, null);
String xmlValidationLevel = dasConfig.getDeployXmlValidation();
archivist.setXMLValidationLevel(xmlValidationLevel);
if (xmlValidationLevel.equals("none")) {
Expand All @@ -187,7 +181,7 @@ public Application createApplicationFromStandardDD(
if (desc instanceof Application) {
application = (Application) desc;
} else {
ModuleDescriptor newModule = archivist.createModuleDescriptor(desc);
ModuleDescriptor<BundleDescriptor> newModule = archivist.createModuleDescriptor(desc);
newModule.setArchiveUri(archive.getURI().getSchemeSpecificPart());
String moduleName = newModule.getModuleName();
application = Application.createVirtualApplication(moduleName, newModule);
Expand Down Expand Up @@ -225,7 +219,7 @@ public Application openWith(Application application, ReadableArchive archive, Ar
*/
public Application openArchive(URI jarFile, String archiveType, boolean handleRuntimeInfo)
throws IOException, SAXException {
Archivist archivist = archivistFactory.getArchivist(archiveType);
Archivist<? extends BundleDescriptor> archivist = archivistFactory.getArchivist(archiveType);
return openArchive(archivist, jarFile, handleRuntimeInfo);
}

Expand All @@ -241,28 +235,20 @@ public String getApplicationName(File jarFile) throws IOException {
"{0} does not exist", new Object[]{jarFile}));
}

/*
*Add finally clause containing explicit close of jar file.
*/
JarFile jar = null;
try {
jar = new JarFile(jarFile);
try (JarFile jar = new JarFile(jarFile)) {
ApplicationDeploymentDescriptorFile node = new ApplicationDeploymentDescriptorFile();
node.setXMLValidation(false);
ZipEntry deploymentEntry = jar.getEntry(node.getDeploymentDescriptorPath());
if (deploymentEntry != null) {
try {
Application application = (Application) node.read(jar.getInputStream(deploymentEntry));
return application.getDisplayName();
} catch (Exception pe) {
logger.log(Level.WARNING, "Error occurred", pe);
}
if (deploymentEntry == null) {
return null;
}
} finally {
if (jar != null) {
jar.close();
try {
Application application = (Application) node.read(jar.getInputStream(deploymentEntry));
return application.getDisplayName();
} catch (Exception pe) {
logger.log(Level.WARNING, "Error occurred", pe);
return null;
}
}
return null;
}
}
}

0 comments on commit 251927e

Please sign in to comment.