langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/Startup.java
author rfield
Tue, 11 Apr 2017 17:26:52 -0700
changeset 44683 610dc2b48954
parent 43263 ca999fb7b46d
permissions -rw-r--r--
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43263
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
     1
/*
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
     4
 *
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    10
 *
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    15
 * accompanied this code).
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    16
 *
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    20
 *
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    23
 * questions.
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    24
 */
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    25
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    26
package jdk.internal.jshell.tool;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    27
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    28
import java.nio.file.AccessDeniedException;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    29
import java.nio.file.Files;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    30
import java.nio.file.NoSuchFileException;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    31
import java.time.LocalDateTime;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    32
import java.time.format.DateTimeFormatter;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    33
import java.time.format.FormatStyle;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    34
import java.util.ArrayList;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    35
import java.util.Collections;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    36
import java.util.List;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    37
import java.util.Objects;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    38
import static java.util.stream.Collectors.joining;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    39
import static java.util.stream.Collectors.toList;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    40
import static jdk.internal.jshell.tool.JShellTool.RECORD_SEPARATOR;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    41
import static jdk.internal.jshell.tool.JShellTool.getResource;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    42
import static jdk.internal.jshell.tool.JShellTool.readResource;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    43
import static jdk.internal.jshell.tool.JShellTool.toPathResolvingUserHome;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    44
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    45
/**
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    46
 * Processing start-up "script" information.  The startup may consist of several
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    47
 * entries, each of which may have been read from a user file or be a built-in
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    48
 * resource.  The startup may also be empty ("-none"); Which is stored as the
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    49
 * empty string different from unset (null).  Built-in resources come from
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    50
 * resource files.  Startup is stored as named elements rather than concatenated
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    51
 * text, for display purposes but also importantly so that when resources update
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    52
 * with new releases the built-in will update.
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    53
 * @author Robert Field
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    54
 */
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    55
class Startup {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    56
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    57
    // Store one entry in the start-up list
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    58
    private static class StartupEntry {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    59
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    60
        // is this a JShell built-in?
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    61
        private final boolean isBuiltIn;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    62
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    63
        // the file or resource name
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    64
        private final String name;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    65
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    66
        // the commands/snippets as text
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    67
        private final String content;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    68
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    69
        // for files, the date/time read in -- makes clear it is a snapshot
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    70
        private final String timeStamp;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    71
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    72
        StartupEntry(boolean isBuiltIn, String name, String content) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    73
            this(isBuiltIn, name, content, "");
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    74
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    75
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    76
        StartupEntry(boolean isBuiltIn, String name, String content, String timeStamp) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    77
            this.isBuiltIn = isBuiltIn;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    78
            this.name = name;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    79
            this.content = content;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    80
            this.timeStamp = timeStamp;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    81
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    82
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    83
        // string form to store in storage (e.g. Preferences)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    84
        String storedForm() {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    85
            return (isBuiltIn ? "*" : "-") + RECORD_SEPARATOR +
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    86
                    name + RECORD_SEPARATOR +
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    87
                    timeStamp + RECORD_SEPARATOR +
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    88
                    content + RECORD_SEPARATOR;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    89
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    90
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    91
        // the content
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    92
        @Override
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    93
        public String toString() {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    94
            return content;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    95
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    96
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    97
        @Override
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    98
        public int hashCode() {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
    99
            int hash = 7;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   100
            hash = 41 * hash + (this.isBuiltIn ? 1 : 0);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   101
            hash = 41 * hash + Objects.hashCode(this.name);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   102
            if (!isBuiltIn) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   103
                hash = 41 * hash + Objects.hashCode(this.content);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   104
            }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   105
            return hash;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   106
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   107
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   108
        // built-ins match on name only.  Time stamp isn't considered
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   109
        @Override
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   110
        public boolean equals(Object o) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   111
            if (!(o instanceof StartupEntry)) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   112
                return false;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   113
            }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   114
            StartupEntry sue = (StartupEntry) o;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   115
            return isBuiltIn == sue.isBuiltIn &&
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   116
                     name.equals(sue.name) &&
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   117
                     (isBuiltIn || content.equals(sue.content));
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   118
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   119
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   120
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   121
    private static final String DEFAULT_STARTUP_NAME = "DEFAULT";
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   122
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   123
    // cached DEFAULT start-up
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   124
    private static Startup defaultStartup = null;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   125
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   126
    // the list of entries
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   127
    private List<StartupEntry> entries;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   128
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   129
    // the concatenated content of the list of entries
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   130
    private String content;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   131
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   132
    // created only with factory methods (below)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   133
    private Startup(List<StartupEntry> entries) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   134
        this.entries = entries;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   135
        this.content = entries.stream()
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   136
                .map(sue -> sue.toString())
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   137
                .collect(joining());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   138
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   139
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   140
    private Startup(StartupEntry entry) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   141
        this(Collections.singletonList(entry));
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   142
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   143
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   144
    // retrieve the content
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   145
    @Override
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   146
    public String toString() {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   147
        return content;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   148
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   149
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   150
    @Override
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   151
    public int hashCode() {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   152
        return 9  + Objects.hashCode(this.entries);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   153
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   154
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   155
    @Override
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   156
    public boolean equals(Object o) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   157
        return (o instanceof Startup)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   158
                && entries.equals(((Startup) o).entries);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   159
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   160
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   161
    // are there no entries ("-none")?
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   162
    boolean isEmpty() {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   163
        return entries.isEmpty();
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   164
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   165
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   166
    // is this the "-default" setting -- one entry which is DEFAULT
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   167
    boolean isDefault() {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   168
        if (entries.size() == 1) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   169
            StartupEntry sue = entries.get(0);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   170
            if (sue.isBuiltIn && sue.name.equals(DEFAULT_STARTUP_NAME)) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   171
                return true;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   172
            }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   173
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   174
        return false;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   175
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   176
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   177
    // string form to store in storage (e.g. Preferences)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   178
    String storedForm() {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   179
        return entries.stream()
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   180
                .map(sue -> sue.storedForm())
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   181
                .collect(joining());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   182
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   183
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   184
    // show commands to re-create
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   185
    String show(boolean isRetained) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   186
        String cmd = "/set start " + (isRetained ? "-retain " : "");
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   187
        if (isDefault()) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   188
            return cmd + "-default\n";
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   189
        } else if (isEmpty()) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   190
            return cmd + "-none\n";
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   191
        } else {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   192
            return entries.stream()
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   193
                    .map(sue -> sue.name)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   194
                    .collect(joining(" ", cmd, "\n"));
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   195
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   196
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   197
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   198
    // show corresponding contents for show()
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   199
    String showDetail() {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   200
        if (isDefault() || isEmpty()) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   201
            return "";
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   202
        } else {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   203
            return entries.stream()
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   204
                    .map(sue -> "---- " + sue.name
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   205
                            + (sue.timeStamp.isEmpty()
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   206
                                    ? ""
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   207
                                    : " @ " + sue.timeStamp)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   208
                            + " ----\n" + sue.content)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   209
                    .collect(joining());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   210
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   211
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   212
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   213
    /**
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   214
     * Factory method: Unpack from stored form.
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   215
     *
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   216
     * @param storedForm the Startup in the form as stored on persistent
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   217
     * storage (e.g. Preferences)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   218
     * @param mh handler for error messages
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   219
     * @return Startup, or default startup when error (message has been printed)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   220
     */
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   221
    static Startup unpack(String storedForm, MessageHandler mh) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   222
        if (storedForm != null) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   223
            if (storedForm.isEmpty()) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   224
                return noStartup();
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   225
            }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   226
            try {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   227
                String[] all = storedForm.split(RECORD_SEPARATOR);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   228
                if (all.length == 1) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   229
                    // legacy (content only)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   230
                    return new Startup(new StartupEntry(false, "user.jsh", storedForm));
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   231
                } else if (all.length % 4 == 0) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   232
                    List<StartupEntry> e = new ArrayList<>(all.length / 4);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   233
                    for (int i = 0; i < all.length; i += 4) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   234
                        final boolean isBuiltIn;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   235
                        switch (all[i]) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   236
                            case "*":
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   237
                                isBuiltIn = true;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   238
                                break;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   239
                            case "-":
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   240
                                isBuiltIn = false;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   241
                                break;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   242
                            default:
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   243
                                throw new IllegalArgumentException("Unexpected StartupEntry kind: " + all[i]);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   244
                        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   245
                        String name = all[i + 1];
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   246
                        String timeStamp = all[i + 2];
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   247
                        String content = all[i + 3];
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   248
                        if (isBuiltIn) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   249
                            // update to current definition, use stored if removed/error
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   250
                            String resource = getResource(name);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   251
                            if (resource != null) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   252
                                content = resource;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   253
                            }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   254
                        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   255
                        e.add(new StartupEntry(isBuiltIn, name, content, timeStamp));
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   256
                    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   257
                    return new Startup(e);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   258
                } else {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   259
                    throw new IllegalArgumentException("Unexpected StartupEntry entry count: " + all.length);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   260
                }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   261
            } catch (Exception ex) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   262
                mh.errormsg("jshell.err.corrupted.stored.startup", ex.getMessage());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   263
            }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   264
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   265
        return defaultStartup(mh);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   266
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   267
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   268
    /**
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   269
     * Factory method: Read Startup from a list of external files or resources.
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   270
     *
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   271
     * @param fns list of file/resource names to access
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   272
     * @param context printable non-natural language context for errors
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   273
     * @param mh handler for error messages
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   274
     * @return files as Startup, or null when error (message has been printed)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   275
     */
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   276
    static Startup fromFileList(List<String> fns, String context, MessageHandler mh) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   277
        List<StartupEntry> entries = fns.stream()
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   278
                .map(fn -> readFile(fn, context, mh))
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   279
                .collect(toList());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   280
        if (entries.stream().anyMatch(sue -> sue == null)) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   281
            return null;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   282
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   283
        return new Startup(entries);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   284
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   285
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   286
    /**
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   287
     * Read a external file or a resource.
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   288
     *
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   289
     * @param filename file/resource to access
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   290
     * @param context printable non-natural language context for errors
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   291
     * @param mh handler for error messages
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   292
     * @return file as startup entry, or null when error (message has been printed)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   293
     */
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   294
    private static StartupEntry readFile(String filename, String context, MessageHandler mh) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   295
        if (filename != null) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   296
            try {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   297
                byte[] encoded = Files.readAllBytes(toPathResolvingUserHome(filename));
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   298
                return new StartupEntry(false, filename, new String(encoded),
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   299
                        LocalDateTime.now().format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)));
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   300
            } catch (AccessDeniedException e) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   301
                mh.errormsg("jshell.err.file.not.accessible", context, filename, e.getMessage());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   302
            } catch (NoSuchFileException e) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   303
                String resource = getResource(filename);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   304
                if (resource != null) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   305
                    // Not found as file, but found as resource
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   306
                    return new StartupEntry(true, filename, resource);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   307
                }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   308
                mh.errormsg("jshell.err.file.not.found", context, filename);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   309
            } catch (Exception e) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   310
                mh.errormsg("jshell.err.file.exception", context, filename, e);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   311
            }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   312
        } else {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   313
            mh.errormsg("jshell.err.file.filename", context);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   314
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   315
        return null;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   316
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   317
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   318
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   319
    /**
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   320
     * Factory method: The empty Startup ("-none").
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   321
     *
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   322
     * @return the empty Startup
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   323
     */
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   324
    static Startup noStartup() {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   325
        return new Startup(Collections.emptyList());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   326
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   327
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   328
    /**
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   329
     * Factory method: The default Startup ("-default.").
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   330
     *
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   331
     * @param mh handler for error messages
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   332
     * @return The default Startup, or empty startup when error (message has been printed)
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   333
     */
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   334
    static Startup defaultStartup(MessageHandler mh) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   335
        if (defaultStartup != null) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   336
            return defaultStartup;
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   337
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   338
        try {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   339
            String content = readResource(DEFAULT_STARTUP_NAME);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   340
            return defaultStartup = new Startup(
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   341
                    new StartupEntry(true, DEFAULT_STARTUP_NAME, content));
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   342
        } catch (AccessDeniedException e) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   343
            mh.errormsg("jshell.err.file.not.accessible", "jshell", DEFAULT_STARTUP_NAME, e.getMessage());
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   344
        } catch (NoSuchFileException e) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   345
            mh.errormsg("jshell.err.file.not.found", "jshell", DEFAULT_STARTUP_NAME);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   346
        } catch (Exception e) {
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   347
            mh.errormsg("jshell.err.file.exception", "jshell", DEFAULT_STARTUP_NAME, e);
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   348
        }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   349
        return defaultStartup = noStartup();
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   350
    }
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   351
ca999fb7b46d 8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
diff changeset
   352
}