Skip to content

Commit

Permalink
Merge pull request #24616 from dmatej/resource-leaks
Browse files Browse the repository at this point in the history
Fixes for resource leaks
  • Loading branch information
arjantijms authored Feb 21, 2024
2 parents 7a65c43 + bd6374a commit 352fc91
Show file tree
Hide file tree
Showing 101 changed files with 2,795 additions and 3,861 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private byte[] readByteCode(final String className) throws ClassNotFoundExceptio
throw new ClassNotFoundException(className);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileUtils.copy(is, baos, is.available());
FileUtils.copy(is, baos);
return baos.toByteArray();
} catch (IOException e) {
throw new ClassNotFoundException(className, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022, 2023 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 All @@ -18,6 +19,8 @@

import com.sun.enterprise.glassfish.bootstrap.MainHelper;
import com.sun.enterprise.module.bootstrap.StartupContext;
import com.sun.enterprise.util.io.FileUtils;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -70,11 +73,9 @@ private static File getRootDirectory() {
throw new RuntimeException(ex);
}
if (jarURI.getScheme().startsWith("http")) {
/*
* We do not really rely on the root directory during Java
* Web Start launches but we must return something.
*/
return new File(System.getProperty("user.home"));
// We do not really rely on the root directory during Java
// Web Start launches but we must return something.
return FileUtils.USER_HOME;
}
File jarFile = new File(jarURI);
File dirFile = jarFile.getParentFile().getParentFile();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2023 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 @@ -27,6 +27,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.net.URI;
import java.net.URLClassLoader;
import java.security.AccessController;
Expand All @@ -48,14 +50,14 @@
* @author tjquinn
*/
public class MainClassLaunchable implements Launchable {
private static final Logger LOG = System.getLogger(MainClassLaunchable.class.getName());

private final Class<?> mainClass;
private ApplicationClientDescriptor acDesc;
private ClassLoader classLoader;
private AppClientArchivist archivist;

MainClassLaunchable(final ServiceLocator habitat, final Class mainClass) {
super();
MainClassLaunchable(final ServiceLocator habitat, final Class<?> mainClass) {
this.mainClass = mainClass;
}

Expand All @@ -66,20 +68,11 @@ public Class<?> getMainClass() throws ClassNotFoundException {

@Override
public ApplicationClientDescriptor getDescriptor(final URLClassLoader loader) throws IOException, SAXException {
/*
* There is no developer-provided descriptor possible so just
* use a default one.
*/
// There is no developer-provided descriptor possible so just use a default one.
if (acDesc == null) {
ReadableArchive tempArchive = null;
final ACCClassLoader tempLoader = AccessController.doPrivileged(
new PrivilegedAction<ACCClassLoader>() {

@Override
public ACCClassLoader run() {
return new ACCClassLoader(loader.getURLs(), loader.getParent());
}
});
PrivilegedAction<ACCClassLoader> action = () -> new ACCClassLoader(loader.getURLs(), loader.getParent());
final ACCClassLoader tempLoader = AccessController.doPrivileged(action);
tempArchive = createArchive(tempLoader, mainClass);
final AppClientArchivist acArchivist = getArchivist(tempArchive, tempLoader);
archivist.setClassLoader(tempLoader);
Expand All @@ -97,34 +90,9 @@ private String appNameFromMainClass(final Class c) {
return c.getName();
}

// private ReadableArchive createArchive(final ClassLoader loader,
// final Class mainClass) throws IOException, URISyntaxException {
// Manifest mf = new Manifest();
// mf.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass.getName());
// final File tempFile = File.createTempFile("acc", ".jar");
// tempFile.deleteOnExit();
// JarOutputStream jos = new JarOutputStream(
// new BufferedOutputStream(new FileOutputStream(tempFile)), mf);
// final String mainClassResourceName = mainClass.getName().replace('.', '/') + ".class";
// final ZipEntry mainClassEntry = new ZipEntry(mainClassResourceName);
// jos.putNextEntry(mainClassEntry);
// InputStream is = loader.getResourceAsStream(mainClassResourceName);
// int bytesRead;
// byte[] buffer = new byte[1024];
// while ( (bytesRead = is.read(buffer)) != -1) {
// jos.write(buffer, 0, bytesRead);
// }
// is.close();
// jos.closeEntry();
// jos.close();
//
// final InputJarArchive result = new InputJarArchive();
// result.open(new URI("jar", tempFile.toURI().toASCIIString(), null));
// return result;
// }

private ReadableArchive createArchive(final ClassLoader loader,
final Class mainClass) throws IOException {

private ReadableArchive createArchive(final ClassLoader loader, final Class<?> mainClass) throws IOException {
LOG.log(Level.DEBUG, "createArchive(loader, mainClass={0})", mainClass);
ByteArrayOutputStream baos = new ByteArrayOutputStream();

Manifest mf = new Manifest();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2023 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 All @@ -16,40 +17,41 @@

package org.glassfish.appclient.server.core;

import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.Applications;
import com.sun.enterprise.module.HK2Module;
import java.io.IOException;
import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.deployment.Application;
import com.sun.enterprise.deployment.ApplicationClientDescriptor;
import com.sun.enterprise.deployment.archivist.AppClientArchivist;
import com.sun.enterprise.module.HK2Module;
import com.sun.enterprise.module.ModulesRegistry;
import com.sun.logging.LogDomains;

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

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.logging.Logger;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.deployment.DeploymentContext;
import org.glassfish.api.deployment.MetaData;
import org.glassfish.api.deployment.UndeployCommandParameters;
import org.glassfish.appclient.server.core.jws.servedcontent.ASJarSigner;
import org.glassfish.deployment.common.DeploymentException;
import org.glassfish.javaee.core.deployment.JavaEEDeployer;

import org.jvnet.hk2.annotations.Service;
import org.glassfish.hk2.api.PostConstruct;
import jakarta.inject.Singleton;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.appclient.server.core.jws.JWSAdapterManager;
import org.glassfish.appclient.server.core.jws.JavaWebStartInfo;
import org.glassfish.appclient.server.core.jws.servedcontent.ASJarSigner;
import org.glassfish.deployment.common.Artifacts;
import org.glassfish.deployment.common.DeploymentException;
import org.glassfish.deployment.common.DeploymentUtils;
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.javaee.core.deployment.JavaEEDeployer;
import org.jvnet.hk2.annotations.Service;

/**
* AppClient module deployer.
* <p>
Expand Down Expand Up @@ -160,14 +162,14 @@ public class AppClientDeployer
* Maps the app name to the user-friendly context root for that app.
*/
private final Map<String,String> appAndClientNameToUserFriendlyContextRoot =
new HashMap<String,String>();
new HashMap<>();



/** the class loader which knows about the org.glassfish.main.appclient.gf-client-module */
private ClassLoader gfClientModuleClassLoader;

/*
/**
* Each app client server application will listen for config change
* events - for creation, deletion, or change of java-web-start-enabled
* property settings. Because they are not handled as services hk2 will
Expand All @@ -177,8 +179,7 @@ public class AppClientDeployer
* all app client server applications so the deployer can forward
* notifications to each app client server app.
*/
final private Set<AppClientServerApplication> appClientApps =
new HashSet<AppClientServerApplication>();
private final Set<AppClientServerApplication> appClientApps = new HashSet<>();

public AppClientDeployer() {
}
Expand Down Expand Up @@ -282,8 +283,7 @@ protected void generateArtifacts(DeploymentContext dc) throws DeploymentExceptio
* @param helper
* @param dc
*/
private void recordUserFriendlyContextRoot(final AppClientDeployerHelper helper,
final DeploymentContext dc) {
private void recordUserFriendlyContextRoot(final AppClientDeployerHelper helper, final DeploymentContext dc) {
final String path = JWSAdapterManager.userFriendlyContextRoot(helper.appClientDesc(), dc.getAppProps());
dc.getModuleProps().put("jws.user.friendly.path", path);
}
Expand Down Expand Up @@ -322,23 +322,17 @@ private AppClientDeployerHelper getSavedHelperOrCreateHelper(final DeploymentCon
final String key = HELPER_KEY_NAME + moduleURI(dc);
AppClientDeployerHelper h = null;

final AppClientDeployerHelper.Proxy p = dc.getTransientAppMetaData(key,
AppClientDeployerHelper.Proxy.class);
final AppClientDeployerHelper.Proxy p = dc.getTransientAppMetaData(key, AppClientDeployerHelper.Proxy.class);
if (p != null) {
h = p.helper();
}

if (h == null) {
h = dc.getTransientAppMetaData(key,
StandaloneAppClientDeployerHelper.class);
h = dc.getTransientAppMetaData(key, StandaloneAppClientDeployerHelper.class);
}
if (h == null) {
/*
* We are probably loading a previously-deployed app client, so
* the helper has not be created yet. Create it.
*/
h = createAndSaveHelper(dc,
gfClientModuleClassLoader);
// We are probably loading a previously-deployed app client, so
// the helper has not be created yet. Create it.
h = createAndSaveHelper(dc, gfClientModuleClassLoader);
}
return h;
}
Expand All @@ -348,20 +342,20 @@ private String moduleURI(final DeploymentContext dc) {
return acd.getModuleDescriptor().getArchiveUri();
}

public void recordContextRoot(final String appName,
final String clientURIWithinEAR,
final String userFriendlyContextRoot) {
appAndClientNameToUserFriendlyContextRoot.put(
keyToAppAndClientNameMap(appName, clientURIWithinEAR),
userFriendlyContextRoot);

public void recordContextRoot(final String appName, final String clientURIWithinEAR,
final String userFriendlyContextRoot) {
String key = keyToAppAndClientNameMap(appName, clientURIWithinEAR);
appAndClientNameToUserFriendlyContextRoot.put(key, userFriendlyContextRoot);
}

public void removeContextRoot(final String appName,
final String clientURIWithinEAR) {
appAndClientNameToUserFriendlyContextRoot.remove(
keyToAppAndClientNameMap(appName, clientURIWithinEAR));

public void removeContextRoot(final String appName, final String clientURIWithinEAR) {
String key = keyToAppAndClientNameMap(appName, clientURIWithinEAR);
appAndClientNameToUserFriendlyContextRoot.remove(key);
}


/**
* Returns the user-friendly context root for the specified app client.
* <p>
Expand All @@ -371,19 +365,13 @@ public void removeContextRoot(final String appName,
* @param clientModuleURI
* @return
*/
public String userFriendlyContextRoot(final String appName,
final String clientModuleURI) {
return appAndClientNameToUserFriendlyContextRoot.get(
keyToAppAndClientNameMap(appName, clientModuleURI));
public String userFriendlyContextRoot(final String appName, final String clientModuleURI) {
String key = keyToAppAndClientNameMap(appName, clientModuleURI);
return appAndClientNameToUserFriendlyContextRoot.get(key);
}



private String keyToAppAndClientNameMap(final String appName,
final String moduleURIText) {
private String keyToAppAndClientNameMap(final String appName, final String moduleURIText) {
return appName + "/" + (moduleURIText == null ? appName : moduleURIText);
}



}
Loading

0 comments on commit 352fc91

Please sign in to comment.