src/jdk.jpackage/share/classes/jdk/jpackage/internal/OverridableResource.java
author herrick
Wed, 16 Oct 2019 09:57:23 -0400
branchJDK-8200758-branch
changeset 58647 2c43b89b1679
child 58762 0fe62353385b
permissions -rw-r--r--
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes Submitted-by: asemenyuk Reviewed-by: aherrick, almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58647
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
     1
/*
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
     4
 *
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    10
 *
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    15
 * accompanied this code).
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    16
 *
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    20
 *
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    23
 * questions.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    24
 */
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    25
package jdk.jpackage.internal;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    26
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    27
import java.io.*;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    28
import java.nio.charset.StandardCharsets;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    29
import java.nio.file.Files;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    30
import java.nio.file.Path;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    31
import java.nio.file.StandardCopyOption;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    32
import java.text.MessageFormat;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    33
import java.util.HashMap;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    34
import java.util.List;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    35
import java.util.Map;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    36
import java.util.Optional;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    37
import java.util.stream.Collectors;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    38
import java.util.stream.Stream;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    39
import static jdk.jpackage.internal.StandardBundlerParam.RESOURCE_DIR;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    40
import jdk.jpackage.internal.resources.ResourceLocator;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    41
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    42
/**
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    43
 * Resource file that may have the default value supplied by jpackage. It can be
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    44
 * overridden by a file from resource directory set with {@code --resource-dir}
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    45
 * jpackage parameter.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    46
 *
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    47
 * Resource has default name and public name. Default name is the name of a file
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    48
 * in {@code jdk.jpackage.internal.resources} package that provides the default
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    49
 * value of the resource.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    50
 *
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    51
 * Public name is a path relative to resource directory to a file with custom
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    52
 * value of the resource.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    53
 *
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    54
 * Use #setPublicName to set the public name.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    55
 *
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    56
 * If #setPublicName was not called, name of file passed in #saveToFile function
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    57
 * will be used as a public name.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    58
 *
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    59
 * Use #setExternal to set arbitrary file as a source of resource. If non-null
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    60
 * value was passed in #setExternal call that value will be used as a path to file
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    61
 * to copy in the destination file passed in #saveToFile function call.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    62
 */
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    63
final class OverridableResource {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    64
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    65
    OverridableResource(String defaultName) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    66
        this.defaultName = defaultName;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    67
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    68
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    69
    OverridableResource setSubstitutionData(Map<String, String> v) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    70
        if (v != null) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    71
            // Disconnect `v`
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    72
            substitutionData = new HashMap<>(v);
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    73
        } else {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    74
            substitutionData = null;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    75
        }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    76
        return this;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    77
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    78
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    79
    OverridableResource setCategory(String v) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    80
        category = v;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    81
        return this;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    82
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    83
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    84
    String getCategory() {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    85
        return category;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    86
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    87
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    88
    OverridableResource setResourceDir(Path v) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    89
        resourceDir = v;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    90
        return this;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    91
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    92
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    93
    OverridableResource setResourceDir(File v) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    94
        return setResourceDir(toPath(v));
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    95
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    96
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    97
    /**
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    98
     * Set name of file to look for in resource dir.
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
    99
     *
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   100
     * @return this
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   101
     */
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   102
    OverridableResource setPublicName(Path v) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   103
        publicName = v;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   104
        return this;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   105
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   106
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   107
    OverridableResource setPublicName(String v) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   108
        return setPublicName(Path.of(v));
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   109
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   110
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   111
    OverridableResource setExternal(Path v) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   112
        externalPath = v;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   113
        return this;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   114
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   115
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   116
    OverridableResource setExternal(File v) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   117
        return setExternal(toPath(v));
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   118
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   119
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   120
    void saveToFile(Path dest) throws IOException {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   121
        final String printableCategory;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   122
        if (category != null) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   123
            printableCategory = String.format("[%s]", category);
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   124
        } else {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   125
            printableCategory = "";
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   126
        }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   127
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   128
        if (externalPath != null && externalPath.toFile().exists()) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   129
            Log.verbose(MessageFormat.format(I18N.getString(
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   130
                    "message.using-custom-resource-from-file"),
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   131
                    printableCategory,
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   132
                    externalPath.toAbsolutePath().normalize()));
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   133
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   134
            try (InputStream in = Files.newInputStream(externalPath)) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   135
                processResourceStream(in, dest);
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   136
            }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   137
            return;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   138
        }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   139
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   140
        final Path resourceName = Optional.ofNullable(publicName).orElse(
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   141
                dest.getFileName());
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   142
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   143
        if (resourceDir != null) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   144
            final Path customResource = resourceDir.resolve(resourceName);
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   145
            if (customResource.toFile().exists()) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   146
                Log.verbose(MessageFormat.format(I18N.getString(
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   147
                        "message.using-custom-resource"), printableCategory,
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   148
                        resourceDir.normalize().toAbsolutePath().relativize(
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   149
                                customResource.normalize().toAbsolutePath())));
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   150
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   151
                try (InputStream in = Files.newInputStream(customResource)) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   152
                    processResourceStream(in, dest);
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   153
                }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   154
                return;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   155
            }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   156
        }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   157
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   158
        if (defaultName != null) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   159
            Log.verbose(MessageFormat.format(
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   160
                    I18N.getString("message.using-default-resource"),
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   161
                    defaultName, printableCategory, resourceName));
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   162
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   163
            try (InputStream in = readDefault(defaultName)) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   164
                processResourceStream(in, dest);
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   165
            }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   166
        }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   167
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   168
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   169
    void saveToFile(File dest) throws IOException {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   170
        saveToFile(dest.toPath());
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   171
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   172
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   173
    static InputStream readDefault(String resourceName) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   174
        return ResourceLocator.class.getResourceAsStream(resourceName);
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   175
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   176
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   177
    static OverridableResource createResource(String defaultName,
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   178
            Map<String, ? super Object> params) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   179
        return new OverridableResource(defaultName).setResourceDir(
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   180
                RESOURCE_DIR.fetchFrom(params));
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   181
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   182
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   183
    private static List<String> substitute(Stream<String> lines,
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   184
            Map<String, String> substitutionData) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   185
        return lines.map(line -> {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   186
            String result = line;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   187
            for (var entry : substitutionData.entrySet()) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   188
                result = result.replace(entry.getKey(), Optional.ofNullable(
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   189
                        entry.getValue()).orElse(""));
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   190
            }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   191
            return result;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   192
        }).collect(Collectors.toList());
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   193
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   194
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   195
    private static Path toPath(File v) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   196
        if (v != null) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   197
            return v.toPath();
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   198
        }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   199
        return null;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   200
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   201
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   202
    private void processResourceStream(InputStream rawResource, Path dest)
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   203
            throws IOException {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   204
        if (substitutionData == null) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   205
            Files.createDirectories(dest.getParent());
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   206
            Files.copy(rawResource, dest, StandardCopyOption.REPLACE_EXISTING);
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   207
        } else {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   208
            // Utf8 in and out
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   209
            try (BufferedReader reader = new BufferedReader(
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   210
                    new InputStreamReader(rawResource, StandardCharsets.UTF_8))) {
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   211
                Files.createDirectories(dest.getParent());
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   212
                Files.write(dest, substitute(reader.lines(), substitutionData));
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   213
            }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   214
        }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   215
    }
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   216
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   217
    private Map<String, String> substitutionData;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   218
    private String category;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   219
    private Path resourceDir;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   220
    private Path publicName;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   221
    private Path externalPath;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   222
    private final String defaultName;
2c43b89b1679 8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
diff changeset
   223
}