Skip to content

Commit

Permalink
Refactored renameUploadedFileOrCopyInPlaceFile
Browse files Browse the repository at this point in the history
- Now throws exceptions when something goes wrong
- Doesn't ignore failed renames
- AddLibraryCommand uses result of this method for the classloader

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Nov 3, 2024
1 parent ea43ee3 commit 41ec342
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2011, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -47,6 +48,8 @@
import org.jvnet.hk2.config.UnprocessedChangeEvent;
import org.jvnet.hk2.config.UnprocessedChangeEvents;

import static org.glassfish.deployment.admin.DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile;

@Service(name="add-library")
@PerLookup
@CommandLock(CommandLock.LockType.NONE)
Expand Down Expand Up @@ -93,26 +96,22 @@ public void execute(AdminCommandContext context) {
// rename or copy the library file to the appropriate
// library directory
try {
List<UnprocessedChangeEvent> unprocessed =
new ArrayList<UnprocessedChangeEvent>();

StringBuffer msg = new StringBuffer();

List<UnprocessedChangeEvent> unprocessed = new ArrayList<>();
StringBuilder msg = new StringBuilder();
for (File libraryFile : files) {
if (libraryFile.exists()) {
DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(
libDir, libraryFile, logger, env);
File resultFile = renameUploadedFileOrCopyInPlaceFile(libDir, libraryFile,
env.getApplicationRepositoryPath());
if (typeCommon) {
commonCLService.addToClassPath(libraryFile.toURI().toURL());
commonCLService.addToClassPath(resultFile.toURI().toURL());
} else {
PropertyChangeEvent pe = new PropertyChangeEvent(libDir,
"add-library", null, libraryFile);
UnprocessedChangeEvent uce = new UnprocessedChangeEvent(
pe, "add-library");
PropertyChangeEvent pe = new PropertyChangeEvent(libDir, "add-library", null, resultFile);
UnprocessedChangeEvent uce = new UnprocessedChangeEvent(pe, "add-library");
unprocessed.add(uce);
}
} else {
msg.append(localStrings.getLocalString("lfnf","Library file not found", libraryFile.getAbsolutePath()));
msg.append(
localStrings.getLocalString("lfnf", "Library file not found", libraryFile.getAbsolutePath()));
}
}
if (msg.length() > 0) {
Expand All @@ -122,10 +121,8 @@ public void execute(AdminCommandContext context) {
}

// set the restart required flag
UnprocessedChangeEvents uces = new UnprocessedChangeEvents(
unprocessed);
List<UnprocessedChangeEvents> ucesList =
new ArrayList<UnprocessedChangeEvents>();
UnprocessedChangeEvents uces = new UnprocessedChangeEvents(unprocessed);
List<UnprocessedChangeEvents> ucesList = new ArrayList<>();
ucesList.add(uces);
ucl.unprocessedTransactedEvents(ucesList);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.config.Transaction;

import static java.util.logging.Level.WARNING;
import static org.glassfish.deployment.admin.DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile;

/**
* Deploy command
*
Expand Down Expand Up @@ -600,14 +603,14 @@ private void moveAppFilesToPermanentLocation(
final Logger logger) throws IOException {
final File finalUploadDir = deploymentContext.getAppInternalDir();
final File finalAltDDDir = deploymentContext.getAppAltDDDir();
if (!finalUploadDir.mkdirs()) {
logger.log(Level.FINE, " Attempting to create upload directory {0} was reported as failed; attempting to continue",
new Object[] { finalUploadDir.getAbsolutePath() });
if (!finalUploadDir.isDirectory() && !finalUploadDir.mkdirs()) {
throw new IOException("Failed to create the upload directory " + finalUploadDir.getAbsolutePath());
}
safeCopyOfApp = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(finalUploadDir, originalPathValue, logger, env);
safeCopyOfDeploymentPlan = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(finalUploadDir, deploymentplan, logger, env);
safeCopyOfAltDD = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(finalAltDDDir, altdd, logger, env);
safeCopyOfRuntimeAltDD = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(finalAltDDDir, runtimealtdd, logger, env);
File applicationsDir = env.getApplicationRepositoryPath();
safeCopyOfApp = renameUploadedFileOrCopyInPlaceFile(finalUploadDir, originalPathValue, applicationsDir);
safeCopyOfDeploymentPlan = renameUploadedFileOrCopyInPlaceFile(finalUploadDir, deploymentplan, applicationsDir);
safeCopyOfAltDD = renameUploadedFileOrCopyInPlaceFile(finalAltDDDir, altdd, applicationsDir);
safeCopyOfRuntimeAltDD = renameUploadedFileOrCopyInPlaceFile(finalAltDDDir, runtimealtdd, applicationsDir);
}

private void recordFileLocations(
Expand Down Expand Up @@ -925,7 +928,7 @@ private void validateDeploymentProperties(Properties properties,
ActionReport subReport = context.getActionReport().addSubActionsReport();
subReport.setActionExitCode(ActionReport.ExitCode.WARNING);
subReport.setMessage(warningMsg);
context.getLogger().log(Level.WARNING, warningMsg);
context.getLogger().log(WARNING, warningMsg);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2021, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright 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 Down Expand Up @@ -33,16 +33,13 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.glassfish.api.ActionReport;
import org.glassfish.api.admin.AccessRequired;
import org.glassfish.api.admin.AdminCommand;
import org.glassfish.api.admin.AdminCommandContext;
import org.glassfish.api.admin.FailurePolicy;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.deployment.OpsParams;
import org.glassfish.common.util.admin.ParameterMapExtractor;
import org.glassfish.hk2.api.ServiceLocator;
Expand Down Expand Up @@ -92,7 +89,7 @@ public static ActionReport.ExitCode replicateEnableDisableToContainingCluster(
final Cluster containingCluster = domain.getClusterForInstance(target);
if (containingCluster != null) {
final ParameterMapExtractor extractor = new ParameterMapExtractor(command);
final ParameterMap pMap = extractor.extract(Collections.EMPTY_LIST);
final ParameterMap pMap = extractor.extract(Collections.emptyList());
pMap.set("DEFAULT", appName);

return ClusterOperationUtil.replicateCommand(
Expand Down Expand Up @@ -127,69 +124,57 @@ public static String getTarget(ParameterMap parameters, OpsParams.Origin origin,
return targetName;
}


/**
* @param targetDirectory existing or nonexisting (but creatable) target directory
* @param fileParam relative or absolute path
* @param appsDir If the fileParam resides within the applications directory then it has been
* uploaded. In that case, move it.
* @return result
* @throws IOException
*/
public static File renameUploadedFileOrCopyInPlaceFile(
final File finalUploadDir,
final File targetDirectory,
final File fileParam,
final Logger logger,
ServerEnvironment env) throws IOException {
final File appsDir) throws IOException {
if (fileParam == null) {
return null;
}
/*
* If the fileParam resides within the applications directory then
* it has been uploaded. In that case, rename it.
*/
final File appsDir = env.getApplicationRepositoryPath();

/*
* The default answer is the in-place file, to handle the
* directory-deployment case or the in-place archive case if we ae
* not copying the in-place archive.
*/
File result = fileParam;

if ( ! fileParam.isDirectory() && ! appsDir.toURI().relativize(fileParam.toURI()).isAbsolute()) {
/*
* The file lies within the apps directory, so it was
* uploaded.
*/
result = new File(finalUploadDir, fileParam.getName());
// The default answer is the in-place file, to handle the
// directory-deployment case or the in-place archive case if we are
// not copying the in-place archive.
if (!fileParam.isDirectory() && !appsDir.toURI().relativize(fileParam.toURI()).isAbsolute()) {
// The file lies within the apps directory, so it was uploaded.
final File result = new File(targetDirectory, fileParam.getName());
final long lastMod = fileParam.lastModified();
FileUtils.renameFile(fileParam, result);
if ( ! result.setLastModified(lastMod)) {
logger.log(Level.FINE, "In renaming {0} to {1} could not setLastModified; continuing",
new Object[] {fileParam.getAbsolutePath(),
result.getAbsolutePath()
});
if (!FileUtils.renameFile(fileParam, result)) {
throw new IOException(
"Failed to move file " + fileParam.getAbsolutePath() + " to " + result.getAbsolutePath());
}
} else {
final boolean copyInPlaceArchive = Boolean.valueOf(
System.getProperty(COPY_IN_PLACE_ARCHIVE_PROP_NAME, "true"));
if ( ! fileParam.isDirectory() && copyInPlaceArchive) {
/*
* The file was not uploaded and the in-place file is not a directory,
* so copy the archive to the permanent location.
*/
final long startTime = System.currentTimeMillis();
result = new File(finalUploadDir, fileParam.getName());
FileUtils.copy(fileParam, result);
if ( ! result.setLastModified(fileParam.lastModified())) {
logger.log(Level.FINE, "Could not set lastModified for {0}; continuing",
result.getAbsolutePath());
}
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "*** In-place archive copy of {0} took {1} ms",
new Object[]{
fileParam.getAbsolutePath(),
System.currentTimeMillis() - startTime});
}
if (!result.setLastModified(lastMod)) {
throw new IOException("Failed to set the last modified timestamp of file " + result.getAbsolutePath());
}
return result;
}

final boolean copyInPlaceArchive = Boolean.valueOf(System.getProperty(COPY_IN_PLACE_ARCHIVE_PROP_NAME, "true"));
if (fileParam.isDirectory() || !copyInPlaceArchive) {
return fileParam;
}

// The file was not uploaded and the in-place file is not a directory,
// so copy the archive to the permanent location.
final File result = new File(targetDirectory, fileParam.getName());
FileUtils.copy(fileParam, result);
if (!result.setLastModified(fileParam.lastModified())) {
throw new IOException("Failed to set the last modified timestamp of file " + result.getAbsolutePath());
}
return result;
}

private static StringBuilder getTargetResourceName(final Domain d,
final String target) {

private static StringBuilder getTargetResourceName(final Domain d, final String target) {
final StringBuilder sb = new StringBuilder();
ConfigBeanProxy p = d.getServerNamed(target);
if (p == null) {
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, 2024 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 Down Expand Up @@ -307,11 +307,11 @@ private void moveAltDDFilesToPermanentLocation(
final ExtendedDeploymentContext deploymentContext,
final Logger logger) throws IOException {
final File finalAltDDDir = deploymentContext.getAppAltDDDir();
if ( ! finalAltDDDir.mkdirs()) {
logger.log(Level.FINE," Attempting to create directory {0} was reported as failed; attempting to continue",
new Object[] {finalAltDDDir.getAbsolutePath()});
if (!finalAltDDDir.isDirectory() && !finalAltDDDir.mkdirs()) {
throw new IOException("Failed to create the directory " + finalAltDDDir.getAbsolutePath());
}
DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile( finalAltDDDir, altdd, logger, env);
DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile( finalAltDDDir, runtimealtdd, logger, env);
File applicationsDir = env.getApplicationRepositoryPath();
DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(finalAltDDDir, altdd, applicationsDir);
DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(finalAltDDDir, runtimealtdd, applicationsDir);
}
}

0 comments on commit 41ec342

Please sign in to comment.