Skip to content

Commit

Permalink
8223955: Eliminate or reduce mixing of old File API and new Path/File…
Browse files Browse the repository at this point in the history
…s APIs

Reviewed-by: herrick, asemenyuk
  • Loading branch information
Alexander Matveev committed Jul 7, 2020
1 parent c782d0e commit ed05d57
Show file tree
Hide file tree
Showing 34 changed files with 537 additions and 514 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ private void addFileAssociationIconFiles(ShellCommands shellCommands)
File.separatorChar, '-') + IOUtils.getSuffix( File.separatorChar, '-') + IOUtils.getSuffix(
assoc.data.iconPath)); assoc.data.iconPath));


IOUtils.copyFile(assoc.data.iconPath.toFile(), IOUtils.copyFile(assoc.data.iconPath,
faIconFile.srcPath().toFile()); faIconFile.srcPath());


shellCommands.addIcon(mimeType, faIconFile.installPath(), shellCommands.addIcon(mimeType, faIconFile.installPath(),
assoc.iconSize); assoc.iconSize);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@


package jdk.incubator.jpackage.internal; package jdk.incubator.jpackage.internal;


import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
Expand All @@ -39,20 +38,21 @@


public class LinuxAppImageBuilder extends AbstractAppImageBuilder { public class LinuxAppImageBuilder extends AbstractAppImageBuilder {


static final BundlerParamInfo<File> ICON_PNG = static final BundlerParamInfo<Path> ICON_PNG =
new StandardBundlerParam<>( new StandardBundlerParam<>(
"icon.png", "icon.png",
File.class, Path.class,
params -> { params -> {
File f = ICON.fetchFrom(params); Path f = ICON.fetchFrom(params);
if (f != null && !f.getName().toLowerCase().endsWith(".png")) { if (f != null && f.getFileName() != null && !f.getFileName()
.toString().toLowerCase().endsWith(".png")) {
Log.error(MessageFormat.format( Log.error(MessageFormat.format(
I18N.getString("message.icon-not-png"), f)); I18N.getString("message.icon-not-png"), f));
return null; return null;
} }
return f; return f;
}, },
(s, p) -> new File(s)); (s, p) -> Path.of(s));


final static String DEFAULT_ICON = "java32.png"; final static String DEFAULT_ICON = "java32.png";


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@


package jdk.incubator.jpackage.internal; package jdk.incubator.jpackage.internal;


import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
import java.nio.file.Files; import java.nio.file.Files;
Expand Down Expand Up @@ -178,13 +177,13 @@ protected List<ToolValidator> getToolValidators(
} }


@Override @Override
protected File buildPackageBundle( protected Path buildPackageBundle(
Map<String, String> replacementData, Map<String, String> replacementData,
Map<String, ? super Object> params, File outputParentDir) throws Map<String, ? super Object> params, Path outputParentDir) throws
PackagerException, IOException { PackagerException, IOException {


prepareProjectConfig(replacementData, params); prepareProjectConfig(replacementData, params);
adjustPermissionsRecursive(createMetaPackage(params).sourceRoot().toFile()); adjustPermissionsRecursive(createMetaPackage(params).sourceRoot());
return buildDeb(params, outputParentDir); return buildDeb(params, outputParentDir);
} }


Expand Down Expand Up @@ -309,12 +308,12 @@ protected List<ConfigException> verifyOutputBundle(
* *
* This cannot be directly backport to 22u which is built with 1.6 * This cannot be directly backport to 22u which is built with 1.6
*/ */
private void setPermissions(File file, String permissions) { private void setPermissions(Path file, String permissions) {
Set<PosixFilePermission> filePermissions = Set<PosixFilePermission> filePermissions =
PosixFilePermissions.fromString(permissions); PosixFilePermissions.fromString(permissions);
try { try {
if (file.exists()) { if (Files.exists(file)) {
Files.setPosixFilePermissions(file.toPath(), filePermissions); Files.setPosixFilePermissions(file, filePermissions);
} }
} catch (IOException ex) { } catch (IOException ex) {
Log.error(ex.getMessage()); Log.error(ex.getMessage());
Expand All @@ -335,16 +334,16 @@ public static boolean isDebian() {
return false; return false;
} }


private void adjustPermissionsRecursive(File dir) throws IOException { private void adjustPermissionsRecursive(Path dir) throws IOException {
Files.walkFileTree(dir.toPath(), new SimpleFileVisitor<Path>() { Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override @Override
public FileVisitResult visitFile(Path file, public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) BasicFileAttributes attrs)
throws IOException { throws IOException {
if (file.endsWith(".so") || !Files.isExecutable(file)) { if (file.endsWith(".so") || !Files.isExecutable(file)) {
setPermissions(file.toFile(), "rw-r--r--"); setPermissions(file, "rw-r--r--");
} else if (Files.isExecutable(file)) { } else if (Files.isExecutable(file)) {
setPermissions(file.toFile(), "rwxr-xr-x"); setPermissions(file, "rwxr-xr-x");
} }
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
Expand All @@ -353,7 +352,7 @@ public FileVisitResult visitFile(Path file,
public FileVisitResult postVisitDirectory(Path dir, IOException e) public FileVisitResult postVisitDirectory(Path dir, IOException e)
throws IOException { throws IOException {
if (e == null) { if (e == null) {
setPermissions(dir.toFile(), "rwxr-xr-x"); setPermissions(dir, "rwxr-xr-x");
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} else { } else {
// directory iteration failed // directory iteration failed
Expand Down Expand Up @@ -383,7 +382,7 @@ void create(Map<String, String> data, Map<String, ? super Object> params)
.setSubstitutionData(data) .setSubstitutionData(data)
.saveToFile(dstFilePath); .saveToFile(dstFilePath);
if (permissions != null) { if (permissions != null) {
setPermissions(dstFilePath.toFile(), permissions); setPermissions(dstFilePath, permissions);
} }
} }


Expand Down Expand Up @@ -415,7 +414,7 @@ private void prepareProjectConfig(Map<String, String> data,


if (!StandardBundlerParam.isRuntimeInstaller(params)) { if (!StandardBundlerParam.isRuntimeInstaller(params)) {
debianFiles.add(new DebianFile( debianFiles.add(new DebianFile(
getConfig_CopyrightFile(params).toPath(), getConfig_CopyrightFile(params),
"resource.copyright-file")); "resource.copyright-file"));
} }


Expand All @@ -440,7 +439,7 @@ protected Map<String, String> createReplacementData(
return data; return data;
} }


private File getConfig_CopyrightFile(Map<String, ? super Object> params) { private Path getConfig_CopyrightFile(Map<String, ? super Object> params) {
final String installDir = LINUX_INSTALL_DIR.fetchFrom(params); final String installDir = LINUX_INSTALL_DIR.fetchFrom(params);
final String packageName = PACKAGE_NAME.fetchFrom(params); final String packageName = PACKAGE_NAME.fetchFrom(params);


Expand All @@ -452,15 +451,15 @@ private File getConfig_CopyrightFile(Map<String, ? super Object> params) {
} }


return createMetaPackage(params).sourceRoot().resolve( return createMetaPackage(params).sourceRoot().resolve(
Path.of("/").relativize(installPath)).toFile(); Path.of("/").relativize(installPath));
} }


private File buildDeb(Map<String, ? super Object> params, private Path buildDeb(Map<String, ? super Object> params,
File outdir) throws IOException { Path outdir) throws IOException {
File outFile = new File(outdir, Path outFile = outdir.resolve(
FULL_PACKAGE_NAME.fetchFrom(params)+".deb"); FULL_PACKAGE_NAME.fetchFrom(params)+".deb");
Log.verbose(MessageFormat.format(I18N.getString( Log.verbose(MessageFormat.format(I18N.getString(
"message.outputting-to-location"), outFile.getAbsolutePath())); "message.outputting-to-location"), outFile.toAbsolutePath().toString()));


PlatformPackage thePackage = createMetaPackage(params); PlatformPackage thePackage = createMetaPackage(params);


Expand All @@ -470,13 +469,13 @@ private File buildDeb(Map<String, ? super Object> params,
cmdline.add("--verbose"); cmdline.add("--verbose");
} }
cmdline.addAll(List.of("-b", thePackage.sourceRoot().toString(), cmdline.addAll(List.of("-b", thePackage.sourceRoot().toString(),
outFile.getAbsolutePath())); outFile.toAbsolutePath().toString()));


// run dpkg // run dpkg
Executor.of(cmdline.toArray(String[]::new)).executeExpectSuccess(); Executor.of(cmdline.toArray(String[]::new)).executeExpectSuccess();


Log.verbose(MessageFormat.format(I18N.getString( Log.verbose(MessageFormat.format(I18N.getString(
"message.output-to-location"), outFile.getAbsolutePath())); "message.output-to-location"), outFile.toAbsolutePath().toString()));


return outFile; return outFile;
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/ */
package jdk.incubator.jpackage.internal; package jdk.incubator.jpackage.internal;


import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.InvalidPathException; import java.nio.file.InvalidPathException;
import java.nio.file.Path; import java.nio.file.Path;
Expand Down Expand Up @@ -104,29 +103,29 @@ final public String getBundleType() {
} }


@Override @Override
final public File execute(Map<String, ? super Object> params, final public Path execute(Map<String, ? super Object> params,
File outputParentDir) throws PackagerException { Path outputParentDir) throws PackagerException {
IOUtils.writableOutputDir(outputParentDir.toPath()); IOUtils.writableOutputDir(outputParentDir);


PlatformPackage thePackage = createMetaPackage(params); PlatformPackage thePackage = createMetaPackage(params);


Function<File, ApplicationLayout> initAppImageLayout = imageRoot -> { Function<Path, ApplicationLayout> initAppImageLayout = imageRoot -> {
ApplicationLayout layout = appImageLayout(params); ApplicationLayout layout = appImageLayout(params);
layout.pathGroup().setPath(new Object(), layout.pathGroup().setPath(new Object(),
AppImageFile.getPathInAppImage(Path.of(""))); AppImageFile.getPathInAppImage(Path.of("")));
return layout.resolveAt(imageRoot.toPath()); return layout.resolveAt(imageRoot);
}; };


try { try {
File appImage = StandardBundlerParam.getPredefinedAppImage(params); Path appImage = StandardBundlerParam.getPredefinedAppImage(params);


// we either have an application image or need to build one // we either have an application image or need to build one
if (appImage != null) { if (appImage != null) {
initAppImageLayout.apply(appImage).copy( initAppImageLayout.apply(appImage).copy(
thePackage.sourceApplicationLayout()); thePackage.sourceApplicationLayout());
} else { } else {
final Path srcAppImageRoot = thePackage.sourceRoot().resolve("src"); final Path srcAppImageRoot = thePackage.sourceRoot().resolve("src");
appImage = appImageBundler.execute(params, srcAppImageRoot.toFile()); appImage = appImageBundler.execute(params, srcAppImageRoot);
ApplicationLayout srcAppLayout = initAppImageLayout.apply( ApplicationLayout srcAppLayout = initAppImageLayout.apply(
appImage); appImage);
if (appImage.equals(PREDEFINED_RUNTIME_IMAGE.fetchFrom(params))) { if (appImage.equals(PREDEFINED_RUNTIME_IMAGE.fetchFrom(params))) {
Expand All @@ -137,7 +136,7 @@ final public File execute(Map<String, ? super Object> params,
// Application image is a newly created directory tree. // Application image is a newly created directory tree.
// Move it. // Move it.
srcAppLayout.move(thePackage.sourceApplicationLayout()); srcAppLayout.move(thePackage.sourceApplicationLayout());
IOUtils.deleteRecursive(srcAppImageRoot.toFile()); IOUtils.deleteRecursive(srcAppImageRoot);
} }
} }


Expand All @@ -153,10 +152,10 @@ final public File execute(Map<String, ? super Object> params,


data.putAll(createReplacementData(params)); data.putAll(createReplacementData(params));


File packageBundle = buildPackageBundle(Collections.unmodifiableMap( Path packageBundle = buildPackageBundle(Collections.unmodifiableMap(
data), params, outputParentDir); data), params, outputParentDir);


verifyOutputBundle(params, packageBundle.toPath()).stream() verifyOutputBundle(params, packageBundle).stream()
.filter(Objects::nonNull) .filter(Objects::nonNull)
.forEachOrdered(ex -> { .forEachOrdered(ex -> {
Log.verbose(ex.getLocalizedMessage()); Log.verbose(ex.getLocalizedMessage());
Expand Down Expand Up @@ -240,9 +239,9 @@ abstract protected void doValidate(Map<String, ? super Object> params)
abstract protected Map<String, String> createReplacementData( abstract protected Map<String, String> createReplacementData(
Map<String, ? super Object> params) throws IOException; Map<String, ? super Object> params) throws IOException;


abstract protected File buildPackageBundle( abstract protected Path buildPackageBundle(
Map<String, String> replacementData, Map<String, String> replacementData,
Map<String, ? super Object> params, File outputParentDir) throws Map<String, ? super Object> params, Path outputParentDir) throws
PackagerException, IOException; PackagerException, IOException;


final protected PlatformPackage createMetaPackage( final protected PlatformPackage createMetaPackage(
Expand All @@ -266,7 +265,7 @@ public String name() {


@Override @Override
public Path sourceRoot() { public Path sourceRoot() {
return IMAGES_ROOT.fetchFrom(params).toPath().toAbsolutePath(); return IMAGES_ROOT.fetchFrom(params).toAbsolutePath();
} }


@Override @Override
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@


package jdk.incubator.jpackage.internal; package jdk.incubator.jpackage.internal;


import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.text.MessageFormat; import java.text.MessageFormat;
Expand Down Expand Up @@ -147,9 +146,9 @@ protected List<ToolValidator> getToolValidators(
} }


@Override @Override
protected File buildPackageBundle( protected Path buildPackageBundle(
Map<String, String> replacementData, Map<String, String> replacementData,
Map<String, ? super Object> params, File outputParentDir) throws Map<String, ? super Object> params, Path outputParentDir) throws
PackagerException, IOException { PackagerException, IOException {


Path specFile = specFile(params); Path specFile = specFile(params);
Expand All @@ -160,7 +159,7 @@ protected File buildPackageBundle(
.setSubstitutionData(replacementData) .setSubstitutionData(replacementData)
.saveToFile(specFile); .saveToFile(specFile);


return buildRPM(params, outputParentDir.toPath()).toFile(); return buildRPM(params, outputParentDir);
} }


@Override @Override
Expand Down Expand Up @@ -275,7 +274,7 @@ private String rpmArch() throws IOException {
} }


private Path specFile(Map<String, ? super Object> params) { private Path specFile(Map<String, ? super Object> params) {
return TEMP_ROOT.fetchFrom(params).toPath().resolve(Path.of("SPECS", return TEMP_ROOT.fetchFrom(params).resolve(Path.of("SPECS",
PACKAGE_NAME.fetchFrom(params) + ".spec")); PACKAGE_NAME.fetchFrom(params) + ".spec"));
} }


Expand All @@ -302,7 +301,7 @@ private Path buildRPM(Map<String, ? super Object> params,
"--define", String.format("%%_rpmdir %s", rpmFile.getParent()), "--define", String.format("%%_rpmdir %s", rpmFile.getParent()),
// do not use other system directories to build as current user // do not use other system directories to build as current user
"--define", String.format("%%_topdir %s", "--define", String.format("%%_topdir %s",
TEMP_ROOT.fetchFrom(params).toPath().toAbsolutePath()), TEMP_ROOT.fetchFrom(params).toAbsolutePath()),
"--define", String.format("%%_rpmfilename %s", rpmFile.getFileName()) "--define", String.format("%%_rpmfilename %s", rpmFile.getFileName())
).executeExpectSuccess(); ).executeExpectSuccess();


Expand Down
Loading

0 comments on commit ed05d57

Please sign in to comment.